-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
assignment.py
123 lines (101 loc) · 3.34 KB
/
assignment.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
# Copyright 2010 Hakan Kjellerstrand [email protected]
#
# 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.
"""
Assignment problem in Google CP Solver.
Winston 'Operations Research', Assignment Problems, page 393f
(generalized version with added test column)
Compare with the following models:
* Comet : http://www.hakank.org/comet/assignment.co
* ECLiPSE : http://www.hakank.org/eclipse/assignment.ecl
* Gecode : http://www.hakank.org/gecode/assignment.cpp
* MiniZinc: http://www.hakank.org/minizinc/assignment.mzn
* Tailor/Essence': http://www.hakank.org/tailor/assignment.eprime
* SICStus: http://hakank.org/sicstus/assignment.pl
This model was created by Hakan Kjellerstrand ([email protected])
Also see my other Google CP Solver models:
http://www.hakank.org/google_or_tools/
"""
from ortools.constraint_solver import pywrapcp
def main(cost, rows, cols):
# Create the solver.
solver = pywrapcp.Solver("n-queens")
#
# data
#
# declare variables
total_cost = solver.IntVar(0, 100, "total_cost")
x = []
for i in range(rows):
t = []
for j in range(cols):
t.append(solver.IntVar(0, 1, "x[%i,%i]" % (i, j)))
x.append(t)
x_flat = [x[i][j] for i in range(rows) for j in range(cols)]
#
# constraints
#
# total_cost
solver.Add(total_cost == solver.Sum(
[solver.ScalProd(x_row, cost_row) for (x_row, cost_row) in zip(x, cost)]))
# exacly one assignment per row, all rows must be assigned
[
solver.Add(solver.Sum([x[row][j]
for j in range(cols)]) == 1)
for row in range(rows)
]
# zero or one assignments per column
[
solver.Add(solver.Sum([x[i][col]
for i in range(rows)]) <= 1)
for col in range(cols)
]
objective = solver.Minimize(total_cost, 1)
#
# solution and search
#
solution = solver.Assignment()
solution.Add(x_flat)
solution.Add(total_cost)
# db: DecisionBuilder
db = solver.Phase(x_flat, solver.INT_VAR_SIMPLE, solver.ASSIGN_MIN_VALUE)
solver.NewSearch(db, [objective])
num_solutions = 0
while solver.NextSolution():
print("total_cost:", total_cost.Value())
for i in range(rows):
for j in range(cols):
print(x[i][j].Value(), end=" ")
print()
print()
for i in range(rows):
print("Task:", i, end=" ")
for j in range(cols):
if x[i][j].Value() == 1:
print(" is done by ", j)
print()
num_solutions += 1
solver.EndSearch()
print()
print("num_solutions:", num_solutions)
print("failures:", solver.Failures())
print("branches:", solver.Branches())
print("WallTime:", solver.WallTime())
# Problem instance
# hakank: I added the fifth column to make it more
# interesting
rows = 4
cols = 5
cost = [[14, 5, 8, 7, 15], [2, 12, 6, 5, 3], [7, 8, 3, 9, 7], [2, 4, 6, 10, 1]]
if __name__ == "__main__":
main(cost, rows, cols)