2017-10-17 11:42:24 +02:00
|
|
|
# Copyright 2010-2017 Google
|
|
|
|
|
# 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.
|
2017-10-17 11:58:29 +02:00
|
|
|
"""Gate Scheduling problem.
|
|
|
|
|
|
|
|
|
|
We have a set of jobs to perform (duration, width).
|
|
|
|
|
We have two parallel machines that can perform this job.
|
|
|
|
|
One machine can only perform one job at a time.
|
|
|
|
|
At any point in time, the sum of the width of the two active jobs does not
|
2017-11-21 03:54:06 +01:00
|
|
|
exceed a max_length.
|
2017-10-17 11:58:29 +02:00
|
|
|
|
|
|
|
|
The objective is to minimize the max end time of all jobs.
|
|
|
|
|
"""
|
|
|
|
|
|
2017-10-17 11:42:24 +02:00
|
|
|
from ortools.sat.python import cp_model
|
2017-11-24 13:49:39 +01:00
|
|
|
from ortools.sat.python import visualization
|
2017-10-17 11:42:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2018-09-06 22:57:59 +02:00
|
|
|
"""Solves the gate scheduling problem."""
|
2017-10-17 11:42:24 +02:00
|
|
|
model = cp_model.CpModel()
|
|
|
|
|
|
2018-06-11 11:51:18 +02:00
|
|
|
jobs = [[3, 3], [2, 5], [1, 3], [3, 7], [7, 3], [2, 2], [2, 2], [5, 5],
|
|
|
|
|
[10, 2], [4, 3], [2, 6], [1, 2], [6, 8], [4, 5], [3, 7]]
|
2017-10-17 11:42:24 +02:00
|
|
|
|
|
|
|
|
max_length = 10
|
|
|
|
|
|
|
|
|
|
horizon = sum(t[0] for t in jobs)
|
|
|
|
|
num_jobs = len(jobs)
|
|
|
|
|
all_jobs = range(num_jobs)
|
|
|
|
|
|
|
|
|
|
intervals = []
|
|
|
|
|
intervals0 = []
|
|
|
|
|
intervals1 = []
|
|
|
|
|
performed = []
|
|
|
|
|
starts = []
|
|
|
|
|
ends = []
|
|
|
|
|
demands = []
|
|
|
|
|
|
|
|
|
|
for i in all_jobs:
|
2017-10-18 11:09:13 +02:00
|
|
|
# Create main interval.
|
2017-10-17 11:42:24 +02:00
|
|
|
start = model.NewIntVar(0, horizon, 'start_%i' % i)
|
|
|
|
|
duration = jobs[i][0]
|
|
|
|
|
end = model.NewIntVar(0, horizon, 'end_%i' % i)
|
|
|
|
|
interval = model.NewIntervalVar(start, duration, end, 'interval_%i' % i)
|
|
|
|
|
starts.append(start)
|
|
|
|
|
intervals.append(interval)
|
|
|
|
|
ends.append(end)
|
|
|
|
|
demands.append(jobs[i][1])
|
|
|
|
|
|
|
|
|
|
performed_on_m0 = model.NewBoolVar('perform_%i_on_m0' % i)
|
|
|
|
|
performed.append(performed_on_m0)
|
2017-10-18 11:09:13 +02:00
|
|
|
|
|
|
|
|
# Create an optional copy of interval to be executed on machine 0.
|
2018-08-31 16:56:55 +02:00
|
|
|
start0 = model.NewIntVar(0, horizon, 'start_%i_on_m0' % i)
|
2018-09-06 22:57:59 +02:00
|
|
|
end0 = model.NewIntVar(0, horizon, 'end_%i_on_m0' % i)
|
2017-10-17 11:42:24 +02:00
|
|
|
interval0 = model.NewOptionalIntervalVar(
|
|
|
|
|
start0, duration, end0, performed_on_m0, 'interval_%i_on_m0' % i)
|
|
|
|
|
intervals0.append(interval0)
|
|
|
|
|
|
2017-10-18 11:09:13 +02:00
|
|
|
# Create an optional copy of interval to be executed on machine 1.
|
2018-08-31 16:56:55 +02:00
|
|
|
start1 = model.NewIntVar(0, horizon, 'start_%i_on_m1' % i)
|
|
|
|
|
end1 = model.NewIntVar(0, horizon, 'end_%i_on_m1' % i)
|
2018-06-11 11:51:18 +02:00
|
|
|
interval1 = model.NewOptionalIntervalVar(start1, duration, end1,
|
|
|
|
|
performed_on_m0.Not(),
|
|
|
|
|
'interval_%i_on_m1' % i)
|
2017-10-17 11:42:24 +02:00
|
|
|
intervals1.append(interval1)
|
|
|
|
|
|
|
|
|
|
# We only propagate the constraint if the tasks is performed on the machine.
|
|
|
|
|
model.Add(start0 == start).OnlyEnforceIf(performed_on_m0)
|
|
|
|
|
model.Add(start1 == start).OnlyEnforceIf(performed_on_m0.Not())
|
|
|
|
|
|
|
|
|
|
# Max Length constraint (modeled as a cumulative)
|
|
|
|
|
model.AddCumulative(intervals, demands, max_length)
|
|
|
|
|
|
|
|
|
|
# Choose which machine to perform the jobs on.
|
|
|
|
|
model.AddNoOverlap(intervals0)
|
|
|
|
|
model.AddNoOverlap(intervals1)
|
|
|
|
|
|
|
|
|
|
# Objective variable.
|
|
|
|
|
makespan = model.NewIntVar(0, horizon, 'makespan')
|
|
|
|
|
model.AddMaxEquality(makespan, ends)
|
|
|
|
|
model.Minimize(makespan)
|
|
|
|
|
|
2017-10-17 13:08:10 +02:00
|
|
|
# Symmetry breaking.
|
|
|
|
|
model.Add(performed[0] == 0)
|
|
|
|
|
|
2017-10-17 11:42:24 +02:00
|
|
|
# Solve model.
|
|
|
|
|
solver = cp_model.CpSolver()
|
|
|
|
|
solver.Solve(model)
|
2017-11-21 03:54:06 +01:00
|
|
|
|
|
|
|
|
# Output solution.
|
2017-11-24 14:47:10 +01:00
|
|
|
if visualization.RunFromIPython():
|
2017-11-24 13:49:39 +01:00
|
|
|
output = visualization.SvgWrapper(solver.ObjectiveValue(), max_length, 40.0)
|
|
|
|
|
output.AddTitle('Makespan = %i' % solver.ObjectiveValue())
|
|
|
|
|
color_manager = visualization.ColorManager()
|
|
|
|
|
color_manager.SeedRandomColor(0)
|
|
|
|
|
|
|
|
|
|
for i in all_jobs:
|
|
|
|
|
performed_machine = 1 - solver.Value(performed[i])
|
|
|
|
|
start = solver.Value(starts[i])
|
2018-09-06 22:57:59 +02:00
|
|
|
d_x = jobs[i][0]
|
|
|
|
|
d_y = jobs[i][1]
|
|
|
|
|
s_y = performed_machine * (max_length - d_y)
|
|
|
|
|
output.AddRectangle(start, s_y, d_x, d_y, color_manager.RandomColor(),
|
2017-11-24 13:49:39 +01:00
|
|
|
'black', 'j%i' % i)
|
|
|
|
|
|
|
|
|
|
output.AddXScale()
|
|
|
|
|
output.AddYScale()
|
|
|
|
|
output.Display()
|
|
|
|
|
else:
|
|
|
|
|
print('Solution')
|
|
|
|
|
print(' - makespan = %i' % solver.ObjectiveValue())
|
|
|
|
|
for i in all_jobs:
|
|
|
|
|
performed_machine = 1 - solver.Value(performed[i])
|
|
|
|
|
start = solver.Value(starts[i])
|
2018-06-11 11:51:18 +02:00
|
|
|
print(' - Job %i starts at %i on machine %i' % (i, start,
|
|
|
|
|
performed_machine))
|
2017-11-24 13:49:39 +01:00
|
|
|
print('Statistics')
|
|
|
|
|
print(' - conflicts : %i' % solver.NumConflicts())
|
|
|
|
|
print(' - branches : %i' % solver.NumBranches())
|
|
|
|
|
print(' - wall time : %f ms' % solver.WallTime())
|
2017-10-17 11:42:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
main()
|