IBM ILOG Solver User's Manual > The Basics > Using Objectives: Map Coloring with Minimum Colors > Model |
Model |
INDEX
![]() |
Once you have written a description of your problem, you can use Concert Technology classes to model it.
Step 2 - | Open the example file |
Open the example file YourSolverHome/examples/src/tutorial/colormin_partial.cpp
in your development environment.
In this exercise, you use constrained enumerated variables (more on this later) and therefore you use the include file <ilsolver/ilosolverany.h>
.
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. Since you already know how to do this, it is provided for you in the exercise code:
int main(){ IloEnv env; try { IloModel model(env); |
After you create an environment and a model, you declare the variables. In Chapter 2, Modeling and Solving a Simple Problem: Map Coloring, you used constrained integer variables, instances of the class IloIntVar
, to represent the colors of the different countries. Each variable had a possible integer value of 0
to 3
, representing the colors blue (0
), white (1
), yellow (2
), and green (3
). In this lesson, you will use constrained enumerated variables to represent the unknown information--the color of the country.
Constrained enumerated variables represent constrained variables whose values are objects. There is no restriction on the type of the values for constrained enumerated variables because these variables work with only the addresses of their values. This class of variable may not seem necessary to you for simple problems, but as your application grows bigger, using object encoding instead of integer encoding can prove quite practical. If you use objects directly as values, you can simplify the representation of your problem, making the model and implementation more intuitive. In this way, object encoding leads to programs that are more legible and more efficient since they are more straightforward.
Concert Technology uses instances of the class IloAnyVar
to represent constrained enumerated variables. The first parameter of the constructor is the environment.The second parameter is an array of IloAny
objects. These represent the possible values for the variable represented by the instance of IloAnyVar
. The last parameter is an optional name used for debug and trace purposes. Here is a constructor:
IloAnyVar(const IloEnv env, const IloAnyArray array, const char* name = 0); |
Before creating the constrained enumerated variables to represent the colors of the different countries, you create an instance of IloAnyArray
to represent the colors. The first parameter is the environment and the second parameter is the size of the array. The other parameters are the IloAny
objects in the array. Here is a constructor:
IloAnyArray(const IloEnv env, IloInt n, const IloAny p0, const IloAny p1, . . .); |
Step 3 - | Create the array of values |
Add the following code after the comment //Create the array of values
IloAnyArray Colors(env, 3, blue, white, yellow); |
The array Colors
contains three objects of type IloAny
, blue
, white
, and yellow
, representing the three colors available for coloring the map.
Next, you declare the constrained enumerated variables, one for each country. Each variable represents the unknown information--the color of the country. The possible values of the variable are those given in the array Colors
. These decision variables will contain the solution to the problem, once it is solved.
Step 4 - | Create the decision variables |
Add the following code after the comment //Create the decision variables
IloAnyVar Belgium(env, Colors), Denmark(env, Colors), France(env, Colors), Germany(env, Colors), Luxembourg(env, Colors), Netherlands(env, Colors); |
Now, you add the set of constraints that all neighboring countries, with the exception of Luxembourg, cannot be the same color. Since you have already done this in Chapter 2, Modeling and Solving a Simple Problem: Map Coloring, this code is provided for you:
model.add(France != Belgium); model.add(France != Germany); model.add(Belgium != Netherlands); model.add(Germany != Netherlands); model.add(Germany != Denmark); model.add(Germany != Belgium); |
Then you create an objective. This objective will express the relative importance, in order of priority, of the following relaxed constraints:
An objective is simply an expression which can be maximized or minimized. In this case, you are going to maximize the expression. You need to find a way to model the three relaxed constraints and assign a relative importance to them. You already know how to model the three relaxed constraints using the !=
operator:
Luxembourg != Germany Luxembourg != Belgium Luxembourg != France |
Now instead of adding them directly to the model, you combine them, creating a metaconstraint. You can do this because constraints have value. The value of a constraint is IloTrue
if the constraint is satisfied or IloFalse
if the constraint is not satisfied. Another way of looking at the value of a constraint is as a Boolean value that can be represented as a binary integer. In this representation, constraints that are true have an integer value of 1 and constraints that are false have an integer value of 0. This convention makes it possible to use constraints themselves in an expression. You can use arithmetic or logical operators with constraints, combine them with each other, or maximize an expression that contains constraints.
MetaconstraintsMetaconstraints are created by combining constraints. Constraints can be combined by using arithmetic and logical operators. Constraints can be imposed on other constraints. For more information on metaconstraints, see "More on metaconstraints". |
To create the objective, you use the function IloMaximize
, which creates an instance of the class IloObjective
. The function IloMaximize
takes the environment as its first parameter and an expression as its second parameter. The last parameter is an optional name used for debug and trace purposes. Here is a constructor for IloMaximize
:
IloObjective IloMaximize(const IloEnv env, const IloExpr expr, const char* name=0); |
Step 5 - | Create the objective |
Add the following code after the comment //Create the objective
IloObjective obj = IloMaximize(env, 9043 * (Luxembourg != Germany) + 568 * (Luxembourg != Belgium) + 257 * (Luxembourg != France)); |
When you create the objective, you use the multiplication operator to link each of the relaxed constraints with a coefficient that represents the relative importance of that relaxed constraint. If the constraint is true, the coefficient will be multiplied by 1. If the constraint is false, the coefficient will be multiplied by 0. You create a metaconstraint by combining the three relaxed constraints in an expression. When Solver searches for a solution, it tries to maximize the value of this expression. Therefore, the relaxed constraints which have a larger coefficient (because they are considered more important) will be more likely to form part of the solution.
For example, if Solver found a solution where France and Luxembourg were different colors, then the relaxed constraint Luxembourg != France
would be true and would be represented by the integer value of 1. If, in the same solution, Luxembourg was the same color as both Germany and Belgium, then the relaxed constraints Luxembourg != Germany
and Luxembourg != Belgium
would both be false and would be represented by the integer value of 0. The expression would then be:
(257 * 1) + (9043 * 0) + (568 * 0) = 257
You use the member function IloModel::add
to add the objective to the model. You must explicitly add an objective to the model or Solver will not be able to use it in the search for a solution.
Step 6 - | Add the objective to the model |
Add the following code after the comment //Add the objective to the model
model.add(obj); |
© Copyright IBM Corp. 1987, 2009. Legal terms. | PREVIOUS NEXT |