Decision Optimization

Decision Optimization

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

 View Only
Expand all | Collapse all

Dual values in LP

  • 1.  Dual values in LP

    Posted Mon October 14, 2013 06:32 AM

    Originally posted by: MariusGi


    Hello, I'm new to cplex and would like to calculate dual values in c++.
    I try this function :
    cplex.getDuals(vals, con);
    env.out() << "Duals  = " << vals << endl;

    But it prints me an empty array. I was copying syntax from this example: http://rcc.its.psu.edu/resources/software/cplex/ilolpex1.cpp

    I think IloRangeArray has to be filled, but I don't know how.

    Here is my code :

    IloEnv env;
     try {   
         
     IloModel model(env);
          IloNumVarArray vars(env);
     IloRangeArray con(env);
         
     for(int i=0;i<2*NN+2;i++)
          vars.add(IloNumVar(env));
         
     IloNumArray q(env);
     q.add(IloNum(3.5));
     q.add(IloNum(7.1));  
     
     IloNumArray T(env);
     T.add(IloNum (4.3));
     T.add(IloNum (1));
     T.add(IloNum (1));
     T.add(IloNum (0));
     
     IloNumArray W(env,4,1,3,2,-1);
     
     IloNumArray c(env,2,3,1);
     
     IloExpr Sumpiqtyi(env);
     for(int i=2;i<2*NN+2;i++)
     {
     if(i%2==0)
     Sumpiqtyi=Sumpiqtyi+q[0]*vars[i];
     else
    Sumpiqtyi=Sumpiqtyi+q[1]*vars[i];
     }  
     
          model.add(IloMinimize(env, c[0]*vars[0] + c[1]* vars[1] + 1/NN *Sumpiqtyi));
     
     model.add(vars[0]+vars[1]>=5);
     for(int i=2;i<NN+2;i++){
     model.add( T[0]*vars[0] + T[1]* vars[1] + W[0]*vars[2*i-2]+W[1]*vars[2*i-1] >= 9.9);
     model.add( T[2]*vars[0] + T[3]* vars[1]+ W[2]*vars[2*i-2]+W[3]*vars[2*i-1] >= 9.1);
     }
     
          IloCplex cplex(model);
          if ( !cplex.solve() ) {
             env.error() << "Failed to optimize LP." << endl;
             throw(-1);
          }
     
         IloNumArray vals(env);
          
          env.out() << "Solution status = " << cplex.getStatus() << endl;
          env.out() << "Solution value = " << cplex.getObjValue() << endl;
          cplex.getValues(vals, vars);
          env.out() << "Values = " << vals << endl;
          cplex.getDuals(vals, con);
          env.out() << "Duals  = " << vals << endl;         
     
       }   
       catch (IloException& e) {
          cerr << "Concert exception caught: " << e << endl;
       }
       catch (...) {
          cerr << "Unknown exception caught" << endl;
       }
     
       env.end();
       return 0;
    }

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Dual values in LP

    Posted Mon October 14, 2013 06:51 AM

    Instead of this

    model.add(vars[0]+vars[1]>=5);

    do that

    IloRange r(env, 5, vars[0] + vars[1], IloInfinity);
    con.add(r);
    model.add(r);

    (and similarly for the other constraints you add). This is the same but gets the con array populated as well. Another alternative is to not do

    model.add(r);

    all the time but only

    model.add(con);

    in the end. That will add all constraints to the model that are in the con array at this point in time.


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Dual values in LP

    Posted Mon October 14, 2013 07:13 AM

    Originally posted by: MariusGi


    Thanx alot !
    Problem solved.


    #CPLEXOptimizers
    #DecisionOptimization