Home Forums Can i optimize functions like this?

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #1874
    hessinemaaoui
    Keymaster

    //Hi, Here is a code trying to find x such that x’Vx*0.5-c’x is minimized constrained on sum of elements of x =1 ,but
    //it doesn’t work well and I think the problem is on optim.solve which may not be able to optimize the functions like crosssum and dot by //myself, can anyone help to find the mistakes?
    //Also, any better way to deal with a quadratic programming problem?
    //Here is the code.

    import com.numericalmethod.suanshu.optimization.constrained.general.penaltymethod.PenaltyMethod;
    import com.numericalmethod.suanshu.optimization.unconstrained.quasinewton.BFGS;
    import com.numericalmethod.suanshu.analysis.function.rn2r1.RealScalarFunction;
    import com.numericalmethod.suanshu.number.DoubleUtils;
    import com.numericalmethod.suanshu.optimization.constrained.general.ConstrainedProblem;
    import com.numericalmethod.suanshu.vector.doubles.Vector;
    import com.numericalmethod.suanshu.optimization.constrained.general.constraint.EqualityConstraints;
    import com.numericalmethod.suanshu.optimization.constrained.general.constraint.LessThanConstraints;
    import com.numericalmethod.suanshu.matrix.doubles.matrixtype.dense.DenseMatrix;
    import com.numericalmethod.suanshu.vector.doubles.dense.DenseVector;
    import com.numericalmethod.suanshu.matrix.doubles.matrixtype.dense.DenseMatrixUtils;
    import java.util.Arrays;
    public class Quadprog{
        public static void main(String[]arg){
    double [] u = new double []{0.15,0.2,0.4,0.2,0.05,0.3,0.4,0.3,0.06};
    DenseMatrix I1 = new DenseMatrix(u,3,3);
    double [] a = new double []{1,0.5,0.7};
    DenseVector mu = new DenseVector(a);

    System.out.println(Arrays.toString(Solver(I1,mu)));
        }
       
       
        public static double [] Solver (final DenseMatrix V , final DenseVector mu){
           
      final double [][] v = DenseMatrixUtils.to2DArray(V);   
      final double []y = mu.toArray();
      final int len = mu.length;
     
       
       
        RealScalarFunction f = new RealScalarFunction() {
            public double evaluate (double…x) {
                return 0.5*crosssum(x,v)-dot(x,y);
            }
            public int dimension4Domain() {
                    return len;
                }

                public int dimension4Range() {
                    return 1;
                }
    };
       
       
        RealScalarFunction c1 = new RealScalarFunction() {
           
            public double evaluate (double …x) {
                return sum(x)-1;
            }
              public int dimension4Domain() {
                    return len;
                }

                public int dimension4Range() {
                    return 1;
                }
        };
       
       
            RealScalarFunction c2 = new RealScalarFunction() {
           
            public double evaluate (double …x) {
                return -sum(x);
            }
              public int dimension4Domain() {
                    return len;
                }

                public int dimension4Range() {
                    return 1;
                }
        };
        ConstrainedProblem problem = new ConstrainedProblem(f,new EqualityConstraints(c1),new LessThanConstraints(c2));
    PenaltyMethod optim = new PenaltyMethod(PenaltyMethod.DEFAULT_PENALTY_FUNCTION_FACTORY,
                    1e30,
                    new BFGS());
            optim.solve(problem, 1e-2, 200);
    Vector xmin = optim.search(new DenseVector(new double []{0,0,0}));  // as len=3
       
            double fxmin = f.evaluate(xmin.toArray());

            System.out.println(String.format(“f(%s) = %f”, DoubleUtils.doubleArray2StringArray(xmin.toArray()), fxmin));

    return xmin.toArray();
               
    }

       
       
        public static double sum (double []x) {  //finding sum of elements of an array
            double sum =0;
            for (int i = 0; i<=x.length-1 ;i++) {
                sum=sum+x;
        }
            return sum;
    }
       
            public static double crosssum (double []x ,double [][]v) {  //finding x’vx
            double sum =0;
            for (int i = 0; i<=x.length-1 ;i++) {
                for (int j=0; j<=x.length-1 ; j++){
              sum=sum+v
    [j]*x*x[j];
        }
           
    }
        return sum;
       
    }
           
            public static double dot(double []x , double []y){  //finding dot product of two arrays
                double dot = 0;
                for (int i=0; i<=x.length-1; i++){
                    dot = dot +x
    *y;
                }
                return dot;
            }
           
    }

Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.