Originally posted by: rdumeur
Dear valkiri,
You don't need to copy the model because you can use internal solves that will restore the state of the solver. Please consider the following code where a single goal RootGoal performs 3 internal solves (one that sets new constraints to the model). Please have a look to the CP optimizer C++ API methods IloCP::add and IloCP::solve(IlcGoal, IlcBool).
I hope this helps,
ILCGOAL1(AssignGoal, IlcIntVarArray, array) {
IlcGoal goal;
for(IlcInt i = 0; i < array.getSize(); ++i) {
if(! array[i].isFixed()) {
goal = IlcAnd(IlcOr(array[i] == array[i].getMin(),
array[i] != array[i].getMin()), this);
break;
}
}
return goal;
}
ILCGOAL2(StoreResult, IlcIntVarArray, array, IlcIntArray, values) {
for(IlcInt i = 0; i < array.getSize(); ++i)
values[i] = array[i].getValue();
return 0;
}
ILCGOAL2(AssignWithConstraints, IlcIntVarArray, array, IlcIntArray, values) {
IloCP cp = getCP();
for(IlcInt i = 0; i < array.getSize()/2; ++i)
cp.add(array[i] != values[i]);
return AssignGoal(cp, array);
}
ILCGOAL1(RootGoal, IlcIntVarArray, array) {
IloCP cp = getCP();
IlcIntArray values(cp, array.getSize());
cp.solve(IlcAnd(AssignGoal(cp, array), StoreResult(cp, array, values)), IlcTrue);
cp.out() << "without constraint: ";
for(IlcInt i = 0; i < values.getSize(); ++i) cp.out() << " " << values[i];
cp.out() << endl;
cp.solve(IlcAnd(AssignWithConstraints(cp, array, values), StoreResult(cp, array, values)), IlcTrue);
cp.out() << "with constraint";
for(IlcInt i = 0; i < values.getSize(); ++i) cp.out() << " " << values[i];
cp.out() << endl;
cp.solve(IlcAnd(AssignGoal(cp, array), StoreResult(cp, array, values)), IlcTrue);
cp.out() << "again without constraint";
for(IlcInt i = 0; i < values.getSize(); ++i) cp.out() << " " << values[i];
cp.out() << endl;
return 0;
}
ILOCPGOALWRAPPER1(RootWrapper, cp, IloIntVarArray, array) {
return RootGoal(cp, cp.getIntVarArray(array));
}
int main() {
IloEnv env;
IloModel model(env);
IloIntVarArray array(env, 10, 1, 10);
model.add(IloAllDiff(env, array));
IloCP cp(model);
cp.setParameter(IloCP::Workers, 1); // single worker mode.
cp.solve(RootWrapper(env, array));
cp.end();
env.end();
return 0;
}
#CPOptimizer#DecisionOptimization