190 lines
5.6 KiB
Plaintext
190 lines
5.6 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "google",
|
|
"metadata": {},
|
|
"source": [
|
|
"##### Copyright 2025 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": [
|
|
"# xkcd"
|
|
]
|
|
},
|
|
{
|
|
"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/xkcd.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/xkcd.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",
|
|
" 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"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "code",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from ortools.constraint_solver import pywrapcp\n",
|
|
"\n",
|
|
"\n",
|
|
"def main():\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",
|
|
"\n",
|
|
"main()\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"language_info": {
|
|
"name": "python"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|