0% found this document useful (0 votes)
6 views25 pages

Advanced CPLEX

The document discusses IBM ILOG scripts and how they can be used to interact with optimization models. It covers executing blocks, flow control, setting initial solutions, handling conflict constraints, and solving models iteratively. Various code examples are provided to demonstrate these scripting capabilities.

Uploaded by

Trúc Ly
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views25 pages

Advanced CPLEX

The document discusses IBM ILOG scripts and how they can be used to interact with optimization models. It covers executing blocks, flow control, setting initial solutions, handling conflict constraints, and solving models iteratively. Various code examples are provided to demonstrate these scripting capabilities.

Uploaded by

Trúc Ly
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Ilog Script

Phan Nguyen Ky Phuc

June 20, 2020

Contents
1 IBM ILOG scripts 2

2 execute {...} block: 2


2.1 main {...} block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

3 Flow Control 3

4 Setting the initial solution 6

5 Handle the conflict constraints 7

6 Solve the model iteratively 8

7 Getting the data elements 9

8 Important Note 10

9 Knapsack Example with multiple runs 11

10 The 1st Example of solving the problem in 2 stages 15

11 The 2nd Example of solving the problem in 2 stages 19

12 Column Generation 21

13 Column Generation 25

14 Reference 25

1
Ho Chi Minh City International University Scheduling
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc

1 IBM ILOG scripts


• The script language is designed to interact with models

• Script statements either start with the main or execute

• execute {...} statement is used for preprocessing and postprocessing scripts.

• main{...} statement is used for a flow control script.

• Script variables are declared with the keyword var

• Script can call java file to interact more with model

2 execute {...} block:


• Takes responsibility for preprocesing and postprocesing .

• The variables declared in a execute block are local and cannot be accessed by
other block

• Execute blocks placed before the objective are part of preprocessing

• Execute blocks placed after the model are part of postprocessing.

• The scripting context within an execute block corresponds to the model declara-
tions in *.mod file.

General syntax for changing setting


1 execute {
2 cp . param . param_name = param_value ; // For CP e n g i n e
3 c p l e x . param_name=param_value ; // For Cplex e n g i n e
4 }

2.1 main {...} block

Main block is used for flow control

• For controlling how models are instantiated and solved.

• For using several models with different data, to run multiple "solve" on a model, and
to modify the model data between one solve and the next.

• It is particularly useful when solving a model with modified data several times, or if
using different models to solve the problem (model decomposition)

Chapter 09 Page 2
Ho Chi Minh City International University Scheduling
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc

3 Flow Control

To better understand the integration of IBM ILOG Script with OPL, you can think of
execute or main blocks as being nested in a scope. This scope is equivalent to
1 var s c o p e = new Object ( ) ;
2 s c o p e . thisOplModel = // t h e i n s t a n c e o f IloOplModel f o r t h i s . mod f i l e
3 s c o p e . c p l e x = // t h e I l o C p l e x i n s t a n c e used t o i n s t a n t i a t e t h e IloOplModel
4 with ( s c o p e ) {
5 with ( thisOplModel ) {
6 // t h e e x e c u t e o r main b l o c k g o e s h e r e
7 }
8 }

Figure 1: Structure

It can be seen that to access the constraint, variables, etc.. it must be called as
1 OplModel . c o n s t r a i n t I D . method ( )
2 OplModel . c o n s t r a i n t I D . a t t r i b u t e
3

4 OplModel . varID . method ( )


5 OplModel . varID . a t t r i b u t e

The accessing for constraint, variables, dataElement, etc., in OPL model is only available
after the OPL model is generated The basic flow control structure

• Step 1: Declare model source

• Step 2: Declare the data source

Chapter 09 Page 3
Ho Chi Minh City International University Scheduling
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc

• Step 3: Make model definition based on the model source

• Step 4: Declare the solving engine cplex or cp

• Step 5: Make opl model based on model definition and solving engine

• Step 6: Add data source into the opl model

• Step 7: Generate model

• Step 8: Solve the model

• Step 9: End all things

Chapter 09 Page 4
Ho Chi Minh City International University Scheduling
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc

Sample Template
1 main {
2 var s o u r c e = new I l o O p l M o d e l S o u r c e ( "ModelA . mod" ) ;
3 var data = new I l o O p l D a t a S o u r c e ( " Data1 . dat " ) ;
4 var c p l e x = new I l o C p l e x ( ) ;
5 var d e f = new I l o O p l M o d e l D e f i n i t i o n ( s o u r c e ) ;
6 var o p l = new IloOplModel ( def , c p l e x ) ;
7 o p l . addDataSource ( data ) ;
8 opl . generate () ;
9 i f ( cplex . solve () ) {
10 w r i t e l n ( "OBJ = " + c p l e x . getObjValue ( ) ) ;
11 }
12 else {
13 w r i t e l n ( "No s o l u t i o n " ) ;
14 }
15 o p l . end ( ) ;
16 data . end ( ) ;
17 d e f . end ( ) ;
18 c p l e x . end ( ) ;
19 s o u r c e . end ( ) ;
20 }

