06 Miniguide For CPLEX Usage: Phan Nguyen Ky Phuc February 25, 2020
06 Miniguide For CPLEX Usage: Phan Nguyen Ky Phuc February 25, 2020
Contents
1 Structure 1
3 Example 3
4 FAQs 6
4.1 Question 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
4.2 Question 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
4.3 Question 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
4.4 Question 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4.5 Question 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
4.6 Question 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
4.7 Question 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4.8 Question 8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1 Structure
• File model (*.mod): Use to describe the model, and declare parameters
1
Ho Chi Minh City International University Deterministic Models in Operation Research
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc
• File data (*.dat): Make the connection between data store in database file, write the output to the
database file
When writing the file.mod, the program should follow the order below:
• Declare the decision variable (keyword: dvar common types: float, float+,int, int+, boolean)
• Set up PRE SETUP ( keyword: execute common settings : cplex.epgap , cplex.tilim) . In case the size
of the problem is too large, this setting is necessary to find the solution within time limitation.
• Connect the database file with the CPLEX program. In case the data is stored in EXCEL file, the
following keyword is used( keyword: SheetConnection).
– All scalar parameters, i.e. parameters have only one value, should be put in a shame sheet.
– All one dimensional data should be put in a shame sheet. Each datum should occupy a column
of sheet
– Two dimensional data should be put in separate sheet.
– Example: Capacity from SheetRead(Data,"SheetName !A1: B10");
• When the dimension of solution is greater than 2. They require to be unfolded to write to the excel
file.
3 Example
Consider the Knapsack Problem with 4 item types, where the parameters of weight, benefit, and maximum
number for each type are given as follow:
Weight={1,3,4,5}, Benefit={3,5,7,9} ,Max Number={1,2,2,1}
The maximum capacity of the system is 10. Find the maximum benefit that we can achieve
Mathematical Model
Size of the problem
i : index of the item type
Parameters
Bi : the benefit of an item type i
Wi : the weight of an item type i
ni : the maximum quantity of item type i
Cap: maximum capacity of the system
Decision variables
Xi : the number of item type i is adopted
Objective:
X
max Z = Bi Xi
i
Constrainst:
0 ≤ Xi ≤ ni
X
Wi Xi ≤ Cap
∀i
15 s u b j e c t to {
16 constraint_1 :
17 f o r a l l ( i i n Type ) {
18 X[ i ] <= n [ i ] ;
19 }
20 constraint_2 :
21 sum ( i i n Type ) W[ i ] ∗ X[ i ] <= Cap ;
22 }
23
24 e x e c u t e WRITE_RESULT{
25 v a r 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 " ) ;
26 o f i l e . w r i t e l n ( c p l e x . getObjValue ( ) ) ; / / Get t h e v a l u e o f t h e o b j e c t i v e f u n c t i o n
27 f o r ( i i n Type ) {
28 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 t y p e [ " , i , " ] : " ,X[ i ] ) ;
29 }
30 o f i l e . w r i t e l n ( "−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−" ) ;
31 o f i l e . close () ;
32 }
File(*.xlsx)
Consider the Assignment Problem with 4 workers, 4 jobs.Find the maximum benefit that we can achieve. If
the benefit assignment matrix is given follows:
Worker/Job J1 J2 J3 J4
W1 1 4 5 0
W2 3 2 1 4
W3 1 4 5 1
W4 6 2 5 3
The constraints
Mathematical Programming
Size of the problem
i : index of the worker
j : index of the job
Parameters
Bij : the benefit if the worker i is assigned to job j
Decision variables
Xij : binary variable , Xij = 1 if the worker i is assigned to the job j otherwise Xij = 0.
Objective:
XX
max Z = Wij Xij
∀i ∀j
Constrainst:
X
Wij Xij = 1, ∀i
∀j
X
Wij Xij = 1, ∀
∀i
15 f o r a l l ( i i n Worker ) {
16 sum ( j i n Job )X[ i ] [ j ] == 1 ;
17 }
18 constraint_2 :
19 f o r a l l ( j i n Job ) {
20 sum ( i i n Worker )X[ i ] [ j ]==1;
21 }
22 }
23 e x e c u t e WRITE_RESULT{
24 v a r 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 " ) ;
25 o f i l e . w r i t e l n ( c p l e x . getObjValue ( ) ) ; / / Get t h e v a l u e o f t h e o b j e c t i v e f u n c t i o n
26 f o r ( i i n Worker ) f o r ( j i n Job ) {
27 i f (X[ i ] [ j ]==1) {
28 o f i l e . w r i t e l n ( "Work [ " , i , " ] w i l l be a s s i g n e d t o Job [ " , j , " ] " ) ;
29 }
30 }
31 o f i l e . w r i t e l n ( "−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−" ) ;
32 o f i l e . close () ;
33 }
34
File (*.dat)
1 S h e e t C o n n e c t i o n MyData ( " a s s g i n m e n t . x l s x " ) ;
2 Numbworker from SheetRead ( MyData , " c o n s t a n t ! B1" ) ;
3 Numbjob from SheetRead ( MyData , " c o n s t a n t ! B2" ) ;
4 B e n e f i t from SheetRead ( MyData , " S h e e t 2 ! B2 : E5" ) ;
5 X t o S h e e t W r i t e ( MyData , " S h e e t 3 ! A1 : D4" ) ;
File (*.xlsx)
4 FAQs
4.1 Question 1
File (*.xlsx)
A B C D E F G
(a) sheet "Scalar" of “MatrixConvert.xlsx” file
1 P1 P2 P3 P4 P5
A B 2 Month 1 1 2 4 5 5
Level 1
1 Number of Months 2 3 Month 2 2 3 5 4 4
2 Number of Levels 3 4 Month 1 2 4 3 4 5
Level 2
3 Number of Products 5 5 Month 2 1 1 1 3 2
6 Month 1 2 4 3 6 1
Level 3
7 Month 2 2 3 5 7 2
File(*.mod)
1 i n t numbMonth = . . . ;
2 i n t numbProduct = . . . ;
3 i n t numbLevel = . . . ;
4 r a n g e Month = 1 . . numbMonth ;
5 r a n g e Product = 1 . . numbProduct ;
6 r a n g e L e v e l = 1 . . numbLevel ;
7 r a n g e TempRange = 1 . . numbLevel ∗ numbMonth ; / / c r e a t e
8 f l o a t TempMatrix [ TempRange ] [ Product ] = . . . ;
9 f l o a t A[ i i n L e v e l ] [ j i n Month ] [ l i n Product ] = TempMatrix [ ( i − 1 ) ∗ numbMonth + j ] [ l ] ;
File(*.dat)
4.2 Question 2
File (*.xlsx)
File(*.mod)
1 i n t numbMonth = . . . ;
2 i n t numbProduct = . . . ;
A B
1 Number of Quarters 2
2 Number of Months 2
3 Number of Levels 3
4 Number of Products 5
(b) sheet "4DimData" of “MatrixConvert.xlsx” file
A B C D E F G H
1 P1 P2 P3 P4 P5
2 Month 1 1 2 4 5 5
Level 1
3 Month 2 2 3 5 4 4
4 Month 1 2 4 3 4 5
Quarter 1 Level 2
5 Month 2 1 1 1 3 2
6 Month 1 2 4 3 6 1
Level 3
7 Month 2 2 3 5 7 2
8 Month 1 4 5 7 8 8
Level 1
9 Month 2 3 1 5 6 11
10 Month 1 4 2 5 6 4
Quarter 2 Level 2
11 Month 2 6 4 13 8 9
12 Month 1 11 2 11 3 8
Level 3
13 Month 2 2 3 5 9 7
3 i n t numbLevel = . . . ;
4 i n t numbQuarter = . . . ;
5 r a n g e Month = 1 . . numbMonth ;
6 r a n g e Product = 1 . . numbProduct ;
7 r a n g e L e v e l = 1 . . numbLevel ;
8 r a n g e Q u a r t e r = 1 . . numbQuarter ;
9 r a n g e TempRange = 1 . . numbQuarter ∗ numbLevel ∗ numbMonth ; / / c r e a t e temporary m a t r i x
10 f l o a t TempMatrix [ TempRange ] [ Product ] = . . . ;
11 f l o a t A[ h i n Q u a r t e r ] [ i i n L e v e l ] [ j i n Month ] [ l i n Product ] = TempMatrix [ ( h − 1 ) ∗
numbLevel ∗ numbMonth + ( i − 1 ) ∗ numbMonth + j ] [ l ] ;
File(*.dat)
4.3 Question 3
Ask:How can we setup the constraints on the index with simple filter when using forall ?
Answer: Using following format
1 f o r a l l ( i i n F a c i l i t y , j i n Product : filter )
For example
1 f o r a l l ( i i n F a c i l i t y , j i n Product : i <j )
2 f o r a l l ( i i n F a c i l i t y , j i n Product : Param [ i ] [ j ]==0)
3 f o r a l l ( i i n F a c i l i t y , j i n Product : Param [ i ] [ j ]>=0)
Note: when the filter condition becomes complex, it is required if-then command
1 f o r a l l ( i in job ) {
2 i f ( ( sum j i n j o b ) Param [ i ] [ j ]==0) {
3 Fin [ i ]==Pro [ i ] ;
4 }
5 }
4.4 Question 4
Ask: How can we express the prerequisite constraints or set constraints in CPLEX
Answer: Convert the constraints into the matrix form
Example
Given the project includes 5 activities as figure above. Each activity will have its processing time. Find the
1 3 5
The most difficult part of this problem is to deal with the order of the activities. To handle this, transform
the graph into the prerequisite matrix.
J/J 1 2 3 4 5
(b) Processing Time Matrix
1 0 0 0 0 0
2 0 0 0 0 0 Action 1 2 3 4 5
3 1 0 0 0 0 Processing Time 3 2 4 5 8
4 1 0 0 0 0
5 0 1 1 1 0
In the prerequisit matrix P re(i, j) = 1 means that activity j must be finished right before activity i.
File(*.mod)
1 i n t numbJob = . . . ;
2 r a n g e j o b = 1 . . numbJob ;
3 i n t Pro [ j o b ] = . . . ;
4 i n t Pre [ j o b ] [ j o b ] = . . . ;
5 dvar f l o a t+ Fin [ j o b ] ;
6 m i n i m i z e sum ( i i n j o b ) Fin [ i ] ;
7 s u b j e c t to {
8 f o r a l l ( i i n job , j i n j o b : Pre [ i ] [ j ]==1)
4.5 Question 5
In using the implication, the left side of => must be the variable, i.e., X[i].
4.6 Question 6
Ask: Solve the transportation problem of the supply chain below: Answer:
1 1 1
2 2 2
3 3 3
Manufacturers Distributors Retailers
Parameters
M Dij : unit transportation cost between manufacturer i and distributor j
CapM Dij : capacity of arc between manufacturer i and distributors j
Variables
Xij = quantity of product from manufacturer i and distribution center j
Yjk = quantity of product from distribution center j and retailer k
Zj = 1 if the distribution center j is open, Zj = 0 otherwise
X XX XX
max Z = Z j Fj + Xij M Dij + Yjk DRjk
j i j j k
subject to:
X
Xij = Mi , ∀i
j
X X
Xij = Yjk , ∀j
i k
X
Yjk = Rk , ∀i
j
CapM D 1 2 3 CapDK 1 2 3
1 10000 10000 300 1 10000 10000 1000
2 10000 10000 400 2 10000 10000 1000
3 10000 10000 200 3 500 600 200
(c) One Dimensional Data (d) DR Distrance Matrix
Mi Fj Rk DR 1 2 3
300 3000 250 1 20 17 15
400 4000 250 2 13 22 20
500 100000 700 3 1 2 1
(e) MD Distance Matrix
MD 1 2 3
1 20 17 1
2 13 22 1
3 15 19 1
File (*.mod)
1 i n t numbManu = . . . ;
2 i n t numbDist = . . . ;
3 i n t numbRetl = . . . ;
4 r a n g e Manu= 1 . .numbManu ;
5 r a n g e D i s t = 1 . . numbDist ;
6 r a n g e R e t l = 1 . . numbRetl ;
7 f l o a t MD[ Manu ] [ D i s t ] = . . . ;
8 f l o a t CapMD[ Manu ] [ D i s t ] = . . . ;
9 f l o a t DR[ D i s t ] [ R e t l ] = . . . ;
10 f l o a t CapDR [ D i s t ] [ R e t l ] = . . . ;
11 f l o a t F[ Dist ] = . . . ;
12 f l o a t M[ Manu ] = . . . ;
13 f l o a t R[ R e t l ] = . . . ;
14 dvar f l o a t+ X[ Manu ] [ D i s t ] ;
15 dvar f l o a t+ Y[ D i s t ] [ R e t l ] ;
16 dvar b o o l e a n Z [ D i s t ] ;
17 minimize (
18 sum ( j i n D i s t )F [ j ] ∗ Z [ j ]+sum ( i i n Manu , j i n D i s t )X[ i ] [ j ] ∗MD[ i ] [ j ]+sum ( j i n D i s t , k i n R e t l )Y[
j ] [ k ] ∗DR[ j ] [ k ] ) ;
19 s u b j e c t to {
20 f o r a l l ( i i n Manu)
21 sum ( j i n D i s t )X[ i ] [ j ]==M[ i ] ;
22
23 f o r a l l (k in Retl )
24 sum ( j i n D i s t )Y[ j ] [ k]==R[ k ] ;
25
26 f o r a l l ( i i n Manu , j i n D i s t )
27 X[ i ] [ j ]<=CapMD[ i ] [ j ] ;
28
29 f o r a l l ( j in Dist , k in Retl )
30 Y[ j ] [ k]<=CapDR [ j ] [ k ] ;
31
32 f o r a l l ( j in Dist )
33 sum ( i i n Manu)X[ i ] [ j ]==sum ( k i n R e t l )Y[ j ] [ k ] ;
34
35 f o r a l l ( j i n D i s t , i i n Manu)
36 Z [ j ]==0=>X[ i ] [ j ]==0;
37 }
File ( *.dat)
4.7 Question 7
2
4 8
1 5
2
5 1
3 4 4
P re 1 2 3 4 5 P ost 1 2 3 4 5
1 0 0 0 0 0 1 0 1 1 0 0
2 1 0 0 0 0 2 0 0 0 1 1
3 1 0 0 0 0 3 0 0 0 1 0
4 0 1 1 0 0 4 0 0 1 0 1
5 0 1 0 1 0 5 0 0 0 0 0
(c) The distance matrix
Distance 1 2 3 4 5
1 1000 1000 1000 1000 1000
2 1000 1000 1000 2 8
3 1000 1000 1000 4 1000
4 1000 1000 1000 1000 1
5 1000 1000 1000 1000 1000
File(*.mod)
1 i n t numbNode = . . . ;
2 r a n g e Node = 1 . . numbNode ;
3 f l o a t D i s t [ Node ] [ Node ] = . . . ;
4 f l o a t Pre [ Node ] [ Node ] = . . . ;
5 f l o a t Post [ Node ] [ Node ] = . . . ;
6 dvar b o o l e a n x [ Node ] [ Node ] ;
7 m i n i m i z e sum ( i i n Node , j i n Node ) x [ i ] [ j ] ∗ D i s t [ i ] [ j ] ;
8 s u b j e c t to {
9 sum ( i i n 2 . . numbNode−1)x [ 1 ] [ i ]==1;
10 sum ( i i n 2 . . numbNode−1)x [ i ] [ numbNode]==1;
11
12 f o r a l l ( i i n 2 . . numbNode−1){
13 sum ( j i n Node ) x [ j ] [ i ]==sum ( j i n Node ) x [ i ] [ j ] ;
14 }
15
16 f o r a l l ( i i n Node , j i n Node ) {
17 x [ j ] [ i ]<=Pre [ i ] [ j ] ;
18 x [ i ] [ j ]<=Post [ i ] [ j ] ;
19 }
20 }
File(*.dat)
1 S h e e t C o n n e c t i o n Data ( " S h o r t e s t P a t h . x l s x " ) ;
2 numbNode=5;
3 Pre from SheetRead ( Data , " S h e e t 1 ! B2 : F6" ) ;
4 Post from SheetRead ( Data , " S h e e t 1 ! B10 : F14" ) ;
5 D i s t from SheetRead ( Data , " S h e e t 2 ! B2 : F6" ) ;
4.8 Question 8
Problem: Sometime the result should be interpreted into the readable form. For example, in the case of VRP
problem, the solutions are often expressed as X[i][j] = 0 or 1. It is often that only values with X[i][j] = 1
are concerned. These values can be written to a text file as follows:
1 e x e c u t e WRITE_RESULT{
2 v a r 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 " ) ;
3 o f i l e . w r i t e l n ( c p l e x . getObjValue ( ) ) ; / / Get t h e v a l u e o f t h e o b j e c t i v e f u n c t i o n
4 f o r ( i in rangeI ) f o r ( j in rangeJ ) {
5 i f ( x [ i ] [ j ]==1) {
6 o f i l e . w r i t e l n ( "The v a l u e o f X[ " , i , " ] [ " , j , " ] : " , x [ i ] [ j ] ) ;
7 }
8 }
9 o f i l e . w r i t e l n ( "−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−" ) ;
10 o f i l e . close () ;
11 }
Problem: Sometimes it is exhausted to enumerate all data, due to the data size. In this case, it is better
to use tuple approach to handle this. The tuple can be understood as a user-defined type. The tuple is
especially useful when the data are spare.
Step in using tuple:
Example:
Consider the Assignment problem when the size is 10 jobs, 10 workers. In this problem, it is often that given
a specific worker, he can be only in charge of some jobs. Other jobs are unsuitable for him.
In traditional approach, the benefit of these jobs will be assigned to −∞ in the benefit matrix. If we declare
a full benefit matrix, it requires 10 × 10 entries and the same number of variables. The situation is even
worse when the size of the problem increase to 1000 × 1000, it requires 106 entries.
To reduce the efforts for data input as well as the number of variables, tuple is used.
The framework for using tuple is given as follows:
1 t u p l e tupleName {
2 dataType a t t r i b u t e 1 ;
3 dataType a t t r i b u t e 2 ;
4 };
5 s e t o f ( tupleName ) t u p l e S e t = . . . ;
6 f l o a t ParameterA [ t u p l e S e t ] = . . . ;
7 dvar b o o l e a n X[ t u p l e S e t ] ;
The tuple set includes all tuples that will be used as index . Consider the Assignment Problem with Benefit
Data given as tuple:
A B C
1 Worker Job Benefit
2 1 1 4
3 1 2 3
(a) The data in "Sheet1.xlsx" 4 1 3 7
5 1 4 5
A B
6 2 2 7
1 numJob 5
7 2 3 5
2 numWorker 5
8 3 3 8
9 3 4 6
10 4 4 7
11 4 5 9
12 5 4 7
13 5 5 9
File(*.mod)
1 i n t numJob = . . . ;
2 i n t numWorker = . . . ;
3 t u p l e WorkJob{
4 i n t Worker ;
5 i n t Job ;
6 }
7 r a n g e Job = 1 . . numJob ;
8 r a n g e Worker = 1 . . numWorker ;
9 s e t o f ( WorkJob ) WorkJobSet = . . . ;
10 f l o a t B e n e f i t [ WorkJobSet ] = . . . ;
11 dvar b o o l e a n X[ WorkJobSet ] ;
12
13 e x e c u t e PRE_PROCESSING {
14 c p l e x . epgap = 0 . 0 0 1 ;
15 c p l e x . t i l i m = 60∗60 ;
16 }
17 maximize sum(<w, j >i n WorkJobSet ) B e n e f i t [<w, j >]∗X[<w, j > ] ;
18 s u b j e c t to {
19 constraint_1 :
20 f o r a l l ( j i n Job ) {
21 sum(<w, j > i n WorkJobSet )X[<w, j >]==1;
22 }
23 constraint_2 :
24 f o r a l l (w i n Worker ) {
25 sum(<w, j > i n WorkJobSet )X[<w, j >]==1;
26 }
27 }
28 e x e c u t e WRITE_RESULT{
29 v a r 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 o f i l e . w r i t e l n ( "The o b j e c t i v e f u n c t i o n v a l u e : " , c p l e x . getObjValue ( ) ) ; / / Get t h e v a l u e o f
the o b j e c t i v e f u n c t i o n
31 f o r ( v a r r i n WorkJobSet ) {
32 i f (X[ r ]==1) {
33 o f i l e . w r i t e l n ( " Worker [ " , r . Worker , " ] w i l l be a s s i g n e d t o Job [ " , r . Job , " ] " ) ;
34 }
35 }
36 o f i l e . w r i t e l n ( "−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−" ) ;
37 o f i l e . close () ;
38 }
File(*.dat)
1 S h e e t C o n n e c t i o n TupleData ( " AssignmentTuple . x l s x " ) ;
2 numJob from SheetRead ( TupleData , " S h e e t 1 ! B1" ) ;
3 numWorker from SheetRead ( TupleData , " S h e e t 1 ! B2" ) ;
4 WorkJobSet from SheetRead ( TupleData , " S h e e t 2 ! A2 : B13" ) ;
5 B e n e f i t from SheetRead ( TupleData , " S h e e t 2 ! C2 : C13" ) ;