225 lines
6.4 KiB
Plaintext
225 lines
6.4 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": [
|
|
"# blending"
|
|
]
|
|
},
|
|
{
|
|
"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/master/examples/notebook/contrib/blending.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/master/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
|
|
"</td>\n",
|
|
"<td>\n",
|
|
"<a href=\"https://github.com/google/or-tools/blob/master/examples/contrib/blending.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/master/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",
|
|
" Blending problem in Google or-tools.\n",
|
|
"\n",
|
|
" From the OPL model blending.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(sol='CBC'):\n",
|
|
"\n",
|
|
" # Create the solver.\n",
|
|
"\n",
|
|
" print('Solver: ', sol)\n",
|
|
"\n",
|
|
" # using GLPK\n",
|
|
" if sol == 'GLPK':\n",
|
|
" solver = pywraplp.Solver('CoinsGridGLPK',\n",
|
|
" pywraplp.Solver.GLPK_MIXED_INTEGER_PROGRAMMING)\n",
|
|
" else:\n",
|
|
" # Using CBC\n",
|
|
" solver = pywraplp.Solver('CoinsGridCBC',\n",
|
|
" pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)\n",
|
|
"\n",
|
|
" #\n",
|
|
" # data\n",
|
|
" #\n",
|
|
" NbMetals = 3\n",
|
|
" NbRaw = 2\n",
|
|
" NbScrap = 2\n",
|
|
" NbIngo = 1\n",
|
|
" Metals = list(range(NbMetals))\n",
|
|
" Raws = list(range(NbRaw))\n",
|
|
" Scraps = list(range(NbScrap))\n",
|
|
" Ingos = list(range(NbIngo))\n",
|
|
"\n",
|
|
" CostMetal = [22, 10, 13]\n",
|
|
" CostRaw = [6, 5]\n",
|
|
" CostScrap = [7, 8]\n",
|
|
" CostIngo = [9]\n",
|
|
" Low = [0.05, 0.30, 0.60]\n",
|
|
" Up = [0.10, 0.40, 0.80]\n",
|
|
" PercRaw = [[0.20, 0.01], [0.05, 0], [0.05, 0.30]]\n",
|
|
" PercScrap = [[0, 0.01], [0.60, 0], [0.40, 0.70]]\n",
|
|
" PercIngo = [[0.10], [0.45], [0.45]]\n",
|
|
" Alloy = 71\n",
|
|
"\n",
|
|
" #\n",
|
|
" # variables\n",
|
|
" #\n",
|
|
" p = [solver.NumVar(0, solver.Infinity(), 'p[%i]' % i) for i in Metals]\n",
|
|
" r = [solver.NumVar(0, solver.Infinity(), 'r[%i]' % i) for i in Raws]\n",
|
|
" s = [solver.NumVar(0, solver.Infinity(), 's[%i]' % i) for i in Scraps]\n",
|
|
" ii = [solver.IntVar(0, solver.Infinity(), 'ii[%i]' % i) for i in Ingos]\n",
|
|
" metal = [\n",
|
|
" solver.NumVar(Low[j] * Alloy, Up[j] * Alloy, 'metal[%i]' % j)\n",
|
|
" for j in Metals\n",
|
|
" ]\n",
|
|
"\n",
|
|
" z = solver.NumVar(0, solver.Infinity(), 'z')\n",
|
|
"\n",
|
|
" #\n",
|
|
" # constraints\n",
|
|
" #\n",
|
|
"\n",
|
|
" solver.Add(z == solver.Sum([CostMetal[i] * p[i] for i in Metals]) +\n",
|
|
" solver.Sum([CostRaw[i] * r[i] for i in Raws]) +\n",
|
|
" solver.Sum([CostScrap[i] * s[i] for i in Scraps]) +\n",
|
|
" solver.Sum([CostIngo[i] * ii[i] for i in Ingos]))\n",
|
|
"\n",
|
|
" for j in Metals:\n",
|
|
" solver.Add(\n",
|
|
" metal[j] == p[j] + solver.Sum([PercRaw[j][k] * r[k] for k in Raws]) +\n",
|
|
" solver.Sum([PercScrap[j][k] * s[k] for k in Scraps]) +\n",
|
|
" solver.Sum([PercIngo[j][k] * ii[k] for k in Ingos]))\n",
|
|
"\n",
|
|
" solver.Add(solver.Sum(metal) == Alloy)\n",
|
|
"\n",
|
|
" objective = solver.Minimize(z)\n",
|
|
"\n",
|
|
" #\n",
|
|
" # solution and search\n",
|
|
" #\n",
|
|
" solver.Solve()\n",
|
|
"\n",
|
|
" print()\n",
|
|
"\n",
|
|
" print('z = ', solver.Objective().Value())\n",
|
|
" print('Metals')\n",
|
|
" for i in Metals:\n",
|
|
" print(p[i].SolutionValue(), end=' ')\n",
|
|
" print()\n",
|
|
"\n",
|
|
" print('Raws')\n",
|
|
" for i in Raws:\n",
|
|
" print(r[i].SolutionValue(), end=' ')\n",
|
|
" print()\n",
|
|
"\n",
|
|
" print('Scraps')\n",
|
|
" for i in Scraps:\n",
|
|
" print(s[i].SolutionValue(), end=' ')\n",
|
|
" print()\n",
|
|
"\n",
|
|
" print('Ingos')\n",
|
|
" for i in Ingos:\n",
|
|
" print(ii[i].SolutionValue(), end=' ')\n",
|
|
" print()\n",
|
|
"\n",
|
|
" print('Metals')\n",
|
|
" for i in Metals:\n",
|
|
" print(metal[i].SolutionValue(), end=' ')\n",
|
|
" print()\n",
|
|
"\n",
|
|
" print()\n",
|
|
"\n",
|
|
" print('walltime :', solver.WallTime(), 'ms')\n",
|
|
" if sol == 'CBC':\n",
|
|
" print('iterations:', solver.Iterations())\n",
|
|
"\n",
|
|
"\n",
|
|
"sol = 'CBC'\n",
|
|
"\n",
|
|
"if len(sys.argv) > 1:\n",
|
|
" sol = sys.argv[1]\n",
|
|
" if sol != 'GLPK' and sol != 'CBC':\n",
|
|
" print('Solver must be either GLPK or CBC')\n",
|
|
" sys.exit(1)\n",
|
|
"\n",
|
|
"main(sol)\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|