Download as pdf or txt
Download as pdf or txt
You are on page 1of 78

Parameters

𝐷𝑠𝑑 : demand of day shift for skill s


𝑁𝑠𝑑 : demand of night shift for skill s
𝐴𝑠𝑑 : demand of afternoon shift for skill s
<l,s>: tuples shows that labor l can do skill s < 𝑙, 𝑠 > ∈ 𝑅
Monday ={1,8,15,22}
Variable
𝑋𝐷𝑙𝑑 : binary =1 if labor l works on day shift d
𝑋𝐴𝑙𝑑 :
𝑋𝑁𝑙𝑑 :
𝑋𝑅𝑙𝑑 : =1 if l is off on day d
𝑌𝑙 : binary if labor l is used.
Objective function
min ∑ 𝑌𝑙
𝑙
Ở đây minh thấy rằng Workload, continuous và OR
constraint là ko đổi nên mình sẽ ko modify những chỗ này
cái mình cần là modify demand constraints
Subject to
∑ 𝑋𝐷𝑙𝑑 ≥ 𝐷𝑠𝑑 ∀𝑠, 𝑑
<𝑙,𝑠> ∈𝑅
Với mỗi giá trị s and d, thì tổng các l có skill s sẽ phải lớn
hơn nhu cầu của ca
∑ 𝑋𝐴𝑙𝑑 ≥ 𝐴𝑠𝑑 ∀𝑠, 𝑑
<𝑙,𝑠> ∈𝑅

∑ 𝑋𝑁𝑙𝑑 ≥ 𝑁𝑠𝑑 ∀𝑠, 𝑑


<𝑙,𝑠> ∈𝑅

Rôi 1 vấn đề đặt ra ở đây là nếu 1 người có thể đảm


nhiêm nhiều skill và mình muốn trong 1 ca người ta chỉ
được phụ trách 1 skill thì mình modify thế nào
Parameters
𝐷𝑠𝑑 : demand of day shift for skill s
𝑁𝑠𝑑 : demand of night shift for skill s
𝐴𝑠𝑑 : demand of afternoon shift for skill s
<s,l>: tuples shows that labor l can do skill s < 𝑙, 𝑠 > ∈ 𝑅
Monday ={1,8,15,22}
Variable
𝑋𝐷<𝑠𝑙𝑑> : binary =1 if labor l works on day shift d
𝑋𝐴<𝑠𝑙𝑑> :
𝑋𝑁<𝑠𝑙𝑑> :
𝑋𝑅𝑙𝑑 : =1 if l is off on day d
𝑌𝑙 : binary if labor l is used.

∑ 𝑋𝐷<𝑠𝑙>𝑑 ≥ 𝐷𝑠𝑑 ∀𝑠, 𝑑


<𝑠,𝑙> ∈𝑅

∑ 𝑋𝐴<𝑠𝑙>𝑑 ≥ 𝐴𝑠𝑑 ∀𝑠, 𝑑


<𝑠,𝑙> ∈𝑅

∑ 𝑋𝑁<𝑠,𝑙>𝑑 ≥ 𝑁𝑠𝑑 ∀𝑠, 𝑑


<𝑠,𝑙> ∈𝑅
Exclusive constraints
∑ 𝑋𝐷<𝑙,𝑠>,𝑑 ≤ 1 ∀𝑙, 𝑑
<𝑙,𝑠> ∈𝑅

∑ 𝑋𝐴<𝑙,𝑠>,𝑑 ≤ 1 ∀𝑙, 𝑑
<𝑙,𝑠> ∈𝑅

∑ 𝑋𝑁<𝑙,𝑠>,𝑑 ≤ 1 ∀𝑙, 𝑑
<𝑙,𝑠> ∈𝑅
Workforce constraint

∑𝑡=0…6 ∑<𝑙,𝑠> 𝑋𝐷<𝑙,𝑠>,𝑑+𝑡 + ∑𝑡=0…6 ∑<𝑙,𝑠> 𝑋𝐴<𝑙,𝑠>,𝑑+𝑡 +


∑𝑡=0…6 ∑<𝑙,𝑠> 𝑋𝑁<𝑙,𝑠>,𝑑+𝑡 = 5 𝑌𝑙 ∀𝑙, 𝑑 ∈ 𝑀𝑜𝑛𝑑𝑎𝑦

∑<𝑙,𝑠> 𝑋𝐷<𝑙,𝑠>,𝑑 + ∑<𝑙,𝑠> 𝑋𝐴<𝑙,𝑠>,𝑑 + ∑<𝑙,𝑠> 𝑋𝑁<𝑙,𝑠>,𝑑 +


𝑋𝑅𝑙,𝑑 = 1 ∀𝑙, 𝑑
XR la off day nen ko co skill

Continuos constraints
∑ 𝑋𝑁<𝑙,𝑠>,𝑑 + ∑ 𝑋𝐷<𝑙,𝑠>,𝑑+1 ≤ 1∀𝑙, 𝑑
<𝑙,𝑠> <𝑙,𝑠>
∑𝑡=0..6 𝑋𝑅𝑙,𝑑+𝑡 ≥ 1 ∀𝑑 = 1. .22 6 ngay lien tiep phai co 1
ngay nghi
OR constraint
𝑌𝑙 ≥ 𝑋𝑁<𝑙,𝑠>,𝑑
𝑌𝑙 ≥ 𝑋𝐷<𝑙,𝑠>,𝑑
𝑌𝑙 ≥ 𝑋𝐴<𝑙,𝑠>,𝑑

𝑌𝑙 ≤ ∑ ∑ 𝑋𝐷<𝑙𝑠>,𝑑 + ∑ ∑ 𝑋𝐴<𝑙𝑠>,𝑑
<𝑙,𝑠> 𝑑 <𝑙,𝑠> 𝑑

+ ∑ ∑ 𝑋𝑁<𝑙𝑠>,𝑑
<𝑙,𝑠> 𝑑
THE INTERNATIONAL UNIVERSITY (IU) Course: Production Planning & Scheduling
Department of Industrial System Engineering

FINAL EXAMINATION

SCHEDULING & SEQUENCING Duration:


120 minutes
Head of Department of Lecturer: Student ID: Date:
Industrial & Systems 15 July, 2020
Engineering

Name:

Dr. Nguyen Van Hop Dr.Phan Nguyen Ky Phuc

INSTRUCTIONS:
1. This is an open book examination.
2. Use of calculator is allowed; discussion and material transfer are strictly prohibited.

Total pages: 3 (including this page)

PART I (100 %)
Question 1: (30 points)
Consider 4 machines and Minimum Part Set of 4 jobs corresponding with flow shop model where each
job follows the sequence: 1→2→3→4
Job 1 2 3 4
P1j 1 1 1 0
P2j 2 2 1 2
P3j 1 3 2 1
P4j 3 2 1 0

a) Apply the Profile Fitting Heuristic Algorithm for the above Data. (20pts)
b) Now assumed that a buffer with capacity 1 is inserted between machine 1 and 2. As a result of this
when job is finished on machine 1 and machine 2 is busy, it can move to buffer and stay at here not block
the machine 1. Apply the 1st iteration of Profile Fitting Heuristic Algorithm to find the job at the 2nd
position in the sequence (10pts)

Question 2: (30 points)


Assume that there are 4 Tool Sets and 6 Activities. The processing time of each activity is 1.

Activity 1 2 3 4 5 6 7
Tool 1 1 0 1 1 0 1 1
Tool 2 1 1 1 0 0 0 0
Tool 3 0 1 0 0 1 1 0

Production Planning & Scheduling– Final Exam 15, July, 2020 1


THE INTERNATIONAL UNIVERSITY (IU) Course: Production Planning & Scheduling
Department of Industrial System Engineering

Tool 4 1 0 1 1 0 0 1

Apply the Graph Coloring Algorithm for finding the schedule

Question 3: (30 points)


Find the minimize make span of following problem using CPLEX.
Table 1: Data for example
Job Sequence Processing Time
1 1,2,3,1 𝑝11 = 1, 𝑝21 = 2, 𝑝31 = 5, 𝑝11 = 4,
2 2,3,2 𝑝22 = 5, 𝑝32 = 4, 𝑝22 = 5,
3 4,1,3,2 𝑝43 = 4, 𝑝13 = 4, 𝑝33 = 3, 𝑝23 = 2,
4 1,4,2,4 𝑝14 = 2, 𝑝44 = 3, 𝑝24 = 2, 𝑝44 = 3

Question 4: (10points)
In the workforce scheduling problem, assume that after minimizing the number of required
workers, we want to satisfy workers wishes. In other words, workers prefer to have 2 consecutive
days off. Modelling this problem.

GOOD LUCKS!

Production Planning & Scheduling– Final Exam 15, July, 2020 2


THE INTERNATIONAL UNIVERSITY (IU) Course: Production Planning & Scheduling
Department of Industrial System Engineering

FINAL EXAMINATION

SCHEDULING & SEQUENCING Duration:


120 minutes
Head of Department of Lecturer: Student ID: Date:
Industrial & Systems 15 July, 2020
Engineering

Name:

Dr. Nguyen Van Hop Dr.Phan Nguyen Ky Phuc

INSTRUCTIONS:
1. This is an open book examination.
2. Use of calculator is allowed; discussion and material transfer are strictly prohibited.

Total pages: 3 (including this page)

PART I (100 %)
Question 1: (30 points)
Consider 4 machines and Minimum Part Set of 4 jobs corresponding with flow shop model where each
job follows the sequence: 1→2→3→4
Job 1 2 3 4
P1j 1 1 1 0
P2j 2 2 1 2
P3j 1 3 2 1
P4j 3 2 1 0

a) Apply the Profile Fitting Heuristic Algorithm for the above Data. (20pts)
b) Now assumed that a buffer with capacity 1 is inserted between machine 1 and 2. As a result of this
when job is finished on machine 1 and machine 2 is busy, it can move to buffer and stay at here not block
the machine 1. Apply the 1st iteration of Profile Fitting Heuristic Algorithm to find the job at the 2nd
position in the sequence (10pts)

Question 2: (30 points)


Assume that there are 4 Tool Sets and 6 Activities. The processing time of each activity is 1.

Activity 1 2 3 4 5 6 7
Tool 1 1 0 1 1 0 1 1
Tool 2 1 1 1 0 0 0 0
Tool 3 0 1 0 0 1 1 0

Production Planning & Scheduling– Final Exam 15, July, 2020 1


THE INTERNATIONAL UNIVERSITY (IU) Course: Production Planning & Scheduling
Department of Industrial System Engineering

Tool 4 1 0 1 1 0 0 1

Apply the Graph Coloring Algorithm for finding the schedule

Question 3: (30 points)


Find the minimize make span of following problem using CPLEX.
Table 1: Data for example
Job Sequence Processing Time
1 1,2,3,1 𝑝11 = 1, 𝑝21 = 2, 𝑝31 = 5, 𝑝11 = 4,
2 2,3,2 𝑝22 = 5, 𝑝32 = 4, 𝑝22 = 5,
3 4,1,3,2 𝑝43 = 4, 𝑝13 = 4, 𝑝33 = 3, 𝑝23 = 2,
4 1,4,2,4 𝑝14 = 2, 𝑝44 = 3, 𝑝24 = 2, 𝑝44 = 3

Question 4: (10points)
In the workforce scheduling problem, assume that after minimizing the number of required
workers, we want to satisfy workers wishes. In other words, workers prefer to have 2 consecutive
days off. Modelling this problem.

GOOD LUCKS!

Production Planning & Scheduling– Final Exam 15, July, 2020 2


Q5(10 marks)
Assumed that we need to schedule for safeguards for one
week. There are 3 shifts on each day, namely, morning,
afternoon and evening. Guards cannot work on 2 consecutive
shifts. One guard must work less than or equal to 6 shifts and
number of night shifts must be less than or equal to 3. One
shift must have at least one guard. The objective is to find the
minimum required number of safeguards. Modelling this
problem.

𝑋𝐷𝑖𝑑 : binary =1, if person i works on day shift d


𝑋𝐴𝑖𝑑 : binary =1, if person i works on afternoon shift d
𝑋𝐸𝑖𝑑 : binary =1, if person i works on evening shift d
𝑋𝑅𝑖𝑑 : binary =1, if person i off on day d
𝑌𝑖 : binary =1 if person i is hired

min ∑ 𝑌𝑖
𝑖
Demand constraints

∑ 𝑋𝐷𝑖𝑑 ≥ 1, ∀𝑑
𝑖

∑ 𝑋𝐸𝑖𝑑 ≥ 1, ∀𝑑
𝑖

∑ 𝑋𝐴𝑖𝑑 ≥ 1, ∀𝑑
𝑖
For one day
𝑋𝐴𝑖𝑑 + 𝑋𝐷𝑖𝑑 + 𝑋𝐸𝑖𝑑 + 𝑋𝑅𝑖𝑑 = 1, ∀𝑖, 𝑑
Consecutive constraint
𝑋𝐸𝑖𝑑 + 𝑋𝐷𝑖𝑑+1 ≤ 1 ∀𝑖, 𝑑 = 1. .6

∑ 𝑋𝐴𝑖𝑑 + ∑ 𝑋𝐸𝑖𝑑 + ∑ 𝑋𝐷𝑖𝑑 ≤ 6 𝑌𝑖 , ∀𝑖


𝑑 𝑑 𝑑

∑ 𝑋𝐸𝑖𝑑 ≤ 3 𝑌𝑖
𝑑

𝑌𝑖 ≥ 𝑋𝐴𝑖𝑑 ∀𝑖, 𝑑
𝑌𝑖 ≥ 𝑋𝐸𝑖𝑑 ∀𝑖, 𝑑
𝑌𝑖 ≥ 𝑋𝐷𝑖𝑑 ∀𝑖, 𝑑

𝑌𝑖 ≤ ∑ 𝑋𝐸𝑖𝑑 + ∑ 𝑋𝐷𝑖𝑑 + ∑ 𝑋𝐴𝑖𝑑 , ∀𝑖


