182 lines
4.9 KiB
Plaintext
182 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": [
|
|
"# knapsack_mip"
|
|
]
|
|
},
|
|
{
|
|
"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/knapsack_mip.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/knapsack_mip.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",
|
|
" Knapsack problem using MIP in Google or-tools.\n",
|
|
"\n",
|
|
" From the OPL model knapsack.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",
|
|
" print('Solver: ', sol)\n",
|
|
" solver = pywraplp.Solver.CreateSolver(sol)\n",
|
|
" if not solver:\n",
|
|
" return\n",
|
|
"\n",
|
|
" #\n",
|
|
" # data\n",
|
|
" #\n",
|
|
" nb_items = 12\n",
|
|
" nb_resources = 7\n",
|
|
" items = list(range(nb_items))\n",
|
|
" resources = list(range(nb_resources))\n",
|
|
"\n",
|
|
" capacity = [18209, 7692, 1333, 924, 26638, 61188, 13360]\n",
|
|
" value = [96, 76, 56, 11, 86, 10, 66, 86, 83, 12, 9, 81]\n",
|
|
" use = [[19, 1, 10, 1, 1, 14, 152, 11, 1, 1, 1, 1],\n",
|
|
" [0, 4, 53, 0, 0, 80, 0, 4, 5, 0, 0, 0],\n",
|
|
" [4, 660, 3, 0, 30, 0, 3, 0, 4, 90, 0, 0],\n",
|
|
" [7, 0, 18, 6, 770, 330, 7, 0, 0, 6, 0, 0],\n",
|
|
" [0, 20, 0, 4, 52, 3, 0, 0, 0, 5, 4, 0],\n",
|
|
" [0, 0, 40, 70, 4, 63, 0, 0, 60, 0, 4, 0],\n",
|
|
" [0, 32, 0, 0, 0, 5, 0, 3, 0, 660, 0, 9]]\n",
|
|
"\n",
|
|
" max_value = max(capacity)\n",
|
|
"\n",
|
|
" #\n",
|
|
" # variables\n",
|
|
" #\n",
|
|
" take = [solver.IntVar(0, max_value, 'take[%i]' % j) for j in items]\n",
|
|
"\n",
|
|
" # total cost, to be maximized\n",
|
|
" z = solver.Sum([value[i] * take[i] for i in items])\n",
|
|
"\n",
|
|
" #\n",
|
|
" # constraints\n",
|
|
" #\n",
|
|
" for r in resources:\n",
|
|
" solver.Add(solver.Sum([use[r][i] * take[i] for i in items]) <= capacity[r])\n",
|
|
"\n",
|
|
" # objective\n",
|
|
" objective = solver.Maximize(z)\n",
|
|
"\n",
|
|
" #\n",
|
|
" # solution and search\n",
|
|
" #\n",
|
|
" solver.Solve()\n",
|
|
"\n",
|
|
" print()\n",
|
|
" print('z: ', int(solver.Objective().Value()))\n",
|
|
"\n",
|
|
" print('take:', end=' ')\n",
|
|
" for i in items:\n",
|
|
" print(int(take[i].SolutionValue()), end=' ')\n",
|
|
" print()\n",
|
|
"\n",
|
|
" print()\n",
|
|
" print('walltime :', solver.WallTime(), 'ms')\n",
|
|
" if sol == 'CBC':\n",
|
|
" print('iterations:', solver.Iterations())\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"sol = 'CBC'\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": {
|
|
"language_info": {
|
|
"name": "python"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|