204 lines
6.8 KiB
Plaintext
204 lines
6.8 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": [
|
|
"# rostering_with_travel"
|
|
]
|
|
},
|
|
{
|
|
"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/rostering_with_travel.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/rostering_with_travel.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.sat.python import cp_model\n",
|
|
"\n",
|
|
"\n",
|
|
"def SolveRosteringWithTravel():\n",
|
|
" model = cp_model.CpModel()\n",
|
|
"\n",
|
|
" # [duration, start, end, location]\n",
|
|
" jobs = [[3, 0, 6, 1], [5, 0, 6, 0], [1, 3, 7, 1], [1, 3, 5, 0], [3, 0, 3, 0],\n",
|
|
" [3, 0, 8, 0]]\n",
|
|
"\n",
|
|
" max_length = 20\n",
|
|
"\n",
|
|
" num_machines = 3\n",
|
|
" all_machines = range(num_machines)\n",
|
|
"\n",
|
|
" horizon = 20\n",
|
|
" travel_time = 1\n",
|
|
" num_jobs = len(jobs)\n",
|
|
" all_jobs = range(num_jobs)\n",
|
|
"\n",
|
|
" intervals = []\n",
|
|
" optional_intervals = []\n",
|
|
" performed = []\n",
|
|
" starts = []\n",
|
|
" ends = []\n",
|
|
" travels = []\n",
|
|
"\n",
|
|
" for m in all_machines:\n",
|
|
" optional_intervals.append([])\n",
|
|
"\n",
|
|
" for i in all_jobs:\n",
|
|
" # Create main interval.\n",
|
|
" start = model.NewIntVar(jobs[i][1], horizon, 'start_%i' % i)\n",
|
|
" duration = jobs[i][0]\n",
|
|
" end = model.NewIntVar(0, jobs[i][2], 'end_%i' % i)\n",
|
|
" interval = model.NewIntervalVar(start, duration, end, 'interval_%i' % i)\n",
|
|
" starts.append(start)\n",
|
|
" intervals.append(interval)\n",
|
|
" ends.append(end)\n",
|
|
"\n",
|
|
" job_performed = []\n",
|
|
" job_travels = []\n",
|
|
" for m in all_machines:\n",
|
|
" performed_on_m = model.NewBoolVar('perform_%i_on_m%i' % (i, m))\n",
|
|
" job_performed.append(performed_on_m)\n",
|
|
"\n",
|
|
" # Create an optional copy of interval to be executed on a machine\n",
|
|
" location0 = model.NewIntVar(jobs[i][3], jobs[i][3],\n",
|
|
" 'location_%i_on_m%i' % (i, m))\n",
|
|
" start0 = model.NewIntVar(jobs[i][1], horizon, 'start_%i_on_m%i' % (i, m))\n",
|
|
" end0 = model.NewIntVar(0, jobs[i][2], 'end_%i_on_m%i' % (i, m))\n",
|
|
" interval0 = model.NewOptionalIntervalVar(\n",
|
|
" start0, duration, end0, performed_on_m, 'interval_%i_on_m%i' % (i, m))\n",
|
|
" optional_intervals[m].append(interval0)\n",
|
|
"\n",
|
|
" # We only propagate the constraint if the tasks is performed on the machine.\n",
|
|
" model.Add(start0 == start).OnlyEnforceIf(performed_on_m)\n",
|
|
" # Adding travel constraint\n",
|
|
" travel = model.NewBoolVar('is_travel_%i_on_m%i' % (i, m))\n",
|
|
" startT = model.NewIntVar(0, horizon, 'start_%i_on_m%i' % (i, m))\n",
|
|
" endT = model.NewIntVar(0, horizon, 'end_%i_on_m%i' % (i, m))\n",
|
|
" intervalT = model.NewOptionalIntervalVar(\n",
|
|
" startT, travel_time, endT, travel,\n",
|
|
" 'travel_interval_%i_on_m%i' % (i, m))\n",
|
|
" optional_intervals[m].append(intervalT)\n",
|
|
" job_travels.append(travel)\n",
|
|
"\n",
|
|
" model.Add(end0 == startT).OnlyEnforceIf(travel)\n",
|
|
"\n",
|
|
" performed.append(job_performed)\n",
|
|
" travels.append(job_travels)\n",
|
|
"\n",
|
|
" model.Add(sum(job_performed) == 1)\n",
|
|
"\n",
|
|
" for m in all_machines:\n",
|
|
" if m == 1:\n",
|
|
" for i in all_jobs:\n",
|
|
" if i == 2:\n",
|
|
" for c in all_jobs:\n",
|
|
" if (i != c) and (jobs[i][3] != jobs[c][3]):\n",
|
|
" is_job_earlier = model.NewBoolVar('is_j%i_earlier_j%i' % (i, c))\n",
|
|
" model.Add(starts[i] < starts[c]).OnlyEnforceIf(is_job_earlier)\n",
|
|
" model.Add(starts[i] >= starts[c]).OnlyEnforceIf(\n",
|
|
" is_job_earlier.Not())\n",
|
|
"\n",
|
|
" # Max Length constraint (modeled as a cumulative)\n",
|
|
" # model.AddCumulative(intervals, demands, max_length)\n",
|
|
"\n",
|
|
" # Choose which machine to perform the jobs on.\n",
|
|
" for m in all_machines:\n",
|
|
" model.AddNoOverlap(optional_intervals[m])\n",
|
|
"\n",
|
|
" # Objective variable.\n",
|
|
" total_cost = model.NewIntVar(0, 1000, 'cost')\n",
|
|
" model.Add(total_cost == sum(\n",
|
|
" performed[j][m] * (10 * (m + 1)) for j in all_jobs for m in all_machines))\n",
|
|
" model.Minimize(total_cost)\n",
|
|
"\n",
|
|
" # Solve model.\n",
|
|
" solver = cp_model.CpSolver()\n",
|
|
" result = solver.Solve(model)\n",
|
|
"\n",
|
|
" print()\n",
|
|
" print(result)\n",
|
|
" print('Statistics')\n",
|
|
" print(' - conflicts : %i' % solver.NumConflicts())\n",
|
|
" print(' - branches : %i' % solver.NumBranches())\n",
|
|
" print(' - wall time : %f ms' % solver.WallTime())\n",
|
|
"\n",
|
|
"\n",
|
|
"def main():\n",
|
|
" SolveRosteringWithTravel()\n",
|
|
"\n",
|
|
"\n",
|
|
"main()\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"language_info": {
|
|
"name": "python"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|