𝑑 𝑑 𝑑
M: set of machines


J: set of jobs


A : be the set of solid arcs


B : be the set of broken arcs


N : be the set of nodes

K : be the set of maintenance nodes, ví dụ mình có 10 jobs thường và có 4 nhiệm vụ bảo


trì trên 4 máy thì tập K của mình sẽ có dạng <1,11>, <2,12>,<3,13>, <4,14> các task 11,
12, 13,14 ứng với các job bảo trì

Parameters


pmj be processing time of job j on machine m


𝛼𝑚 :be the lower bound of time window

𝛽𝑚 :be the upper bound of time window

𝛾𝑚 :be the maintance time on machine m

Variables


ymj : starting of job j on machine m


xmjk: binary variable xmjk = 1 if job j is processed before job k in machine m; otherwise
xmjk = 0

Cmax : the make span

𝑚𝑖𝑛𝑖𝑚𝑖𝑧𝑒 𝐶𝑚𝑎𝑥

𝑦ℎ𝑗 ≥ 𝑦𝑚𝑗 + 𝑝𝑚𝑗 ∀(𝑚, 𝑗) → (ℎ, 𝑗) ∈ 𝐴

𝐶𝑚𝑎𝑥 ≥ 𝑦𝑚𝑗 + 𝑝𝑚𝑗 ∀(𝑚, 𝑗) ∈ 𝑁 ∪ 𝐾

𝑦𝑚𝑘 ≥ 𝑝𝑚𝑗 + 𝑦𝑚𝑗 − 𝐵𝑖𝑔𝑀 × (1 − 𝑥𝑚𝑗𝑘 ) ∀(𝑚, 𝑗), (𝑚, 𝑘) ∈ 𝑁 ∪ 𝐾

𝑥𝑚𝑗𝑘 + 𝑥𝑚𝑘𝑗 = 1, ∀(𝑚, 𝑗), (𝑚, 𝑘) ∈ 𝑁 ∪ 𝐾


𝑦𝑚𝑗 ≥ 𝛼𝑚 ∀(𝑚, 𝑗) ∈ 𝐾

𝑦𝑚𝑗 + 𝛾𝑚 ≤ 𝛽𝑚 ∀(𝑚, 𝑗) ∈ 𝐾
THE INTERNATIONAL UNIVERSITY (IU) Course: Scheduling & Sequencing
Department of Industrial System Engineering

FINAL EXAMINATION

SCHEDULING & SEQUENCING Duration:


120 minutes
Head of Department of Lecturer: Student ID: Date:
Industrial & Systems 18 June, 2019
Engineering

Name:

Dr. Nguyen Van Hop Dr.Phan Nguyen Ky Phuc

INSTRUCTIONS:
1. This is an open book examination.
2. Use of calculator is allowed; discussion and material transfer are strictly prohibited.

Total pages: 3 (including this page)

PART I (100 %)
ASSESSMENT RUBRIC

Question Descriptions Score Learning Outcomes /attributes

Able to recognize different shop


Apply the Profile Fitting Heuristic configurations, manufacturing
1 25
for flowshop Problem scheduling problems, and performance
measures.
Able to identify basic algorithms and
Apply algorithm for reservation with
2 30 procedures to use in different shop
slack
configurations
Able to identify basic algorithms and
Apply the Graph Coloring algorithm
3 20 procedures to use in different shop
for the Time Tabling Scheduling
configurations
Able to formulate scheduling and
sequencing problems under
Convert the mathematical problem
4 15 Mathematical Programming techniques
into the CPLEX language
and solve them in LINGO, CPLEX,
Python software
Student can convert the real problem Able to formulate scheduling and
5 10
into the mathematical form sequencing problems of real cases

Scheduling & Sequencing– Final Exam 18, June, 2019 1


THE INTERNATIONAL UNIVERSITY (IU) Course: Scheduling & Sequencing
Department of Industrial System Engineering

MARKING SCHEME
Question 1: (25 points)
Consider 4 machines and Minimum Part Set of 4 jobs corresponding with flow shop model where each
job follows the sequence: 1→2→3→4
Job 1 2 3 4
P1j 1 1 2 0
P2j 2 0 2 2
P3j 1 2 1 1
P4j 0 2 1 0

Apply the Profile Fitting Heuristic for the above Data


Answer:
Among 4 jobs, job 3 is the profile job with total processing time is 6
For job 1
Xi3 Pi1 Xi1 Total idle
2 1 4 1
4 2 6 0
5 1 7 1
6 0 7 1
Total idle =3
For job 2
Xi3 Pi2 Xi2 Total idle
2 1 4 1
4 0 5 1
5 2 7 0
6 2 9 1
Total idle =3
For job 4
Xi3 Pi4 Xi4 Total idle
2 0 4 2
4 2 6 0
5 1 7 1
6 0 7 1
Total idle =4
 Choose squence 3-1
Iteration 2
For job 2
Xi1 Pi2 Xi2 Total idle
4 1 6 1
6 0 7 1
7 2 9 0
7 2 10 2
Total idle 4
For job 4
Xi1 Pi4 Xi4 Total idle
4 0 6 2
6 2 8 0
7 1 9 1

Scheduling & Sequencing– Final Exam 18, June, 2019 2


THE INTERNATIONAL UNIVERSITY (IU) Course: Scheduling & Sequencing
Department of Industrial System Engineering

7 0 9 2
Total idle 5
Choose squence 3-1-2
Since job 4 is the last job, the final sequence is 3-1-2-4

Question 2: (30 points)


Given the following data
Job 1 2 3
wi 2 4 2
pi 1 1 2
ri 0 1 1
di 4 4 5
Machine 1 1 2 0
Machine 2 0 1 2

a) Calculate the priority of each job(5pts)


Job 1 2 3
Priority 0.25 0.375 1

b) Write down the summary table of possible resource usage when no job is assigned (10pts)
0 1 2 3 4
Job 1 1 1 1 1 0
Job 2 0 2 2 2 0
Job 3 0 0 0 0 0
Res 1 1 3 3 3 0

0 1 2 3 4
Job 1 0 0 0 0 0
Job 2 0 1 1 1 0
Job 3 0 2 2 2 2
Res 2 0 3 3 3 2

c) Assign the 1st job with the highest priority then write down the table of the update resources and
possible resource usage (10pts)
0 1 2 3 4
Res 1 1 3 3 3 0
Res 2 0 3 3 3 2
G1 1 3 3 3 0
Assigne Job 1 into time slot 0
Update possible usage
0 1 2 3 4
Scheduling & Sequencing– Final Exam 18, June, 2019 3
THE INTERNATIONAL UNIVERSITY (IU) Course: Scheduling & Sequencing
Department of Industrial System Engineering

Res 1 0 2 2 2 0
Res 2 0 3 3 3 2

Update availabe resource


0 1 2 3 4
Res 1 1 2 2 2 2
Res 2 2 2 2 2 2

d) Assign the 2nd job with the second high priority then write down the table of the update resources
and possible resource usage (5pts)
0 1 2 3 4
Res 1 0 2 2 2 0
Res 2 0 3 3 3 2
G1 2 2 2
G2 1.5 1.5 1.5

Job 2 can be assigned into any time slot 1,2,3 => assigne job 2 to time slot 1
Update possible usage

0 1 2 3 4
Res 1 0 0 0 0 0
Res 2 0 2 2 2 2

Update availabe resource


0 1 2 3 4
Res 1 1 0 2 2 2
Res 2 2 1 2 2 2

Question 3: (20 points)


Assume that there are 4 Tool Sets and 6 Activities. The processing time of each activity is 1.

Activity 1 2 3 4 5 6
Tool 1 1 0 1 0 0 1
Tool 2 0 1 1 1 0 0
Tool 3 0 1 0 0 1 1
Tool 4 1 0 1 0 0 0

Apply the Graph Coloring Algorithm for finding the schedule


Answer
Conflict matrix

Scheduling & Sequencing– Final Exam 18, June, 2019 4


THE INTERNATIONAL UNIVERSITY (IU) Course: Scheduling & Sequencing
Department of Industrial System Engineering

Nod 1 2 3 4 5 6
1 0 0 1 0 0 1
2 0 0 1 1 1 1
3 1 1 0 1 0 1
4 0 1 1 0 0 0
5 0 1 0 0 0 1
6 1 1 1 0 1 0
Deg 2 4 4 2 2 4

Node 1 2 3 4 5 6
Deg 2 4 4 2 2 4

Step 1
Node 1 2: 3 4 5 6
C1
Deg 2 - 3 1 1 3

Step 2
Node 1 2: 3: 4 5 6
C1 C2
Deg 1 - - 0 1 2

Step 3
Node 1 2: 3: 4 5 6:
C1 C2 C3
Deg 0 - - 0 0 -
Final schedule
Time slot 1 2 3
Job 2 Job 3 Job 6
Job 1 Job 5 Job 4

Question 4: (15 points)


Following is the VRP problem with 2-index.
set of customers represented by C = {1, . . . , n}, such that a positive demand is associated to each
customer i ∈ C.
All customers must be visited exactly once. Each vehicle has a maximum capacity Q, which limits the
number of customers it can visit before returning to the depot
We represent the problem using a graph G(N , E), in which N = C ∪ {0, n + 1} is the set of nodes
associated to customers in C and to the depot nodes 0 and n + 1.

Scheduling & Sequencing– Final Exam 18, June, 2019 5


THE INTERNATIONAL UNIVERSITY (IU) Course: Scheduling & Sequencing
Department of Industrial System Engineering

The cost of crossing an arc (i, j) ∈ E is denoted by cij . Each node has a demand qi , such that qi > 0 for
each i ∈ C and q0 = qn+1 = 0. The objective of the problem is to determine a set of minimal cost routes that
satisfies all the requirements defined above.
we define the binary decision variable xij that assumes value 1 if and only if there is a route that goes from
customer i to j directly, for i, j ∈ N . In addition, yj is a continuous decision variable corresponding to the
cumulated demand on the route that visits node j ∈ N up to this visit.

Min
𝑛+1 𝑛+1

∑ ∑ 𝑐𝑖𝑗 𝑥𝑖𝑗
𝑖=0 𝑗=0
Constraints
𝑛+1

∑ 𝑥𝑖𝑗 = 1 , ∀𝑖 = 1 … 𝑛
𝑗=1,𝑗≠𝑖
𝑛 𝑛+1

∑ 𝑥𝑖ℎ − ∑ 𝑥ℎ𝑗 = 0, ∀ℎ = 1 … 𝑛
𝑖=0,𝑖≠ℎ 𝑗=1,𝑗≠ℎ
𝑛

∑ 𝑥0𝑗 ≤ 𝐾
𝑗=1
𝑦𝑗 ≥ 𝑦𝑖 + 𝑞𝑗 𝑥𝑖𝑗 − 𝑄(1 − 𝑥𝑖𝑗 ), ∀𝑖, 𝑗 = 0, … 𝑛 + 1
𝑞𝑖 ≤ 𝑦𝑖 ≤ 𝑄, ∀𝑖 = 0 … 𝑛 + 1
𝑥𝑖𝑗 ∈ {0,1}, ∀𝑖, 𝑗 = 0, … 𝑛 + 1

Write down the CPLEX Code for the above problem.


int numCust=...;
range Cust=1..numCust;
range Node=0..numCust+1;
int numVeh;
int Cap;
float Cost[Node][Node];
float q[Cust];
dvar boolean X[Node][Node];
dvar float Y[Node];
minimize sum(i in Node, j in Node)Cost[i][j]*X[i][j];
subject to{
forall(i in Cust){
sum(j in 1..numCust+1:j!=i)X[i][j]==1;
}
forall(h in Cust){
sum(i in 0..numCust:i!=h)X[i][h]-sum(j in 1..numCust+1:j!=h)X[h][j]==0;
}
sum(j in Cust)X[0][j]<=numVeh;
forall(i in Node, j in Node){
Y[j]>=Y[i]+q[j]*X[i][j]-Cap*(1-X[i][j]);
}
forall(i in Node){
q[i]<=Y[i];
Y[i]<=Cap;

Scheduling & Sequencing– Final Exam 18, June, 2019 6


THE INTERNATIONAL UNIVERSITY (IU) Course: Scheduling & Sequencing
Department of Industrial System Engineering

}
}

Question 5: (10 points)


Based on the Nurse Rottering Problem of Chapter 13.
Developed the model to adapt following constraints.
• One day includes 3 shifts: Day, Afternoon, and Night
• The Shift Night must have at least one manager to supervise the Shift Night.
• Set of managers is the subset of workers and denote by M

Parameters
𝐷𝑑 : demand of day shift d
𝑁𝑑 : demand of day shift d
𝐴𝑑 : demand of day shift d
Monday : set of Monday={1,8,15,22}
Variable
𝑋𝐷𝑙𝑑 : binary variable , 𝑋𝐷𝑙𝑑 = 1 if the labor l work on day shift of day d
𝑋𝐴𝑙𝑑 : binary variable , 𝑋𝐴𝑙𝑑 = 1 if the labor l work on afternoon shift of day d
𝑋𝑁𝑙𝑑 : binary variable , 𝑋𝑁𝑙𝑑 = 1 if the labor l work on night shift of day d
𝑋𝑅𝑙𝑑 : binary variable , 𝑋𝑅𝑙𝑑 = 1 if the labor l of on day d

𝑌𝑙 : binary variable 𝑌𝑙 = 1 if the labor l is used

Objective function :
min ∑ 𝑌𝑙
𝑙
Subject to
∑ 𝑋𝐷𝑙𝑑 ≥ 𝐷𝑑 ∀𝑑
𝑙

∑ 𝑋𝐴𝑙𝑑 ≥ 𝐴𝑑 ∀𝑑
𝑙
∑ 𝑋𝑁𝑙𝑑 ≥ 𝑁𝑑 ∀𝑑
𝑙