In default mode

• When main is executed, Step 1 −→ Step 6 are done automatically

• A variable called thisOplModel is available by default.

• This associates to .dat files (if they exist) to run the model.

• It is required to generate and solve

1 main{
2 thisOplModel . g e n e r a t e ( ) ;
3 cplex . solve () ;
4 }

Chapter 09 Page 5
Ho Chi Minh City International University Scheduling
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc

4 Setting the initial solution

• IloOplCplexVectors is used to set initial solution to the MIP

• Values of initial solutions are defined in the *.mod or *.dat file.

• Model must be generated first before attaching initial solution

1 f l o a t i n i t X [ rangeX ] = . . . ;

1 var o p l 2 = new IloOplModel ( def , c p l e x ) ;


2 opl2 . generate () ;
3 var v e c t o r s = new I l o O p l C p l e x V e c t o r s ( ) ;
4 // a t t a c h e d t h e i n i t X t o X where i n i t X i s d e c l a r e d i n dat f i l e
5 v e c t o r s . a t t a c h ( o p l 2 . X, o p l 2 . i n i t X ) ;
6 cplex . solve () ;

Chapter 09 Page 6
Ho Chi Minh City International University Scheduling
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc

5 Handle the conflict constraints

Print down the status


1 main
2 {
3 thisOplModel . g e n e r a t e ( ) ;
4 w r i t e l n ( thisOplModel . p r i n t R e l a x a t i o n ( ) ) ;
5 w r i t e l n ( thisOplModel . p r i n t C o n f l i c t ( ) )
6 }

Assign the preferences to conflict constraints


It is possible to assign preferences to members of a conflict.

• A preference of -1 means that the member is to be absolutely excluded from the conflict.

• A preference of 0 (zero) means that the member is always to be included

• Preferences of positive value represent an ordering by which the conflict refiner will
give preference to the members.

• A group with a higher preference is more likely to be included in the conflict.

• Preferences can thus help guide the refinement process toward a more desirable minimal
conflict.

1 // C r e a t e p r e f e r e n c e s
2 f l o a t prefs [ i in r ] = . . . ;
3 // Attach p r e f s t o c t s c o n s t r a i n t s
4 opl2 . c o n f l i c t I t e r a t o r . attach ( opl2 . cts , opl2 . p r e f s ) ;
5 opl2 . generate () ;
6 writeln ( opl2 . p r i n t C o n f l i c t () ) ;
7 o p l 2 . end ( ) ;

Chapter 09 Page 7
Ho Chi Minh City International University Scheduling
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc

6 Solve the model iteratively

• Step 1. Define a "main" block

• Step 2. Load the necessary structures:

• Step 3. Generate the model from the initial data.

• Step 4. Solve the current optimization model with the current data:

– if there is no solution, then quit


– if there is a solution, print the objective value

• Step 5. Get the data elements from an IloOplModel instance.

• Step 6. Modify some of the data elements:

• Step 7. Create a new OPL model with the modified data:

• Step 8. Complete model: generate the new current optimization model.

Chapter 09 Page 8
Ho Chi Minh City International University Scheduling
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc

7 Getting the data elements

• It is impossible to change the data in a "data source".

• A data source is defined in a .dat file.

• It is able to obtain a copy of the OPL model data, i.e., data elements.

• The data elements can be modified and then used for another model.

• This copy does not include the data source publishers:

Important notes:

• Scalar data, whether in the .mod file or the .dat file, cannot be modified via
scripting.

• It is able to use data elements to add new elements, but only for scalar types.

• Only external data can be modified by script. If, in the .mod file, you have, for
example:
1 int arr [ 1 . . 3 ] = [ 1 , 2 , 3 ] ;

you cannot modify the array arr. You need to declare it as:
1 int arr [ 1 . . 3 ] = . . . ;

and initialize it externally.


You need to create a .dat file that contains the data to update, except for
scalar data.
The scalars that need to be updated would not be initialized in the .mod, or in a .dat.
but in a new instance of OplDataElements that you can then add as a data source:

Chapter 09 Page 9
Ho Chi Minh City International University Scheduling
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc

8 Important Note

• One OPL model only accepts one data source which is the latest added into the model.
As a result of this, defining data on multi dat files is impossible to one OPL model

