You are on page 1of 16

06 Miniguide for CPLEX Usage

Phan Nguyen Ky Phuc

January 26, 2021

Contents

1 Structure 1

1.1 The model file (*.mod) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

1.2 The data file (*.dat) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

1.3 The excel file (*.xlsx) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

1.4 The text file (*.txt) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

2 Some useful expressions 3

3 Example 3

3.1 Example 1: Knapsack Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

3.2 Example 2: Assignment Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

4.8 Question 8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

5 How to use tuple 14

1 Structure

Basically a basic CPLEX project includes 3 file:

• 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

• File Excel (*.xlsx): Store the data for CPLEX program.

• File Text (*.txt): Store the interpretation output of CPLEX program.

• These files must be put in the same folder.

The structure of each file structure is common as:

1.1 The model file (*.mod)

When writing the file.mod, the program should follow the order below:

• Declare the size of each index(keyword: int)

• Declare the range of each index(keyword: range)

• Declare the parameters (keyword: float, int)

• 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.

• Objective function( keyword: maximize, minimize)

• Constraint (keyword: forall)

• Set up POST PROCESSING (keyword: execute)

1.2 The data file (*.dat)

• Connect the database file with the CPLEX program. In case the data is stored in EXCEL file, the
following keyword is used( keyword: SheetConnection).

• Example: SheetConnection Data("ExcelFileNam.xlsx");

• In order to make the data modification is easier, it is recommended that:

– 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");

• Write down the solution to the EXCEL file

– Example: X to SheetWrite(Data,"SheetName !A1: B10");

06 Guide for Cplex Usage Page 2


Ho Chi Minh City International University Deterministic Models in Operation Research
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc

1.3 The excel file (*.xlsx)

• Store Data and Solutions

• Excel file can only store directly 2-dimension solutions.

• When the dimension of solution is greater than 2. They require to be unfolded to write to the excel
file.

1.4 The text file (*.txt)

• Store the interpreted results

2 Some useful expressions

• Min and minl: the minimum of several numeric expressions

• Max and maxl: the maximum of several numeric expressions

• abs: the absolute value of a numeric expression

• piecewise: the piecewise linear combination of a numeric expression

3 Example

3.1 Example 1: Knapsack Problem

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

06 Guide for Cplex Usage Page 3


Ho Chi Minh City International University Deterministic Models in Operation Research
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc

Code for CPLEX


File(*.mod) code
1 i n t numbType = . . . ; / / d e c l a r e t h e s i z e o f t h e i n d e x
2 r a n g e Type = 1 . . numbType ; / / d e c l a r e t h e r a n g e o f item t y p e
3 f l o a t W[ Type ] = . . . ; / / d e c l a r e t h e w e i g h t p a r a m e t e r s a s s o c i a t i n g with r a n g e Type
4 f l o a t B [ Type ] = . . . ; / / d e c l a r e t h e b e n e f i t p a r a m e t e r s a s s o c i a t i n g with r a n g e Type
5 f l o a t n [ Type ] = . . . ; / / d e c l a r e t h e max_numb p a r a m e t e r s a s s o c i a t i n g with r a n g e Type
6 f l o a t Cap = . . . ;
7 dvar i n t+ X[ Type ] ; / / d e c l a r e t h e v a r i a b l e
8 e x e c u t e PRE_SETUP {
9 c p l e x . epgap = 0 . 1 ;
10 cplex . tilim = 100;
11 }
12 maximize
13 sum ( i i n Type ) B [ i ] ∗ X[ i ] ;
14

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 (*.dat) Code


1 S h e e t C o n n e c t i o n MyData ( " KnapsackData . x l s x " ) ;
2 numbType from SheetRead ( MyData , " S c a l a r ! B1" ) ;
3 Cap from SheetRead ( MyData , " S c a l a r ! B2" ) ;
4 W from SheetRead ( MyData , " OneDimension ! A2 : A5" ) ;
5 B from SheetRead ( MyData , " OneDimension ! B2 : B5" ) ;
6 n from SheetRead ( MyData , " OneDimension ! C2 : C5" ) ;

File(*.xlsx)

(b) sheet "OneDimension" of "KnapsackData.xlsx"


file
(a) sheet "Scalar" of "KnapsackData.xlsx" file
A B C
A B 1 Weight Benefit Max Numb
1 Number of Item 4 2 1 3 1
2 Max Capacity 10 3 3 5 2
4 4 7 2
5 5 9 1

Table 1: Data of Knapsack Problem

06 Guide for Cplex Usage Page 4


Ho Chi Minh City International University Deterministic Models in Operation Research
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc

3.2 Example 2: Assignment Problem

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

• Every job must be assigned to one worker.

• Every worker must be in charge of one job.

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 = Bij Xij
∀i ∀j

