Originally posted by: rdumeur
Dear David,
There is no interactive interpreter for CPO so you have to use the API to dump model from your C++ code.
The cpoptimizer/examples/src/cpp/cpofileformat.cpo example illustrates this approach :
const char* Names[] = {"blue", "white", "yellow", "green"};
void createModel(const char* filename) {
IloEnv env;
try {
IloModel model(env);
// Macros ILOSETLOCATION and ILOADD store current source file location.
// Thanks to that locations will be included in the generated file.
IloIntVar Belgium(env, 0, 3, "Belgium"); ILOSETLOCATION(Belgium);
IloIntVar Denmark(env, 0, 3, "Denmark"); ILOSETLOCATION(Denmark);
IloIntVar France(env, 0, 3, "France"); ILOSETLOCATION(France);
IloIntVar Germany(env, 0, 3, "Germany"); ILOSETLOCATION(Germany);
IloIntVar Luxembourg(env, 0, 3, "Luxembourg"); ILOSETLOCATION(Luxembourg);
IloIntVar Netherlands(env, 0, 3, "Netherlands"); ILOSETLOCATION(Netherlands);
ILOADD(model, Belgium != France);
ILOADD(model, Belgium != Germany);
ILOADD(model, Belgium != Netherlands);
ILOADD(model, Belgium != Luxembourg);
ILOADD(model, Denmark != Germany );
ILOADD(model, France != Germany);
ILOADD(model, France != Luxembourg);
ILOADD(model, Germany != Luxembourg);
ILOADD(model, Germany != Netherlands);
IloCP cp(model);
cp.dumpModel(filename);
} catch (IloException& ex) {
env.out() << "Error: " << ex << std::endl;
}
env.end();
}
void solveModel(const char* filename) {
IloEnv env;
try {
IloCP cp(env);
cp.importModel(filename);
// Force blue color (zero) for France:
cp.getIloIntVar("France").setBounds(0, 0);
if (cp.solve()) {
cp.out() << std::endl << "Solution:" << std::endl;
IloIntVarArray vars = cp.getAllIloIntVars();
for (IloInt i = 0; i < vars.getSize(); i++)
cp.out() << vars[i].getName() << ": " << Names[cp.getValue(vars[i])] << std::endl;
}
} catch (IloException& ex) {
env.out() << "Error: " << ex << std::endl;
}
env.end();
}
int main(int argc, const char** argv) {
const char* filename = (argc > 1 ? argv[1] : "cpofileformat.cpo");
createModel(filename);
solveModel(filename);
return 0;
}
If you are using OPL, you can use scripting to convert your POL models to CPO using a function like:
function convertModel(model, data, cpo) {
var rc = new IloOplRunConfiguration(model, data);
rc.oplModel.generate();
var engine = rc.cp;
engine.exportModel(cpo);
rc.end();
}
where where "model" is the .mod file, "data" the data file and "cpo" the path of the .cpo file to create.
I hope this helps.
Cheers,
#CPOptimizer#DecisionOptimization