Retrieving a solution

CP Optimizer provides objects to represent the solution of a problem. These objects allow for easy retrievability of the solution.

When the CP Optimizer engine has found a solution, you can examine the values that have been assigned to the model variables by using the method getValue, a member function of the optimizer object, with the argument being the model variable.

In some cases, you may want to save all or part of a solution for use later. IBM® ILOG® Concert Technology provides a solution class that is useful for transferring stored values from or to the active model objects associated with a solution.

In the C++ API of CP Optimizer, you use the class IloSolution, which is created on the environment.

In the Java™ API of CP Optimizer, you use the interface IloSolution to store and transfer values.

Likewise, in the C# API of CP Optimizer, you use the interface ISolution to store and transfer values.

You must use the methods add, member functions of the solution class to inform the solution that it should store the decision variable or those in the array of decision variables that is passed as an argument to the method. When the optimizer has found a solution that you want to store, you use the method store to store the solution.

Note:

Solution class

The class IloSolution in the C++ API, the interface IloSolution in the Java API and the interface ISolution in the C# API makes it possible to store the values from decision variables and also to fix those variables with stored values.

To illustrate using the C++ API, the optimal solution found for the optimization model from the section Solving an optimization problem can be stored like this:


    IloCP cp(model);
    IloSolution solution(env);
    solution.add(all);
    if (cp.solve()) {
      solution.store(cp);
      cp.out() << "An optimal solution is " << solution << std::endl;
    }

Running this code produces the output:


An optimal solution is IloSolution[ x[0] y[3] z[1] ]

In the C++ API of CP Optimizer, you use the class IloSolution and the methods IloSolution::add and IloSolution::store.

In the Java API of CP Optimizer, you use the interface IloSolution and the methods IloSolution.add and IloSolution.store.

In the C# API of CP Optimizer, you use the interface ISolution and the methods ISolution.Add and ISolution.Store.