∑ 𝑋𝑁𝑙𝑑 ≥ 1 ∀𝑑
𝑙∈𝑀
Workload constraints:
∑ 𝑋𝐷𝑙,𝑑+𝑡 + ∑ 𝑋𝐴𝑙,𝑑+𝑡 + ∑ 𝑋𝑁𝑙,𝑑+𝑡 = 5𝑌𝑙 ∀𝑑 ∈ 𝑀𝑜𝑛𝑑𝑎𝑦
𝑡=0..6 𝑡=0..6 𝑡=0..6

𝑋𝐷𝑙𝑑 + 𝑋𝐴𝑙𝑑 + 𝑋𝑁𝑙𝑑 + 𝑋𝑅𝑙𝑑 = 1 ∀𝑙, 𝑑


Continuous constraints:

𝑋𝑁𝑙𝑑 + 𝑋𝐷𝑙𝑑+1 ≤ 1 ∀𝑑 = 1. .27

Scheduling & Sequencing– Final Exam 18, June, 2019 7


THE INTERNATIONAL UNIVERSITY (IU) Course: Scheduling & Sequencing
Department of Industrial System Engineering

∑ 𝑋𝑅𝑙,𝑑+𝑡 ≥ 1, ∀𝑑 = 1. .22
𝑡=0..6
Contraint for 𝑌𝑙
𝑌𝑙 ≥ 𝑋𝐷𝑙𝑑 ∀𝑙, 𝑑
𝑌𝑙 ≥ 𝑋𝐴𝑙𝑑 ∀𝑙, 𝑑
𝑌𝑙 ≥ 𝑋𝑁𝑙𝑑 ∀𝑙, 𝑑
𝑌𝑙 ≤ ∑ 𝑋𝐷𝑙𝑑 + ∑ 𝑋𝐴𝑙𝑑 + ∑ 𝑋𝑁𝑙𝑑 ∀𝑙
𝑑 𝑑 𝑑
GOOD LUCKS!

Scheduling & Sequencing– Final Exam 18, June, 2019 8


Job 1 2 3 4 Seq 2-4-1-3 Time 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
M1 6 4 6 8 M1
M2 2 10 4 6 M2
M3 4 8 0 2 M3
M4 8 2 6 6 M4
Total 20 24 16 22

F L Time P N Job N L TimeLoss J L


14 6 22 2 1 4
22 4 26 0 3 8
24 0 30 6
30 6 36 0
Total 8

J 1 2 3 4 5 6 7 Time 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
P 3 10 9 4 6 5 3 J1 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0
W 2 3 3 2 1 2 3 J2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0
R 5 0 2 3 2 4 5 J3 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
D 12 10 20 15 18 19 14 J4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
M 1,3 1,2 1.2.3 2,3 1 1 1,2 J5 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0
Total M 2 2 3 2 1 1 2 J6 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0
Index 3 6,667 9 4 6 2,5 2 J7 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0
Sum 1 1 3 3 4 6 6 6 6 6 5 5 4 4 3 3 3 3 2 1 0

Time 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
J1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
J2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0
J3 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
J4 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0
J5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
J6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
J7 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0
S 1 1 2 3 3 4 4 4 4 4 3 3 3 3 2 1 1 1 1 1 0

Time 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
J1 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0
J2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
J3 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
J4 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0
J5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
J6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
J7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
S 0 0 1 2 2 3 3 3 3 3 3 3 2 2 2 1 1 1 1 1 0

Time 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
R1 1 1 3 3 4 6 6 6 6 6 5 5 4 4 3 3 3 3 2 1 0
R2 1 1 2 3 3 4 4 4 4 4 3 3 3 3 2 1 1 1 1 1 0
R3 0 0 1 2 2 3 3 3 3 3 3 3 2 2 2 1 1 1 1 1 0

/*********************************************
* OPL 12.9.0.0 Model
* Author: kyphuc
* Creation Date: May 9, 2022 at 9:25:47 AM
*********************************************/
int numJ=14;
int numM=3;
range rangeJ=1..numJ;
range rangeM=1..numM;
int P[rangeJ]=[45,42,41,37,36,33,31,30,27,24,22,20,18,17];
dvar boolean X[rangeJ][rangeM];
dvar float+ Vmax;
dvar float+ Vmin;
minimize Vmax-Vmin;
subject to{
forall(j in rangeJ){
sum(m in rangeM)X[j][m]==1;
}
forall(m in rangeM){
Vmax>=sum(j in rangeJ)P[j]*X[j][m];
Vmin<=sum(j in rangeJ)P[j]*X[j][m];
}
}
THE INTERNATIONAL UNIVERSITY (IU) Course: Scheduling & Sequencing
Department of Industrial System Engineering

FINAL EXAMINATION

SCHEDULING & SEQUENCING Duration:


120 minutes
Head of Department of Lecturer: Student ID: Date:
Industrial & Systems 21 June, 2021
Engineering

Name:

Dr. Nguyen Van Hop Dr.Phan Nguyen Ky Phuc

INSTRUCTIONS:
1. This is an open book examination.
2. Laptop is allowed; discussion and material transfer are strictly prohibited.

Total pages: 3 (including this page)

PART I (100 %)

Question 1: (25 points)


Apply profile fitting heurisitc, find the sequence for this flow shop problem
Job 1 2 3 4
Machine 1 2 3 2 4
Machine 2 3 4 5 5
Machine 3 4 2 3 2
Machine 4 5 3 4 6

Question 2: (25 points)


Apply the 1st iteration of shifting bottle neck algorithm for the following job shop problem
Job Sequence Processing Time
1 1,2,3,4 𝑝1,1 = 4, 𝑝2,1 = 2, 𝑝3,1 = 4, 𝑝4,1 = 3
2 1,3,4 𝑝1,2 = 4, 𝑝3,2 = 2, 𝑝4,2 = 4
3 2,3,4 𝑝2,3 = 1, 𝑝3,3 = 2, 𝑝4,3 = 5
4 1,3,4 𝑝1,4 = 4, 𝑝3,4 = 2, 𝑝4,4 = 4

Scheduling & Sequencing– Final Exam 21, June, 2021 1


THE INTERNATIONAL UNIVERSITY (IU) Course: Scheduling & Sequencing
Department of Industrial System Engineering

Question 3: (25 points)

Apply the graph coloring algorithm to find the schedule for the following problem
Job 1 2 3 4 5 6 7 8
Res 1 1 0 0 0 1 1 0 0
Res 2 0 1 1 0 0 1 0 1
Res 3 1 0 1 0 0 0 1 0
Res 4 0 1 0 1 0 1 0 0
Res 5 0 0 0 0 1 0 0 1

Question 4: (25 points)


Given the following data
Job 1 2 3
wi 2 3 2
pi 2 2 2
ri 0 2 1
di 7 5 9
Machine type 1 1 2 0
Machine type 2 1 1 2
If the number of both machine type 1 and type 2 are 2.
a) Calculate the priority of each job(5pts)
b) Write down the summary table of possible resource usage when no job is assigned (5pts)
c) Assign the 1st job with the highest priority then write down the table of the update resources and
possible resource usage (10pts)
d) Assign the 2nd job with the second high priority then write down the table of the update resources
and possible resource usage (5pts)

GOOD LUCKS!

Scheduling & Sequencing– Final Exam 21, June, 2021 2


THE INTERNATIONAL UNIVERSITY (IU) Course: Scheduling & Sequencing
Department of Industrial System Engineering

FINAL EXAMINATION

SCHEDULING & SEQUENCING Duration:


120 minutes
Dean of School of Industrial Lecturer: Student ID: Date:
& Systems Engineering 18 June, 2021

Name:

Dr. Nguyen Van Hop Dr.Phan Nguyen Ky Phuc

INSTRUCTIONS:
1. This is an open book examination.
2. Using CPLEX, EXCEL is encouraged. Using other materials is also allowed. Dicussion is prohibited
3. Please record your monitor screen during your working process. Upload it on the G-drive or
the Youtube. Paste the link at the end of your work so I can check it.
4. Use the last digit of your student ID, divided by 5, get the remainder. Substitue the value of R
by the remainder
For example: If your student ID is IELSIU18098. The last digit is 8, divided by 5. The remainder is
3. 3 is used to subtitue for R in the question.
5. Since update the video required. You can paste the link later into the following link
https://docs.google.com/spreadsheets/d/1yc_mgpjxQ-sxbmParnK_HRKxMBj_Yu-OFVOU-
h8kgAY/edit?usp=sharing
I lock the link at 6pm after that none can access the file.

Total pages: 3 (including this page)

PART I 2(100 %)
Question 1: (20 points)
Apply the Shifting Bottle Neck for the following job shop problem
Job Sequence Processing Time
1 1,3,2 𝑝11 = 2, 𝑝31 = 4 + 𝑅, 𝑝21 = 5
2 2,3,1 𝑝22 = 3, 𝑝32 = 3+R, 𝑝12 = 2
3 1,2,3 𝑝13 = 1, 𝑝23 = 2, 𝑝33 = 4 + 𝑅
4 2,1,3 𝑝24 = 2, 𝑝14 = 3, 𝑝34 = 1 + 𝑅

Scheduling & Sequencing– Final Exam 18, June, 2021 1


THE INTERNATIONAL UNIVERSITY (IU) Course: Scheduling & Sequencing
Department of Industrial System Engineering

Question 2: (20 points)


Consider 4 machines and Minimum Part Set of 4 jobs corresponding with flow shop model where each
job follows the sequence: 1→2→3→4
Job 1 2 3 4
P1j 3 1+R 2 1
P2j 2 0+R 2 2
P3j 3 2+R 1 1
P4j 0 2+R 1 0

Apply the Profile Fitting Heuristic for the above Data

Question 3: (20 points)


There are two machines type 1 and two machines type 2. Given the following data:

Job 1 2 3
wi 2 4 5
pi 1 2 2
ri 1 0 1
di 3+R 4+R 4+R
Machine 1 1 2 0
Machine 2 1 1 2

a) Calculate the priority of each job(5pts)


b) Write down the summary table of possible resource usage when no job is assigned (5pts)
c) Assign the 1st job with the highest priority then write down the table of the update resources and
possible resource usage (10pts)
Question 4: (20 points)
Assume that there are 4 Tool Sets and 8 Activities. The processing time of each activity is 1.

Activity 1 2 3 4 5 6 7 8
Tool 1 1 0 1 0 0 1 1 0
Tool 2 0 1 L 1 0 0 1 0
Tool 3 0 L 0 0 1 1 L 1
Tool 4 1 0 1 0 0 0 0 0

If R is odd, i.e., 1,3 then L is 1


If R is even, i.e., 0,2,4 then L is 0
Apply the Graph Coloring Algorithm for finding the schedule

Question 5: (20 points)


In the jobshop problem where each station has only one machine, due to the mantainance requirements,
except form the normal jobs there are some preventive maintanance actions for each machine. These
preventive actions must be done within time window 𝛼𝑚 and 𝛽𝑚 of machine m. Let 𝛾𝑚 be the length of

Scheduling & Sequencing– Final Exam 18, June, 2021 2


THE INTERNATIONAL UNIVERSITY (IU) Course: Scheduling & Sequencing
Department of Industrial System Engineering

preventive maintanance action on machine m. Modified the original job shop problem to make the
mathematical mentioned above.

Write down the mathematical model. Introduce the annotations if required.

Please record your monitor screen during your working process. Upload it on the G-drive or the
Youtube. Paste the link to the following excel file. See the sheet name correspoding to your group
https://docs.google.com/spreadsheets/d/1yc_mgpjxQ-sxbmParnK_HRKxMBj_Yu-OFVOU-
h8kgAY/edit?usp=sharing

GOOD LUCKS!

Scheduling & Sequencing– Final Exam 18, June, 2021 3


CPLEX Part 2

Phan Nguyen Ky Phuc

April 20, 2022

Contents
1 Constraint Programming 3

2 Constraint Programming structure 5

3 Tuple and Set 6


3.1 Declare tuple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.2 Declare set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.3 Declare subset from available set . . . . . . . . . . . . . . . . . . . . . . . . 7

4 Constraints 7

5 Data Initialization 9

6 Declare generic LB and UB for variables 10

7 Set operators 11

8 Sensitivity analysis 11

9 Logical constraints 13

10 Scheduling 14

11 Expression on Interval constraints 15

12 Notations 17

13 Piecewise linear and stepwise linear functions 18

14 Intensity and size 20

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

15 Declare an interval variable 21

16 Unary constraints on interval variables 22

17 Precedence Constraint 23

18 Constraints on groups of interval variables 24

19 Sequencing variables 25

20 State function 26
20.1 Syntax and examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

21 Cumulative functions 28
21.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
21.2 Cumulative expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
21.3 Constraints on cumul function expression . . . . . . . . . . . . . . . . . . . . 29
21.4 Combine several cumul function expression . . . . . . . . . . . . . . . . . . . 29

22 Example of job shop 30

23 Example of cumul function for job shop with parallel machine 32

24 Example of dependent setup time for job shop 34

25 Example of dependent setup time for for jobshop with parallel machine 37

26 Access the solution in postprocessing. 40

27 Setting CP parameters 41

28 Customize the search phase 43


28.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
28.2 Type of mono-criterion search phase: . . . . . . . . . . . . . . . . . . . . . . 44
28.3 Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
28.4 Search phases for scheduling . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
28.5 Change Search behavior through setting . . . . . . . . . . . . . . . . . . . . 47

29 Multiple objective function 48

Constraint Programming Part 2 Page 2


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

1 Constraint Programming

Constraint programming has native support for:

• Nonlinear costs or constraints

• Logical constraints and statements

• Constraints on and between interval variables

• Compatibility or incompatibility constraints

• It applies to only discrete decision variables

• No gap measure

• Explicit modeling for max, min, abs

• More memory usage per decision variable