• The dataSource cannot be edited in main block only dataElement is allowed to be


edited and updated in main block

• Each run configuration can only compose of one mod file and one dat file.

• Variables and constraints of OPL model are available to access only after the model is
generated

• It is possible to create several configurations in main block

• After the model has been generated, some transformation on OPL model such as
relaxing integer variables can be done as
1 OplModel . c o n v e r t A l l I n t V a r s ( ) ;
2 // o r u n c o n v e r t
3 OplModel . u n c o n v e r t A l l I n t V a r s ( ) ;

Figure 2: General Structure

Chapter 09 Page 10
Ho Chi Minh City International University Scheduling
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc

9 Knapsack Example with multiple runs

This example uses 2 approaches

• Data elements

• Different data sources

The Data1.dat file


1 numbType =4;
2 W =[1 , 2 , 3 , 2 ] ;
3 B =[2 , 3 , 5 , 3 ] ;
4 n =[2 ,1 ,2 ,2];
5 Cap=9;

The Data2.dat file


1 numbType =4;
2 W =[1 , 1 , 3 , 2 ] ;
3 B =[2 , 3 , 5 , 3 ] ;
4 n =[2 ,5 ,2 ,2];
5 Cap=11;

The IterativeKnapsack.mod file using dataElements


1 i n t numbType = . . . ;
2 r a n g e Type = 1 . . numbType ;
3 f l o a t W[ Type ] = . . . ;
4 f l o a t B [ Type ] = . . . ;
5 f l o a t n [ Type ] = . . . ;
6 f l o a t Cap = . . . ;
7 dvar i n t+ X[ Type ] ;
8 e x e c u t e PRE_SETUP {
9 cplex . tilim = 100;
10 }
11 maximize
12 sum ( i i n Type ) B [ i ] ∗ X[ i ] ;
13

14 s u b j e c t to {
15 constraint_1 :
16 f o r a l l ( i i n Type ) {
17 X[ i ] <= n [ i ] ;
18 }
19 constraint_2 :
20 sum ( i i n Type ) W[ i ] ∗ X[ i ] <= Cap ;
21 }
22 main {
23 var o f i l e = new I l o O p l O u t p u t F i l e ( " R e s u l t . t x t " ) ;

Chapter 09 Page 11
Ho Chi Minh City International University Scheduling
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc

24 var S o u r c e = new I l o O p l M o d e l S o u r c e ( " I t e r a t i v e K n a p s a c k . mod" ) ;


25 var Data = new I l o O p l D a t a S o u r c e ( " Data1 . dat " ) ;
26 var Def = new I l o O p l M o d e l D e f i n i t i o n ( S o u r c e ) ;
27 var c p l e x = new I l o C p l e x ( ) ;
28 var Opl = new IloOplModel ( Def , c p l e x ) ;
29 Opl . addDataSource ( Data ) ;
30 Opl . g e n e r a t e ( ) ;
31 cplex . solve () ;
32

33 o f i l e . w r i t e l n ( "The 1 s t run : " )


34 o f i l e . w r i t e l n ( "The o b j e c t i v e v a l u e : " , c p l e x . getObjValue ( ) ) ;
35 o f i l e . w r i t e l n ( "The c a p a c i t y : " , Opl . Cap ) ;
36 f o r ( var i =1; i<=Opl . numbType ; i ++){
37 o f i l e . w r i t e l n ( "The v a l u e o f p r o d u c t type [ " , i , " ] : " , Opl .X[ i ] ) ;
38 }
39 o f i l e . w r i t e l n ( "−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−" ) ;
40 Opl . p o s t P r o c e s s ( ) ;
41

42 var D a t a E l t s = Opl . dataElements ;


43 var Opl2 = new IloOplModel ( Def , c p l e x ) ;
44 Opl2 . addDataSource ( D a t a E l t s ) ;
45 // M o d i f i e d t h e Data Capacity and Weight
46 D a t a E l t s . Cap = 1 2 ;
47 D a t a E l t s .W[ 1 ] = 3 ;
48 D a t a E l t s .W[ 2 ] = 3 ;
49 D a t a E l t s .W[ 3 ] = 3 ;
50 D a t a E l t s .W[ 4 ] = 3 ;
51 //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
52 Opl2 . g e n e r a t e ( ) ;
53 cplex . solve () ;
54 o f i l e . w r i t e l n ( "The 2nd run : " )
55 o f i l e . w r i t e l n ( "The o b j e c t i v e v a l u e : " , c p l e x . getObjValue ( ) ) ;
56 o f i l e . w r i t e l n ( "The c a p a c i t y : " , Opl2 . Cap ) ;
57 f o r ( var i =1; i<=Opl2 . numbType ; i ++){
58 o f i l e . w r i t e l n ( "The v a l u e o f p r o d u c t type [ " , i , " ] : " , Opl2 .X[ i ] ) ;
59 }
60 f o r ( var i =1; i<=Opl2 . numbType ; i ++){
61 o f i l e . w r i t e l n ( "The w e i g h t o f p r o d u c t type [ " , i , " ] : " , Opl2 .W[ i ] ) ;
62 }
63 o f i l e . close () ;
64

65 }

