172 lines
4.7 KiB
Plaintext
172 lines
4.7 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": [
|
|
"# volsay3"
|
|
]
|
|
},
|
|
{
|
|
"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/volsay3.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/volsay3.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",
|
|
" Using arrays.\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",
|
|
" num_products = 2\n",
|
|
"\n",
|
|
" products = ['Gas', 'Chloride']\n",
|
|
" components = ['nitrogen', 'hydrogen', 'chlorine']\n",
|
|
"\n",
|
|
" demand = [[1, 3, 0], [1, 4, 1]]\n",
|
|
" profit = [30, 40]\n",
|
|
" stock = [50, 180, 40]\n",
|
|
"\n",
|
|
" # declare variables\n",
|
|
" production = [\n",
|
|
" solver.NumVar(0, 100000, 'production[%i]' % i)\n",
|
|
" for i in range(num_products)\n",
|
|
" ]\n",
|
|
"\n",
|
|
" #\n",
|
|
" # constraints\n",
|
|
" #\n",
|
|
" for c in range(len(components)):\n",
|
|
" solver.Add(\n",
|
|
" solver.Sum([demand[p][c] * production[p]\n",
|
|
" for p in range(len(products))]) <= stock[c])\n",
|
|
"\n",
|
|
" # objective\n",
|
|
" # Note: there is no support for solver.ScalProd in the LP/IP interface\n",
|
|
" objective = solver.Maximize(\n",
|
|
" solver.Sum([production[p] * profit[p] for p in range(num_products)]))\n",
|
|
"\n",
|
|
" print('NumConstraints:', solver.NumConstraints())\n",
|
|
" print('NumVariables:', solver.NumVariables())\n",
|
|
" print()\n",
|
|
"\n",
|
|
" #\n",
|
|
" # solution and search\n",
|
|
" #\n",
|
|
" solver.Solve()\n",
|
|
"\n",
|
|
" print()\n",
|
|
" print('objective = ', solver.Objective().Value())\n",
|
|
" for i in range(num_products):\n",
|
|
" print(products[i], '=', production[i].SolutionValue(), end=' ')\n",
|
|
" print('ReducedCost = ', production[i].ReducedCost())\n",
|
|
"\n",
|
|
" print()\n",
|
|
" print('walltime :', solver.WallTime(), 'ms')\n",
|
|
" print('iterations:', solver.Iterations())\n",
|
|
"\n",
|
|
"\n",
|
|
"main('Volsay')\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"language_info": {
|
|
"name": "python"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|