Example
1 u s i n g CP;
2 i n t nbPerm = . . . ;
3 r a n g e r = 1 . . nbPerm ;
4 int dist [ r ] [ r ] = . . . ;
5 int flow [ r ] [ r ] = . . . ;
6 execute {
7 cp . param . t i m e L i m i t =30;
8 }
9 dvar i n t perm [ 1 . . nbPerm ] i n r ;
10 dexpr i n t c o s t [ i i n r ] [ j i n r ] = d i s t [ i ] [ j ] ∗ f l o w [ perm [ i ] ] [ perm [ j ] ] ;
11 minimize sum ( i i n r , j i n r ) c o s t [ i ] [ j ] ;
12 s u b j e c t to {
13 a l l D i f f e r e n t ( perm ) ;
14 };

Explicit modeling for max, min, abs


Since constraint programming does not have linear relaxation to optimize a relaxed problem
after each decision on integer variables, the MP way of modeling constraints such as max,
min, cannot be used directly for CP
1 minimize m + . . . ;
2 s u b j e c t to {
3 f o r a l l ( i i n 1 . . 1 0 ) m >= x [ i ] ;
4 ....
5 }

In CP, it is safer and more efficient to write:

Constraint Programming Part 2 Page 3


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

1 minimize . . . ;
2 s u b j e c t to {
3 m == max( i i n 1 . . 1 0 ) x [ i ] ;
4 ....
5 }

Constraint Programming Part 2 Page 4


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

2 Constraint Programming structure

A CP model must start with the statement using CP;


1 u s i n g CP;

The CP program should follow

• Declare the size of problem

• Declare the tuple

• Declare range,set, array

• Declare variables

• Declare functions: statefunction, piecewise linear function, cumul function

• Make pre processing

• Objective function

• Constraints

• Make post processing

• Main block for flow control

Constraint Programming Part 2 Page 5


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

3 Tuple and Set

3.1 Declare tuple


1 t u p l e tupleName {
2 dataType atbName1 ;
3 dataType atbName2 ;
4 }

Tuple can be nested


1 tuple connection {
2 string o ;
3 string d;
4 }
5 tuple route {
6 string p;
7 connection e ;
8 }

An attribute in tuple can be declared with keyword key this keyword can be used directly
to access to to tuple.
For example
1 u s i n g CP;
2

3 t u p l e A c t i v i t y { key s t r i n g name ; i n t d u r a t i o n ; i n t type ; }


4 { Activity } Acts = { <"One" , 1 0 , 1>, <"Two" , 4 , 2>, <" Three " , 7 , 1> } ;
5

6 dvar i n t e r v a l i t v [ i i n Acts ] s i z e i . d u r a t i o n ;
7 dvar s e q u e n c e s e q i n i t v t y p e s a l l ( i i n Acts ) i . type ;
8

9 execute {
10 s e t t i n g s . bigmapthreshold = 100000;
11 }
12

13 s u b j e c t to {
14 noOverlap ( s e q ) ;
15 b e f o r e ( seq , i t v [<"Two" >] , i t v [<"One" >]) ;
16 }
17

18 tuple ActSolution { int s t a r t ; Activity act ; }


19 s o r t e d { A c t S o l u t i o n } S o l u t i o n = { <s t a r t O f ( i t v [ i ] ) , i > | i i n Acts } ;
20

21 execute {
22 w r i t e l n ( seq ) ;
23 for (a in Solution ) {
24 w r i t e l n ( a . a c t . name ) ;

Constraint Programming Part 2 Page 6


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

25 }
26 }

3.2 Declare set


1 s e t o f ( tupleName ) setName = . . . ;

or
1 { tupleName } setName = . . . ;

For Example
1 s e t o f ( Node ) NodeSet = . . . ;
2 {Node} NodeSet = . . . ;
3 { i n t } SetA = . . . ;
4 { s t r i n g } SetB = . . . ;

3.3 Declare subset from available set


1 { string } Cities =...;
2 { s t r i n g } Products = . . . ;
3 f l o a t Capacity = . . . ;
4 tuple connection { string o ; string d ; }
5 tuple route {
6 string p;
7 connection e ;
8 }
9 { r o u t e } Routes = . . . ;
10 { c o n n e c t i o n } C o n n e c t i o n s = { c | <p , c> i n Routes } ;
11 tuple supply {
12 string p;
13 string o ;
14 }
15 { s u p p l y } S u p p l i e s = { <p , c . o> | <p , c> i n Routes } ;
16 f l o a t Supply [ S u p p l i e s ] = . . . ;
17 t u p l e customer {
18 string p;
19 string d;
20 }
21 { customer } Customers = { <p , c . d> | <p , c> i n Routes } ;

4 Constraints

Constraints can also be declared in arrays.


The main objective of declaring constraints is to obtain dual and slack in sensitivity
analysis

Constraint Programming Part 2 Page 7


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

1 c o n s t r a i n t c t S u p p l y [ rangeX ] [ rangeY ] ;

Constraint Programming Part 2 Page 8


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

5 Data Initialization

OPL offers a variety of ways of initializing data. One particularly useful feature is the
possibility of associating indices with values to avoid various kinds of errors.
*.mod file
1 { s t r i n g } Products = . . . ;
2 { s t r i n g } Components = . . . ;
3 f l o a t Demand [ Products ] [ Components ] = . . . ;
4 f l o a t P r o f i t [ Products ] = . . . ;
5 f l o a t Stock [ Components ] = . . . ;
6 dvar f l o a t+ P r o d u c t i o n [ Products ] ;

Data in *.dat file


1 Products = { " g a s " , " c h l o r i d e " } ;
2 Components = { " n i t r o g e n " , " hydrogen " , " c h l o r i n e " } ;
3 P r o f i t = #[" g a s " : 3 0 , " c h l o r i d e " : 4 0 ] # ;
4 Stock = #[" n i t r o g e n " : 5 0 , " hydrogen " : 1 8 0 , " c h l o r i n e " : 4 0 ] # ;
5 Demand = #[
6 " g a s " : #[ " n i t r o g e n " : 1 " hydrogen " : 3 " c h l o r i n e " : 0 ]# ,
7 " c h l o r i d e " : #[ " n i t r o g e n " : 1 " hydrogen " : 4 " c h l o r i n e " : 1 ]#
8 ]#;

When using index:value pairs, the delimiters #[ and ]# must be used instead of [ and ]. The
*mod.file
1 { s t r i n g } Products = . . . ;
2 { s t r i n g } Resources = . . . ;
3 t u p l e productData {
4 f l o a t demand ;
5 float insideCost ;
6 f l o a t outsideCost ;
7 f l o a t consumption [ R e s o u r c e s ] ;
8 }
9 productData Product [ Products ] = . . . ;
10 f l o a t Capacity [ R e s o u r c e s ] = . . . ;
11 dvar f l o a t+ I n s i d e [ Products ] ;
12 dvar f l o a t+ O u t s i d e [ Products ] ;

The *dat.file
1 Products = { " k l u s k i " , " c a p e l l i n i " , " f e t t u c i n e " } ;
2 Resources = { " f l o u r " , " eggs " };
3 Product = #[
4 k l u s k i : < 1 0 0 , 0 . 6 , 0 . 8 , [ 0 . 5 , 0 . 2 ] >,
5 c a p e l l i n i : < 2 0 0 , 0 . 8 , 0 . 9 , [ 0 . 4 , 0 . 4 ] >,
6 f e t t u c i n e : < 300 , 0 . 3 , 0 . 4 , [ 0 . 3 , 0.6 ] >
7 ]#;
8 Capacity = [ 2 0 , 40 ] ;

Constraint Programming Part 2 Page 9


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

The instruction
1 ProductData p r o d u c t [ Products ] = . . . ;

declares an array with number of arrays equal to Product array and each element has type
of ProductData

6 Declare generic LB and UB for variables

LB and UB can be computed through other arrays parameters


For example
1 dvar f l o a t m[ j i n ArrayX ] i n LB [ j ] ∗ K . . Up [ j ] ∗ K;

Or
1 i n t MaxValue = max( r i n ArrayX ) Cap [ r ] ;
2 dvar i n t Y[ arrayY ] i n 0 . . MaxValue ;

Constraint Programming Part 2 Page 10


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

7 Set operators
1 | Expression " in " Expression
2 | E x p r e s s i o n " not i n " E x p r e s s i o n
3 | Expression " i n t e r " Expression
4 | E x p r e s s i o n " union " E x p r e s s i o n
5 | Expression " d i f f " Expression
6 | Expression " symdiff " Expression

These code lines initialize i to 1, u to 1,2,3,4,5, d to 2,3, and sd to 2,3,4,5.


1 { i n t } s1 = { 1 , 2 , 3 } ;
2 { i n t } s2 = { 1 , 4 , 5 } ;
3 { i n t } i = s1 i n t e r s2 ;
4 { i n t } u = s 1 union s 2 ;
5 { i n t } d = s1 d i f f s2 ;
6 { i n t } sd = s 1 s y m d i f f s 2 ;

8 Sensitivity analysis

Information on constraints in MIP

• c.LB the lower bound

• c.UB the upper bound

• c.slack the slack value

• c.dual the value of the associated dual variable

• v.reducedCost returned the reduced cost of variables v

Example
1 u s i n g CPLEX;
2 i n t nbPro =5;
3 i n t nbRes =4;
4 r a n g e Pro = 1 . . nbPro ;
5 r a n g e Res = 1 . . nbRes ;
6 i n t A[ Res ] [ Pro ] = [ [ 1 , 2 , 2 , 1 , 3 ] , [ 0 , 1 , 3 , 4 , 5 ] , [ 2 , 2 , 2 , 1 , 0 ] , [ 4 , 5 , 6 , 2 , 3 ] ] ;
7 i n t Cap [ Res ] = [ 1 0 0 0 , 2 0 0 0 , 1 0 0 0 , 3 0 0 0 ] ;
8 i n t Ben [ Pro ] = [ 2 , 3 , 3 , 2 , 4 ] ;
9 dvar f l o a t+ X[ Pro ] ;
10 c o n s t r a i n t ctRes [ Res ] ;
11 maximize sum ( p i n Pro )X[ p ] ∗ Ben [ p ] ;
12 s u b j e c t to {
13 f o r a l l ( r i n Res ) {
14 ctRes [ r ] : // Resource c o n s t r a i n t

Constraint Programming Part 2 Page 11


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

15 sum ( p i n Pro )X[ p ] ∗A[ r ] [ p]<=Cap [ r ] ;


16 }
17 }
18 e x e c u t e POSTPROCESSING{
19 f o r ( i i n Res ) {
20 writeln ( " Information of constraint [ " , i , " ] " ) ;
21 w r i t e l n ( " d u a l : " , ctRes [ i ] . d u a l ) ;
22 w r i t e l n ( " s l a c k : " , ctRes [ i ] . s l a c k )
23 w r i t e l n ( "−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−" ) ;
24 };
25 f o r ( p i n Pro ) {
26 w r i t e l n ( " ReducedCost o f v a r i a b l e [ " , p , " ] " ) ;
27 w r i t e l n (X[ p ] . r e d u c e d C o s t ) ;
28 }
29 }

Constraint Programming Part 2 Page 12


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

9 Logical constraints

• &&: Logic AND

• ||: Logic OR

• !: Logic NOT

• ! =: Different form

• ==: Equivalent

The following nonlinear expressions are accepted in an OPL model:

• 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

• A linear constraint can appear as a term in a logical constraint

1 (X == 0 ) | | (Y >= 2 0 ) ;
2 (X== 0 ) && ( Z != 2 0 ) ;
3 (X>= 2 0 ) | | (V >= 2 0 ) => R>= 2 0 ;

Constraint Programming Part 2 Page 13


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

10 Scheduling

Most scheduling applications consist of interval decision variables that have start and
end time.
Interval variable a has domain dom(a) is a subset of {⊥} ∪ {[s, e)|s, e ∈ Z, s ≤ e}
If interval variable a is fixed, then:

• interval is absent: a = {⊥}

• interval is present: a = [s, e)

• s(a) integer start value

• e(a) integer end value

• l(a) positive integer length defined as e(a) − s(a).

• The presence status x(a) will be equal to 1.

• For a fixed interval that is absent, x(a) = 0 and the start, end and length are undefined.

Constraint Programming Part 2 Page 14


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

11 Expression on Interval constraints

To access to the different attributes of an interval variable, it can be used : startOf, endOf,
lengthOf, and sizeOf
For example: 
s(a) if x(a)
startOf (a, dval) =
dval otherwise

Other functions:

• startOf(a,dval)

• endOf(a,dval)

• lengthOf(a,dval)

• sizeOf(a,dval)

• presenceOf(a)

Let a and b be the optional interval variable, the presenceOf constraint is useful when

• Interval a must be present in solution presenceOf (a) == 1

• a is present then b must be present is equivalent to presenceOf (a) => presenceOf (b).

• if a and b are optional, and just one of them is allowed in the solution

presenceOf (a) + presenceOf (b) == 1

To evaluate the piecewise linear function on a given bound of an interval, it can be used :
startEval, endEval, lengthEval, and sizeEval,
In the case of optional intervals an integer value dval must be specified which represents the
value of the expression when the interval is absent. If this value is omitted, it is supposed
to be 0.
For example: 
F (s(a)) if x(a)
startEval(a, F, dval) =
dval otherwise

Other functions:

• startEval(a,dval)

• endEval(a,dval)

Constraint Programming Part 2 Page 15


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

• lengthEval(a,dval)

• sizeEval(a,dval)

Constraint Programming Part 2 Page 16


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

12 Notations

The main notations used throughout the scheduling section are defined here. Vectors are
denoted by capital letters, for example Y . The size of a vector Y is denoted |Y |.
If n = |Y |, the vector is denoted Y = (y1 , · · · , yn ).

Notation Type Description


