Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Problem with IloMaximize

    Posted 11/04/13 06:33 AM

    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


  • 2.  Re: Problem with IloMaximize

    Posted 11/04/13 07:00 AM

    Does it work as expected if you do

    IloObjective Objective = IloMaximize(env);
    model.add(Objective);
    Objective.setExpr(Obj);

    instead of

    IloObjective Objective = IloObjective(env);
    model.add(Objective);
    Objective.setExpr(IloMaximize(env, Obj));

    ? I think you are using IloMaximize in the wrong way in the second case. You should call

    cplex.exportModel("model.lp");

    before calling cplex.solve() and then look at the generated model.lp file in both cases. I bet in the second case the model is not what you expected?


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Problem with IloMaximize

    Posted 11/04/13 07:39 AM

    Originally posted by: sjayaswal


    Thank you so much Daniel for pointing out the mistake.

    It does work now.

    Thanks again!

    Regards,

    Sachin


    #CPLEXOptimizers
    #DecisionOptimization