Constrainst:
X
Xij = 1, ∀i
∀j
X
Xij = 1, ∀j
∀i

Code for CPLEX


File (*.mod)
1 i n t Numbworker = . . . ;
2 i n t Numbjob = . . . ;
3 r a n g e Worker = 1 . . Numbworker ;
4 r a n g e Job = 1 . . Numbjob ;
5 f l o a t B e n e f i t [ Worker ] [ Job ] = . . . ;
6 dvar b o o l e a n X [ Worker ] [ Job ] ;
7 e x e c u t e PRE_PROCESSING {
8 c p l e x . epgap = 0 . 1 ;
9 c p l e x . t i l i m = 100 ;
10 }
11 maximize
12 sum ( i i n Worker , j i n Job ) X[ i ] [ j ] ∗ B e n e f i t [ i ] [ j ] ;
13 s u b j e c t to {
14 constraint_1 :
15 f o r a l l ( i i n Worker ) {
16 sum ( j i n Job )X[ i ] [ j ] == 1 ;
17 }

06 Guide for Cplex Usage Page 5


Ho Chi Minh City International University Deterministic Models in Operation Research
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc

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)

(b) sheet "Sheet2" of "assginment.xlsx" file

(a) sheet "constant" of "assginment.xlsx" file A B C D E


A B 1 Worker/Job 1 2 3 4
2 1 1 4 5 0
1 Numb of workers 4
3 2 3 2 1 4
2 Numb of jobs 4
4 3 1 4 5 1
5 4 6 2 5 3

Table 2: Data for Assignment Problem

4 FAQs

4.1 Question 1

Ask: How can 3 dimensions data be read from an Excel file?


Answer: Firstly, create a temporary 1 or 2 dimensional data for reading data from excel file. Then convert
them into the right form.
Example 1
We need to read a 3 dimension data A[Level][M onth][P roduct] from an excel file. The name of the Excel
file is “MatrixConvert.xlsx”. This file includes 2 sheets.

• The 1st sheet names “Scalar”.

• The 2nd sheet names “3DimData”.

File (*.xlsx)

06 Guide for Cplex Usage Page 6


Ho Chi Minh City International University Deterministic Models in Operation Research
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc

(b) sheet "3DimData" of “MatrixConvert.xlsx” file

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

Table 3: Data for Reading 3 Dimensional Data Problem

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)

1 S h e e t C o n n e c t i o n Data ( " MatrixConvert . x l s x " ) ;


2 numbMonth from SheetRead ( Data , " S c a l a r ! B1" ) ;
3 numbLevel from SheetRead ( Data , " S c a l a r ! B2" ) ;
4 numbProduct from SheetRead ( Data , " S c a l a r ! B3" ) ;
5 TempMatrix from SheetRead ( Data , " 3DimData ! C2 : G7" ) ;

4.2 Question 2

Ask: How can 4 dimensions data be read from an Excel files?


Answer: Firstly, create a temporary 1 or 2 dimensional data for reading data from excel file. Then convert
them into the right form.
Example 1
We need to read a 4 dimension data A[Quarter][Level][M onth][P roduct] from an excel file. The name of
the Excel file is “MatrixConvert.xlsx”. This file includes 2 sheets.

• The 1st sheet names “Scalar”.

• The 2nd sheet names “3DimData”.

File (*.xlsx)
File(*.mod)

1 i n t numbMonth = ...;
2 i n t numbProduct = ...;
3 i n t numbLevel = ...;
4 i n t numbQuarter = ...;
5 r a n g e Month = 1 . . numbMonth ;

06 Guide for Cplex Usage Page 7


Ho Chi Minh City International University Deterministic Models in Operation Research
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc

(a) sheet "Scalar" of “MatrixConvert.xlsx” file

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

Table 4: Data for Reading 4 Dimensional Data

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)

1 S h e e t C o n n e c t i o n Data ( " MatrixConvert4D . x l s x " ) ;


2 numbQuarter from SheetRead ( Data , " S c a l a r ! B1" ) ;
3 numbLevel from SheetRead ( Data , " S c a l a r ! B2" ) ;
4 numbMonth from SheetRead ( Data , " S c a l a r ! B3" ) ;
5 numbProduct from SheetRead ( Data , " S c a l a r ! B4" ) ;
6 TempMatrix from SheetRead ( Data , " 4DimData ! D2 : H13" ) ;

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

06 Guide for Cplex Usage Page 8


Ho Chi Minh City International University Deterministic Models in Operation Research
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc

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

Figure 1: The Graph of Question 4

minimum completion time of the project.

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.

(a) 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

Table 5: Data for Question 4

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)
9 Fin [ i ]>=Fin [ j ]+ Pro [ i ] ;
10 f o r a l l ( i in job )