a, b Interval variable
x(a) Boolean variable execution status of a
s(a), e(a), l(a) integer expression start, end, lenght of a
sz(a) integer expression size of a
A, B vector of interval variables
p interval sequence variable
f, g, h cumul function expression
state function
a, b fixed interval
x(a) boolean presence status of a
s(a), e(a), l(a) integer expression start, end, lenght of a
sz(a) integer size of a
π integer variable permutation
z integer(expression) delay of a precedence
t integer date
T vector of integers array of dates
S vector of floats array of slopes
T vector of integers array of dates
v, w integer or float miscellaneous integer or float value
V, W vector of integers or floats vector of miscellaneous values
align boolean alignment flag
F, G, H piecewise linear function R → R
vector of cumul function expressions
T (p, a) integer type of interval a in sequence p
M integer matrix transition distance matrix
F+ set of functions set of all functions f : Z → Z +

Constraint Programming Part 2 Page 17


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

13 Piecewise linear and stepwise linear functions

Pieceswise linear function


A piecewise linear function F (t) is defined by a tuple F = piecewise(S, T, t0 , v0 ) where:

• S: vector of (n + 1) elements

• T : vector of (n + 1) elements

• ∀t ∈ [ti , ti+1 ] : F (t) = F (ti ) + si+1 (t − ti )

Stepwise function
A stepwise function is a special case of the piecewise linear function, where all slopes are
equal to 0 and the domain of F are integer. A stepwise function F (t) is defined by a tuple
F = stepwise(V, T ) where:

• ∀t ∈ [ti , ti+1 ) : F (t) = vi+1

Figure 1: Piecewise Functions

Examples
The following piecewise and stepwise function are depicted in the diagram

• A V-shape function: f (10) = 0, slope −1 when x ≤ 10, slope s when x ≥ 10:


1 pwlFunction F1 = p i e c e w i s e {−1−>10; s } ( 1 0 , 0 ) ;
2

• An array of V-shaped functions indexed by i in [1..n] with at (x = T [i], y = 0), when


x ≤ T [i] slope −U [i] and when x ≥ T [i] slope V [i] :
1 pwlFunction F [ i i n 1 . . n ] = p i e c e w i s e {−U[ i ]−>T [ i ] ; V[ i ] } (T [ i ] , 0 ) ;
2

Constraint Programming Part 2 Page 18


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

• A stepwise function with value 0 before 0, 100 on [0, 20), value 60 on [20, 30), and
value 100 later on:
1 s t e p F u n c t i o n F2 = s t e p w i s e { 0−>0; 100−>20; 60−>30; 100 } ;
2

• A stepwise function with value 0 everywhere except on intervals [7i, 7i + 5) for i in


[0, 51] where the value is 100:
1 s t e p F u n c t i o n F3 = s t e p w i s e ( i i n 0 . . 5 1 , p i n 0 . . 1 ) { 100∗ p −> ( 7 ∗ i ) +(5∗p ) ;
0};
2

Syntax of piece wise linear function


1 pwlFunction F = p i e c e w i s e ( i i n 1 . . n ) { S [ i ]−>T [ i ] ; S [ n+1] } ( t0 , v0 ) ;
2 pwlFunction F = p i e c e w i s e { V[1]−>T [ 1 ] , . . . , V[ n]−>T [ n ] , V[ n+1] } ;
3 pwlFunction F [ i i n . . . ] = p i e c e w i s e ( . . . ) [ ... ];

Syntax of step wise linear function


1 s t e p F u n c t i o n F = s t e p w i s e ( i i n 1 . . n ) { V[ i ]−>T [ i ] ; V[ n+1] } ;
2 s t e p F u n c t i o n F = s t e p w i s e { V[1]−>T [ 1 ] , . . . , V[ n]−>T [ n ] , V[ n+1] } ;
3 stepFunction F[ i in . . . ] = stepwise ( . . . ) [ ... ];

Constraint Programming Part 2 Page 19


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

14 Intensity and size

Sometimes the intensity of "work" is not the same during the whole interval.
For example

• A worker who does not work during weekends (his work intensity during weekends is
0 %)

• On Friday he works only for half a day (his intensity during Friday is 50%).

• For this worker, 7 man-days of work will span for longer than just 7 days.

In this example 7 man-days represents what we call the size of the interval; that is, what
the length of the interval would be if the intensity function was always at 100 %.
To model such situations, you can specify a range for the size of an interval variable
and an integer stepwise intensity function F.
For a fixed present interval the following relation will be enforced at any solution between
the start, end, size sz of the interval and the integer granularity G (by default, the intensity
function is expressed as a percentage so the granularity G is 100):
Z e(a)
F (t)
sz(a) ≤ d(t) < sz(a) + 1
s(a) G
If no intensity is specified, it is supposed to be the constant full intensity function = 100%
so in that case sz(a) = l(a). Note that the size is not defined for absent intervals
The following figure depicts an interval variable of size 14 with its intensity function. A valid
solution is represented where the interval starts at 10 and ends at 27. Indeed in this case:
Z e(a)
F (t) 1420
sz(a) ≤ d(t) = = 14.2
s(a) G 100

Figure 2: Intensity Functions

Constraint Programming Part 2 Page 20


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

15 Declare an interval variable

Declare an interval variable include following parameters:

• optional[true/false]: whether the variable is optional

• StartMin · · · EndMax: the range of variable

• The size

• The intensity F

Example
1 dvar i n t e r v a l a [ o p t i o n a l [ ( I s O p t i o n a l ) ] ]
2 [ i n StartMin . . EndMax ]
3 [ s i z e SZ | i n SZMin . . SZMax ]
4 [ intensity F]
5 // Example j : job , s : s t a t i o n , m: machine
6 r a n g e Time = 0 . . 2 0 0 ;
7 dvar i n t e r v a l X[< j , s ,m> i n TaskSet ] o p t i o n a l i n Time s i z e ProTime[< j , s > ] ;
8

The rest is used for developed on other language such as C++ or Java
Syntax :

1 VarName : [ o p t i o n a l R a n g e : s t a r t R a n g e −−(s i z e R a n g e ) lengthRange−−>endRange ]

A1[0..1: 10..990 – (5..10)5..990 –> 25..1000]

• An absent interval variable is displayed as A1[0]

• The second range may be omitted in case the size and length of the interval variable
are necessarily equal.

• When the values are fixed, the ranges min · · · max are replaced by a single value.

• For instance, the following display represents a fixed interval variable of size 5 that is
present, starts at 10 and ends at 35:
1 A1 [ 1 : 10 −− ( 5 ) 25 −−> 3 5 ]

Constraint Programming Part 2 Page 21


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

16 Unary constraints on interval variables

CP Optimizer provides constraints for modelling restrictions that an interval cannot start,
cannot end or cannot overlap a set of fixed dates.

• forbidStart(a, F), if the interval is present, it cannot start at t where F (t) = 0.

• forbidEnd(a, F), if the interval is present, it cannot end at t where F (t − 1) = 0.

• forbidExtent(a, F), if the interval is present, it cannot overlap t where F (t) = 0.

Note that none of these constraints can be used in meta-constraint

Constraint Programming Part 2 Page 22


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

17 Precedence Constraint

• Precedence constraints have the form T C(a, b, z)

• a and b are interval variables. z is optionally minimum delay

• Precedence constraints do not imply the noOverlap constraint

List of precedence constraints

• startBeforeStart(a,b,z): s(a) + z ≤ s(b)

• startBeforeEnd(a,b,z): s(a) + z ≤ e(b)

• endBeforeStart(a,b,z): e(a) + z ≤ s(b)

• endBeforeEnd(a,b,z): e(a) + z ≤ e(b)

• startAtStart(a,b,z): s(a) + z = s(b)

• startAtEnd(a,b,z): s(a) + z = e(b)

• endAtStart(a,b,z): e(a) + z = s(b)

• endAtEnd(a,b,z): e(a) + z = e(b)

Note that none of these constraints may be used in a meta-constraint.

Constraint Programming Part 2 Page 23


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

18 Constraints on groups of interval variables

Span constraint:
span(a, {b1 , · · · , bn }) states that interval a starts together with the first present interval from
{b1 , · · · , bn } and ends together with the last one.
Alternative constraint:
alternative (a, {1, · · · , bn }) states that if interval a is presented then exactly one of intervals
{b1 , · · · , bn } is present and a starts and ends together with this chosen one.
Alternative constraint:
This can also be specified by a non-negative integer cardinality c, alternative(a, {b1 , .., bn }, c).
In this case, c interval variables that will be selected among the set b1 , · · · , bn and those c
selected intervals will have to start and end together with interval variable a.
Synchronize constraint:
synchronize(a, {b1 , .., bn }) makes intervals b1 , · · · , bn start and end together with interval a
(if a is present).

Constraint Programming Part 2 Page 24


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

19 Sequencing variables

An interval sequence variable is defined on a set of interval variables A. Informally speaking,


the value of an interval sequence variable represents a total ordering of the interval variables
of A. Note that any absent interval variables are not considered in the ordering.
Example
1 // Example j : job , s : s t a t i o n , m: machine ,
2 // seqM : s e q u e n c e on Machine with r e q u i r e t h e i n f o r m a t i o n o f type o f each j o b
3 // These t y p e s a r e used f o r l o o k i n g up t h e s e t u p time
4 r a n g e Time = 0 . . 2 0 0 ;
5 dvar i n t e r v a l X[< j , s ,m> i n TaskSet ] o p t i o n a l i n 0 . . 1 0 0 s i z e ProTime[< j , s > ] ;
6 dvar s e q u e n c e seqM [m i n Mach ] i n a l l (< j , s ,m> i n TaskSet )X[< j , s ,m>] t y p e s a l l (<
j , s ,m, t> i n TaskTypeSet ) t ;
7 dvar s e q u e n c e s e q J [ j i n Job ] i n a l l (< j , s ,m> i n TaskSet )X[< j , s ,m> ] ;

Let A be a set of fixed intervals and n denote the cardinality of A.


Constraints on sequence variables

• f irst(p, a) states that a must be the first interval of the sequence p.

• last(p, a) states that a must be the last interval of the sequence p.

• bef ore(p, a, b) states that a must appear before b in the sequence p.

• prev(p, a, b) states that a is just before b in the sequence p

The no overlap constraint on an interval

• noOverlap(p) there is no overlap on interval sequence variable p

• noOverlap(p, M ) If a transition distance matrix M is specified, it defines the minimal


non-negative distance that must separate two consecutive intervals in the sequence

Constraint Programming Part 2 Page 25


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

20 State function

A state function is a set of non-overlapping intervals over which the function maintains a
particular non- negative integer state.
In between those intervals, the state of the function is not defined, typically because of an
ongoing transition between two states
A state function example:

• [start = 0, end = 100) : state = 0,

• [start = 150, end = 250) : state = 1,

• [start = 250, end = 300) : state = 1,

• [start = 320, end = 420) : state = 2,

• [start = 460, end = 560) : state = 0, .

The notions of start and end alignment which is particularly useful for modeling parallel
batches.

20.1 Syntax and examples

If f is a state function, a an interval variable, v, vmin , vmax non-negative integers and


algns ,algne two boolean values:

• alwaysEqual(f, s, e, v[, aligns , aligne ]): whenever a is present, f must be defined ev-
erywhere between the start and the end of a and be constant and equal to non-negative
value v.

– If algns is true, it means that a must start at the beginning of the interval where
f is in state s.
– If algne is true, it means that a must end at the end of the interval where f in
state s.

• alwaysConstant(f, a, algns , algne ) means that when a is present, f must be defined


between the start and the end of a and be constant.

• alwaysN oState(f, a) means that when a is present, [s(a), e(a)) ∩ D(f ) = ∅

• alwaysIn(f, a, vmin , vmax ) means that when a is present, between the start and the end
of a, function f , if defined, must belong to the range [vmin, vmax] where 0 ≤ V min ≤
V max.

Constraint Programming Part 2 Page 26


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

• alwaysEqual(f,s,e,v[,aligns , aligne ]);

• alwaysEqual(f,a,v[,aligns , aligne ]);

• alwaysConstant(f,s,e,[,aligns , aligne ]);

• alwaysConstant(f,a,[,aligns , aligne ]);

• alwaysNoState(f,s,e);

• alwaysNoState(f,a);

• alwaysIn(f,u,v,hmin , hmax );

• alwaysIn(f,a,hmin , hmax );

Note that these constraints cannot be used in meta-constraints.


Example
Problem statement:
A machine can be equipped with a tool among a set of n possible tools. Each operation o
executed on the machine needs a specific tool RequiredT ool[o]. The machine can process
several operation simultaneously provided these operations are compatible with the tool
currently installed on the machine. Changing the tool installed on the machine needs some
constant set-up time which is supposed to be independent from the tools.
1 i n t nbTools = . . . ;
2 i n t nbOps = . . . ;
3 i n t setupTime = . . . ;
4 r a n g e T o o l s = 1 . . nbTools ;
5 r a n g e O p e r a t i o n s = 1 . . nbOps ;
6 i n t Duration [ O p e r a t i o n s ] = . . . ;
7 i n t RequiredTool [ O p e r a t i o n s ] = . . . ;
8 dvar i n t e r v a l op [ o i n O p e r a t i o n s ] s i z e Duration [ o ] ;
9 tuple t r i p l e t { int t l 1 ; int t l 2 ; int value ; };
10 { t r i p l e t } T r a n s i t i o n = { <t l 1 , t l 2 , setupTIme> } t l 1 , t l 2 i n T o o l s } ;
11 s t a t e F u n c t i o n machineTool with T r a n s i t i o n ;
12 constraints {
13 f o r a l l ( o in Operations ) {
14 alwaysEqual ( machineTool , op [ o ] , RequiredTool [ o ] ) ;
15 }
16 }

Constraint Programming Part 2 Page 27


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

21 Cumulative functions

21.1 Introduction

• The cumulated usage of the resource by the activities is usually represented by a


function of time.

