I am new at cplex and I am trying to optimize a function of x by creating a binary variable in constraint such that:
if x[i] > 0 then y[i] = 1 or if x[i] = 0 then y[i] = 0
and then constraint is the summation of y < some number. The constraint are getting created as I want but the y's are not getting updated while solving the optimization.
I created an expression by adding each y which is later used in constraint along with upper bound and lower bound.
IloNumVar[] y = cplex.numVarArray(size, 0, 1);
IloLinearNumExpr expr = cplex.linearNumExpr();
for (int k = 0; k < size; k++) {
cplex.ifThen(cplex.ge(x[k], 0), cplex.eq(y[k], 1));
cplex.ifThen(cplex.eq(x[k], 0), cplex.eq(y[k], 0));
expr.addTerm(1, y[k]);
}
cplex.addLe(expr,ub);
cplex.addGe(expr,lb);
The .lp gives me the proper constraints but while solving it is not updating the y values and that's why the results are not making sense. I was wondering if there is a way to do. I tried doing cplex.addTerm(cplex(ifThen...)) but it doesn't take constraints as input. Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire