Accessing intermediate solutions
As CP Optimizer searched for an optimal solution to an optimization problem, the search will generally encounter a sequence of solutions that improve the objective function.
As the CP Optimizer engine solves an optimization problem, a sequence of solutions that improve the objective function are produced until an optimal solution is found. In some cases, you may want to have access to this sequence of intermediate solutions. CP Optimizer provides a simple interface that provides this access via member functions of the optimizer object.
To prepare the optimizer for search, you call the member
function startNewSearch, a member function
of the optimizer object. To instruct the optimizer to find a solution,
you call the method next. This method returns
true (IloTrue in the C++ API) if the optimizer
finds a solution (not necessarily an optimal one) and false (IloFalse in the C++ API) if the problem is infeasible.
To find the next solution, you again call the method next.
If the method returns true, then the optimizer has found a new solution
with a strictly better objective value than the previous one. If the
optimizer does not find another solution, the value returned by this
method is false. When you have finished searching, you can call the
method end to free the memory and reset
the state of the optimizer. A typical code using these methods of
the optimizer object in the C++ API would be similar to:
IloCP cp(model);
cp.setParameter(IloCP::LogVerbosity, IloCP::Quiet);
cp.startNewSearch();
while(cp.next()){
cp.out() << "objective value = " << cp.getObjValue()
<< ", x = " << cp.getValue(x)
<< ", y = " << cp.getValue(y)
<< ", z = " << cp.getValue(z) << std::endl;
}
cp.end();
Running this code produces the output:
objective value = 5, x = 4, y = 0, z = 1
objective value = 4, x = 0, y = 3, z = 1
This run finds two solutions, one with an objective value of 5 and then one with an objective value of 4, which is the optimum.
In the C++ API of CP Optimizer, you use
the class IloCP and the methods IloCP::startNewSearch and IloCP::next.
In the Java™ API of
CP Optimizer, you use the class IloCP and
the methods IloCP.startNewSearch and IloCP.next.
In the C# API of CP Optimizer, you use
the class CP and the methods CP.StartNewSearch and CP.Next.