151 lines
3.9 KiB
Plaintext
151 lines
3.9 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": [
|
|
"# volsay"
|
|
]
|
|
},
|
|
{
|
|
"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/volsay.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/volsay.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",
|
|
" Volsay problem in Google or-tools.\n",
|
|
"\n",
|
|
" From the OPL model volsay.mod\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.linear_solver import pywraplp\n",
|
|
"\n",
|
|
"\n",
|
|
"def main(unused_argv):\n",
|
|
"\n",
|
|
" # Create the solver.\n",
|
|
"\n",
|
|
" # using GLPK\n",
|
|
" #solver = pywraplp.Solver('CoinsGridGLPK',\n",
|
|
" # pywraplp.Solver.GLPK_LINEAR_PROGRAMMING)\n",
|
|
"\n",
|
|
" # Using CLP\n",
|
|
" solver = pywraplp.Solver.CreateSolver('CLP')\n",
|
|
" if not solver:\n",
|
|
" return\n",
|
|
"\n",
|
|
" # data\n",
|
|
"\n",
|
|
" # declare variables\n",
|
|
" Gas = solver.NumVar(0, 100000, 'Gas')\n",
|
|
" Chloride = solver.NumVar(0, 100000, 'Cloride')\n",
|
|
"\n",
|
|
" #\n",
|
|
" # constraints\n",
|
|
" #\n",
|
|
" solver.Add(Gas + Chloride <= 50)\n",
|
|
" solver.Add(3 * Gas + 4 * Chloride <= 180)\n",
|
|
"\n",
|
|
" # objective\n",
|
|
" objective = solver.Maximize(40 * Gas + 50 * Chloride)\n",
|
|
"\n",
|
|
" print('NumConstraints:', solver.NumConstraints())\n",
|
|
"\n",
|
|
" #\n",
|
|
" # solution and search\n",
|
|
" #\n",
|
|
" solver.Solve()\n",
|
|
"\n",
|
|
" print()\n",
|
|
" print('objective = ', solver.Objective().Value())\n",
|
|
" print('Gas = ', Gas.SolutionValue(), 'ReducedCost =', Gas.ReducedCost())\n",
|
|
" print('Chloride:', Chloride.SolutionValue(), 'ReducedCost =',\n",
|
|
" Chloride.ReducedCost())\n",
|
|
"\n",
|
|
"\n",
|
|
"main('Volsay')\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"language_info": {
|
|
"name": "python"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|