The IterativeKnapsack.mod file using different Datasource


1 i n t numbType = . . . ;
2 r a n g e Type = 1 . . numbType ;
3 f l o a t W[ Type ] = . . . ;

Chapter 09 Page 12
Ho Chi Minh City International University Scheduling
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc

4 f l o a t B [ Type ] = . . . ;
5 f l o a t n [ Type ] = . . . ;
6 f l o a t Cap = . . . ;
7 dvar i n t+ X[ Type ] ;
8 e x e c u t e PRE_SETUP {
9 cplex . tilim = 100;
10 }
11 maximize
12 sum ( i i n Type ) B [ i ] ∗ X[ i ] ;
13

14 s u b j e c t to {
15 constraint_1 :
16 f o r a l l ( i i n Type ) {
17 X[ i ] <= n [ i ] ;
18 }
19 constraint_2 :
20 sum ( i i n Type ) W[ i ] ∗ X[ i ] <= Cap ;
21 }
22 main {
23 var o f i l e = new I l o O p l O u t p u t F i l e ( " R e s u l t . t x t " ) ;
24 var S o u r c e = new I l o O p l M o d e l S o u r c e ( " I t e r a t i v e K n a p s a c k . mod" ) ;
25 var Data = new I l o O p l D a t a S o u r c e ( " Data1 . dat " ) ;
26 var Def = new I l o O p l M o d e l D e f i n i t i o n ( S o u r c e ) ;
27 var c p l e x = new I l o C p l e x ( ) ;
28 var Opl = new IloOplModel ( Def , c p l e x ) ;
29 Opl . addDataSource ( Data ) ;
30 Opl . g e n e r a t e ( ) ;
31 cplex . solve () ;
32

33 o f i l e . w r i t e l n ( "The 1 s t run : " )


34 o f i l e . w r i t e l n ( "The o b j e c t i v e v a l u e : " , c p l e x . getObjValue ( ) ) ;
35 o f i l e . w r i t e l n ( "The c a p a c i t y : " , Opl . Cap ) ;
36 f o r ( var i =1; i<=Opl . numbType ; i ++){
37 o f i l e . w r i t e l n ( "The v a l u e o f p r o d u c t type [ " , i , " ] : " , Opl .X[ i ] ) ;
38 }
39 f o r ( var i =1; i<=Opl . numbType ; i ++){
40 o f i l e . w r i t e l n ( "The w e i g h t o f p r o d u c t type [ " , i , " ] : " , Opl .W[ i ] ) ;
41 }
42 o f i l e . w r i t e l n ( "−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−" ) ;
43

44 Opl . p o s t P r o c e s s ( ) ;
45 var Data2 = new I l o O p l D a t a S o u r c e ( " Data2 . dat " ) ;
46 var Opl2 = new IloOplModel ( Def , c p l e x ) ;
47 Opl2 . addDataSource ( Data2 ) ;
48 Opl2 . g e n e r a t e ( ) ;
49 cplex . solve () ;
50

51 o f i l e . w r i t e l n ( "The 2nd run : " )

Chapter 09 Page 13
Ho Chi Minh City International University Scheduling
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc

52 o f i l e . w r i t e l n ( "The o b j e c t i v e v a l u e : " , c p l e x . getObjValue ( ) ) ;


53 o f i l e . w r i t e l n ( "The c a p a c i t y : " , Opl2 . Cap ) ;
54 f o r ( var i =1; i<=Opl2 . numbType ; i ++){
55 o f i l e . w r i t e l n ( "The v a l u e o f p r o d u c t type [ " , i , " ] : " , Opl2 .X[ i ] ) ;
56 }
57 f o r ( var i =1; i<=Opl2 . numbType ; i ++){
58 o f i l e . w r i t e l n ( "The w e i g h t o f p r o d u c t type [ " , i , " ] : " , Opl2 .W[ i ] ) ;
59 }
60 o f i l e . close () ;
61

62 }

Chapter 09 Page 14
Ho Chi Minh City International University Scheduling
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc

10 The 1st Example of solving the problem in 2 stages

The problem in this example includes two stages

