1{
2 "cells": [
3  {
4   "cell_type": "markdown",
5   "id": "google",
6   "metadata": {},
7   "source": [
8    "##### Copyright 2021 Google LLC."
9   ]
10  },
11  {
12   "cell_type": "markdown",
13   "id": "apache",
14   "metadata": {},
15   "source": [
16    "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
17    "you may not use this file except in compliance with the License.\n",
18    "You may obtain a copy of the License at\n",
19    "\n",
20    "    http://www.apache.org/licenses/LICENSE-2.0\n",
21    "\n",
22    "Unless required by applicable law or agreed to in writing, software\n",
23    "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
24    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
25    "See the License for the specific language governing permissions and\n",
26    "limitations under the License.\n"
27   ]
28  },
29  {
30   "cell_type": "markdown",
31   "id": "basename",
32   "metadata": {},
33   "source": [
34    "# assignment_task_sizes_mip"
35   ]
36  },
37  {
38   "cell_type": "markdown",
39   "id": "link",
40   "metadata": {},
41   "source": [
42    "<table align=\"left\">\n",
43    "<td>\n",
44    "<a href=\"https://colab.research.google.com/github/google/or-tools/blob/master/examples/notebook/linear_solver/assignment_task_sizes_mip.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/master/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
45    "</td>\n",
46    "<td>\n",
47    "<a href=\"https://github.com/google/or-tools/blob/master/ortools/linear_solver/samples/assignment_task_sizes_mip.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/master/tools/github_32px.png\"/>View source on GitHub</a>\n",
48    "</td>\n",
49    "</table>"
50   ]
51  },
52  {
53   "cell_type": "markdown",
54   "id": "doc",
55   "metadata": {},
56   "source": [
57    "First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
58   ]
59  },
60  {
61   "cell_type": "code",
62   "execution_count": null,
63   "id": "install",
64   "metadata": {},
65   "outputs": [],
66   "source": [
67    "!pip install ortools"
68   ]
69  },
70  {
71   "cell_type": "code",
72   "execution_count": null,
73   "id": "code",
74   "metadata": {},
75   "outputs": [],
76   "source": [
77    "#!/usr/bin/env python3\n",
78    "# Copyright 2010-2021 Google LLC\n",
79    "# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
80    "# you may not use this file except in compliance with the License.\n",
81    "# You may obtain a copy of the License at\n",
82    "#\n",
83    "#     http://www.apache.org/licenses/LICENSE-2.0\n",
84    "#\n",
85    "# Unless required by applicable law or agreed to in writing, software\n",
86    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
87    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
88    "# See the License for the specific language governing permissions and\n",
89    "# limitations under the License.\n",
90    "# [START program]\n",
91    "\"\"\"MIP example that solves an assignment problem.\"\"\"\n",
92    "# [START import]\n",
93    "from ortools.linear_solver import pywraplp\n",
94    "# [END import]\n",
95    "\n",
96    "\n",
97    "# Data\n",
98    "# [START data]\n",
99    "costs = [\n",
100    "    [90, 76, 75, 70, 50, 74, 12, 68],\n",
101    "    [35, 85, 55, 65, 48, 101, 70, 83],\n",
102    "    [125, 95, 90, 105, 59, 120, 36, 73],\n",
103    "    [45, 110, 95, 115, 104, 83, 37, 71],\n",
104    "    [60, 105, 80, 75, 59, 62, 93, 88],\n",
105    "    [45, 65, 110, 95, 47, 31, 81, 34],\n",
106    "    [38, 51, 107, 41, 69, 99, 115, 48],\n",
107    "    [47, 85, 57, 71, 92, 77, 109, 36],\n",
108    "    [39, 63, 97, 49, 118, 56, 92, 61],\n",
109    "    [47, 101, 71, 60, 88, 109, 52, 90],\n",
110    "]\n",
111    "num_workers = len(costs)\n",
112    "num_tasks = len(costs[0])\n",
113    "\n",
114    "task_sizes = [10, 7, 3, 12, 15, 4, 11, 5]\n",
115    "# Maximum total of task sizes for any worker\n",
116    "total_size_max = 15\n",
117    "# [END data]\n",
118    "\n",
119    "# Solver\n",
120    "# [START solver]\n",
121    "# Create the mip solver with the SCIP backend.\n",
122    "solver = pywraplp.Solver.CreateSolver('SCIP')\n",
123    "\n",
124    "# [END solver]\n",
125    "\n",
126    "# Variables\n",
127    "# [START variables]\n",
128    "# x[i, j] is an array of 0-1 variables, which will be 1\n",
129    "# if worker i is assigned to task j.\n",
130    "x = {}\n",
131    "for worker in range(num_workers):\n",
132    "    for task in range(num_tasks):\n",
133    "        x[worker, task] = solver.IntVar(0, 1, f'x[{worker},{task}]')\n",
134    "# [END variables]\n",
135    "\n",
136    "# Constraints\n",
137    "# [START constraints]\n",
138    "# The total size of the tasks each worker takes on is at most total_size_max.\n",
139    "for worker in range(num_workers):\n",
140    "    solver.Add(\n",
141    "        solver.Sum([\n",
142    "            task_sizes[task] * x[worker, task] for task in range(num_tasks)\n",
143    "        ]) <= total_size_max)\n",
144    "\n",
145    "# Each task is assigned to exactly one worker.\n",
146    "for task in range(num_tasks):\n",
147    "    solver.Add(\n",
148    "        solver.Sum([x[worker, task] for worker in range(num_workers)]) == 1)\n",
149    "# [END constraints]\n",
150    "\n",
151    "# Objective\n",
152    "# [START objective]\n",
153    "objective_terms = []\n",
154    "for worker in range(num_workers):\n",
155    "    for task in range(num_tasks):\n",
156    "        objective_terms.append(costs[worker][task] * x[worker, task])\n",
157    "solver.Minimize(solver.Sum(objective_terms))\n",
158    "# [END objective]\n",
159    "\n",
160    "# Solve\n",
161    "# [START solve]\n",
162    "status = solver.Solve()\n",
163    "# [END solve]\n",
164    "\n",
165    "# Print solution.\n",
166    "# [START print_solution]\n",
167    "if status == pywraplp.Solver.OPTIMAL or status == pywraplp.Solver.FEASIBLE:\n",
168    "    print(f'Total cost = {solver.Objective().Value()}\\n')\n",
169    "    for worker in range(num_workers):\n",
170    "        for task in range(num_tasks):\n",
171    "            if x[worker, task].solution_value() > 0.5:\n",
172    "                print(f'Worker {worker} assigned to task {task}.' +\n",
173    "                      f' Cost = {costs[worker][task]}')\n",
174    "else:\n",
175    "    print('No solution found.')\n",
176    "# [END print_solution]\n",
177    "\n"
178   ]
179  }
180 ],
181 "metadata": {},
182 "nbformat": 4,
183 "nbformat_minor": 5
184}
185