• An activity usually increases the cumulated resource usage function at its start time
and decreases it when it releases the resource at its end time (pulse function).

• CP Optimizer introduces the notion of the cumulative function expression, which is a


function that represents the sum of individual contributions of intervals.

Let u, v ∈ Zand h, hmin , hmax ∈ Z + and a be an interval variable, the following elementary
cumul function expressions are illustrated in the following figure:

Figure 3: Cumul Functions

21.2 Cumulative expression

• pulse(u,v,h)

• step(u,h)

Constraint Programming Part 2 Page 28


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

• pulse(a,h)

• pulse(a,hmin , hmax )

• stepAtStart(a,h)

• stepAtStart(a,hmin , hmax )

• stepAtEnd(a,h)

• stepAtEnd(a,hmin , hmax )

21.3 Constraints on cumul function expression

• alwaysIn(f, u, v, hmin , hmax ) means that the values of f must be in the range [hmin , hmax ]
on the interval [u, v).

• alwaysIn(f, a, hmin , hmax ) means that if a is present, the values of f must be in the
range [hmin , hmax ] between s(a) and e(a).

• f ≤ hmax means that f cannot take values greater than hmax . It is equivalent to
alwaysIn(f, −∞, +∞, 0, hmax )

• f ≥ hmin means that f cannot take values lower than hmin . It is equivalent to
alwaysIn(f, −∞, +∞, hmin , +∞)

• dexpr int h = heightAtStart(a,f[,dval]);

• dexpr int h =heightAtEnd(a,f[,dval]);

21.4 Combine several cumul function expression

Let a and b be two interval and function f defined by:

f = pulse(a, 3) + pulse(a, 2) − stepAtEnd(a, 1) + stepAtStart(b, 2) − stepAtEnd(b, 3).

If a presents, heightAtStart(a, f ) = 5 and heightAtEnd(a, f ) = 4


Note that these constraints cannot be used in meta-constraints.

Constraint Programming Part 2 Page 29


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

22 Example of job shop

Given following job shop problem

Table 1: Parameters of Example

Job Sequence Processing Time


1 1,2,3 p11 = 10, p21 = 8, p31 = 4
2 2,1,4,3 p22 = 8, p12 = 3, p42 = 5, p32 = 6
3 1,2,4 p13 = 4, p23 = 7, p43 = 3

1 /∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
2 ∗ OPL 1 2 . 9 . 0 . 0 Model
3 ∗ Author : kyphuc
4 ∗ C r e a t i o n Date : Jun 5 , 2020 a t 7 : 1 4 : 1 0 PM
5 ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
6 u s i n g CP;
7 i n t nbJob =3;
8 i n t nbMachine =4;
9 r a n g e Job = 1 . . nbJob ;
10 r a n g e Machine = 1 . . nbMachine ;
11 t u p l e Node {
12 i n t machine ;
13 i n t job ;
14 };
15 tuple SolidArc {
16 i n t job ;
17 i n t pre ;
18 i n t suc ;
19 };
20 /∗
21 Job 1 : 1−2−3
22 Job 2 : 2−1−4−3
23 Job 3 : 1−2−4
24 ∗/
25 {Node} NodeSet ={<1,1>,<2,1>,<3,1>,
26 <2,2>,<1,2>,<4,2>,<3,2>,
27 <1 ,3 > , <2 ,3 > , <4 ,3 >};
28 { S o l i d A r c } S o l i d A r c S e t ={ <1 ,1 ,2 > , <1 ,2 ,3 > ,
29 <2 ,2 ,1 > , <2 ,1 ,4 > , <2 ,4 ,3 > ,
30 <3 ,1 ,2 > , <3 ,2 ,4 >};
31 i n t ProTime [ NodeSet ] = [ 1 0 , 8 , 4 ,
32 8 ,3 ,5 ,6 ,
33 4 ,7 ,3];
34 r a n g e Time = 0 . . 3 0 ;
35 dvar i n t e r v a l X[ i i n NodeSet ] i n Time s i z e ProTime [ i ] ;
36 dvar s e q u e n c e seqM [m i n Machine ] i n a l l (<m, j > i n NodeSet ) X[<m, j > ] ;
37 dvar s e q u e n c e s e q J [ j i n Job ] i n a l l (<m, j > i n NodeSet )X[<m, j > ] ;

Constraint Programming Part 2 Page 30


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

38 dvar i n t+ Cmax ;
39

40 execute {
41 cp . param . TimeLimit =4∗60; // s e t u p t h e time l i m i t
42 }
43

44 minimize Cmax ;
45 s u b j e c t to {
46

47 f o r a l l ( m i n Machine ) {
48 noOverlap ( seqM [m] ) ;
49 }
50 f o r a l l ( j i n Job ) {
51 noOverlap ( s e q J [ j ] ) ;
52 }
53 f o r a l l ( n i n NodeSet ) {
54 Cmax >= endOf (X[ n ] ) ;
55 }
56 f o r a l l (< j , p , s> i n S o l i d A r c S e t ) {
57 b e f o r e ( s e q J [ j ] ,X[<p , j >] ,X[< s , j >]) ;
58 }
59

60 }
61 e x e c u t e RESULT{
62 f o r (m i n Machine )
63 {
64 w r i t e l n ( seqM [m] ) ;
65 }
66 }

In this job shop there are 2 kinds of sequences:

• seqM: to find the sequence of job on machine

• seqJ: to ensure that job must be processed regarding to its order

Note:
In this example, range of Time is set from 0 to 30 when the optimal Cmax = 28. If time
range is set smaller than 28, it cannot find the feasible solution.

Constraint Programming Part 2 Page 31


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

23 Example of cumul function for job shop with parallel


machine
1 /∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
2 ∗ OPL 1 2 . 9 . 0 . 0 Model
3 ∗ Author : kyphuc
4 ∗ C r e a t i o n Date : Jun 1 0 , 2020 a t 9 : 4 4 : 1 3 AM
5 ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
6 u s i n g CP;
7 i n t nbJob =3;
8 i n t nbSta =4;
9

10 t u p l e Node{
11 int station ;
12 i n t job ;
13 }
14 tuple SolidArc {
15 i n t job ;
16 i n t pre ;
17 i n t suc ;
18 }
19 /∗
20 Job 1 : 1−2−3
21 Job 2 : 2−1−4−3
22 Job 3 : 1−2−4
23 ∗/
24 {Node} NodeSet ={<1,1>,<2,1>,<3,1>,
25 <2,2>,<1,2>,<4,2>,<3,2>,
26 <1 ,3 > , <2 ,3 > , <4 ,3 >};
27 { S o l i d A r c } S o l i d A r c S e t ={ <1 ,1 ,2 > , <1 ,2 ,3 > ,
28 <2 ,2 ,1 > , <2 ,1 ,4 > , <2 ,4 ,3 > ,
29 <3 ,1 ,2 > , <3 ,2 ,4 >};
30 r a n g e Job = 1 . . nbJob ;
31 r a n g e Sta = 1 . . nbSta ;
32 i n t ProTime [ NodeSet ] = [ 1 0 , 8 , 4 , 8 , 3 , 5 , 6 , 4 , 7 , 3 ] ;
33 i n t nbMach [ Sta ] = [ 2 , 2 , 2 , 2 ] ;
34 dvar i n t e r v a l X[ r i n NodeSet ] i n 0 . . 3 0 s i z e ProTime [ r ] ;
35 dvar s e q u e n c e seqS [ s i n Sta ] i n a l l (<s , j > i n NodeSet )X[< s , j > ] ;
36 dvar s e q u e n c e s e q J [ j i n Job ] i n a l l (<s , j > i n NodeSet )X[< s , j > ] ;
37 cumulFunction f [ s i n Sta ]=sum(<s , j > i n NodeSet ) p u l s e (X[< s , j > ] , 1 ) ;
38 dvar i n t+ Cmax ;
39 e x e c u t e SETTING{
40 cp . param . TimeLimit =3∗60;
41 }
42 minimize Cmax ;
43 s u b j e c t to {
44 Cmax==max( n i n NodeSet ) endOf (X[ n ] ) ;

Constraint Programming Part 2 Page 32


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

45 f o r a l l ( s i n Sta ) {
46 f [ s]<=nbMach [ s ] ;
47 }
48

49 f o r a l l (< j , p , s> i n S o l i d A r c S e t ) {
50 b e f o r e ( s e q J [ j ] ,X[<p , j >] ,X[< s , j >]) ;
51 }
52

53 f o r a l l ( j i n Job ) {
54 noOverlap ( s e q J [ j ] ) ;
55 }
56 }

Note:
In this job shop each station has more than one machine. As a result of this, it requires a
cumul function to represent how many machines are occupied in a station at a specific time
t

Constraint Programming Part 2 Page 33


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

24 Example of dependent setup time for job shop

keyword: types
The keyword types is used to associate a non-negative integer (called the type of the interval
variable) with each interval variable in a sequence.
Model files (.mod)
1 dvar i n t e r v a l a [ i i n . . . ] ...; // I n t e r v a l v a r i a b l e s
2 i n t T[ i in . . . ] = . . . ; // I n t e g e r non−n e g a t i v e t y p e s
3 dvar s e q u e n c e s i n a l l ( i i n . . . ) a [ i ] t y p e s a l l ( i i n . . . ) T [ i ] ;

Types usage
Types are often used when the setup time needs to be considered
The integer type is used in the form of a noOverlap constraint that uses a transition distance.
The transition distance is modeled as a tuple set < t1 , t2 , dmin > that specifies a minimal
distance between pairs of types (t1 , t2 ).
It states that if a2 of type t2 is scheduled after a1 of type t1 in the sequence, a distance of
at least dmin must separate the end of a1 from the start of a2 (provided that a1 and a2 are
present).This constraint can be written as
1 t u p l e t r i p l e t { i n t t 1 ; i n t t 2 ; i n t dmin ; } ;
2 { triplet } tdistance = . . . ;
3

4 constraints {
5 noOverlap ( seq , t d i s t a n c e ) ;
6 }

Job Shop with dependent setup time


1 u s i n g CP;
2 i n t nbJob =3;
3 i n t nbMachine =4;
4 r a n g e Job = 1 . . nbJob ;
5 r a n g e Machine = 1 . . nbMachine ;
6 t u p l e Node {
7 i n t machine ;
8 i n t job ;
9 };
10 t u p l e NodeType{
11 i n t machine ;
12 i n t job ;
13 i n t type ;
14 };
15

16 tuple SolidArc {
17 i n t job ;
18 i n t pre ;

Constraint Programming Part 2 Page 34


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

19 i n t suc ;
20 };
21

22 t u p l e Setup {
23 i n t type1 ;
24 i n t type2 ;
25 i n t time ;
26 };
27 /∗
28 Job 1 : 1−2−3
29 Job 2 : 2−1−4−3
30 Job 3 : 1−2−4
31 ∗/
32 { Setup } S e t u p S e t ={ <1 ,5 ,3 > , <5 ,1 ,3 > , <5 ,8 ,2 > , <8 ,5 ,1 > , <1 ,8 ,3 > , <8 ,1 ,5 > ,
33 <2 ,4 ,3 > , <4 ,2 ,1 > , <2 ,9 ,5 > , <9 ,2 ,5 > , <4 ,9 ,3 > , <9 ,4 ,1 > ,
34 <3 ,7 ,2 > , <7 ,3 ,1 > ,
35 <6 ,10 ,1 > , <10 ,6 ,1 >};
36 {NodeType} NodeTypeSet ={ <1 ,1 ,1 > , <2 ,1 ,2 > , <3 ,1 ,3 > ,
37 <2 ,2 ,4 > , <1 ,2 ,5 > , <4 ,2 ,6 > , <3 ,2 ,7 > ,
38 <1 ,3 ,8 > , <2 ,3 ,9 > , <4 ,3 ,10 >};
39 {Node} NodeSet={<m, j >|<m, j , t> i n NodeTypeSet } ;
40 { S o l i d A r c } S o l i d A r c S e t ={ <1 ,1 ,2 > , <1 ,2 ,3 > ,
41 <2 ,2 ,1 > , <2 ,1 ,4 > , <2 ,4 ,3 > ,
42 <3 ,1 ,2 > , <3 ,2 ,4 >};
43 i n t ProTime [ NodeSet ] = [ 1 0 , 8 , 4 ,
44 8 ,3 ,5 ,6 ,
45 4 ,7 ,3];
46 r a n g e Time = 0 . . 2 0 0 0 ;
47 dvar i n t e r v a l X[ i i n NodeSet ] i n Time s i z e ProTime [ i ] ;
48 dvar s e q u e n c e seqM [m i n Machine ] i n a l l (<m, j > i n NodeSet ) X[<m, j >] t y p e s a l l (<
m, j , t> i n NodeTypeSet ) t ;
49 dvar s e q u e n c e s e q J [ j i n Job ] i n a l l (<m, j > i n NodeSet )X[<m, j > ] ;
50 dvar i n t+ Cmax ;
51

52 execute {
53 cp . param . TimeLimit =4∗60; // s e t u p t h e time l i m i t
54 }
55

56 minimize Cmax ;
57 s u b j e c t to {
58 f o r a l l ( m i n Machine ) {
59 noOverlap ( seqM [m] , S e t u p S e t ) ;
60 }
61 f o r a l l ( j i n Job ) {
62 noOverlap ( s e q J [ j ] ) ;
63 }
64 f o r a l l ( n i n NodeSet ) {
65 Cmax >= endOf (X[ n ] ) ;

Constraint Programming Part 2 Page 35


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

66 }
67 f o r a l l (< j , p , s> i n S o l i d A r c S e t ) {
68 b e f o r e ( s e q J [ j ] ,X[<p , j >] ,X[< s , j >]) ;
69 }
70 }
71 e x e c u t e RESULT{
72 f o r (m i n Machine )
73 {
74 w r i t e l n ( seqM [m] ) ;
75 }
76 }

