forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathjobshop_ft06_distance_sat.py
executable file
·147 lines (119 loc) · 4.65 KB
/
jobshop_ft06_distance_sat.py
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python3
# Copyright 2010-2024 Google LLC
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""This model implements a variation of the ft06 jobshop.
A jobshop is a standard scheduling problem when you must sequence a
series of tasks on a set of machines. Each job contains one task per
machine. The order of execution and the length of each job on each
machine is task dependent.
The objective is to minimize the maximum completion time of all
jobs. This is called the makespan.
This variation introduces a minimum distance between all the jobs on each
machine.
"""
import collections
from ortools.sat.python import cp_model
def distance_between_jobs(x: int, y: int) -> int:
"""Returns the distance between tasks of job x and tasks of job y."""
return abs(x - y)
def jobshop_ft06_distance() -> None:
"""Solves the ft06 jobshop with distances between tasks."""
# Creates the model.
model = cp_model.CpModel()
machines_count = 6
jobs_count = 6
all_machines = range(0, machines_count)
all_jobs = range(0, jobs_count)
durations = [
[1, 3, 6, 7, 3, 6],
[8, 5, 10, 10, 10, 4],
[5, 4, 8, 9, 1, 7],
[5, 5, 5, 3, 8, 9],
[9, 3, 5, 4, 3, 1],
[3, 3, 9, 10, 4, 1],
]
machines = [
[2, 0, 1, 3, 5, 4],
[1, 2, 4, 5, 0, 3],
[2, 3, 5, 0, 1, 4],
[1, 0, 2, 3, 4, 5],
[2, 1, 4, 5, 0, 3],
[1, 3, 5, 0, 4, 2],
]
# Computes horizon statically.
horizon = 150
task_type = collections.namedtuple("task_type", "start end interval")
# Creates jobs.
all_tasks = {}
for i in all_jobs:
for j in all_machines:
start_var = model.new_int_var(0, horizon, f"start_{i}_{j}")
duration = durations[i][j]
end_var = model.new_int_var(0, horizon, f"end_{i}_{j}")
interval_var = model.new_interval_var(
start_var, duration, end_var, f"interval_{i}_{j}"
)
all_tasks[(i, j)] = task_type(
start=start_var, end=end_var, interval=interval_var
)
# Create disjuctive constraints.
for i in all_machines:
job_intervals = []
job_indices = []
job_starts = []
job_ends = []
for j in all_jobs:
for k in all_machines:
if machines[j][k] == i:
job_intervals.append(all_tasks[(j, k)].interval)
job_indices.append(j)
job_starts.append(all_tasks[(j, k)].start)
job_ends.append(all_tasks[(j, k)].end)
model.add_no_overlap(job_intervals)
arcs = []
for j1 in range(len(job_intervals)):
# Initial arc from the dummy node (0) to a task.
start_lit = model.new_bool_var(f"{j1} is first job")
arcs.append((0, j1 + 1, start_lit))
# Final arc from an arc to the dummy node.
arcs.append((j1 + 1, 0, model.new_bool_var(f"{j1} is last job")))
for j2 in range(len(job_intervals)):
if j1 == j2:
continue
lit = model.new_bool_var(f"{j2} follows {j1}")
arcs.append((j1 + 1, j2 + 1, lit))
# We add the reified precedence to link the literal with the
# times of the two tasks.
min_distance = distance_between_jobs(j1, j2)
model.add(
job_starts[j2] >= job_ends[j1] + min_distance
).only_enforce_if(lit)
model.add_circuit(arcs)
# Precedences inside a job.
for i in all_jobs:
for j in range(0, machines_count - 1):
model.add(all_tasks[(i, j + 1)].start >= all_tasks[(i, j)].end)
# Makespan objective.
obj_var = model.new_int_var(0, horizon, "makespan")
model.add_max_equality(
obj_var, [all_tasks[(i, machines_count - 1)].end for i in all_jobs]
)
model.minimize(obj_var)
# Solve model.
solver = cp_model.CpSolver()
status = solver.solve(model)
# Output solution.
if status == cp_model.OPTIMAL:
print(f"Optimal makespan: {solver.objective_value}")
print(solver.response_stats())
jobshop_ft06_distance()