IBM ILOG Solver User's Manual > The Basics > Modeling and Solving a Simple Problem: Map Coloring > Model |
Model |
INDEX
![]() |
Once you have written a description of your problem, you can use Concert Technology classes to model it. After you create a model of your problem, you can use Solver classes and member functions to search for a solution.
Step 2 - | Open the example file |
Open the example file YourSolverHome/examples/src/tutorial/color_partial.cpp
in your development environment. This file is a program that is only partially completed. You will fill in the blanks in each step in this lesson. At the end, you will have completed the program code and you can compile and run the program.
In this exercise, you use constrained integer variables and therefore you use the include file <ilsolver/ilosolverint.h>
. To catch exceptions, you use a try/catch
block. The code for creating the array of names for the color values and for printing out the solution is provided.
Note |
When you compile the library with STL support, the macro ILOSTLBEGIN is equivalent to using namespace std . On other ports, it is a machine instruction that does nothing.
|
The first step in converting your natural language description of the problem into code using Concert Technology classes is to create an environment and a model.
Step 3 - | Create the environment |
Add the following code after the comment //Create the environment
IloEnv env; |
An environment, an instance of the class IloEnv
, manages internal modeling issues. It handles output, memory management for modeling objects, and termination of search algorithms. Normally an application needs only one environment, but you can create as many environments as you wish.
The initialization of the environment creates internal data structures to be used in the rest of the code. Once this is done, you can create a model. A model, an instance of the class IloModel
, is a container for modeling objects such as variables and constraints. A model is created using an environment.
Step 4 - | Create the model |
Add the following code after the comment //Create the model
IloModel model(env); |
After solving your problem, you can reclaim memory for all modeling objects and clean up internal data structures by calling IloEnv::end
for every environment you have created. This should be always be done before you exit your application. This is already included in the exercise code.
IloInt
|
Concert Technology gives you the means to represent the unknowns in this problem--the color of each country on the map--as constrained integer variables. A constrained integer variable is a variable that takes integers as possible values. The possible values are represented as a domain of integers with an upper bound and a lower bound. The variables are said to be constrained because you can place constraints on them.
Constrained integer variables are represented by the class IloIntVar
in Concert Technology. The first parameter of the class IloIntVar
constructor is always the environment. The second parameter is the lower bound of the domain of possible values, which defaults to 0. The third parameter is the upper bound of the domain of possible values. The upper bound defaults to IloIntMax
, which represents the largest possible positive integer on a given platform. The fourth parameter is an optional name used for debug and trace purposes. Here is a constructor:
IloIntVar(const IloEnv env, IloInt lb = 0, IloInt ub = IloIntMax const char* name = 0); |
After you create an environment and a model, you declare the constrained integer variables, one for each country. Each variable represents the unknown information--the color of the country. Each constrained integer variable takes a value in the domain of integers from 0
to 3
. These values represent the possible colors:
0
represents the color blue.
1
represents the color white.
2
represents the color yellow.
3
represents the color green.
These decision variables will contain the solution to the problem, once it is solved.
Step 5 - | Declare the decision variables |
Add the following code after the comment //Declare the decision variables
IloIntVar Belgium(env, 0, 3), Denmark(env, 0, 3), France(env, 0, 3), Germany(env, 0, 3), Luxembourg(env, 0, 3), Netherlands(env, 0, 3); |
Concert Technology allows you to express constraints involving integer variables using the following operators:
==
<=
<
>=
>
!=
In this example, you use a constraint to require that when two countries are neighbors, they cannot be the same color. For example, the statement Belgium != France
indicates that France and Belgium are neighbors, so they should not share the same color. Explicitly, it means that the value Solver finds for the constrained integer variable Belgium
cannot equal the value Solver finds for the constrained integer variable France
.
You use the member function IloModel::add
to add constraints to the model. You must explicitly add a constraint to the model or Solver will not be able to use it in the search for a solution.
Step 6 - | Add the constraints |
Add the following code after the comment //Add the constraints
© Copyright IBM Corp. 1987, 2009. Legal terms. | PREVIOUS NEXT |