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