Example: copying a model
Shows how to copy a model, emphasizing special considerations about copying elements of the model explicitly.
If you want to copy an existing model in a .NET application of CPLEX, you might consider using the method MakeClone of the interface IModel. However, that interface method must be implemented to copy any elements (such as an objective function, rows (constraints), or columns (variables)) that you have added to the original model as you built up the original model and populated it.
The following example is a variation of the example LPex2.cs, showing how to copy the original model and add the elements explicitly to clone the entire model.
using ILOG.Concert;
using ILOG.CPLEX;
using System.Collections;
public class LPex2
{
public static void Main(string[] args)
{
try
{
Cplex cplex_orig = new Cplex();
cplex_orig.ImportModel("C:\\test\\bip2.lp");
IEnumerator matrixEnum = cplex_orig.GetLPMatrixEnumerator();
matrixEnum.MoveNext();
ILPMatrix lp = (ILPMatrix)matrixEnum.Current;
CloneManager cm = new SimpleCloneManager(cplex_orig);
Cplex cplex = new Cplex();
IObjective obj = (IObjective)cplex_orig.GetObjective().MakeClone(cm);
cplex.Add(obj);
for(int r=0;r < lp.Ranges.Length;r++){
System.Console.WriteLine(lp.Ranges[r].ToString());
IRange temp = (IRange)lp.Ranges[r].MakeClone(cm);
cplex.Add(temp);
}
if (cplex.Solve())
{
System.Console.WriteLine("Model Feasible");
System.Console.WriteLine("Solution status = " + cplex.GetStatus());
System.Console.WriteLine("Solution value = " + cplex.ObjValue);
double[] x = cplex.GetValues(lp);
for (int j = 0; j < x.Length; ++j)
System.Console.WriteLine("Variable Name:" + lp.GetNumVar(j).Name + ";
Value = " + x[j]+";
LB="+lp.GetNumVar(j).LB+";
UB="+lp.GetNumVar(j).UB+";
Type="+lp.GetNumVar(j).Type.ToString());
}
else
{
System.Console.WriteLine("Solution status = " + cplex.GetStatus());
}
}
catch (ILOG.Concert.Exception e)
{
System.Console.WriteLine("Concert exception caught: " + e);
}
}
}
That example imports a model into a CPLEX object. In your application, you need to change the class and location of the LP file to fit your actual situation.