2018-02-19 15:21:05 +01:00
|
|
|
# Copyright 2010 Hakan Kjellerstrand hakank@gmail.com
|
2010-10-14 06:16:18 +00:00
|
|
|
#
|
2012-01-21 17:15:03 +00:00
|
|
|
# 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
|
2010-10-14 06:16:18 +00:00
|
|
|
#
|
2012-01-21 17:15:03 +00:00
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
2010-10-14 06:16:18 +00:00
|
|
|
#
|
2012-01-21 17:15:03 +00:00
|
|
|
# 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.
|
2010-10-14 06:16:18 +00:00
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
Car sequencing in Google CP Solver.
|
|
|
|
|
|
|
|
|
|
This model is based on the car sequencing model in
|
|
|
|
|
Pascal Van Hentenryck
|
|
|
|
|
'The OPL Optimization Programming Language', page 184ff.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Compare with the following models:
|
|
|
|
|
* MiniZinc: http://hakank.org/minizinc/car.mzn
|
|
|
|
|
* Comet: http://hakank.org/comet/car.co
|
|
|
|
|
|
2018-02-19 15:21:05 +01:00
|
|
|
This model was created by Hakan Kjellerstrand (hakank@gmail.com)
|
2014-05-22 20:13:16 +00:00
|
|
|
Also see my other Google CP Solver models:
|
|
|
|
|
http://www.hakank.org/google_or_tools/
|
2010-10-14 06:16:18 +00:00
|
|
|
"""
|
2016-01-14 16:49:37 +01:00
|
|
|
from __future__ import print_function
|
2010-10-14 06:16:18 +00:00
|
|
|
import sys
|
|
|
|
|
|
2013-12-24 11:35:01 +00:00
|
|
|
from ortools.constraint_solver import pywrapcp
|
2010-10-14 06:16:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def main(num_sol=3):
|
2012-01-21 17:15:03 +00:00
|
|
|
|
2014-05-22 20:13:16 +00:00
|
|
|
# Create the solver.
|
|
|
|
|
solver = pywrapcp.Solver("Car sequence")
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# data
|
|
|
|
|
#
|
|
|
|
|
nbCars = 6
|
|
|
|
|
nbOptions = 5
|
|
|
|
|
nbSlots = 10
|
|
|
|
|
|
2016-01-14 16:49:37 +01:00
|
|
|
Cars = list(range(nbCars))
|
|
|
|
|
Options = list(range(nbOptions))
|
|
|
|
|
Slots = list(range(nbSlots))
|
2014-05-22 20:13:16 +00:00
|
|
|
|
|
|
|
|
# car 0 1 2 3 4 5
|
|
|
|
|
demand = [1, 1, 2, 2, 2, 2]
|
|
|
|
|
|
|
|
|
|
option = [
|
|
|
|
|
# car 0 1 2 3 4 5
|
|
|
|
|
[1, 0, 0, 0, 1, 1], # option 1
|
|
|
|
|
[0, 0, 1, 1, 0, 1], # option 2
|
|
|
|
|
[1, 0, 0, 0, 1, 0], # option 3
|
|
|
|
|
[1, 1, 0, 1, 0, 0], # option 4
|
|
|
|
|
[0, 0, 1, 0, 0, 0] # option 5
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
capacity = [
|
|
|
|
|
(1, 2),
|
|
|
|
|
(2, 3),
|
|
|
|
|
(1, 3),
|
|
|
|
|
(2, 5),
|
|
|
|
|
(1, 5)
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
optionDemand = [sum([demand[j] * option[i][j] for j in Cars])
|
|
|
|
|
for i in Options]
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# declare variables
|
|
|
|
|
#
|
|
|
|
|
slot = [solver.IntVar(0, nbCars - 1, "slot[%i]" % i) for i in Slots]
|
|
|
|
|
setup = {}
|
|
|
|
|
for i in Options:
|
|
|
|
|
for j in Slots:
|
|
|
|
|
setup[(i, j)] = solver.IntVar(0, 1, "setup[%i,%i]" % (i, j))
|
|
|
|
|
setup_flat = [setup[i, j] for i in Options for j in Slots]
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# constraints
|
|
|
|
|
#
|
|
|
|
|
for c in Cars:
|
|
|
|
|
b = [solver.IsEqualCstVar(slot[s], c) for s in Slots]
|
|
|
|
|
solver.Add(solver.Sum(b) == demand[c])
|
|
|
|
|
|
|
|
|
|
for o in Options:
|
|
|
|
|
for s in range(0, nbSlots - capacity[o][1] + 1):
|
|
|
|
|
b = [setup[o, j] for j in range(s, s + capacity[o][1] - 1)]
|
|
|
|
|
solver.Add(solver.Sum(b) <= capacity[o][0])
|
|
|
|
|
|
|
|
|
|
for o in Options:
|
|
|
|
|
for s in Slots:
|
|
|
|
|
solver.Add(setup[(o, s)] == solver.Element(option[o], slot[s]))
|
|
|
|
|
|
|
|
|
|
for o in Options:
|
|
|
|
|
for i in range(optionDemand[o]):
|
2016-01-14 16:49:37 +01:00
|
|
|
s_range = list(range(0, nbSlots - (i + 1) * capacity[o][1]))
|
2014-05-22 20:13:16 +00:00
|
|
|
ss = [setup[o, s] for s in s_range]
|
|
|
|
|
cc = optionDemand[o] - (i + 1) * capacity[o][0]
|
|
|
|
|
if len(ss) > 0 and cc >= 0:
|
|
|
|
|
solver.Add(solver.Sum(ss) >= cc)
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# search and result
|
|
|
|
|
#
|
|
|
|
|
db = solver.Phase(slot + setup_flat,
|
|
|
|
|
solver.CHOOSE_FIRST_UNBOUND,
|
|
|
|
|
solver.ASSIGN_MIN_VALUE)
|
|
|
|
|
|
|
|
|
|
solver.NewSearch(db)
|
|
|
|
|
num_solutions = 0
|
|
|
|
|
while solver.NextSolution():
|
2016-01-14 16:49:37 +01:00
|
|
|
print("slot:%s" % ",".join([str(slot[i].Value()) for i in Slots]))
|
|
|
|
|
print("setup:")
|
2010-10-14 06:16:18 +00:00
|
|
|
for o in Options:
|
2016-01-14 16:49:37 +01:00
|
|
|
print("%i/%i:" % (capacity[o][0], capacity[o][1]), end=' ')
|
2014-05-22 20:13:16 +00:00
|
|
|
for s in Slots:
|
2016-01-14 16:49:37 +01:00
|
|
|
print(setup[o, s].Value(), end=' ')
|
|
|
|
|
print()
|
|
|
|
|
print()
|
2014-05-22 20:13:16 +00:00
|
|
|
num_solutions += 1
|
2010-10-14 06:16:18 +00:00
|
|
|
|
2014-05-22 20:13:16 +00:00
|
|
|
if num_solutions >= num_sol:
|
|
|
|
|
break
|
2010-10-14 06:16:18 +00:00
|
|
|
|
2014-05-22 20:13:16 +00:00
|
|
|
solver.EndSearch()
|
2012-01-21 17:15:03 +00:00
|
|
|
|
2016-01-14 16:49:37 +01:00
|
|
|
print()
|
|
|
|
|
print("num_solutions:", num_solutions)
|
|
|
|
|
print("failures:", solver.Failures())
|
|
|
|
|
print("branches:", solver.Branches())
|
|
|
|
|
print("WallTime:", solver.WallTime())
|
2010-10-14 06:16:18 +00:00
|
|
|
|
|
|
|
|
num_sol = 3
|
2014-05-22 20:13:16 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
|
if len(sys.argv) > 1:
|
2015-12-15 23:59:46 -08:00
|
|
|
num_sol = int(sys.argv[1])
|
2014-05-22 20:13:16 +00:00
|
|
|
main(num_sol)
|