IBM ILOG Solver User's Manual > More on Solving > Searching for Optimal Solutions: Replanning Warehouses > Multiphase search

The member functions IloSolver::startNewSearch and IloSolver::next, in conjunction with the classes IloSolution and IloRestoreSolution, enable you to search for solutions of a given problem, to edit that problem, and to search for solutions to this new problem. You can even search for solutions of the new problem that are close to solutions of the old one.

Let's assume that you want to search for all solutions of a problem provided you do not change the values of the last four variables. In order to do this, you create two solution objects, one for the variables you do not want to change, and the other for the variables that can be changed. You add the last four variables of the problem to the IloSolution object frozen and declare a goal restoreFrozen, which uses the function IloRestoreSolution to restore the values of the solution frozen. You add the first four variables of the problem to the IloSolution object free and declare a goal storeFree, which uses the function IloStoreSolution to store the values of the solution free.

    solver.out() << "store 4 last vars" << endl;
    IloSolution frozen(env);
    frozen.add(x[4]);
    frozen.add(x[5]);
    frozen.add(x[6]);
    frozen.add(x[7]);
    frozen.store(solver);
    IloGoal restoreFrozen = IloRestoreSolution(env, frozen);

    IloSolution free(env);
    free.add(x[0]);
    free.add(x[1]);
    free.add(x[2]);
    free.add(x[3]);
    IloGoal storeFree = IloStoreSolution(env, free);

    solver.startNewSearch(restoreFrozen && goal && storeFree);
    while (solver.next()) {
      Print(solver, x);
    }
    solver.endSearch();

You use the following code to search for our new solutions. The member function IloSolver::startNewSearch calls the goals restoreFrozen, goal, and storeFree in the search. The solver adds the frozen variables, searches for a solution using goal as its objective, and stores the values of the free variables:

    solver.out() << "repeat last solution" << endl;
    IloGoal restoreFree = IloRestoreSolution(env, free);
    solver.solve(restoreFrozen && restoreFree);
    Print(solver, x);

If you want, for example, to obtain a solution that keeps the same values for the last four variables and that respects an additional constraint, it is sufficient to restore the variables you want to keep, add the additional constraint, and search again for a solution, like this:

    solver.out() << "change solution" << endl;
    model.add(x[OL] < 3);
    solver.startNewSearch(restoreFrozen && goal);
    while (solver.next()) {
      Print(solver, x);
    }
    solver.endSearch();
    solver.printInformation();