177 lines
5.6 KiB
Plaintext
177 lines
5.6 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"##### Copyright 2020 Google LLC."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"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",
|
|
"metadata": {},
|
|
"source": [
|
|
"# coins3"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<table align=\"left\">\n",
|
|
"<td>\n",
|
|
"<a href=\"https://colab.research.google.com/github/google/or-tools/blob/master/examples/notebook/contrib/coins3.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/master/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
|
|
"</td>\n",
|
|
"<td>\n",
|
|
"<a href=\"https://github.com/google/or-tools/blob/master/examples/contrib/coins3.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/master/tools/github_32px.png\"/>View source on GitHub</a>\n",
|
|
"</td>\n",
|
|
"</table>"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"!pip install ortools"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Copyright 2010 Hakan Kjellerstrand hakank@gmail.com\n",
|
|
"#\n",
|
|
"# 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",
|
|
"\"\"\"\n",
|
|
"\n",
|
|
" Coin application in Google CP Solver.\n",
|
|
"\n",
|
|
" From 'Constraint Logic Programming using ECLiPSe'\n",
|
|
" pages 99f and 234 ff.\n",
|
|
" The solution in ECLiPSe is at page 236.\n",
|
|
"\n",
|
|
" '''\n",
|
|
" What is the minimum number of coins that allows one to pay _exactly_\n",
|
|
" any amount smaller than one Euro? Recall that there are six different\n",
|
|
" euro cents, of denomination 1, 2, 5, 10, 20, 50\n",
|
|
" '''\n",
|
|
"\n",
|
|
" Compare with the following models:\n",
|
|
" * MiniZinc: http://hakank.org/minizinc/coins3.mzn\n",
|
|
" * Comet : http://www.hakank.org/comet/coins3.co\n",
|
|
" * Gecode : http://hakank.org/gecode/coins3.cpp\n",
|
|
" * SICStus : http://hakank.org/sicstus/coins3.pl\n",
|
|
"\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",
|
|
"\"\"\"\n",
|
|
"\n",
|
|
"from __future__ import print_function\n",
|
|
"\n",
|
|
"import sys\n",
|
|
"from ortools.constraint_solver import pywrapcp\n",
|
|
"\n",
|
|
"\n",
|
|
"# Create the solver.\n",
|
|
"solver = pywrapcp.Solver(\"Coins\")\n",
|
|
"\n",
|
|
"#\n",
|
|
"# data\n",
|
|
"#\n",
|
|
"n = 6 # number of different coins\n",
|
|
"variables = [1, 2, 5, 10, 25, 50]\n",
|
|
"\n",
|
|
"# declare variables\n",
|
|
"x = [solver.IntVar(0, 99, \"x%i\" % i) for i in range(n)]\n",
|
|
"num_coins = solver.IntVar(0, 99, \"num_coins\")\n",
|
|
"\n",
|
|
"#\n",
|
|
"# constraints\n",
|
|
"#\n",
|
|
"\n",
|
|
"# number of used coins, to be minimized\n",
|
|
"solver.Add(num_coins == solver.Sum(x))\n",
|
|
"\n",
|
|
"# Check that all changes from 1 to 99 can be made.\n",
|
|
"for j in range(1, 100):\n",
|
|
" tmp = [solver.IntVar(0, 99, \"b%i\" % i) for i in range(n)]\n",
|
|
" solver.Add(solver.ScalProd(tmp, variables) == j)\n",
|
|
" [solver.Add(tmp[i] <= x[i]) for i in range(n)]\n",
|
|
"\n",
|
|
"# objective\n",
|
|
"objective = solver.Minimize(num_coins, 1)\n",
|
|
"\n",
|
|
"#\n",
|
|
"# solution and search\n",
|
|
"#\n",
|
|
"solution = solver.Assignment()\n",
|
|
"solution.Add(x)\n",
|
|
"solution.Add(num_coins)\n",
|
|
"solution.AddObjective(num_coins)\n",
|
|
"\n",
|
|
"db = solver.Phase(x, solver.CHOOSE_MIN_SIZE_LOWEST_MAX,\n",
|
|
" solver.ASSIGN_MIN_VALUE)\n",
|
|
"\n",
|
|
"solver.NewSearch(db, [objective])\n",
|
|
"num_solutions = 0\n",
|
|
"while solver.NextSolution():\n",
|
|
" print(\"x: \", [x[i].Value() for i in range(n)])\n",
|
|
" print(\"num_coins:\", num_coins.Value())\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"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 4
|
|
}
|