Advanced CPLEX
Advanced CPLEX
Contents
1 IBM ILOG scripts 2
3 Flow Control 3
8 Important Note 10
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
• The variables declared in a execute block are local and cannot be accessed by
other block
• The scripting context within an execute block corresponds to the model declara-
tions in *.mod file.
• 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
The accessing for constraint, variables, dataElement, etc., in OPL model is only available
after the OPL model is generated The basic flow control structure
Chapter 09 Page 3
Ho Chi Minh City International University Scheduling
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc
• Step 5: Make opl model based on model definition and solving engine
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
• This associates to .dat files (if they exist) to run the model.
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
1 f l o a t i n i t X [ rangeX ] = . . . ;
Chapter 09 Page 6
Ho Chi Minh City International University Scheduling
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc
• A preference of -1 means that the member is to be absolutely excluded from the conflict.
• Preferences of positive value represent an ordering by which the conflict refiner will
give preference to the members.
• 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
• Step 4. Solve the current optimization model with the current data:
Chapter 09 Page 8
Ho Chi Minh City International University Scheduling
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc
• 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.
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 ] = . . . ;
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
• 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
• 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 ( ) ;
Chapter 09 Page 10
Ho Chi Minh City International University Scheduling
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc
• Data elements
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
65 }
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
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
Chapter 09 Page 13
Ho Chi Minh City International University Scheduling
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc
62 }
Chapter 09 Page 14
Ho Chi Minh City International University Scheduling
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc
• Stage 1: The manager try to maximize the purchased material with a given budget
M
X
max Xm
m=1
subject to
X m ≥ dm ∀m
M
X
pm Xm ≤ Budget ∀m
m=1
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 ;
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
Chapter 09 Page 18
Ho Chi Minh City International University Scheduling
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc
Approach
• In this example, one master problem is generated. The master problem will create
several configuration to solve sub problems
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
Chapter 09 Page 21
Ho Chi Minh City International University Scheduling
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc
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
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
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
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
Chapter 09 Page 24
Ho Chi Minh City International University Scheduling
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc
Chapter 09 Page 25