• Stage 1: The manager try to maximize the purchased material with a given budget

• Stage 2: The manager try to maximize the benefit of a production planning

Problem Statement of stage 1


Index
m: index of material m = 1 · · · M
Parameters
dm demands of material m
pm price of material m
Budget the total budget
Decision variables
Xm : the purchased quantity of material m
Mathematical model

M
X
max Xm
m=1

subject to

X m ≥ dm ∀m
M
X
pm Xm ≤ Budget ∀m
m=1

Problem Statement of stage 2


Index
m: index of material m = 1 · · · M
p: index of product p = 1 · · · P
Parameters
Bp benefit of one product p
αpm number of material type m required for one unit type p
Qm the number of material type m
Decision variables
Yp : the number of products type p is manufactured
Mathematical model

P
X
max Bp Yp
p=1

Chapter 09 Page 15
Ho Chi Minh City International University Scheduling
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc

subject to
P
X
αpm Yp ≤ Qm ∀m
p=1

Process

• All parameters should be declared in one *.mod file containing the main block and
corresponding *.dat file of the 1st run.

• Data source for the 2nd run should use the data elements and is created from the
Opl.dataElements of the 1st run

• Assume X is solution of the opl1 problem. And it is used as parameter K for opl2
problem. So let data2 is the data Elements of opl2 problem. The value of X can be
assigned to K as follows
1 data2 .K=o p l 1 .X. s o l u t i o n V a l u e ;

Figure 3: Project Structure of Example 1

Chapter 09 Page 16
Ho Chi Minh City International University Scheduling
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc

File Stage1.dat
1 nbMat = 4 ;
2 nbPro = 3 ;
3 p = [1 ,2 ,1 ,2];
4 d = [100 ,200 ,200 ,100];
5 Budget= 1 5 0 0 ;
6 a =[[1 ,0 ,1 ,0] ,[1 ,1 ,0 ,0] ,[1 ,1 ,1 ,1]];
7 Ben = [ 1 , 1 , 2 ] ;

File Stage2.mod
1 i n t nbPro = . . . ;
2 i n t nbMat = . . . ;
3 r a n g e Mat = 1 . . nbMat ;
4 r a n g e Pro = 1 . . nbPro ;
5 f l o a t Ben [ Pro ] = . . . ;
6 f l o a t a [ Pro ] [ Mat ] = . . . ;
7 i n t Q[ Mat ] = . . . ;
8 dvar i n t+ Y[ Pro ] ;
9 maximize sum ( p i n Pro ) Ben [ p ] ∗Y[ p ] ;
10 s u b j e c t to {
11 f o r a l l (m i n Mat ) {
12 sum ( p i n Pro )Y[ p ] ∗ a [ p ] [m]<=Q[m] ;
13 }
14 }

Chapter 09 Page 17
Ho Chi Minh City International University Scheduling
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc

File Stage1.mod
1 /∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
2 ∗ OPL 1 2 . 9 . 0 . 0 Model
3 ∗ Author : kyphuc
4 ∗ C r e a t i o n Date : Jun 3 , 2020 a t 8 : 3 5 : 5 4 AM
5 ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
6 i n t nbMat = . . . ;
7 i n t nbPro = . . . ;
8 r a n g e Mat = 1 . . nbMat ;
9 r a n g e Pro = 1 . . nbPro ;
10 f l o a t p [ Mat ] = . . . ;
11 f l o a t d [ Mat ] = . . . ;
12 f l o a t Budget= . . . ;
13 f l o a t a [ Pro ] [ Mat ] = . . . ;
14 f l o a t Ben [ Pro ] = . . . ;
15 dvar i n t+ X[ Mat ] ;
16 maximize
17 sum ( m i n Mat ) X[m] ;
18

