{ "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": [ "# nurses_sat" ] }, { "cell_type": "markdown", "id": "link", "metadata": {}, "source": [ "\n", "\n", "\n", "
\n", "Run in Google Colab\n", "\n", "View source on GitHub\n", "
" ] }, { "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": "markdown", "id": "description", "metadata": {}, "source": [ "\n", "Example of a simple nurse scheduling problem." ] }, { "cell_type": "code", "execution_count": null, "id": "code", "metadata": {}, "outputs": [], "source": [ "from ortools.sat.python import cp_model\n", "\n", "\n", "\n", "def main() -> None:\n", " # Data.\n", " num_nurses = 4\n", " num_shifts = 3\n", " num_days = 3\n", " all_nurses = range(num_nurses)\n", " all_shifts = range(num_shifts)\n", " all_days = range(num_days)\n", "\n", " # Creates the model.\n", " model = cp_model.CpModel()\n", "\n", " # Creates shift variables.\n", " # shifts[(n, d, s)]: nurse 'n' works shift 's' on day 'd'.\n", " shifts = {}\n", " for n in all_nurses:\n", " for d in all_days:\n", " for s in all_shifts:\n", " shifts[(n, d, s)] = model.new_bool_var(f\"shift_n{n}_d{d}_s{s}\")\n", "\n", " # Each shift is assigned to exactly one nurse in the schedule period.\n", " for d in all_days:\n", " for s in all_shifts:\n", " model.add_exactly_one(shifts[(n, d, s)] for n in all_nurses)\n", "\n", " # Each nurse works at most one shift per day.\n", " for n in all_nurses:\n", " for d in all_days:\n", " model.add_at_most_one(shifts[(n, d, s)] for s in all_shifts)\n", "\n", " # Try to distribute the shifts evenly, so that each nurse works\n", " # min_shifts_per_nurse shifts. If this is not possible, because the total\n", " # number of shifts is not divisible by the number of nurses, some nurses will\n", " # be assigned one more shift.\n", " min_shifts_per_nurse = (num_shifts * num_days) // num_nurses\n", " if num_shifts * num_days % num_nurses == 0:\n", " max_shifts_per_nurse = min_shifts_per_nurse\n", " else:\n", " max_shifts_per_nurse = min_shifts_per_nurse + 1\n", " for n in all_nurses:\n", " shifts_worked = []\n", " for d in all_days:\n", " for s in all_shifts:\n", " shifts_worked.append(shifts[(n, d, s)])\n", " model.add(min_shifts_per_nurse <= sum(shifts_worked))\n", " model.add(sum(shifts_worked) <= max_shifts_per_nurse)\n", "\n", " # Creates the solver and solve.\n", " solver = cp_model.CpSolver()\n", " solver.parameters.linearization_level = 0\n", " # Enumerate all solutions.\n", " solver.parameters.enumerate_all_solutions = True\n", "\n", " class NursesPartialSolutionPrinter(cp_model.CpSolverSolutionCallback):\n", " \"\"\"Print intermediate solutions.\"\"\"\n", "\n", " def __init__(self, shifts, num_nurses, num_days, num_shifts, limit):\n", " cp_model.CpSolverSolutionCallback.__init__(self)\n", " self._shifts = shifts\n", " self._num_nurses = num_nurses\n", " self._num_days = num_days\n", " self._num_shifts = num_shifts\n", " self._solution_count = 0\n", " self._solution_limit = limit\n", "\n", " def on_solution_callback(self):\n", " self._solution_count += 1\n", " print(f\"Solution {self._solution_count}\")\n", " for d in range(self._num_days):\n", " print(f\"Day {d}\")\n", " for n in range(self._num_nurses):\n", " is_working = False\n", " for s in range(self._num_shifts):\n", " if self.value(self._shifts[(n, d, s)]):\n", " is_working = True\n", " print(f\" Nurse {n} works shift {s}\")\n", " if not is_working:\n", " print(f\" Nurse {n} does not work\")\n", " if self._solution_count >= self._solution_limit:\n", " print(f\"Stop search after {self._solution_limit} solutions\")\n", " self.stop_search()\n", "\n", " def solutionCount(self):\n", " return self._solution_count\n", "\n", " # Display the first five solutions.\n", " solution_limit = 5\n", " solution_printer = NursesPartialSolutionPrinter(\n", " shifts, num_nurses, num_days, num_shifts, solution_limit\n", " )\n", "\n", " solver.solve(model, solution_printer)\n", "\n", " # Statistics.\n", " print(\"\\nStatistics\")\n", " print(f\" - conflicts : {solver.num_conflicts}\")\n", " print(f\" - branches : {solver.num_branches}\")\n", " print(f\" - wall time : {solver.wall_time} s\")\n", " print(f\" - solutions found: {solution_printer.solutionCount()}\")\n", "\n", "\n", "main()\n", "\n" ] } ], "metadata": { "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 5 }