Files
ortools-clone/examples/notebook/contrib/subset_sum.ipynb
2022-06-27 15:42:26 +02:00

185 lines
5.3 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "google",
"metadata": {},
"source": [
"##### Copyright 2022 Google LLC."
]
},
{
"cell_type": "markdown",
"id": "apache",
"metadata": {},
"source": [
"Licensed under the Apache License, Version 2.0 (the \"License\");\n",
"you may not use this file except in compliance with the License.\n",
"You may obtain a copy of the License at\n",
"\n",
" http://www.apache.org/licenses/LICENSE-2.0\n",
"\n",
"Unless required by applicable law or agreed to in writing, software\n",
"distributed under the License is distributed on an \"AS IS\" BASIS,\n",
"WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
"See the License for the specific language governing permissions and\n",
"limitations under the License.\n"
]
},
{
"cell_type": "markdown",
"id": "basename",
"metadata": {},
"source": [
"# subset_sum"
]
},
{
"cell_type": "markdown",
"id": "link",
"metadata": {},
"source": [
"<table align=\"left\">\n",
"<td>\n",
"<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/contrib/subset_sum.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
"</td>\n",
"<td>\n",
"<a href=\"https://github.com/google/or-tools/blob/main/examples/contrib/subset_sum.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
"</td>\n",
"</table>"
]
},
{
"cell_type": "markdown",
"id": "doc",
"metadata": {},
"source": [
"First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "install",
"metadata": {},
"outputs": [],
"source": [
"!pip install ortools"
]
},
{
"cell_type": "markdown",
"id": "description",
"metadata": {},
"source": [
"\n",
"\n",
" Subset sum problem in Google CP Solver.\n",
"\n",
" From Katta G. Murty: 'Optimization Models for Decision Making', page 340\n",
" http://ioe.engin.umich.edu/people/fac/books/murty/opti_model/junior-7.pdf\n",
" '''\n",
" Example 7.8.1\n",
"\n",
" A bank van had several bags of coins, each containing either\n",
" 16, 17, 23, 24, 39, or 40 coins. While the van was parked on the\n",
" street, thieves stole some bags. A total of 100 coins were lost.\n",
" It is required to find how many bags were stolen.\n",
" '''\n",
"\n",
" Compare with the following models:\n",
" * Comet: http://www.hakank.org/comet/subset_sum.co\n",
" * ECLiPSE: http://www.hakank.org/eclipse/subset_sum.ecl\n",
" * Gecode: http://www.hakank.org/gecode/subset_sum.cpp\n",
" * MiniZinc: http://www.hakank.org/minizinc/subset_sum.mzn\n",
" * Tailor/Essence': http://www.hakank.org/tailor/subset_sum.py\n",
" * SICStus: http://hakank.org/sicstus/subset_sum.pl\n",
"\n",
" This model was created by Hakan Kjellerstrand (hakank@gmail.com)\n",
" Also see my other Google CP Solver models:\n",
" http://www.hakank.org/google_or_tools/\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "code",
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"from ortools.constraint_solver import pywrapcp\n",
"\n",
"\n",
"def subset_sum(solver, values, total):\n",
" n = len(values)\n",
" x = [solver.IntVar(0, n) for i in range(n)]\n",
" ss = solver.IntVar(0, n)\n",
"\n",
" solver.Add(ss == solver.Sum(x))\n",
" solver.Add(total == solver.ScalProd(x, values))\n",
"\n",
" return x, ss\n",
"\n",
"\n",
"def main(coins, total):\n",
"\n",
" # Create the solver.\n",
" solver = pywrapcp.Solver(\"n-queens\")\n",
"\n",
" #\n",
" # data\n",
" #\n",
" print(\"coins:\", coins)\n",
" print(\"total:\", total)\n",
" print()\n",
"\n",
" #\n",
" # declare variables\n",
" #\n",
"\n",
" #\n",
" # constraints\n",
" #\n",
" x, ss = subset_sum(solver, coins, total)\n",
"\n",
" #\n",
" # solution and search\n",
" #\n",
" solution = solver.Assignment()\n",
" solution.Add(x)\n",
" solution.Add(ss)\n",
"\n",
" # db: DecisionBuilder\n",
" db = solver.Phase(x, solver.CHOOSE_FIRST_UNBOUND, solver.ASSIGN_MIN_VALUE)\n",
"\n",
" solver.NewSearch(db)\n",
" num_solutions = 0\n",
" while solver.NextSolution():\n",
" print(\"ss:\", ss.Value())\n",
" print(\"x: \", [x[i].Value() for i in range(len(x))])\n",
" print()\n",
" num_solutions += 1\n",
" solver.EndSearch()\n",
"\n",
" print()\n",
" print(\"num_solutions:\", num_solutions)\n",
" print(\"failures:\", solver.Failures())\n",
" print(\"branches:\", solver.Branches())\n",
" print(\"WallTime:\", solver.WallTime())\n",
"\n",
"\n",
"coins = [16, 17, 23, 24, 39, 40]\n",
"total = 100\n",
"if len(sys.argv) > 1:\n",
" total = int(sys.argv[1])\n",
"main(coins, total)\n",
"\n"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}