Decision Optimization

Decision Optimization

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

 View Only
Expand all | Collapse all

how to populate IloLPMatrix while using addGe to set up the model?

  • 1.  how to populate IloLPMatrix while using addGe to set up the model?

    Posted Sat April 17, 2010 11:36 PM

    Originally posted by: Sharron


    I have a queatoin about IloLPMatrix and addGe.

    I was trying to follow the example of AdMIPex5.java to generate user defined cutting planes based on the solution to the LP relaxation. The difference is that eh initial MIP model is not read in from a mps file, but set up in the code using methods like addGe, addLe etc.

    I think this is why I ran into problems while copying the exampe to do the following.

    IloLPMatrix lp = (IloLPMatrix)cplex.LPMatrixIterator().next();

    lp from the above line turns to be NULL.

    I am wondering
    1. What is the relationship between IloLPMatrix and the addLe, addGe commands? I tried to addLPMatrix() to the model, and then used model.addGe methods. but the LPMatrix seems to be empty still.

    2. How do I populate the IloLPMatrix of the moel according to the value that I had set up using addGe and addLe. Is the a method to this easily, or do I have to set them up row by row myself?

    3. I was doing this to get the number of variables and their values by doing lp.getNumVars(). Is there other methods that I can use to get the number of variables and their values wihout doing these, since my system is set up by addLe, addGe etc?
    Thanks a lot for your help on this.
    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: how to populate IloLPMatrix while using addGe to set up the model?

    Posted Mon April 19, 2010 02:43 AM

    Originally posted by: SystemAdmin


    When you add constraints to CPLEX via addGe() these constraints are not added to an instance of IloLPMatrix but are added to a list of instances of IloRange. I.e. the constraints of a model comprise of a list of IloLPMatrix and a list of IloRange instances (and some other lists).
    These lists can be queried by IloCplex.LPMatrixIterator() and IloCplex.rangeIterator().
    However, using the rangeIterator is a little ugly as you have to figure out the runtime type of the expression used in the range before you can get at the coefficients:
    for (Iterator it = cplex.rangeIterator(); it.hasNext(); /* nothing */) {
       IloRange range = (IloRange)it.next();
       IloNumExpr expr = range.getExpr(); // Cannot get the coefficients of expr directly :-(
       if (expr instanceof IloLinearNumExpr) {
          IloLinearNumExpr linExpr = (IloLinearNumExpr)expr;
          for (IloLinearNumExprIterator jt = linExpr.linearIterator(); jt.hasNext(); /* nothing */) {
              IloNumVar var = jt.nextNumVar();
              double coef = jt.getValue();
              ...
           }
       }
       else if (expr instance of ...) {
          ...
       }
    }
    

    Note that if you use the iterator approach described above you may also want to consider IloCplex.conversionIterator(), IloCplex.SOS1iterator() and IloCplex.SOS2iterator().

    Another way to easily get at the coefficients via an IloLPMatrix would be building the model via this matrix. Assume you build your model like this
    // Create variables.
    IloNumVar x = cplex.numVar();
    IloNumVar y = cplex.mumVar();
     
    // Create constraint x + y <= 2.
    IloLinearNumExpr lhs = cplex.linearNumExpr();
    lhs.addTerm(x, 1.0);
    lhs.addTerm(y, 1.0);
    cplex.addLe(lhs, 2.0);
    

    To setup the same model but get appropriate information in an instance of IloLPMatirx you do
    // Create a matrix in which we setup the model.
    IloLPMatrix matrix = cplex.LPMatrix();
     
    // Create variables.
    IloNumVar x = cplex.numVar();
    IloNumVar y = cplex.numVar();
    matrix.addCols(new IloNumVar[]{ x, y });
     
    // Create constraint x + y <= 2.
    IloLinearNumExpr lhs = cplex.linearNumExpr();
    lhs.addTerm(x, 1.0);
    lhs.addTerm(y, 1.0);
    matrix.addRow(cplex.le(lhs, 2.0));
     
    // When all constraints are setup add the matrix to the model.
    cplex.add(matrix);
    

    The steps to change your code are:
    • Allocate an instance of IloLPMatrix before you start creating variables/constraints.
    • Every time you create a variable add it to the matrix via addCols().
    • Instead of using addGe()/addLe()/addEq() to create and add a constraint use ge()/le()/eq() to only create the constraint and then add this constraint to the matrix via addRow().
    • When the matrix is completely setup use cplex.add(matrix) to add all constraints defined by the matrix.
    Of course you can only add constraints with linear expressions to a matrix.

    Note that the second step (adding columns to the matrix via addCols()) is optional as IloLPMatrix.addRow() will add missing variables to the matrix (see the documentation of this function).
    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: how to populate IloLPMatrix while using addGe to set up the model?

    Posted Wed June 02, 2010 07:13 PM

    Originally posted by: Sharron


    hi, Sorry for the late update. Just want to thank you for the help. Followed your suggestion on using IloLPMatrix. Worked like a charm. Thanks!
    #CPLEXOptimizers
    #DecisionOptimization