IBM ILOG Solver User's Manual > The Basics > Using Arrays and Basic Search: Changing Money > Solve |
Solve |
INDEX
![]() |
Solving a problem using constraint programming consists of finding a value for each variable that satisfies the constraints. In this problem, there will be more than one solution that satisfies the constraints. You can use Solver to find multiple solutions for the problem and display them.
You start by creating an instance of the class IloSolver
to solve the problem expressed in the model.
Step 9 - | Create an instance of IloSolver |
Add the following code after the comment //Create an instance of IloSolver
IloSolver solver(model); |
In Chapter 2, Modeling and Solving a Simple Problem: Map Coloring, you used the member function IloSolver::solve
to search for a solution. In this lesson, you want to search for all the possible solutions so you will use three other member functions of the class IloSolver
to do this. You first use the member function IloSolver::startNewSearch
, which uses a default goal to launch the search.
Step 10 - | Search for a solution |
Add the following code after the comment //Search for a solution
solver.startNewSearch(); |
Next, you use a while
loop and the member function IloSolver::next
to search for multiple solutions. IloSolver::next
searches for the next solution in the search tree.
The search space and search treeThe search space is all possible combinations of values. When you model a problem, aim to reduce the search space. In this lesson, you reduced symmetry and limited the number of constrained variables. By doing so, you reduced the size of the search space. After initial constraint propagation, the search space is greatly reduced. This remaining part of the search space is called the search tree. Solver uses search strategies to search for a solution in the search tree. For more information on the search space, the search tree, and initial constraint propagation, see the section "Solve". |
As you learned in Chapter 2, Modeling and Solving a Simple Problem: Map Coloring, Solver uses search strategies to guide the search. Goals are the mechanism by which Solver implements search strategies.
The member function IloSolver::next
, like IloSolver::solve
, controls the execution of goals. The first time one of these member functions is called, it creates a goal stack, an internal data structure that Solver uses to manage goals during a solution search.
The member function IloSolver::next
returns a Boolean value of type IloBool
. If a solution is found, an IloTrue
value is returned and the program displays the solution.
After the while
loop terminates, you must use the member function IloSolver::endSearch
to terminate the search and delete internal objects created by Solver to carry out the search (such as the search tree, goal stack, and so on).
The member functions and streams IloAlgorithm::out
and IloSolver::getValue
are used to display the solution. The following code is provided for you:
Step 11 - | Compile and run the program |
Compile and run the program. You should get the following results:
[1, 10, 20, 100] solution 1: 3 0 1 1 solution 2: 3 0 6 0 solution 3: 3 2 0 1 solution 4: 3 2 5 0 solution 5: 3 4 4 0 solution 6: 3 6 3 0 solution 7: 3 8 2 0 solution 8: 3 10 1 0 solution 9: 3 12 0 0 |
As you can see, there are nine solutions:
The complete program is listed in "Complete program". You can also view it online in the file YourSolverHome/examples/src/money.cpp
.
© Copyright IBM Corp. 1987, 2009. Legal terms. | PREVIOUS NEXT |