19 s u b j e c t to {
20 constraint_1 :
21 f o r a l l ( m i n Mat ) {
22 X[m] >= d [m] ;
23 }
24 constraint_2 :
25 sum ( m i n Mat ) p [m] ∗ X[m] <= Budget ;
26 }
27 main {
28 //−−−−C r e a t e t h e model f o r t h e 1 s t s t a g e −−−−−−−−−−−−−−−−−
29 var o f i l e = new I l o O p l O u t p u t F i l e ( " R e s u l t . t x t " ) ;
30 var Src_Stg1 = new I l o O p l M o d e l S o u r c e ( " S t a g e 1 . mod" ) ;
31 var Data1_Stg1 = new I l o O p l D a t a S o u r c e ( " S t a g e 1 . dat " ) ;
32 var Def_Stg1 = new I l o O p l M o d e l D e f i n i t i o n ( Src_Stg1 ) ;
33 var Cplex_Stg1 = new I l o C p l e x ( ) ;
34 var Opl_Stg1 = new IloOplModel ( Def_Stg1 , Cplex_Stg1 ) ;
35 Opl_Stg1 . addDataSource ( Data1_Stg1 ) ;
36 Opl_Stg1 . g e n e r a t e ( ) ;
37 Cplex_Stg1 . s o l v e ( ) ;
38 f o r ( var i = 1 ; i <= Opl_Stg1 . nbMat ; i ++) {
39 o f i l e . w r i t e l n ( "The q u a n t i t y o f m a t e r i a l type [ " , i , " ] : " , Opl_Stg1 .X[ i ] .
solutionValue ) ;
40 }
41 w r i t e l n ( Opl_Stg1 .X) ;
42

43 //−−−−−C r e a t e t h e model f o r t h e 2nd s t a g e −−−−−−−−−−−−−−−−


44 //−−−−−−−−−−−−−−−−−−−−−−−−−−−−
45 var Src_Stg2 = new I l o O p l M o d e l S o u r c e ( " S t a g e 2 . mod" ) ;

Chapter 09 Page 18
Ho Chi Minh City International University Scheduling
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc

46 var Def_Stg2 = new I l o O p l M o d e l D e f i n i t i o n ( Src_Stg2 ) ;


47 var Cplex_Stg2 = new I l o C p l e x ( ) ;
48 var Opl_Stg2 = new IloOplModel ( Def_Stg2 , Cplex_Stg2 ) ;
49 var Data_Stg2 = new IloOplDataElements ( ) ;
50 var D a t 1 E l t s = Opl_Stg1 . dataElements ;
51

52 Data_Stg2 . nbPro = D a t 1 E l t s . nbPro ;


53 Data_Stg2 . nbMat = D a t 1 E l t s . nbMat ;
54 Data_Stg2 .Q=Opl_Stg1 .X. s o l u t i o n V a l u e ;
55 Data_Stg2 . a = D a t 1 E l t s . a ;
56 Data_Stg2 . Ben = D a t 1 E l t s . Ben ;
57 Opl_Stg2 . addDataSource ( Data_Stg2 ) ;
58 Opl_Stg2 . g e n e r a t e ( ) ;
59 Cplex_Stg2 . s o l v e ( ) ;
60 f o r ( var i = 1 ; i <= Opl_Stg2 . nbPro ; i ++) {
61 o f i l e . w r i t e l n ( "The q u a n t i t y o f p r o d u c t type [ " , i , " ] : " , Opl_Stg2 .Y[ i ] .
solutionValue ) ;
62 }
63 o f i l e . close () ;
64 }
65

11 The 2nd Example of solving the problem in 2 stages

Approach

• In this example, one master problem is generated. The master problem will create
several configuration to solve sub problems

• Data are passed to sub problem through dataElements

Figure 4: Project Structure of Example 2

commonDef.mod

Chapter 09 Page 19
Ho Chi Minh City International University Scheduling
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc

1 i n t nbMat = . . . ;
2 i n t nbPro = . . . ;
3 r a n g e Mat = 1 . . nbMat ;
4 r a n g e Pro = 1 . . nbPro ;
5 f l o a t p [ Mat ] = . . . ;
6 f l o a t d [ Mat ] = . . . ;
7 f l o a t Budget= . . . ;
8 f l o a t a [ Pro ] [ Mat ] = . . . ;
9 f l o a t Ben [ Pro ] = . . . ;

commonDat.dat
1 nbMat = 4 ;
2 nbPro = 3 ;
3 p = [1 ,2 ,1 ,2];
4 d = [100 ,200 ,200 ,100];
5 Budget= 1 5 0 0 ;
6 a =[[1 ,0 ,1 ,0] ,[1 ,1 ,0 ,0] ,[1 ,1 ,1 ,1]];
7 Ben = [ 1 , 1 , 2 ] ;

controlFlow.mod
1 i n c l u d e "commonDef . mod" ;
2 main {
3 thisOplModel . g e n e r a t e ( ) ;
4 data=thisOplModel . dataElements ;
5 var s t g 1 = new I l o O p l R u n C o n f i g u r a t i o n ( " S t a g e 1 . mod" ) ;
6 s t g 1 . oplModel . addDataSource ( data ) ;
7 w r i t e l n ( data . Ben ) ;
8 s t g 1 . oplModel . g e n e r a t e ( ) ;
9 stg1 . cplex . solve () ;
10 s t g 1 . oplModel . p o s t P r o c e s s ( ) ;
11

12 //−C r e a t e 2nd c o n f i g u r a t i o n
13 var s t g 2 = new I l o O p l R u n C o n f i g u r a t i o n ( " S t a g e 2 . mod" ) ;
14 data .Q=s t g 1 . oplModel .X. s o l u t i o n V a l u e ;
15 s t g 2 . oplModel . addDataSource ( data ) ;
16 s t g 2 . oplModel . g e n e r a t e ( ) ;
17 stg2 . cplex . solve () ;
18 s t g 2 . oplModel . p o s t P r o c e s s ( ) ;
19 }

