Originally posted by: SystemAdmin
The problem is that a .mod file (implicitly) defines which engines you can use to solve it: If the .mod file contains the directive 'using CPLEX;' then you can solve it only with IloCplex(). If the .mode file contains the directive 'using CP;' then you can solve it only with IloCP(). If the .mod file does not contain a 'using' directive then it is treated as if it had 'using CPLEX;'.
You can work around this limitation by creating a .mod file without 'using' directive, copy that file to a new file with explicit 'using' directive and then read that new file. Here is a small example that does that:
range idx = 1 .. 2; dvar int+ x[idx]; minimize 1*x[1] + 3*x[2]; subject to
{ 4*x[1] + 5*x[2] >= 1;
} main
{ var input; var output;
// Create a copy of the .mod file that has an explicit 'using CPLEX;' directive. input =
new IloOplInputFile(
"TwoEngines.mod"); output =
new IloOplOutputFile(
"TwoEnginesCPLEX.mod"); output.writeln(
"using CPLEX;");
while (!input.eof)
{ output.writeln(input.readline());
} output.close(); input.close(); var source1 =
new IloOplModelSource(
"TwoEnginesCPLEX.mod"); var model1 =
new IloOplModelDefinition(source1); var engine1 =
new IloCplex(); var opl1 =
new IloOplModel(model1, engine1);
// Uncomment the next two lines if your model uses a data file.
//var data1 = new IloOplDataSource("TwoEngines.dat");
//opl1.addDataSource(data1); opl1.generate();
if (engine1.solve())
{ writeln(
"Optimal objective (CPLEX): " + engine1.getObjValue());
}
else
{ writeln(
"infeasible");
}
// Create a copy of the .mod file that has an explicit 'using CP;' directive. input =
new IloOplInputFile(
"TwoEngines.mod"); output =
new IloOplOutputFile(
"TwoEnginesCP.mod"); output.writeln(
"using CP;");
while (!input.eof)
{ output.writeln(input.readline());
} output.close(); input.close(); var source2 =
new IloOplModelSource(
"TwoEnginesCP.mod"); var model2 =
new IloOplModelDefinition(source2); var engine2 =
new IloCP(); var opl2 =
new IloOplModel(model2, engine2);
// Uncomment the next two lines if your model uses a data file.
//var data2 = new IloOplDataSource("TwoEngines.dat");
//opl2.addDataSource(data2); opl2.generate();
if (engine2.solve())
{ writeln(
"Optimal objective (CP): " + engine2.getObjValue());
}
else
{ writeln(
"infeasible");
}
}
Since this is pure OPL question please refer to the
OPL Forum for further guidance.
#CPLEXOptimizers#DecisionOptimization