Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Merging two cplex objects

    Posted 05/15/12 04:41 AM

    Originally posted by: Inspiredlallu


    Hello,

    I have two cplex objects which I want to merge and create an lp or a sav file.

    Lets say, first LP object has an inequation as ax+ by <= c

    and the second object has an inequation as dx + ey <= f.

    Now, when I create a third object which has both the inequations in it and save it as an lp file. It gives me the correct output. But if I save it as a sav file or an mps, then it doesnt give me the correct output.

    On further investigation I found that the third object is not treating the x and y from the different objects as same variable. How can I merge the two objects such that the variables in different objects are treated as same variable in the resultant object.

    Thanks and Regards,
    Neha Bhatia
    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Merging two cplex objects

    Posted 05/16/12 03:48 AM

    Originally posted by: SystemAdmin


    Note that two different variables with the same name are still different for CPLEX. CPLEX does not care much about names, they are only used for debugging. In order to merge two models you need to do the following:
    1. Create a new model that will hold the result.
    2. For each variable in the models to be merged create a variable in the new model with the same name.
    3. "Copy" all constraints from the models to be merged into the new model. While copying the constraints you need to replace references to variables by the appropriate references to the variable instances in the new model.
    Here is a code that illustrates how to do that:
    
    
    
    import ilog.cplex.*; 
    
    import ilog.concert.*; 
    
    import java.util.*;   
    /** Example code to illustrate how to merge two problems. */ 
    
    public 
    
    final 
    
    class Merge 
    { 
    /** Class to map variables from one IloCplex instance to variables with the * same name in a different IloCplex instance. */ 
    
    private 
    
    static 
    
    final 
    
    class NumVarMap 
    { 
    
    private 
    
    final Map<String,IloNumVar> map = 
    
    new TreeMap<String,IloNumVar>(); 
    
    private 
    
    final IloCplex cplex; 
    
    public NumVarMap(IloCplex cplex) 
    { this.cplex = cplex; 
    } 
    /** Get the variable with the same name as <code>v</code>. * The function looks up the variable with the same name as <code>v</code> * and returns that. If no such variable exists then a new variable is * created with all properties copied from <code>v</code>. The new * variable is created using the IloCplex instance passed to the * constructor. */ 
    
    public 
    
    synchronized IloNumVar get(IloNumVar v) 
    
    throws IloException 
    { IloNumVar var = map.get(v.getName()); 
    
    if (var == 
    
    null) 
    { var = cplex.numVar(v.getLB(), v.getUB(), v.getType(), v.getName()); map.put(var.getName(), var); 
    } 
    
    return var; 
    }   
    }; 
    /** Add model <code>from</code> to model <code>to</code>. * @param to The model to augment. * @param from The model to be added. * @param map  A map that maps variables in <code>from</code> to variables *             in <code>to</code>. */ 
    
    private 
    
    static 
    
    void addModel(IloCplex to, IloCplex from, NumVarMap map) 
    
    throws IloException 
    { 
    // NOTE: We assume here that FROM was just read via from.importModel() 
    //       and has only linear constraints. In that case all variables and 
    //       constraints are stored in instances of IloLPMatrix and we can 
    //       easily scan all variables and constraints in the model. 
    
    for (java.util.Iterator it = from.LPMatrixIterator(); it.hasNext(); 
    /* nothing */) 
    { 
    
    final IloLPMatrix m = (IloLPMatrix)it.next(); 
    
    for (IloNumVar v : m.getNumVars()) map.get(v); 
    
    for (IloRange r : m.getRanges()) 
    { IloLinearNumExpr expr = (IloLinearNumExpr)r.getExpr(); IloLinearNumExpr copy = to.linearNumExpr(); 
    
    for (IloLinearNumExprIterator et = expr.linearIterator(); et.hasNext(); 
    /* nothing */) 
    { IloNumVar v = map.get(et.nextNumVar()); copy.addTerm(et.getValue(), v); 
    } to.addRange(r.getLB(), copy, r.getUB(), r.getName()); 
    } 
    } 
    }   
    
    public 
    
    static 
    
    void main(String[] args) 
    { 
    
    try 
    { IloCplex result = 
    
    new IloCplex(); NumVarMap map = 
    
    new NumVarMap(result); 
    
    for (
    
    int i = 0; i < args.length; ++i) 
    { IloCplex cpx = 
    
    new IloCplex(); cpx.importModel(args[i]); addModel(result, cpx, map); cpx.end(); 
    } System.out.println(result); result.exportModel(
    "result.lp"); result.end(); 
    } 
    
    catch (IloException e) 
    { System.err.println(e.getMessage()); e.printStackTrace(); System.exit(-1); 
    } 
    } 
    }
    

    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Merging two cplex objects

    Posted 05/16/12 06:25 AM

    Originally posted by: Inspiredlallu


    Thank you so very much Daniel. It solved a very big problem of mine.
    #CPLEXOptimizers
    #DecisionOptimization