Removed assignment_b.py
This commit is contained in:
@@ -1,119 +0,0 @@
|
||||
# Copyright 2010 Hakan Kjellerstrand hakank@bonetmail.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
|
||||
|
||||
This model was created by Hakan Kjellerstrand (hakank@bonetmail.com)
|
||||
Also see my other Google CP Solver models: http://www.hakank.org/google_or_tools/
|
||||
"""
|
||||
|
||||
from 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):
|
||||
for j in range(cols):
|
||||
x[(i, j)] = solver.IntVar(0, 1, 'x(%i,%i)' % (i, j))
|
||||
x_flat = [x[(i[j] for i in range(rows) for j in range(cols)]
|
||||
|
||||
#
|
||||
# constraints
|
||||
#
|
||||
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
|
||||
for row in range(rows):
|
||||
r = [x[row][j] for j in range(cols)]
|
||||
solver.Add(solver.Sum(r) == 1)
|
||||
|
||||
# zero or one assignments per column
|
||||
for col in range(cols):
|
||||
c = [x[i][col] for i in range(rows)]
|
||||
solver.Add(solver.Sum(c) <= 1)
|
||||
|
||||
|
||||
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()
|
||||
xval = [x[i][j].Value() for i in range(rows) for j in range(cols)]
|
||||
print "x:", xval
|
||||
for i in range(rows):
|
||||
for j in range(cols):
|
||||
print xval[i*cols+j],
|
||||
print
|
||||
print
|
||||
num_solutions += 1
|
||||
solver.EndSearch()
|
||||
|
||||
print
|
||||
print "num_solutions:", num_solutions
|
||||
print "failures:", solver.failures()
|
||||
print "branches:", solver.branches()
|
||||
print "wall_time:", solver.wall_time()
|
||||
|
||||
|
||||
# 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)
|
||||
Reference in New Issue
Block a user