Stage1.mod
1 i n c l u d e "commonDef . mod" ;
2 dvar i n t+ X[ Mat ] ;
3 maximize
4 sum ( m i n Mat ) X[m] ;
5

6 s u b j e c t to {

Chapter 09 Page 20
Ho Chi Minh City International University Scheduling
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc

7 constraint_1 :
8 f o r a l l ( m i n Mat ) {
9 X[m] >= d [m] ;
10 }
11 constraint_2 :
12 sum ( m i n Mat ) p [m] ∗ X[m] <= Budget ;
13 }
14 execute {
15 f o r ( var i =1; i<=nbMat ; i ++){
16 w r i t e l n ( "X−−" , i , "−−" ,X[ i ] ) ;
17 }
18 }

Stage2.mod
1 i n c l u d e "commonDef . mod" ;
2 i n t Q[ Mat ] = . . . ;
3 dvar i n t+ Y[ Pro ] ;
4 maximize sum ( p i n Pro ) Ben [ p ] ∗Y[ p ] ;
5 s u b j e c t to {
6 f o r a l l (m i n Mat ) {
7 sum ( p i n Pro )Y[ p ] ∗ a [ p ] [m]<=Q[m] ;
8 }
9 }
10 execute {
11 f o r ( var i =1; i<=nbPro ; i ++){
12 w r i t e l n ( "Y−−" , i , "−−" ,Y[ i ] ) ;
13 }
14 }

12 Column Generation

This section get the vellino example from the software.


Problem Statement
This configuration problem involves placing objects of different materials (glass, plastic,
steel, wood, and copper) into bins of various types (red, blue, green), subject to capacity
(each bin type has a maximum) and compatibility constraints. All objects must be placed
into a bin and the total number of bins must be minimized.
Compatibility constraints The compatibility constraints are:

• Red bins cannot contain plastic or steel.

• Blue bins cannot contain wood or plastic.

• Green bins cannot contain steel or glass.

Chapter 09 Page 21
Ho Chi Minh City International University Scheduling
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc

• Red bins contain at most one wooden component.

• Green bins contain at most two wooden components.

• Wood requires plastic.

• Glass excludes copper.

• Copper excludes plastic.

Decomposition and column generation


To solve this problem, the technique used is known as column generation. Basically, the
problem is a linear set covering problem where each decision variable corresponds to a pos-
sible configuration. An auxiliary problem is to generate all the possible configurations.
Constraint programming is used to solve this configuration generation problem, as most of
the compatibility constraints are logical constraints for which the CP Optimizer engine offers
good support.
In this example
card function: is an OPL function to return the number of items in a set or a range
item(set,i) Gives access to the i-th item of a set or to a tuple from a tuple set. When
accessing the n-th item, counting starts at 0.

Figure 5: Project Structure of Vellino

vellinocommon.mod
1 { s t r i n g } Colors = . . . ;
2 int capacity [ Colors ] = . . . ;

Chapter 09 Page 22
Ho Chi Minh City International University Scheduling
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc

3 i n t maxCapacity = max( c i n C o l o r s ) c a p a c i t y [ c ] ;
4

6 { s t r i n g } Components = . . . ;
7 i n t demand [ Components ] = . . . ;
8 i n t maxDemand = max( c i n Components ) demand [ c ] ;
9 t u p l e Bin {
10 key i n t i d ;
11 string color ;
12 i n t n [ Components ] ;
13 };
14 { Bin } Bins = . . . ;
15 i n t nBins = c a r d ( Bins ) ;

vellinocommon.dat
1 Colors = { " red " , " blue " , " green " } ;
2 capacity = [ 3 , 1 , 4 ] ;
3

4 Components = { " g l a s s " , " p l a s t i c " , " s t e e l " , "wood" , " c o p p e r " } ;
5 demand = [ 2 , 4 , 3 , 6 , 4 ] ;
6

7 Bins = { } ;

vellinogenBin.mod
1 // G e n e r a t i n g t h e Bins i n V e l l i n o ’ s Problem ( v e l l i n o g e n B i n . mod) .
2 u s i n g CP;
3

4 i n c l u d e " vellinocommon . mod" ;


5