Note:
The statement
1 dvar s e q u e n c e seqM [m i n Machine ] i n a l l (<m, j > i n NodeSet ) X[<m, j >] t y p e s a l l (<
m, j , t> i n NodeTypeSet ) t ;

will take the Type attribute values in < m, j, t > NodeTypeSet In the above example each
task must have a type number to identify the dependent setup time between 2 tasks.

Constraint Programming Part 2 Page 36


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

25 Example of dependent setup time for for jobshop with


parallel machine
1 /∗ ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
2 ∗ OPL 1 2 . 9 . 0 . 0 Model
3 ∗ Author : kyphuc
4 ∗ C r e a t i o n Date : Jun 1 3 , 2020 a t 8 : 0 6 : 4 9 AM
5 ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗ ∗/
6 u s i n g CP;
7 i n t nbJob =3;
8 i n t nbSta =4;
9 i n t nbMach=8;
10 r a n g e Mach = 1 . . nbMach ;
11 r a n g e Job = 1 . . nbJob ;
12 r a n g e Sta = 1 . . nbSta ;
13 t u p l e Task {
14 i n t job ;
15 int sta ;
16 i n t mach ;
17 };
18 t u p l e TaskType{
19 i n t job ;
20 int sta ;
21 i n t mach ;
22 i n t type ;
23 };
24 t u p l e MachSta{
25 int sta ;
26 i n t mach ;
27 }
28 t u p l e Node{
29 i n t job ;
30 int sta ;
31 }
32 tuple SolidArc {
33 i n t job ;
34 i n t preSta ;
35 i n t sucSta ;
36 };
37

38 t u p l e Setup {
39 i n t type1 ;
40 i n t type2 ;
41 i n t time ;
42 };
43 { Setup } S e t u p S e t ={ <1 ,5 ,3 > , <5 ,1 ,3 > , <5 ,8 ,2 > , <8 ,5 ,1 > , <1 ,8 ,3 > , <8 ,1 ,5 > ,
44 <2 ,4 ,3 > , <4 ,2 ,1 > , <2 ,9 ,5 > , <9 ,2 ,5 > , <4 ,9 ,3 > , <9 ,4 ,1 > ,

Constraint Programming Part 2 Page 37


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

45 <3 ,7 ,2 > , <7 ,3 ,1 > ,


46 <6 ,10 ,1 > , <10 ,6 ,1 >};
47 /∗
48 Job 1:1−2−3
49 Job 2:2−1−4−3
50 Job 3:1−2−4
51 ∗/
52 {TaskType} TaskTypeSet
={ <1 ,1 ,1 ,1 > , <1 ,1 ,2 ,1 > , <1 ,2 ,3 ,2 > , <1 ,2 ,4 ,2 > , <1 ,3 ,5 ,3 > , <1 ,3 ,6 ,3 > ,
53 <2 ,2 ,3 ,4 > , <2 ,2 ,4 ,4 > , <2 ,1 ,1 ,5 > , <2 ,1 ,2 ,5 > ,
54 <2 ,4 ,7 ,6 > , <2 ,4 ,8 ,6 > , <2 ,3 ,5 ,7 > , <2 ,3 ,6 ,7 > ,
55 <3 ,1 ,1 ,8 > , <3 ,1 ,2 ,8 > , <3 ,2 ,3 ,9 > , <3 ,2 ,4 ,9 > , <3 ,4 ,7 ,10 > , <3 ,4 ,8 ,10 >
56 };
57 { Task } TaskSet={<j , s ,m>|<j , s ,m, t> i n TaskTypeSet } ;
58 {MachSta} MachStaSet={<s ,m>|<j , s ,m, t> i n TaskTypeSet } ;
59 {Node} NodeSet={<j , s >|<j , s ,m, t> i n TaskTypeSet } ;
60 { S o l i d A r c } S o l i d A r c S e t ={ <1 ,1 ,2 > , <1 ,2 ,3 > ,
61 <2 ,2 ,1 > , <2 ,1 ,4 > , <2 ,4 ,3 > ,
62 <3 ,1 ,2 > , <3 ,2 ,4 >};
63

64 i n t ProTime [ NodeSet ] = [ 1 0 , 8 , 4 ,
65 8 ,3 ,5 ,6 ,
66 4 ,7 ,3];
67 i n t MachOnSta [ Sta ] = [ 2 , 2 , 2 , 2 ] ;
68 r a n g e Time = 0 . . 2 0 0 ;
69 dvar i n t e r v a l X[< j , s ,m> i n TaskSet ] o p t i o n a l i n 0 . . 1 0 0 s i z e ProTime[< j , s > ] ;
70 dvar s e q u e n c e seqM [m i n Mach ] i n a l l (< j , s ,m> i n TaskSet )X[< j , s ,m>] t y p e s a l l (<
j , s ,m, t> i n TaskTypeSet ) t ;
71 dvar s e q u e n c e s e q J [ j i n Job ] i n a l l (< j , s ,m> i n TaskSet )X[< j , s ,m> ] ;
72 dvar i n t+ Cmax ;
73

74 execute {
75 cp . param . TimeLimit =4∗60; // s e t u p t h e time l i m i t
76 }
77

78 minimize Cmax ;
79 s u b j e c t to {
80

81 f o r a l l ( m i n Mach ) {
82 noOverlap ( seqM [m] , S e t u p S e t ) ;
83 }
84 f o r a l l ( j i n Job ) {
85 noOverlap ( s e q J [ j ] ) ;
86 }
87 f o r a l l ( n i n TaskSet ) {
88 Cmax >= endOf (X[ n ] ) ;
89 }
90

Constraint Programming Part 2 Page 38


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

91 f o r a l l (< j , p , s> i n S o l i d A r c S e t ,<p , g> i n MachStaSet ,< s , h> i n MachStaSet ) {


92 b e f o r e ( s e q J [ j ] ,X[< j , p , g >] ,X[< j , s , h >]) ;
93 }
94

95 f o r a l l (< j , s> i n NodeSet ) {


96 sum(< j , s ,m> i n TaskSet ) ( p r e s e n c e O f (X[< j , s ,m>]) ) ==1;
97 }
98

99 }
100 e x e c u t e RESULT{
101 f o r (m i n Mach )
102 {
103 w r i t e l n ( seqM [m] ) ;
104 }
105 }

Constraint Programming Part 2 Page 39


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

26 Access the solution in postprocessing.

It can access information about intervals, sequences, and instances of cumulFunction


or stateFunction in a solution.
The value of a variable in a solution can be accessed using the following instructions:

• Interval variable a: a.present, a.start, a.end, a.size

• Sequence variable p: p.first(), p.next(), p.prev(a), p.next(a)

• State function f : stateFunctionValue(f, i);

• Cumul function f : cumulFunctionValue(f,t) returns the value of f at t


1 i n t v [ t i n 0 . . 5 ] = cumulFunctionValue ( f , t ) ;

• Segment function: segmentStart(f,i), segmentEnd(f,i), segmentValue(f, i), num-


berOfSegments(f ); returns the value regarding to segment i of a cumul function or
a state function f

An example of obtain all pool solutions in CP


1 main{
2 model . g e n e r a t e ( ) ;
3 cp . s t a r t N e w S e a r c h ( ) ;
4 w h i l e ( cp . next ( ) ) {
5 model . p o s t P r o c e s s ( ) ;
6 }
7 }

The model.postP rocess takes responsibility to save the feasible solution into proper file

Constraint Programming Part 2 Page 40


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

27 Setting CP parameters

The IBM ILOG Script syntax to change a CP parameter is:


1 cp . param . paramName = " paramvalue "

For example:
1 cp . param . D e f a u l t I n f e r e n c e L e v e l = "Low"
2

3 // Another example
4

5 var p = cp . param ;
6 p . logPeriod = 10000;
7 p . searchType = " D e p t h F i r s t " ;
8 p . timeLimit = 600;

Constraint Programming Part 2 Page 41


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

Table 2: IBM ILOG Script CP parameters

Parameter Possible Values Default Value


AllDiffInferenceLevel Default, Low, Basic Default
Medium, Extended
AllMinDistanceInferenceLevel Default, Low, Basic Default
Medium, Extended
BranchLimit 2100000000
ChoicePointLimit 2100000000
ConstraintAggregation On/Off On
CountInferenceLevel Default, Low, Basic Default
Medium, Extended
CumulFunctionInferenceLevel Low, Basic, Medium Basic
Medium, Extended
DefaultInferenceLevel Low, Basic Basic
Medium, Extended
ElementInferenceLevel Default, Low, Basic Default
Medium, Extended
FailLimit 2100000000
IntervalSequenceInferenceLevel Low, Basic Basic
Medium, Extended
LogPeriod 1000
LogVerbosity Quiet, Terse Normal
Normal, Verbose
MultiPointNumberOfSearchPoints 30
NoOverlapInferenceLevel Low, Basic Basic
Medium, Extended
OptimalityTolerance 1e-15
PrecedenceInferenceLevel Low, Basic Basic
Medium, Extended
PropagationLog Quiet, Terse Quiet
Normal, Verbose
RandomSeed 0
RelativeOptimalityTolerance 0
RestartFailLimit 100
RestartGrowthFactor 1.05
SearchType DepthFirst, Restart Auto
MultiPoint, Auto
SolutionLimit 2100000000
StateFunctionInferenceLevel Low, Basic Basic
Medium, Extended
TimeLimit Infinity
(number in seconds)
Workers Auto (as many workers
as there are CPUs available)

Constraint Programming Part 2 Page 42


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

28 Customize the search phase

28.1 Introduction

A search phase defines instantiation strategies to help the embedded CP Optimizer search
algorithm.
Most common use

• Define decision variables, or the most important ones

• Define priority-ordered groups of decision variables

Less common use

• Define decision variables and how to instantiate those variables

• For this, there are various predefined evaluators

For instance
The variables x will be instantiated before variables y; then, once x and y are instantiated,
variables z will be instantiated. In some particularly well-designed models, passing such an
order can have a dramatic impact on the solving time.

1 execute {
2 var f = cp . f a c t o r y ;
3 cp . s e t S e a r c h P h a s e s ( f . s e a r c h P h a s e ( x ) , f . s e a r c h P h a s e ( y ) , f . s e a r c h P h a s e ( z ) ) ;
4 }

Customize the search phase include 2 steps

• Define how the engine initializes each phase

• Define the order of search phase

Step 1: in general form


1 var phase1 = s e a r c h P h a s e ( variable_array_from_model , v a r i a b l e _ s e l e c t o r ,
value_selector ) ;

Step 2:
1 cp . s e t S e a r c h P h a s e s ( phase1 , phase2 ) ;

Constraint Programming Part 2 Page 43


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

28.2 Type of mono-criterion search phase:

A mono-criterion search phase can also contain a variable chooser and a value chooser and
belongs to one of 3 types
Type 1:
an array of variables to instantiate (or fix)
1 var phase1 = f . s e a r c h P h a s e ( x ) ;

Type 2:

• a variable chooser that defines how the next variable to instantiate is chosen, and

• a value chooser that defines how values are chosen when instantiating variables.

1 var phase1 = f . s e a r c h P h a s e ( f . s e l e c t L a r g e s t ( f . v a r I n d e x ( x ) ) ,
2 f . s e l e c t L a r g e s t ( f . e x p l i c i t V a l u e E v a l ( v a l u e s , varEval , 0 ) ) ) ;

Type 3:

• an array of decision variables to instantiate (or fix),

• variable chooser that defines how the next variable to instantiate is chosen, and

• a value chooser that defines how values are chosen when instantiating variables.

1 var phase1 = f . s e a r c h P h a s e ( x ,
2 f . s e l e c t L a r g e s t ( f . varIndex ( x ) ) ,
3 f . s e l e c t L a r g e s t ( f . e x p l i c i t V a l u e E v a l ( v a l u e s , varEval , 0 ) ) ) ;

28.3 Explanation

Variable evaluators
To chose decision variables, the variables are evaluated by an evaluator.Several predefined
evaluators exist in CP Optimizer.
For instance
cp.factory.domainSize() returns the current size of the domain of the variable that is
being evaluated
cp.factory.varIndex(dvar int[]) that returns the index in the array vars of the evaluated
variable.
List of variable evaluators

• cp.factory.domainSize()

• cp.factory.domainMin()

Constraint Programming Part 2 Page 44


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

• cp.factory.domainMax()

• cp.factory.successRate()

• cp.factory.impact()

• cp.factory.localImpact()

• cp.factory.impactOfLastBranch()

• cp.factory.explicitVarEval(dvar int[], int[])

• cp.factory.varIndex(dvar int[])

Variable selectors
In order to select a decision variable with an evaluator, an instance of a variable selector
needs to be created with the evaluator as an input. Here are two functions in the C++
API that can create such selectors:
List of variable choosers

• selectSmallest(eval): will select the variable in the search phase that has the smallest
domain

• selectLargest(eval): will select the variable in the search phase that has the smallest
domain

• selectRandomVar(): chooses a variable randomly. It is useful for breaking ties

Search phases with selectors


A selector can be used with a search phase to indicate which variable should be instantiated
to which value.
A search phase that instantiates the decision variables in the array x by choosing the variable
having the smallest domain size and by assigning it to the largest value of its domain can
be defined like this
1 var phase1 = f . s e a r c h P h a s e ( x ,
2 f . s e l e c t S m a l l e s t ( f . domainSize ( ) ) ,
3 f . s e l e c t L a r g e s t ( f . e x p l i c i t V a l u e E v a l ( values , valueEval , 0) ) ) ;

A search phase or an array of search phases defined with selectors can be given to an in-
stance of the optimizer by invoking the method setSearchPhases before calling solve or
startNewSearch:
1 cp . s e t S e a r c h P h a s e s ( phase1 ) ;
2 cp . s o l v e ( ) ;

List of value evaluators

Constraint Programming Part 2 Page 45


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

• cp.factory.value()

• cp.factory.valueImpact()

