Originally posted by: sjayaswal
I am encountering some very weird problem with Cplex. Let me explain it using the following small example:
Maximize 16X1 + 10X2 + 4X4
s.t.:
8X1 + 2X2 + X3 + 4X4 <= 10
X1 + X2 <= 1
X3 + X4 <= 1
X1, X2, X3, X4 binaries
Clearly, the optimal solution to the above problem is: X=[1 0 0 0] or [1 0 1 0] with Obj Value = 16.
Let's try to solve this using the following simple Cplex code:
IloNumVarArray X(env, 4, 0, 1, ILOINT);
IloNumArray X_val(env, 4);
IloModel model(env);
IloExpr Obj(env);
Obj = 16*X[0]+10*X[1]+4*X[3];
model.add(IloMaximize(env,Obj)); //***********************
Obj.end();
model.add(8*X[0]+2*X[1]+X[2]+4*X[3] <= 10);
model.add(X[0]+X[1] <= 1);
model.add(X[2]+X[3] <= 1);
IloCplex cplex(model);
cplex.solve();//solving the MODEL
cplex.getValues(X_val, X);
cout<< "Objective Value = " << cplex.getObjValue() << endl;
cout<<"X = "<<X_val<<endl;
Output:
Objective Value = 16
X = [1, 0, 0, 0]
Now, let's replace the line indicated as //******************* by the following:
IloObjective Objective(env);
model.add(Objective);
Objective.setExpr(IloMaximize(env, Obj));
Output:
Objective Value = 0
X = [0, 0, 0, 0]
Obviously, the output from the second code is wrong. However, if you rewrite the objective as Objective.setExpr(IloMaximize(env, -Obj)) instead of Objective.setExpr(IloMaximize(env, Obj)), I get back the correct output.
Why is Objective.setExpr(IloMaximize(env, Obj)) not working as expected?
Did anyone notice this bug?
Regards,
Sachin
#CPLEXOptimizers#DecisionOptimization