185 lines
4.9 KiB
Plaintext
185 lines
4.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": [
|
|
"# production"
|
|
]
|
|
},
|
|
{
|
|
"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/production.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/production.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",
|
|
" Production planning problem in Google or-tools.\n",
|
|
"\n",
|
|
" From the OPL model production.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": [
|
|
"import sys\n",
|
|
"from ortools.linear_solver import pywraplp\n",
|
|
"\n",
|
|
"\n",
|
|
"def main():\n",
|
|
"\n",
|
|
" # Create the solver.\n",
|
|
" solver = pywraplp.Solver.CreateSolver('glop')\n",
|
|
" if not solver:\n",
|
|
" return\n",
|
|
"\n",
|
|
" #\n",
|
|
" # data\n",
|
|
" #\n",
|
|
" kluski = 0\n",
|
|
" capellini = 1\n",
|
|
" fettucine = 2\n",
|
|
" products = ['kluski', 'capellini', 'fettucine']\n",
|
|
" num_products = len(products)\n",
|
|
"\n",
|
|
" flour = 0\n",
|
|
" eggs = 1\n",
|
|
" resources = ['flour', 'eggs']\n",
|
|
" num_resources = len(resources)\n",
|
|
"\n",
|
|
" consumption = [[0.5, 0.2], [0.4, 0.4], [0.3, 0.6]]\n",
|
|
" capacity = [20, 40]\n",
|
|
" demand = [100, 200, 300]\n",
|
|
" inside_cost = [0.6, 0.8, 0.3]\n",
|
|
" outside_cost = [0.8, 0.9, 0.4]\n",
|
|
"\n",
|
|
" #\n",
|
|
" # declare variables\n",
|
|
" #\n",
|
|
" inside = [\n",
|
|
" solver.NumVar(0, 10000, 'inside[%i]' % p) for p in range(num_products)\n",
|
|
" ]\n",
|
|
" outside = [\n",
|
|
" solver.NumVar(0, 10000, 'outside[%i]' % p) for p in range(num_products)\n",
|
|
" ]\n",
|
|
"\n",
|
|
" # to minimize\n",
|
|
" z = solver.Sum([\n",
|
|
" inside_cost[p] * inside[p] + outside_cost[p] * outside[p]\n",
|
|
" for p in range(num_products)\n",
|
|
" ])\n",
|
|
"\n",
|
|
" #\n",
|
|
" # constraints\n",
|
|
" #\n",
|
|
" for r in range(num_resources):\n",
|
|
" solver.Add(\n",
|
|
" solver.Sum([consumption[p][r] * inside[p]\n",
|
|
" for p in range(num_products)]) <= capacity[r])\n",
|
|
"\n",
|
|
" for p in range(num_products):\n",
|
|
" solver.Add(inside[p] + outside[p] >= demand[p])\n",
|
|
"\n",
|
|
" objective = solver.Minimize(z)\n",
|
|
"\n",
|
|
" solver.Solve()\n",
|
|
"\n",
|
|
" print()\n",
|
|
" print('z = ', solver.Objective().Value())\n",
|
|
"\n",
|
|
" for p in range(num_products):\n",
|
|
" print(\n",
|
|
" products[p],\n",
|
|
" ': inside:',\n",
|
|
" inside[p].SolutionValue(),\n",
|
|
" '(ReducedCost:',\n",
|
|
" inside[p].ReducedCost(),\n",
|
|
" ')',\n",
|
|
" end=' ')\n",
|
|
" print('outside:', outside[p].SolutionValue(), ' (ReducedCost:',\n",
|
|
" outside[p].ReducedCost(), ')')\n",
|
|
" print()\n",
|
|
"\n",
|
|
"\n",
|
|
"main()\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"language_info": {
|
|
"name": "python"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|