• cp.factory.valueSuccessRate()

• cp.factory.explicitValueEval(int[], int[], double)

• cp.factory.valueIndex(int[])

Example
1 var f = cp . f a c t o r y ;
2 var s e l e c t V a r = f . s e l e c t S m a l l e s t ( f . domainSize ( ) ) ;
3 var s e l e c t V a l u e = f . selectRandomValue ( ) ;
4 var assignRoom = f . s e a r c h P h a s e ( room , s e l e c t V a r , s e l e c t V a l u e ) ;
5 var a s s i g n T e a c h e r = f . s e a r c h P h a s e ( t e a c h e r , s e l e c t V a r , s e l e c t V a l u e ) ;
6 var a s s i g n S t a r t = f . s e a r c h P h a s e ( S t a r t , s e l e c t V a r , s e l e c t V a l u e ) ;
7 cp . s e t S e a r c h P h a s e s ( a s s i g n T e a c h e r , a s s i g n S t a r t , assignRoom ) ;

28.4 Search phases for scheduling

Search phases can be used on interval variables as well as on sequence variables.


Two types of search phases are available for scheduling: search phases on interval variables
and search phases on sequence variables.
Search phase on interval variables
A search phase on interval variables works on a unique interval variable or on an array of
interval variables. During this phase CP Optimizer fixes the value of the specified interval
variable(s): each interval variable will be assigned a presence status and for each present in-
terval, a start and an end value. This search phase fixes the start and end values of interval
variables in an unidirectional manner, starting to fix first the intervals that will be assigned
a small start or end value.
Search phase on sequence variables
A search phase on sequence variables works on a unique sequence variable or on an array of
sequence variables. During this phase CP Optimizer fixes the value of the specified sequence
variable(s): each sequence variable will be assigned a totally ordered sequence of present
interval variables. Note that this search phase also fixes the presence statuses of the inter-
vals involved in the sequence variables. This phase does not fix the start and end values of
interval variables. It is recommended to use this search phase only if the possible range for
start and end values of all interval variables is limited (for example by some known horizon
that limits their maximal values).

Constraint Programming Part 2 Page 46


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

28.5 Change Search behavior through setting

Setting limits
1 execute {
2 cp . param . F a i l L i m i t = 5 0 0 ;
3 cp . param . TimeLimit = 2 0 ;
4 }

Changing the search behavior


1 execute {
2 cp . param . searchType = " m u l t i P o i n t " ;
3 w r i t e l n ( " S e a r c h type i s " + cp . param . searchType ) ;
4 cp . param . D e f a u l t I n f e r e n c e L e v e l = " Extended " ;
5 }

1 var p = cp . param ;
2 p . logPeriod = 10000;
3 p . searchType = " D e p t h F i r s t " ;
4 p . timeLimit = 600;

Constraint Programming Part 2 Page 47


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

29 Multiple objective function


1 // O b j e c t i v e : f i r s t c r i t e r i o n f o r m i n i m i z i n g t h e c o s t f o r c o n f i g u r i n g and
loading trucks
2 // s e c o n d c r i t e r i o n f o r m i n i m i z i n g t h e number o f t r u c k s
3 dexpr i n t e1 = sum ( t i n Trucks ) ( t r u c k C o s t [ t r u c k C o n f i g s [ t ] ] ∗ ( l o a d [ t ] ! = 0 ) )
4 + sum ( t i n Trucks ) t r a n s i t i o n C o s t [ t ] ;
5 dexpr i n t e2 = numUsed ;
6

7 minimize s t a t i c L e x ( e1 , e2 ) ;
8 // t r y i n g t o minimize c o s t f i r s t

1 staticLexFull ( goals [ ] ,
2 weights [ ] ,
3 priorities [] ,
4 abstol [ ] ,
5 reltol []) ;

goals: is an array of variable expressions.


weights: is an array of integers that are used to weight each goal at each step of the lexi-
cographic problem.
priorities: is an array of integers which define the order of multiple resolutions or which
are used to mix certain goals.
abstol: is an array of floating point numbers defining absolute tolerances.
reltol: is an array of floating point numbers defining relative tolerances.
Description: Function staticLexFull defines a multi-criteria policy.

1 u s i n g CP;
2 i n t n=6;
3 i n t H a l f=n d i v 2 ;
4 range F i r s t H a l f = 1 . . Half ;
5 r a n g e L a s t H a l f = n−H a l f + 1 . . n ;
6 range S t a t e s = 0 . . 1 ;
7 r a n g e Bord = 0 . . ( n+1) ;
8 range I n t e r i o r = 1 . . n ;
9

10 r a n g e o b j = 0 . . ( n∗n ) ;
11

12 tuple neighbors {
13 i n t row ;
14 int col ;
15 }
16

17 { n e i g h b o r s } Neighbor =
18 {<(−1) ,( −1) >,<(−1) ,0>,<(−1) ,1 > , <0 ,( −1) >,<0,1>,<1,(−1) >,<1,0>,<1,1>};
19

Constraint Programming Part 2 Page 48


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

20 dvar i n t L i f e [ Bord ] [ Bord ] i n S t a t e s ;


21 r a n g e Bord1 = 0 . . 3 ;
22 dexpr f l o a t k p i s [ i i n Bord1 ] = sum ( i 1 i n Bord , j 1 i n Bord : i 1 != i ) L i f e [ i 1 ] [
j1 ] ;
23 dvar i n t Obj i n o b j ;
24

25

26 maximize s t a t i c L e x ( k p i s ) ;
27

28 s u b j e c t to {
29 Obj == sum ( i , j i n Bord ) L i f e [ i ] [ j ] ;
30

31 f o r a l l ( i , j in I n t e r i o r ) {
32 2∗ L i f e [ i ] [ j ] − sum ( nb i n Neighbor ) L i f e [ i+nb . row ] [ j+nb . c o l ] <= 0 ;
33 3∗ L i f e [ i ] [ j ] + sum ( nb i n Neighbor ) L i f e [ i+nb . row ] [ j+nb . c o l ] <= 6 ;
34 f o r a l l ( o r d e r e d n1 , n2 , n3 i n Neighbor ) {
35 −L i f e [ i ] [ j ]+ L i f e [ i+n1 . row ] [ j+n1 . c o l ]+ L i f e [ i+n2 . row ] [ j+n2 . c o l ]+ L i f e [ i+
n3 . row ] [ j+n3 . c o l ]−sum ( nb i n Neighbor : nb!=n1 && nb!=n2 && nb!=n3 ) Life
[ i+nb . row ] [ j+nb . c o l ] <= 2 ;
36 }
37 }
38 f o r a l l ( j i n Bord ) {
39 L i f e [ 0 ] [ j ] == 0 ;
40 L i f e [ j ] [ 0 ] == 0 ;
41 L i f e [ j ] [ n+1] == 0 ;
42 L i f e [ n + 1 ] [ j ] == 0 ;
43 }
44 f o r a l l ( i i n Bord : i <n ) {
45 L i f e [ i ] [ 1 ] + L i f e [ i + 1 ] [ 1 ] + L i f e [ i + 2 ] [ 1 ] <= 2 ;
46 L i f e [ 1 ] [ i ]+ L i f e [ 1 ] [ i +1]+ L i f e [ 1 ] [ i +2] <= 2 ;
47 L i f e [ i ] [ n]+ L i f e [ i + 1 ] [ n]+ L i f e [ i + 2 ] [ n ] <= 2 ;
48 L i f e [ n ] [ i ]+ L i f e [ n ] [ i +1]+ L i f e [ n ] [ i +2] <= 2 ;
49 }
50 sum ( i i n F i r s t H a l f , j i n Bord ) L i f e [ i ] [ j ] >=
51 sum ( i i n L a s t H a l f , j i n Bord ) L i f e [ i ] [ j ] ;
52 sum ( i i n Bord , j i n F i r s t H a l f ) L i f e [ i ] [ j ] >=
53 sum ( i i n Bord , j i n L a s t H a l f ) L i f e [ i ] [ j ] ;
54 }
55

56

57 execute {
58 f o r ( var j i n Bord1 ) w r i t e l n ( k p i s [ j ] ) ;
59 }

1 u s i n g CP;
2 i n t n=9;
3 i n t H a l f=n d i v 2 ;
4 range F i r s t H a l f = 1 . . Half ;

Constraint Programming Part 2 Page 49


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

5 r a n g e L a s t H a l f = n−H a l f + 1 . . n ;
6 range S t a t e s = 0 . . 1 ;
7 r a n g e Bord = 0 . . ( n+1) ;
8 range I n t e r i o r = 1 . . n ;
9

10 r a n g e o b j = 0 . . ( n∗n ) ;
11

12 tuple neighbors {
13 i n t row ;
14 int col ;
15 }
16

17 r a n g e Bord1 = 0 . . 3 ;
18 i n t t i m e s [ i i n Bord1 ] = 5+ i ;
19 f l o a t gaps [ i i n Bord1 ] = 0 . 0 1 ;
20

21 execute {
22 c p l e x . lex_TimeLimits = t i m e s ;
23 c p l e x . lex_GapLimits = gaps ;
24 }
25

26 { n e i g h b o r s } Neighbor =
27 {<(−1) ,( −1) >,<(−1) ,0>,<(−1) ,1 > , <0 ,( −1) >,<0,1>,<1,(−1) >,<1,0>,<1,1>};
28

29 dvar i n t L i f e [ Bord ] [ Bord ] i n S t a t e s ;


30

31 dexpr f l o a t k p i s [ i i n Bord1 ] = sum ( i 1 i n Bord , j 1 i n Bord : i 1 != i ) L i f e [ i 1 ] [


j1 ] ;
32 dvar i n t Obj i n o b j ;
33

34 f l o a t w e i g t h s [ i i n Bord1 ] = i +1;
35 f l o a t a b s t o l [ i i n Bord1 ] = 0 . 0 0 0 1 ;
36 f l o a t r e l t o l [ i i n Bord1 ] = 0 . 0 0 0 1 ;
37 i n t p r i o r i t i e s [ i i n Bord1 ] = i ;
38 maximize s t a t i c L e x F u l l ( k p i s , w e i g t h s , p r i o r i t i e s , a b s t o l , r e l t o l ) ;
39

40 s u b j e c t to {
41 Obj == sum ( i , j i n Bord ) L i f e [ i ] [ j ] ;
42

43 f o r a l l ( i , j in I n t e r i o r ) {
44 2∗ L i f e [ i ] [ j ] − sum ( nb i n Neighbor ) L i f e [ i+nb . row ] [ j+nb . c o l ] <= 0 ;
45 3∗ L i f e [ i ] [ j ] + sum ( nb i n Neighbor ) L i f e [ i+nb . row ] [ j+nb . c o l ] <= 6 ;
46 f o r a l l ( o r d e r e d n1 , n2 , n3 i n Neighbor ) {
47 −L i f e [ i ] [ j ]+ L i f e [ i+n1 . row ] [ j+n1 . c o l ]+ L i f e [ i+n2 . row ] [ j+n2 . c o l ]+ L i f e [ i+
n3 . row ] [ j+n3 . c o l ]−sum ( nb i n Neighbor : nb!=n1 && nb!=n2 && nb!=n3 ) Life
[ i+nb . row ] [ j+nb . c o l ] <= 2 ;
48 }
49 }

Constraint Programming Part 2 Page 50


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

50 f o r a l l ( j i n Bord ) {
51 L i f e [ 0 ] [ j ] == 0 ;
52 L i f e [ j ] [ 0 ] == 0 ;
53 L i f e [ j ] [ n+1] == 0 ;
54 L i f e [ n + 1 ] [ j ] == 0 ;
55 }
56 f o r a l l ( i i n Bord : i <n ) {
57 L i f e [ i ] [ 1 ] + L i f e [ i + 1 ] [ 1 ] + L i f e [ i + 2 ] [ 1 ] <= 2 ;
58 L i f e [ 1 ] [ i ]+ L i f e [ 1 ] [ i +1]+ L i f e [ 1 ] [ i +2] <= 2 ;
59 L i f e [ i ] [ n]+ L i f e [ i + 1 ] [ n]+ L i f e [ i + 2 ] [ n ] <= 2 ;
60 L i f e [ n ] [ i ]+ L i f e [ n ] [ i +1]+ L i f e [ n ] [ i +2] <= 2 ;
61 }
62 sum ( i i n F i r s t H a l f , j i n Bord ) L i f e [ i ] [ j ] >=
63 sum ( i i n L a s t H a l f , j i n Bord ) L i f e [ i ] [ j ] ;
64 sum ( i i n Bord , j i n F i r s t H a l f ) L i f e [ i ] [ j ] >=
65 sum ( i i n Bord , j i n L a s t H a l f ) L i f e [ i ] [ j ] ;
66 }
67

68

69 e x e c u t e SCRIPT {
70 var m u l t i = c p l e x . g e t M u l t i O b j N s o l v e s ( ) ;
71 writeln ( " cplex . getMultiObjNsolves " + multi ) ;
72 f o r ( var i = 0 ; i < m u l t i ; i ++) {
73 w r i t e l n ( i + " MultiObjTime " + c p l e x . g e t M u l t i O b j I n f o ( " MultiObjTime " , i ) ) ;
74 w r i t e l n ( i + " MultiObjObjValue " + c p l e x . g e t M u l t i O b j I n f o ( " MultiObjObjValue "
, i));
75 w r i t e l n ( i + " MultiObjBestObjValue " + c p l e x . g e t M u l t i O b j I n f o ( "
MultiObjBestObjValue " , i ) ) ;
76 writeln ( i + " MultiObjPriority " + cplex . getMultiObjInfo ( " MultiObjPriority "
, i));
77 w r i t e l n ( " \n" ) ;
78 }
79 }
80

81

82 execute {
83 f o r ( var j i n Bord1 ) w r i t e l n ( k p i s [ j ] ) ;
84 }

Constraint Programming Part 2 Page 51

You might also like