Files
ortools-clone/examples/python/assignment.py

125 lines
3.4 KiB
Python
Raw Normal View History

2018-02-19 15:21:05 +01:00
# Copyright 2010 Hakan Kjellerstrand hakank@gmail.com
#
# 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
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/
"""
from __future__ import print_function
from ortools.constraint_solver import pywrapcp
def main(cost, rows, cols):
2014-05-22 20:13:16 +00:00
# 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
2018-06-11 11:51:18 +02:00
solver.Add(total_cost == solver.Sum(
[solver.ScalProd(x_row, cost_row) for (x_row, cost_row) in zip(x, cost)]))
2014-05-22 20:13:16 +00:00
# exacly one assignment per row, all rows must be assigned
2018-06-11 11:51:18 +02:00
[
solver.Add(solver.Sum([x[row][j]
for j in range(cols)]) == 1)
for row in range(rows)
]
2014-05-22 20:13:16 +00:00
# zero or one assignments per column
2018-06-11 11:51:18 +02:00
[
solver.Add(solver.Sum([x[i][col]
for i in range(rows)]) <= 1)
for col in range(cols)
]
2014-05-22 20:13:16 +00:00
objective = solver.Minimize(total_cost, 1)
#
# solution and search
#
solution = solver.Assignment()
solution.Add(x_flat)
solution.Add(total_cost)
# db: DecisionBuilder
2018-06-11 11:51:18 +02:00
db = solver.Phase(x_flat, solver.INT_VAR_SIMPLE, solver.ASSIGN_MIN_VALUE)
2014-05-22 20:13:16 +00:00
solver.NewSearch(db, [objective])
num_solutions = 0
while solver.NextSolution():
print("total_cost:", total_cost.Value())
for i in range(rows):
2014-05-22 20:13:16 +00:00
for j in range(cols):
2018-06-11 11:51:18 +02:00
print(x[i][j].Value(), end=" ")
print()
print()
2014-05-22 20:13:16 +00:00
for i in range(rows):
2018-06-11 11:51:18 +02:00
print("Task:", i, end=" ")
2014-05-22 20:13:16 +00:00
for j in range(cols):
if x[i][j].Value() == 1:
print(" is done by ", j)
print()
2014-05-22 20:13:16 +00:00
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
2018-06-11 11:51:18 +02:00
cost = [[14, 5, 8, 7, 15], [2, 12, 6, 5, 3], [7, 8, 3, 9, 7], [2, 4, 6, 10, 1]]
2014-05-22 20:13:16 +00:00
if __name__ == "__main__":
main(cost, rows, cols)