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

110 lines
2.9 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.
"""
Subset sum problem in Google CP Solver.
From Katta G. Murty: 'Optimization Models for Decision Making', page 340
http://ioe.engin.umich.edu/people/fac/books/murty/opti_model/junior-7.pdf
'''
Example 7.8.1
A bank van had several bags of coins, each containing either
16, 17, 23, 24, 39, or 40 coins. While the van was parked on the
street, thieves stole some bags. A total of 100 coins were lost.
It is required to find how many bags were stolen.
'''
Compare with the following models:
* Comet: http://www.hakank.org/comet/subset_sum.co
* ECLiPSE: http://www.hakank.org/eclipse/subset_sum.ecl
* Gecode: http://www.hakank.org/gecode/subset_sum.cpp
* MiniZinc: http://www.hakank.org/minizinc/subset_sum.mzn
* Tailor/Essence': http://www.hakank.org/tailor/subset_sum.py
* SICStus: http://hakank.org/sicstus/subset_sum.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/
"""
2016-01-15 01:42:27 +01:00
from __future__ import print_function
2014-05-22 20:13:16 +00:00
import sys
from ortools.constraint_solver import pywrapcp
def subset_sum(solver, values, total):
2014-05-22 20:13:16 +00:00
n = len(values)
x = [solver.IntVar(0, n) for i in range(n)]
ss = solver.IntVar(0, n)
2014-05-22 20:13:16 +00:00
solver.Add(ss == solver.Sum(x))
solver.Add(total == solver.ScalProd(x, values))
2014-05-22 20:13:16 +00:00
return x, ss
def main(coins, total):
2014-05-22 20:13:16 +00:00
# Create the solver.
solver = pywrapcp.Solver("n-queens")
#
# data
#
2016-01-15 01:42:27 +01:00
print("coins:", coins)
print("total:", total)
print()
2014-05-22 20:13:16 +00:00
#
# declare variables
#
#
# constraints
#
x, ss = subset_sum(solver, coins, total)
#
# solution and search
#
solution = solver.Assignment()
solution.Add(x)
solution.Add(ss)
# db: DecisionBuilder
2018-06-11 11:51:18 +02:00
db = solver.Phase(x, solver.CHOOSE_FIRST_UNBOUND, solver.ASSIGN_MIN_VALUE)
2014-05-22 20:13:16 +00:00
solver.NewSearch(db)
num_solutions = 0
while solver.NextSolution():
2016-01-15 01:42:27 +01:00
print("ss:", ss.Value())
print("x: ", [x[i].Value() for i in range(len(x))])
print()
2014-05-22 20:13:16 +00:00
num_solutions += 1
solver.EndSearch()
2016-01-15 01:42:27 +01:00
print()
print("num_solutions:", num_solutions)
print("failures:", solver.Failures())
print("branches:", solver.Branches())
print("WallTime:", solver.WallTime())
2018-06-11 11:51:18 +02:00
coins = [16, 17, 23, 24, 39, 40]
total = 100
2014-05-22 20:13:16 +00:00
if __name__ == "__main__":
if len(sys.argv) > 1:
total = int(sys.argv[1])
2014-05-22 20:13:16 +00:00
main(coins, total)