06 Guide for Cplex Usage Page 9


Ho Chi Minh City International University Deterministic Models in Operation Research
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc

11 i f ( sum ( j i n j o b ) Pre [ i ] [ j ]==0) {


12 Fin [ i ]==Pro [ i ] ;
13 }
14 }

4.5 Question 5

Ask: How can we express the implication condition in CPLEX ?


Answer: Using =>.
For example
Let x[i] be the binary variable, x[i] = 1 if the facility i is open, otherwise; x[i] = 0
Let Q[i][j] be the quantity of product j manufactured in facility i.
The relationship among these factors can be expressed as:

1 i f x [ i ]=0 then Q[ i ] [ j ]=0

This IF-THEN can be expressed as


X
Q[i][j] ≤ BigM × x[i], ∀i
∀j

In CPLEX it can be expressed as


1 f o r a l l ( i i n F a c i l i t y , j i n Product ) {
2 X[ i ]==0 => Q[ i ] [ j ] == 0 ;
3 }

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

Figure 2: The Supply Chain Model of Question 6

Parameters
M Dij : unit transportation cost between manufacturer i and distributor j
CapM Dij : capacity of arc between manufacturer i and distributors j
DRjk : unit transportation cost between distributors j and retailers k
CapDRj k: capacity of arc between distributors j and retailers k

06 Guide for Cplex Usage Page 10


Ho Chi Minh City International University Deterministic Models in Operation Research
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc

Fj : fixed cost for opening distribution center j


Mi : the product quantity of manufacturer i
Rk : the demand of retailer k
It is assumed that
X X
Mi = Rk
∀i ∀k

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

Xij ≤ M Dij , ∀i, ∀j

Yjk ≤ DRjk , ∀j, ∀k


X
Yjk ≤ BigM × Zj , ∀j
k

The data for this problem are given by:

(a) MD Arc Capacity (b) DK Arc Capacity

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

Table 6: Parameters of Transportation Problem

File (*.mod)

06 Guide for Cplex Usage Page 11


Ho Chi Minh City International University Deterministic Models in Operation Research
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc

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)

1 S h e e t C o n n e c t i o n MyData ( " SupplyData . x l s x " ) ;


2 numbManu=3;
3 numbDist =3;
4 numbRetl =3;
5 MD from SheetRead ( MyData , " S h e e t 2 ! B7 : D9" ) ;
6 CapMD from SheetRead ( MyData , " S h e e t 2 ! B2 : D4" ) ;
7 DR from SheetRead ( MyData , " S h e e t 2 ! G7 : I 9 " ) ;
8 CapDR from SheetRead ( MyData , " S h e e t 2 ! G2 : I 4 " ) ;
9 F from SheetRead ( MyData , " S h e e t 1 ! B2 : B4" ) ;
10 M from SheetRead ( MyData , " S h e e t 1 ! A2 : A4" ) ;
11 R from SheetRead ( MyData , " S h e e t 1 ! C2 : C4" ) ;

4.7 Question 7

Ask: Find the shortest path from node 1 to node 5


The prerequisite matrix:

06 Guide for Cplex Usage Page 12


Ho Chi Minh City International University Deterministic Models in Operation Research
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc

2
4 8
1 5

2
5 1
3 4 4

Figure 3: The Network of Question 7

(a) The prerequisite matrix (b) The post matrix

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

Table 7: Distance Matrix Network

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" ) ;

06 Guide for Cplex Usage Page 13


Ho Chi Minh City International University Deterministic Models in Operation Research
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc

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 }

5 How to use tuple

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:

• Define the tuple

• Create the sets corresponding to tuple

• Read parameters which use tuple as index

• Declare the decision variables which use tuple as index

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 ] ;

06 Guide for Cplex Usage Page 14


Ho Chi Minh City International University Deterministic Models in Operation Research
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc

The tuple set includes all tuples that will be used as index . Consider the Assignment Problem with Benefit
Data given as tuple:

(b) The data in "Sheet2.xlsx"

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

Table 8: Assignment Data

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 }

06 Guide for Cplex Usage Page 15


Ho Chi Minh City International University Deterministic Models in Operation Research
Industrial Systems Engineering Department Lecturer: Phan Nguyen Ky Phuc

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" ) ;

5.1 Define set through other sets

1 t u p l e Node{
2 i n t Machine ;
3 i n t Job ;
4 };
5 t u p l e Arc {
6 i n t Machine ;
7 i n t preJob ;
8 i n t sucJob
9 };
10 {Node} NodeSet = . . . ;
11 { Arc } ArcSet={<m, j , k>|<m, j > i n NodeSet , <m, k> i n NodeSet : j !=k}

06 Guide for Cplex Usage Page 16

You might also like