173 lines
4.8 KiB
Plaintext
173 lines
4.8 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": [
|
|
"# grocery"
|
|
]
|
|
},
|
|
{
|
|
"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/grocery.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/grocery.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",
|
|
" Grocery problem in Google CP Solver.\n",
|
|
"\n",
|
|
" From Christian Schulte, Gert Smolka, Finite Domain\n",
|
|
" http://www.mozart-oz.org/documentation/fdt/\n",
|
|
" Constraint Programming in Oz. A Tutorial. 2001.\n",
|
|
" '''\n",
|
|
" A kid goes into a grocery store and buys four items. The cashier\n",
|
|
" charges $7.11, the kid pays and is about to leave when the cashier\n",
|
|
" calls the kid back, and says 'Hold on, I multiplied the four items\n",
|
|
" instead of adding them; I'll try again; Hah, with adding them the\n",
|
|
" price still comes to $7.11'. What were the prices of the four items?\n",
|
|
" '''\n",
|
|
"\n",
|
|
" Compare with the following models:\n",
|
|
" * MiniZinc: http://hakank.org/minizinc/grocery.mzn\n",
|
|
" * Comet: http://hakank.org/comet/grocery.co\n",
|
|
" * Zinc: http://hakank.org/minizinc/grocery.zinc\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",
|
|
"\n",
|
|
"from ortools.constraint_solver import pywrapcp\n",
|
|
"from functools import reduce\n",
|
|
"\n",
|
|
"\n",
|
|
"def main():\n",
|
|
"\n",
|
|
" # Create the solver.\n",
|
|
" solver = pywrapcp.Solver(\"Grocery\")\n",
|
|
"\n",
|
|
" #\n",
|
|
" # data\n",
|
|
" #\n",
|
|
" n = 4\n",
|
|
" c = 711\n",
|
|
"\n",
|
|
" #\n",
|
|
" # declare variables\n",
|
|
" #\n",
|
|
" item = [solver.IntVar(0, c, \"item[%i]\" % i) for i in range(n)]\n",
|
|
"\n",
|
|
" #\n",
|
|
" # constraints\n",
|
|
" #\n",
|
|
" solver.Add(solver.Sum(item) == c)\n",
|
|
" solver.Add(reduce(lambda x, y: x * y, item) == c * 100**3)\n",
|
|
"\n",
|
|
" # symmetry breaking\n",
|
|
" for i in range(1, n):\n",
|
|
" solver.Add(item[i - 1] < item[i])\n",
|
|
"\n",
|
|
" #\n",
|
|
" # search and result\n",
|
|
" #\n",
|
|
" db = solver.Phase(item, solver.INT_VAR_SIMPLE, solver.INT_VALUE_SIMPLE)\n",
|
|
"\n",
|
|
" solver.NewSearch(db)\n",
|
|
" num_solutions = 0\n",
|
|
" while solver.NextSolution():\n",
|
|
" print(\"item:\", [item[i].Value() for i in range(n)])\n",
|
|
" print()\n",
|
|
" num_solutions += 1\n",
|
|
"\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",
|
|
"main()\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"language_info": {
|
|
"name": "python"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|