Decision Optimization

Decision Optimization

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

 View Only
  • 1.  Updating lhs and rhs of importted model

    Posted Sun September 05, 2010 06:51 AM

    Originally posted by: gkirlik


    Dear All,

    I am importing an MPS formatted model in C++ with Cplex 12.1. Then, I want to modify rhs of each constraint while keeping lhs same. I achieve to reach every constraint by using IloModel::Iterator. Code as follows,

    cplex.importModel(tempModel, "model.mps");
            
    for(IloModel::Iterator it(tempModel); it.ok(); ++it) 
    {
            IloExtractable ext = it.operator *();
                    
            if (ext.isConstraint())
            {
                    IloConstraint cons;
                    cons=ext.asConstraint();
                    cout << cons << endl;
            }
    }
    


    How can I modify each constraint (IloConstraint cons) in the model?
    Best Regards,
    Gokhan
    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Updating lhs and rhs of importted model

    Posted Sun September 05, 2010 10:50 AM

    Originally posted by: SystemAdmin


    Cast cons to IloRange and then use the setLB, setUB or setBounds method depending on whether it is a >=, <= or = constraint.

    /Paul

    Mathematicians are like Frenchmen: whenever you say something to them, they translate it into their own language, and at once it is something entirely different. (Goethe)
    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Updating lhs and rhs of importted model

    Posted Sun September 05, 2010 02:27 PM

    Originally posted by: gkirlik


    Dear Paul,

    Thanks for your help. How can I determine whether the constraint is <=, = or >= for any constraint?

    Best Regards
    Gokhan
    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: Updating lhs and rhs of importted model

    Posted Sun September 05, 2010 03:08 PM

    Originally posted by: SystemAdmin


    To CPLEX, all linear constraints look like "lower bound <= expression <= upper bound". Equations have lower bound = upper bound; inequalities have one finite bound and one infinite bound (which in practice means disgustingly large). After casting to IloRange, you can use IloRange::getLB and IloRange::getUB to get the lower and upper bounds and check them. The C++ interface defines IloInfinity, which you can use to test whether the lower bound is -infinity or the upper bound is +infinity.

    /Paul

    Mathematicians are like Frenchmen: whenever you say something to them, they translate it into their own language, and at once it is something entirely different. (Goethe)
    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: Updating lhs and rhs of importted model

    Posted Wed September 15, 2010 11:15 AM

    Originally posted by: gkirlik


    Dear All,

    I have an additional question about this topic. I have already succeed to modify the bounds of the constraints (Thanks for Paul Rubin). I also want to access all coefficients of the imported model's constraints. I can access objective function coefficients and variables by using following code.

    cplex.importModel(model, "model.mps");
     
    vector<IloNumVar> varArray;
    vector<IloNum> numArray;
     
    for(IloModel::Iterator it(model); it.ok(); ++it) 
    {
            IloExtractable extr = it.operator *();
     
            if (extr.isObjective() == true)
            {
                    IloObjective objective =  extr.asObjective();
                    IloExpr objexpr= objective.getExpr();
                                                    
                    for (IloExpr::LinearIterator it2 = objexpr.getLinearIterator(); it2.ok(); ++it2)
                    {
                            varArray.push_back(it2.getVar()); //variable
                            numArray.push_back(it2.getCoef()); //coefficient
                    }
            }
    }
    


    Similar to IloObjective, IloRange (rng) has also getExpr() method. After I use this method (IloExpr expr= rng.getExpr()), iterator of the expr object returns IloFalse. So that, I can not access any coefficient or variable of the expr.

    How can I access imported model's constraints' coefficients and variables?
    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: Updating lhs and rhs of importted model

    Posted Fri September 17, 2010 03:14 AM

    Originally posted by: SystemAdmin


    The following code works for me
    #include <iostream>
    #include <ilcplex/ilocplex.h>
     
    int
    main(int argc, char **argv)
    {
       try {
          for (int i = 1; i < argc; ++i) {
             IloEnv env;
             IloModel model(env);
             IloCplex cplex(model);
             cplex.importModel(model, argv[i]);
     
             std::cout << "Model " << argv[i] << ":" << std::endl;
             for (IloModel::Iterator it(model); it.ok(); ++it) {
                if ((*it).isConstraint()) {
                   IloRangeI* impl = dynamic_cast<IloRangeI *>((*it).asConstraint().getImpl());
                   if (impl) {
                      IloRange rng(impl);
                      IloExpr expr = rng.getExpr();
                      for (IloExpr::LinearIterator it2 = expr.getLinearIterator(); it2.ok(); ++it2) {
                         std::cout << " " << it2.getCoef() << "*" << it2.getVar();
                      }
                      std::cout << std::endl;
                   }
                }
             }
             env.end();
          }
       } catch (IloException& e) {
          std::cerr << "IloException: " << e << std::endl;
       } catch (std::exception& e) {
          std::cerr << "std::exception: " << e.what() << std::endl;
       } catch (...) {
          std::cerr << "exception!" << std::endl;
       }
       return 0;
    }
    

    If you have other constraints than IloRange instances you will have to handle them similarly.
    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: Updating lhs and rhs of importted model

    Posted Fri September 17, 2010 08:18 AM

    Originally posted by: gkirlik


    I appreciated for your help.

    Gokhan
    #CPLEXOptimizers
    #DecisionOptimization