6 execute {
7 w r i t e l n ( " genbin " ) ;
8 }
9

10 i n t maxcolor = c a r d ( C o l o r s ) −1;
11 r a n g e RColors = 0 . . maxcolor ;
12 i n t c a p a c i t y _ i n t _ i d x [ r i n RColors ] = c a p a c i t y [ item ( C o l o r s , r ) ] ;
13 dvar i n t c o l o r i n RColors ;
14 dvar i n t n [ Components ] i n 0 . . maxCapacity ;
15 s u b j e c t to {
16 1 <= sum ( c i n Components ) n [ c ] ;
17 sum ( c i n Components ) n [ c ] <= c a p a c i t y _ i n t _ i d x [ c o l o r ] ;
18 c o l o r == ord ( C o l o r s , " r e d " ) =>
19 n [ " p l a s t i c " ] == 0 && n [ " s t e e l " ] == 0 && n [ "wood" ] <= 1 ;
20 c o l o r == ord ( C o l o r s , " b l u e " ) =>
21 n [ " p l a s t i c " ] == 0 && n [ "wood" ] == 0 ;
22 c o l o r == ord ( C o l o r s , " g r e e n " ) =>
23 n [ " g l a s s " ] == 0 && n [ " s t e e l " ] == 0 && n [ "wood" ] <= 2 ;

Chapter 09 Page 23
Ho Chi Minh City International University Scheduling
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc

24 n [ "wood" ] >= 1 => n [ " p l a s t i c " ] >= 1 ;


25 n [ " g l a s s " ] == 0 | | n [ " c o p p e r " ] == 0 ;
26 n [ " c o p p e r " ] == 0 | | n [ " p l a s t i c " ] == 0 ;
27 };
28 i n t newId = c a r d ( Bins ) +1;
29 s t r i n g c o l o r S t r i n g V a l u e = item ( C o l o r s , c o l o r ) ;
30

31 execute {
32 w r i t e l n ( "Found b i n with c o l o r : " , c o l o r S t r i n g V a l u e , " and c o n t a i n i n g
elements " , n . solutionValue ) ;
33 }

vellinochooseBin.mod
1 // Choosing t h e Bins i n V e l l i n o ’ s Problem ( v e l l i n o c h o o s e B i n . mod) .
2

3 i n c l u d e " vellinocommon . mod" ;


4

5 execute {
6 writeln ( " choosebin " ) ;
7 }
8 dvar i n t produce [ Bins ] i n 0 . . maxDemand ;
9 minimize
10 sum ( b i n Bins ) produce [ b ] ;
11 s u b j e c t to {
12 f o r a l l ( c i n Components )
13 demandCt : sum ( b i n Bins ) b . n [ c ] ∗ produce [ b ] == demand [ c ] ;
14 };
15 execute {
16 w r i t e l n ( " Chosen : " ) ;
17 f o r ( var b i n Bins )
18 w r i t e l n ( b , " : " , produce [ b ] ) ;
19 }

vellino.mod
1 i n c l u d e " vellinocommon . mod" ;
2

3 main {
4 var master = thisOplModel ;
5 master . g e n e r a t e ( ) ;
6 var data = master . dataElements ;
7

8 var genBin = new I l o O p l R u n C o n f i g u r a t i o n ( " v e l l i n o g e n B i n . mod" ) ;


9 genBin . oplModel . addDataSource ( data ) ;
10 genBin . oplModel . g e n e r a t e ( ) ;
11 genBin . cp . s t a r t N e w S e a r c h ( ) ;
12 w h i l e ( genBin . cp . next ( ) ) {
13 genBin . oplModel . p o s t P r o c e s s ( ) ;

Chapter 09 Page 24
Ho Chi Minh City International University Scheduling
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc

14 data . Bins . add ( genBin . oplModel . newId ,


15 genBin . oplModel . c o l o r S t r i n g V a l u e ,
16 genBin . oplModel . n . s o l u t i o n V a l u e ) ;
17 }
18 genBin . cp . endSearch ( ) ;
19 genBin . end ( ) ;
20 var c h o o s e B i n = new I l o O p l R u n C o n f i g u r a t i o n ( " v e l l i n o c h o o s e B i n . mod" ) ;
21 chooseBin . cplex = cplex ;
22 c h o o s e B i n . oplModel . addDataSource ( data ) ;
23 c h o o s e B i n . oplModel . g e n e r a t e ( ) ;
24 i f ( chooseBin . cplex . s o l v e ( ) ) {
25 c h o o s e B i n . oplModel . p o s t P r o c e s s ( ) ;
26 }
27 c h o o s e B i n . end ( ) ;
28 }

Chapter 09 Page 25

You might also like