209 lines
6.8 KiB
Plaintext
209 lines
6.8 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "google",
|
|
"metadata": {},
|
|
"source": [
|
|
"##### Copyright 2021 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": [
|
|
"# post_office_problem2"
|
|
]
|
|
},
|
|
{
|
|
"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/post_office_problem2.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/post_office_problem2.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": "code",
|
|
"execution_count": null,
|
|
"id": "code",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Copyright 2010 Hakan Kjellerstrand hakank@gmail.com\n",
|
|
"#\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",
|
|
"\"\"\"\n",
|
|
"\n",
|
|
" Post office problem in Google CP Solver.\n",
|
|
"\n",
|
|
" Problem statement:\n",
|
|
" http://www-128.ibm.com/developerworks/linux/library/l-glpk2/\n",
|
|
"\n",
|
|
" From Winston 'Operations Research: Applications and Algorithms':\n",
|
|
" '''\n",
|
|
" A post office requires a different number of full-time employees working\n",
|
|
" on different days of the week [summarized below]. Union rules state that\n",
|
|
" each full-time employee must work for 5 consecutive days and then receive\n",
|
|
" two days off. For example, an employee who works on Monday to Friday\n",
|
|
" must be off on Saturday and Sunday. The post office wants to meet its\n",
|
|
" daily requirements using only full-time employees. Minimize the number\n",
|
|
" of employees that must be hired.\n",
|
|
"\n",
|
|
" To summarize the important information about the problem:\n",
|
|
"\n",
|
|
" * Every full-time worker works for 5 consecutive days and takes 2 days off\n",
|
|
" * Day 1 (Monday): 17 workers needed\n",
|
|
" * Day 2 : 13 workers needed\n",
|
|
" * Day 3 : 15 workers needed\n",
|
|
" * Day 4 : 19 workers needed\n",
|
|
" * Day 5 : 14 workers needed\n",
|
|
" * Day 6 : 16 workers needed\n",
|
|
" * Day 7 (Sunday) : 11 workers needed\n",
|
|
"\n",
|
|
" The post office needs to minimize the number of employees it needs\n",
|
|
" to hire to meet its demand.\n",
|
|
" '''\n",
|
|
"\n",
|
|
" * MiniZinc: http://www.hakank.org/minizinc/post_office_problem2.mzn\n",
|
|
" * SICStus: http://www.hakank.org/sicstus/post_office_problem2.pl\n",
|
|
" * ECLiPSe: http://www.hakank.org/eclipse/post_office_problem2.ecl\n",
|
|
" * Gecode: http://www.hakank.org/gecode/post_office_problem2.cpp\n",
|
|
"\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",
|
|
"\"\"\"\n",
|
|
"from ortools.constraint_solver import pywrapcp\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"# Create the solver.\n",
|
|
"solver = pywrapcp.Solver('Post office problem')\n",
|
|
"\n",
|
|
"#\n",
|
|
"# data\n",
|
|
"#\n",
|
|
"\n",
|
|
"# days 0..6, monday 0\n",
|
|
"n = 7\n",
|
|
"days = list(range(n))\n",
|
|
"need = [17, 13, 15, 19, 14, 16, 11]\n",
|
|
"\n",
|
|
"# Total cost for the 5 day schedule.\n",
|
|
"# Base cost per day is 100.\n",
|
|
"# Working saturday is 100 extra\n",
|
|
"# Working sunday is 200 extra.\n",
|
|
"cost = [500, 600, 800, 800, 800, 800, 700]\n",
|
|
"\n",
|
|
"#\n",
|
|
"# variables\n",
|
|
"#\n",
|
|
"\n",
|
|
"# No. of workers starting at day i\n",
|
|
"x = [solver.IntVar(0, 100, 'x[%i]' % i) for i in days]\n",
|
|
"\n",
|
|
"total_cost = solver.IntVar(0, 20000, 'total_cost')\n",
|
|
"num_workers = solver.IntVar(0, 100, 'num_workers')\n",
|
|
"\n",
|
|
"#\n",
|
|
"# constraints\n",
|
|
"#\n",
|
|
"solver.Add(total_cost == solver.ScalProd(x, cost))\n",
|
|
"solver.Add(num_workers == solver.Sum(x))\n",
|
|
"\n",
|
|
"for i in days:\n",
|
|
" s = solver.Sum(\n",
|
|
" [x[j] for j in days if j != (i + 5) % n and j != (i + 6) % n])\n",
|
|
" solver.Add(s >= need[i])\n",
|
|
"\n",
|
|
"# objective\n",
|
|
"objective = solver.Minimize(total_cost, 1)\n",
|
|
"\n",
|
|
"#\n",
|
|
"# search and result\n",
|
|
"#\n",
|
|
"db = solver.Phase(x, solver.CHOOSE_MIN_SIZE_LOWEST_MIN,\n",
|
|
" solver.ASSIGN_MIN_VALUE)\n",
|
|
"\n",
|
|
"solver.NewSearch(db, [objective])\n",
|
|
"\n",
|
|
"num_solutions = 0\n",
|
|
"\n",
|
|
"while solver.NextSolution():\n",
|
|
" num_solutions += 1\n",
|
|
" print('num_workers:', num_workers.Value())\n",
|
|
" print('total_cost:', total_cost.Value())\n",
|
|
" print('x:', [x[i].Value() for i in days])\n",
|
|
"\n",
|
|
"solver.EndSearch()\n",
|
|
"\n",
|
|
"print()\n",
|
|
"print('num_solutions:', num_solutions)\n",
|
|
"print('failures:', solver.Failures())\n",
|
|
"print('branches:', solver.Branches())\n",
|
|
"print('WallTime:', solver.WallTime())\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|