Files
ortools-clone/examples/notebook/contrib/xkcd.ipynb
2020-09-27 16:31:27 +02:00

183 lines
6.0 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Copyright 2020 The OR-Tools Authors."
]
},
{
"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": [
"# xkcd"
]
},
{
"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/xkcd.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/xkcd.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",
" xkcd problem (Knapsack) in Google CP Solver.\n",
"\n",
" http://xkcd.com/287/\n",
"\n",
" Some amount (or none) of each dish should be ordered to give a total\n",
" of exact 15.05\n",
"\n",
"\n",
" Compare with the following models:\n",
" * Comet: http://www.hakank.org/comet/xkcd.co\n",
" * ECLiPSE: http://www.hakank.org/eclipse/xkcd.ecl\n",
" * Gecode: http://www.hakank.org/gecode/xkcd.cpp\n",
" * Gecode/R: http://www.hakank.org/gecode_r/xkcd.rb\n",
" * MiniZinc: http://www.hakank.org/minizinc/xkcd.mzn\n",
" * Tailor: http://www.hakank.org/minizinc/xkcd.mzn\n",
" * SICtus: http://www.hakank.org/sicstus/xkcd.pl\n",
" * Zinc: http://www.hakank.org/minizinc/xkcd.zinc\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_cp_solver/\n",
"\"\"\"\n",
"from __future__ import print_function\n",
"from ortools.constraint_solver import pywrapcp\n",
"\n",
"\n",
"\n",
"# Create the solver.\n",
"solver = pywrapcp.Solver(\"xkcd knapsack\")\n",
"\n",
"#\n",
"# data\n",
"#\n",
"num_prices = 6\n",
"# for price and total: multiplied by 100 to be able to use integers\n",
"price = [215, 275, 335, 355, 420, 580]\n",
"total = 1505\n",
"\n",
"products = [\n",
" \"mixed fruit\", \"french fries\", \"side salad\", \"host wings\",\n",
" \"mozzarella sticks\", \"samples place\"\n",
"]\n",
"\n",
"# declare variables\n",
"\n",
"# how many items of each dish\n",
"x = [solver.IntVar(0, 10, \"x%i\" % i) for i in range(num_prices)]\n",
"z = solver.IntVar(0, 1505, \"z\")\n",
"\n",
"#\n",
"# constraints\n",
"#\n",
"solver.Add(z == solver.Sum([x[i] * price[i] for i in range(num_prices)]))\n",
"solver.Add(z == total)\n",
"\n",
"#\n",
"# solution and search\n",
"#\n",
"solution = solver.Assignment()\n",
"solution.Add([x[i] for i in range(num_prices)])\n",
"solution.Add(z)\n",
"\n",
"collector = solver.AllSolutionCollector(solution)\n",
"# collector = solver.FirstSolutionCollector(solution)\n",
"# search_log = solver.SearchLog(100, x[0])\n",
"solver.Solve(\n",
" solver.Phase([x[i] for i in range(num_prices)], solver.INT_VAR_SIMPLE,\n",
" solver.ASSIGN_MIN_VALUE), [collector])\n",
"\n",
"num_solutions = collector.SolutionCount()\n",
"print(\"num_solutions: \", num_solutions)\n",
"if num_solutions > 0:\n",
" for s in range(num_solutions):\n",
" print(\"z:\", collector.Value(s, z) / 100.0)\n",
" xval = [collector.Value(s, x[i]) for i in range(num_prices)]\n",
" print(\"x:\", xval)\n",
" for i in range(num_prices):\n",
" if xval[i] > 0:\n",
" print(xval[i], \"of\", products[i], \":\", price[i] / 100.0)\n",
" print()\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",
"else:\n",
" print(\"No solutions found\")\n",
"\n"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 4
}