Files
ortools-clone/examples/notebook/sat/multiple_knapsack_sat.ipynb
2022-04-14 14:31:02 +02:00

167 lines
5.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": [
"# multiple_knapsack_sat"
]
},
{
"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/sat/multiple_knapsack_sat.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/ortools/sat/samples/multiple_knapsack_sat.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": [
"Solves a multiple knapsack problem using the CP-SAT solver."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "code",
"metadata": {},
"outputs": [],
"source": [
"from ortools.sat.python import cp_model\n",
"\n",
"\n",
"def main():\n",
" data = {}\n",
" data['weights'] = [\n",
" 48, 30, 42, 36, 36, 48, 42, 42, 36, 24, 30, 30, 42, 36, 36\n",
" ]\n",
" data['values'] = [\n",
" 10, 30, 25, 50, 35, 30, 15, 40, 30, 35, 45, 10, 20, 30, 25\n",
" ]\n",
" assert len(data['weights']) == len(data['values'])\n",
" data['num_items'] = len(data['weights'])\n",
" data['all_items'] = range(data['num_items'])\n",
"\n",
" data['bin_capacities'] = [100, 100, 100, 100, 100]\n",
" data['num_bins'] = len(data['bin_capacities'])\n",
" data['all_bins'] = range(data['num_bins'])\n",
"\n",
" model = cp_model.CpModel()\n",
"\n",
" # Variables.\n",
" # x[i, b] = 1 if item i is packed in bin b.\n",
" x = {}\n",
" for i in data['all_items']:\n",
" for b in data['all_bins']:\n",
" x[i, b] = model.NewBoolVar(f'x_{i}_{b}')\n",
"\n",
" # Constraints.\n",
" # Each item is assigned to at most one bin.\n",
" for i in data['all_items']:\n",
" model.AddAtMostOne(x[i, b] for b in data['all_bins'])\n",
"\n",
" # The amount packed in each bin cannot exceed its capacity.\n",
" for b in data['all_bins']:\n",
" model.Add(\n",
" sum(x[i, b] * data['weights'][i]\n",
" for i in data['all_items']) <= data['bin_capacities'][b])\n",
"\n",
" # Objective.\n",
" # Maximize total value of packed items.\n",
" objective = []\n",
" for i in data['all_items']:\n",
" for b in data['all_bins']:\n",
" objective.append(\n",
" cp_model.LinearExpr.Term(x[i, b], data['values'][i]))\n",
" model.Maximize(cp_model.LinearExpr.Sum(objective))\n",
"\n",
" solver = cp_model.CpSolver()\n",
" status = solver.Solve(model)\n",
"\n",
" if status == cp_model.OPTIMAL:\n",
" print(f'Total packed value: {solver.ObjectiveValue()}')\n",
" total_weight = 0\n",
" for b in data['all_bins']:\n",
" print(f'Bin {b}')\n",
" bin_weight = 0\n",
" bin_value = 0\n",
" for i in data['all_items']:\n",
" if solver.Value(x[i, b]) > 0:\n",
" print(\n",
" f\"Item {i} weight: {data['weights'][i]} value: {data['values'][i]}\"\n",
" )\n",
" bin_weight += data['weights'][i]\n",
" bin_value += data['values'][i]\n",
" print(f'Packed bin weight: {bin_weight}')\n",
" print(f'Packed bin value: {bin_value}\\n')\n",
" total_weight += bin_weight\n",
" print(f'Total packed weight: {total_weight}')\n",
" else:\n",
" print('The problem does not have an optimal solution.')\n",
"\n",
"\n",
"main()\n",
"\n"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}