188 lines
5.7 KiB
Plaintext
188 lines
5.7 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": [
|
|
"# vendor_scheduling"
|
|
]
|
|
},
|
|
{
|
|
"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/vendor_scheduling.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/vendor_scheduling.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": "code",
|
|
"execution_count": null,
|
|
"id": "code",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from ortools.constraint_solver import pywrapcp\n",
|
|
"\n",
|
|
"\n",
|
|
"def main():\n",
|
|
" # Create the solver.\n",
|
|
" solver = pywrapcp.Solver('Vendors scheduling')\n",
|
|
"\n",
|
|
" #\n",
|
|
" # data\n",
|
|
" #\n",
|
|
" num_vendors = 9\n",
|
|
" num_hours = 10\n",
|
|
" num_work_types = 1\n",
|
|
"\n",
|
|
" trafic = [100, 500, 100, 200, 320, 300, 200, 220, 300, 120]\n",
|
|
" max_trafic_per_vendor = 100\n",
|
|
"\n",
|
|
" # Last columns are :\n",
|
|
" # index_of_the_schedule, sum of worked hours (per work type).\n",
|
|
" # The index is useful for branching.\n",
|
|
" possible_schedules = [[1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 8],\n",
|
|
" [1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 4],\n",
|
|
" [0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 2, 5],\n",
|
|
" [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 3, 4],\n",
|
|
" [1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 4, 3],\n",
|
|
" [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0]]\n",
|
|
"\n",
|
|
" num_possible_schedules = len(possible_schedules)\n",
|
|
" selected_schedules = []\n",
|
|
" vendors_stat = []\n",
|
|
" hours_stat = []\n",
|
|
"\n",
|
|
" #\n",
|
|
" # declare variables\n",
|
|
" #\n",
|
|
" x = {}\n",
|
|
"\n",
|
|
" for i in range(num_vendors):\n",
|
|
" tmp = []\n",
|
|
" for j in range(num_hours):\n",
|
|
" x[i, j] = solver.IntVar(0, num_work_types, 'x[%i,%i]' % (i, j))\n",
|
|
" tmp.append(x[i, j])\n",
|
|
" selected_schedule = solver.IntVar(0, num_possible_schedules - 1,\n",
|
|
" 's[%i]' % i)\n",
|
|
" hours = solver.IntVar(0, num_hours, 'h[%i]' % i)\n",
|
|
" selected_schedules.append(selected_schedule)\n",
|
|
" vendors_stat.append(hours)\n",
|
|
" tmp.append(selected_schedule)\n",
|
|
" tmp.append(hours)\n",
|
|
"\n",
|
|
" solver.Add(solver.AllowedAssignments(tmp, possible_schedules))\n",
|
|
"\n",
|
|
" #\n",
|
|
" # Statistics and constraints for each hour\n",
|
|
" #\n",
|
|
" for j in range(num_hours):\n",
|
|
" workers = solver.Sum([x[i, j] for i in range(num_vendors)]).Var()\n",
|
|
" hours_stat.append(workers)\n",
|
|
" solver.Add(workers * max_trafic_per_vendor >= trafic[j])\n",
|
|
"\n",
|
|
" #\n",
|
|
" # Redundant constraint: sort selected_schedules\n",
|
|
" #\n",
|
|
" for i in range(num_vendors - 1):\n",
|
|
" solver.Add(selected_schedules[i] <= selected_schedules[i + 1])\n",
|
|
"\n",
|
|
" #\n",
|
|
" # Search\n",
|
|
" #\n",
|
|
" db = solver.Phase(selected_schedules, solver.CHOOSE_FIRST_UNBOUND,\n",
|
|
" solver.ASSIGN_MIN_VALUE)\n",
|
|
"\n",
|
|
" solver.NewSearch(db)\n",
|
|
"\n",
|
|
" num_solutions = 0\n",
|
|
" while solver.NextSolution():\n",
|
|
" num_solutions += 1\n",
|
|
"\n",
|
|
" for i in range(num_vendors):\n",
|
|
" print('Vendor %i: ' % i,\n",
|
|
" possible_schedules[selected_schedules[i].Value()])\n",
|
|
" print()\n",
|
|
"\n",
|
|
" print('Statistics per day:')\n",
|
|
" for j in range(num_hours):\n",
|
|
" print('Day%2i: ' % j, end=' ')\n",
|
|
" print(hours_stat[j].Value(), end=' ')\n",
|
|
" print()\n",
|
|
" print()\n",
|
|
"\n",
|
|
" solver.EndSearch()\n",
|
|
" print()\n",
|
|
" print('num_solutions:', num_solutions)\n",
|
|
" print('failures:', solver.Failures())\n",
|
|
" print('branches:', solver.Branches())\n",
|
|
" print('WallTime:', solver.WallTime(), 'ms')\n",
|
|
"\n",
|
|
"\n",
|
|
"main()\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"language_info": {
|
|
"name": "python"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|