0

I am trying to minimize the function a quadratic function sum(sum (w*a)-v)^2

being "a" square array and "w" and "v" two vectors. "a" and "v" are known, "w" is unknown. I am using CPLEX in Java

double [][] input =  {{1,2},{3,4}};
double[] result = {3,2};

//define new model
IloCplex cplex = new IloCplex();

// variables
IloNumVar[] w = new IloNumVar[input[0].length];
for (int i = 0; i < w.length; i++) {
    w[i] = cplex.numVar(0.0, 1.0, IloNumVarType.Float);
}           

IloNumExpr value = cplex.numExpr();
IloNumExpr objective = cplex.numExpr();

for (int i = 0; i < result.length; i++) {

    for (int j = 0; j < result.length; j++) {
        value = cplex.sum(value, cplex.prod(input[i][j], w[i]));
    }
    value = cplex.diff(value, result[i]);
    value = cplex.square(value);

    objective = cplex.sum(objective, value);
}

but I got this exception which I do not understand:

Exception in thread "main" java.lang.UnsupportedOperationException: CpxObjective for general expressions
at ilog.cplex.CpxObjective.setExpr(CpxObjective.java:102)
at ilog.cplex.CpxObjective.<init>(CpxObjective.java:357)
at ilog.cplex.IloCplexModeler.objective(IloCplexModeler.java:796)
at ilog.cplex.IloCplexModeler.minimize(IloCplexModeler.java:714)
at ilog.cplex.IloCplexModeler.minimize(IloCplexModeler.java:810)
at Tont.main(Tont.java:55)

Thanks for helping.

2 Answers2

1

As far as I can tell, your first iteration of the outer loop creates terms like w[i]^2. The second iteration of the outer loops then takes this expression and squares it. Thus creating terms like w[i]^2. This is not supported. Only exponents of 1 and 2 are supported (linear and quadratic objectives).

From what you wrote, I think you are not creating your objective right. It should look something like this (note the initialization of value moved into the loop):

  for (int i = 0; i < result.length; i++) {
     IloNumExpr value = cplex.numExpr();
     for (int j = 0; j < result.length; j++) {
        value = cplex.sum(value, cplex.prod(input[i][j], w[i]));
     }
     value = cplex.diff(value, result[i]);
     value = cplex.square(value);

     objective = cplex.sum(objective, value);
  }
Daniel Junglas
  • 5,830
  • 1
  • 5
  • 22
0

This is the complete code, but it is infeasible. is it because Cplex cannot solve this kind of problems?

double [][] input =  {{1,2},{3,4}};
double[] result = {3,2};

// define new model
IloCplex cplex = new IloCplex();

//cplex.setParam(IloCplex.Param.RootAlgorithm, IloCplex.Algorithm.Auto);

// variables
IloNumVar[] w = cplex.numVarArray(input[0].length, 0, Float.MAX_VALUE);// new IloNumVar[input[0].length];
for (int i = 0; i < w.length; i++) {
    w[i] = cplex.numVar(0.0, 1.0, IloNumVarType.Float);
}           

//IloNumExpr value = cplex.numExpr();
IloNumExpr objective = cplex.numExpr();


  for (int i = 0; i < result.length; i++) {
         IloNumExpr value = cplex.numExpr();
         for (int j = 0; j < result.length; j++) {
            value = cplex.sum(value, cplex.prod(input[i][j], w[i]));
         }
         value = cplex.diff(value, result[i]);
         value = cplex.square(value);

         objective = cplex.sum(objective, value);
      }



cplex.minimize(objective);
//constraints
IloLinearNumExpr weightsAdded = cplex.linearNumExpr();
for (int j = 0; j < w.length; j++) {
    weightsAdded.addTerm(1, w[j]);
}
cplex.addEq(weightsAdded, 1);       

for (int i = 0; i < w.length; i++) {
    cplex.addGe(0, w[i]);
}

// solve model
if (cplex.solve()) {
    System.out.println("obj = "+cplex.getObjValue());
}
else {
    System.out.println("problem not solved");

Thank you