Files
ortools-clone/examples/notebook/linear_solver/assignment_mip.ipynb
2021-04-27 10:56:39 +02:00

164 lines
5.3 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Copyright 2021 Google LLC."
]
},
{
"cell_type": "markdown",
"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",
"metadata": {},
"source": [
"# assignment_mip"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table align=\"left\">\n",
"<td>\n",
"<a href=\"https://colab.research.google.com/github/google/or-tools/blob/master/examples/notebook/linear_solver/assignment_mip.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/linear_solver/samples/assignment_mip.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",
"metadata": {},
"source": [
"First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!pip install ortools"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#!/usr/bin/env python3\n",
"# Copyright 2010-2021 Google LLC\n",
"# 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",
"\"\"\"MIP example that solves an assignment problem.\"\"\"\n",
"# [START program]\n",
"# [START import]\n",
"from ortools.linear_solver import pywraplp\n",
"# [END import]\n",
"\n",
"\n",
"# Data\n",
"# [START data_model]\n",
"costs = [\n",
" [90, 80, 75, 70],\n",
" [35, 85, 55, 65],\n",
" [125, 95, 90, 95],\n",
" [45, 110, 95, 115],\n",
" [50, 100, 90, 100],\n",
"]\n",
"num_workers = len(costs)\n",
"num_tasks = len(costs[0])\n",
"# [END data_model]\n",
"\n",
"# Solver\n",
"# [START solver]\n",
"# Create the mip solver with the SCIP backend.\n",
"solver = pywraplp.Solver.CreateSolver('SCIP')\n",
"\n",
"# [END solver]\n",
"\n",
"# Variables\n",
"# [START variables]\n",
"# x[i, j] is an array of 0-1 variables, which will be 1\n",
"# if worker i is assigned to task j.\n",
"x = {}\n",
"for i in range(num_workers):\n",
" for j in range(num_tasks):\n",
" x[i, j] = solver.IntVar(0, 1, '')\n",
"# [END variables]\n",
"\n",
"# Constraints\n",
"# [START constraints]\n",
"# Each worker is assigned to at most 1 task.\n",
"for i in range(num_workers):\n",
" solver.Add(solver.Sum([x[i, j] for j in range(num_tasks)]) <= 1)\n",
"\n",
"# Each task is assigned to exactly one worker.\n",
"for j in range(num_tasks):\n",
" solver.Add(solver.Sum([x[i, j] for i in range(num_workers)]) == 1)\n",
"# [END constraints]\n",
"\n",
"# Objective\n",
"# [START objective]\n",
"objective_terms = []\n",
"for i in range(num_workers):\n",
" for j in range(num_tasks):\n",
" objective_terms.append(costs[i][j] * x[i, j])\n",
"solver.Minimize(solver.Sum(objective_terms))\n",
"# [END objective]\n",
"\n",
"# Solve\n",
"# [START solve]\n",
"status = solver.Solve()\n",
"# [END solve]\n",
"\n",
"# Print solution.\n",
"# [START print_solution]\n",
"if status == pywraplp.Solver.OPTIMAL or status == pywraplp.Solver.FEASIBLE:\n",
" print('Total cost = ', solver.Objective().Value(), '\\n')\n",
" for i in range(num_workers):\n",
" for j in range(num_tasks):\n",
" # Test if x[i,j] is 1 (with tolerance for floating point arithmetic).\n",
" if x[i, j].solution_value() > 0.5:\n",
" print('Worker %d assigned to task %d. Cost = %d' %\n",
" (i, j, costs[i][j]))\n",
"# [END print_solution]\n",
"\n"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 4
}