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

177 lines
5.6 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": [
"# diet1"
]
},
{
"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/diet1.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/diet1.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",
" Simple diet problem in Google CP Solver.\n",
"\n",
" Standard Operations Research example in Minizinc\n",
"\n",
"\n",
" Minimize the cost for the products:\n",
" Type of Calories Chocolate Sugar Fat\n",
" Food (ounces) (ounces) (ounces)\n",
" Chocolate Cake (1 slice) 400 3 2 2\n",
" Chocolate ice cream (1 scoop) 200 2 2 4\n",
" Cola (1 bottle) 150 0 4 1\n",
" Pineapple cheesecake (1 piece) 500 0 4 5\n",
"\n",
" Compare with the following models:\n",
" * Tailor/Essence': http://hakank.org/tailor/diet1.eprime\n",
" * MiniZinc: http://hakank.org/minizinc/diet1.mzn\n",
" * SICStus: http://hakank.org/sicstus/diet1.pl\n",
" * Zinc: http://hakank.org/minizinc/diet1.zinc\n",
" * Choco: http://hakank.org/choco/Diet.java\n",
" * Comet: http://hakank.org/comet/diet.co\n",
" * ECLiPSe: http://hakank.org/eclipse/diet.ecl\n",
" * Gecode: http://hakank.org/gecode/diet.cpp\n",
" * Gecode/R: http://hakank.org/gecode_r/diet.rb\n",
" * JaCoP: http://hakank.org/JaCoP/Diet.java\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"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "code",
"metadata": {},
"outputs": [],
"source": [
"from ortools.sat.python import cp_model\n",
"\n",
"\n",
"def main(unused_argv):\n",
" # Create the solver.\n",
" model = cp_model.CpModel()\n",
"\n",
" #\n",
" # data\n",
" #\n",
" n = 4\n",
" price = [50, 20, 30, 80] # in cents\n",
" limits = [500, 6, 10, 8] # requirements for each nutrition type\n",
"\n",
" # nutritions for each product\n",
" calories = [400, 200, 150, 500]\n",
" chocolate = [3, 2, 0, 0]\n",
" sugar = [2, 2, 4, 4]\n",
" fat = [2, 4, 1, 5]\n",
"\n",
" #\n",
" # declare variables\n",
" #\n",
" x = [model.NewIntVar(0, 100, \"x%d\" % i) for i in range(n)]\n",
" cost = model.NewIntVar(0, 10000, \"cost\")\n",
"\n",
" #\n",
" # constraints\n",
" #\n",
" model.Add(sum(x[i] * calories[i] for i in range(n)) >= limits[0])\n",
" model.Add(sum(x[i] * chocolate[i] for i in range(n)) >= limits[1])\n",
" model.Add(sum(x[i] * sugar[i] for i in range(n)) >= limits[2])\n",
" model.Add(sum(x[i] * fat[i] for i in range(n)) >= limits[3])\n",
"\n",
" # objective\n",
" model.Minimize(cost)\n",
"\n",
" # Solve model.\n",
" solver = cp_model.CpSolver()\n",
" status = solver.Solve(model)\n",
"\n",
" # Output solution.\n",
" if status == cp_model.OPTIMAL:\n",
" print(\"cost:\", solver.ObjectiveValue())\n",
" print([(\"abcdefghij\" [i], solver.Value(x[i])) for i in range(n)])\n",
" print()\n",
" print(' - status : %s' % solver.StatusName(status))\n",
" print(' - conflicts : %i' % solver.NumConflicts())\n",
" print(' - branches : %i' % solver.NumBranches())\n",
" print(' - wall time : %f ms' % solver.WallTime())\n",
" print()\n",
"\n",
"\n",
"main(\"cp sample\")\n",
"\n"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}