Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.

 View Only
Expand all | Collapse all

simplified least squares with cplex in Java

  • 1.  simplified least squares with cplex in Java

    Posted Tue May 20, 2008 10:02 PM

    Originally posted by: SystemAdmin


    [fk185 said:]

    Hello,
    I'm new to cplex, I hope my question isn't stupid.
    I'm trying to solve a simple bounded least squares problem: min|Cx-d| where x is variable and C and d are Vectors. lb <= x <= ub.<br />As cplex doesn't support least squares (please correct me if i'm wrong), i try to write the problem as min((sum_over_all_i(a_i*x - b_i) )^2)
    (no square root).
    I used QCPex1.java as a starting point. i don't use any constraints, only upper and lower bound.
    I deleted all constraints stuff and edited populateByRow like this:

    private IloNumVar[] populateByRow(IloMPModeler model) throws IloException
    {
    double[] lb = new double[ C_Vector.size() ];
    double[] ub = new double[ C_Vector.size() ];
    for(int i = 0; i != lb.length; i++)
    {
    lb[i] = lowerBound;
    ub[i] = upperBound;
    }

    IloNumVar[] x  = model.numVarArray(C_Vector.size(), lb, ub);

    // sum_i = sum(C_i*x - d_i) )

    // sum_0 = ((C_i*x) - d_i)^2
    IloNumExpr sum_i = model.square(model.diff(model.prod( C_Vector.get(0), x[0]), d_Vector[0]));
    // add x_i to others
    for(int i = 1; i != d_Vector.length; i++ )
    {
    // sum in step i
    IloNumExpr sumInStep = model.square(model.diff(model.prod( C_Matrix.get(i, x[0]), d_Vector[i]));

    // add sumInStep to other sums
    sum_i = model.addEq(sum_i, sumInStep);
    }

    model.minimize(sum_i);
    return x;
    }


    When i call cplex.solve() i get an exception i don't understand: CPLEX Error 6002: Sense must be 'L' or 'G' .
    Can anybody tell me what's wrong?

    Thank you,
    Florian

    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: simplified least squares with cplex in Java

    Posted Wed May 21, 2008 12:54 PM

    Originally posted by: SystemAdmin


    [fk185 said:]

    I think the mistake was

    // add sumInStep to other sums
    sum_i = model.addEq(sum_i, sumInStep);

    it should be

    // add sumInStep to other sums
    sum_i = model.sum(sum_i, sumInStep);



    #CPLEXOptimizers
    #DecisionOptimization