IBM ILOG Solver User's Manual > More on Modeling > Using Set Variables: Crew Scheduling > Model |
Model |
INDEX
![]() |
Once you have written a description of your problem, you can use Concert Technology classes to model it. In this lesson, you declare two functions before the main
function. One is used to display the solution and the other is used to add constraints to the model.
As in Chapter 6, Using the Distribute Constraint: Car Sequencing, you use decision variables in more sophisticated ways in this lesson. You still declare decision variables that will contain the solution to the problem, but you will also declare other decision variables that are used in finding the solution.
Step 2 - | Open the example file |
Open the example file YourSolverHome/examples/src/tutorial/crews_partial.cpp
in your development environment. In the lessons in Part I, you completed most of the exercise code yourself. In this lesson and later lessons, the examples are more complex and more of the code is provided for you. You follow the steps to learn how to do specific tasks within the example code.
In this exercise, you use constrained set variables (more on that later) and therefore you use the include file <ilsolver/ilosolverset.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 in the main
function. The variable i
is used in a "for" loop. 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); IloInt i; |
Next, you create the variables to represent the number of crews, nCrews
, and the number of attributes required for each flight, nAttributes
. The attributes include crew experience level and language skills. In this lesson, the number of crews is initialized to 10 and the number of attributes is initialized to 5 (the minimum number of crew on a flight with a senior experience level, a junior experience level, French skills, German skills, and Spanish skills.) You can easily increase the number of crews or the number of attributes by changing the values of these variables. This code is provided for you:
const IloInt nAttributes = 5; const IloInt nCrews = 10; |
Then, you represent the data of the program.
You represent the names of the flight attendants using a C++ enumeration to associate meaningful names with the integer values. This code is provided for you:
enum Employee {Bill, Bob, Carol, Carolyn, Cathy, David, Ed, Fred, Heather, Inez, Janet, Jean, Jeremy, Joe, Juliet, Marilyn, Mario, Ron, Tom, Tracy}; |
You create an array Staff
to represent all employees. This code is provided for you:
IloNumArray Staff(env, 20, Bill, Bob, Carol, Carolyn, Cathy, David, Ed, Fred, Heather, Inez, Janet, Jean, Jeremy, Joe, Juliet, Marilyn, Mario, Ron, Tom, Tracy); |
Now, you create arrays to represent all employees by experience level and by language skills. If you wanted to extend the example by adding another crew attribute to the model (for example, Italian language skills), you would create another array representing the flight attendants who possess that attribute. This code is provided for you:
After representing the known information of the problem, you declare the variables. The unknown information is the crew composition for each flight (which crew members work on which flights). Concert Technology gives you the means to represent the unknowns in this problem--the crew composition for each flight--as constrained set variables. A constrained set variable is a variable that represents a set of numeric values.
A constrained set variable is defined in terms of two other sets: its required elements and its possible elements. Its required elements are those that must be in the set. Its possible elements are those that may be in the set.
Constrained set variables are represented by the class IloNumSetVar
in Concert Technology. The first parameter of the constructor is the environment. The second parameter is the possible elements of the set and the third parameter is the required elements of the set. You have the option of creating a name for the variable, but you will not do so in this exercise. If only one array is given as a parameter, this array represents the set of possible elements. The last parameter is an optional name used for debug and trace purposes. Here is a constructor:
IloNumSetVar(const IloEnv env, const IloNumArray possible, const IloNumArray required, const char* name=0); |
You use an instance of IloNumSetVarArray
, an array of constrained set numeric variables, to represent the crew composition for all flights. These decision variables will contain the solution to the problem, once it is solved.
Step 3 - | Declare the decision variables |
Add the following code after the comment //Declare the decision variables
IloNumSetVarArray crews(env, nCrews); for(i = 0; i < nCrews; i++) crews[i] = IloNumSetVar(env, Staff); |
Each flight crew is represented by an instance of IloNumSetVar
and the crews for each of the 10 flights are represented by an instance of IloNumSetVarArray
. The array is created by using a "for" loop to create one instance of IloNumSetVar
to represent each crew. Since only one array is given as a parameter, this array represents the set of possible elements. The set of possible elements for each crew is the array Staff
, or all the flight attendants.
Now that you have added the data and declared the variables, you can add the constraints to the model.
First, you add the constraint that the flight attendants must rest at least two flights between assignments. To do this, you use the predefined constraint IloNullIntersect
. This constraint takes two constrained set variables as arguments as well as the environment. It forces the set var1
to have no elements in common with the set var2
. In other words, the intersection of var1
with var2
will be empty when this constraint is satisfied. Here is a constructor:
IloConstraint IloNullIntersect(const IloEnv env, const IloNumSetVar var1, const IloNumSetVar var2); |
You can use IloNullIntersect
to make sure that flight attendants rest at least two flights between assignments.
Step 4 - | Add the constraint on resting between flights |
Add the following code after the comment //Add the constraint on resting between flights
for(i = 0 ; i < nCrews-1 ; i++) { model.add(IloNullIntersect(env, crews[i], crews[i+1])); if (i < nCrews-2) model.add(IloNullIntersect(env, crews[i], crews[i+2])); } |
You first add the constraint that the intersection of the first crew and the second crew should be empty. In other words, these two crews will not share any staff. Then you add the constraint that the intersection of the first crew and the third crew should be empty. This is repeated in a "for" loop for all the flights, up to and including nCrews-1
or Flight # 9. An "if" clause is introduced because there is no need to add the second part of the constraint after Flight # 7.
The rest of the constraints, concerning crew size and requirements for experience levels and language skills, are added to the model by calling the function TeamConstraints
, which is declared at the start of the program. This function takes six parameters:
model
.
crews
, or the crew members selected for each flight.
crewSize
.
crewRequirements
, or the requirements for each flight in terms of crew experience levels and language skills.
attributeSets
, the data reflecting the experience levels and language skills of each flight attendant in sets of decision variables.
dataArrays
, the data reflecting experience levels and language skills of each flight attendant in numeric arrays.
Step 5 - | Declare the TeamConstraints function |
Add the following code after the comment //Declare the TeamConstraints function
© Copyright IBM Corp. 1987, 2009. Legal terms. | PREVIOUS NEXT |