151 lines
4.9 KiB
Plaintext
151 lines
4.9 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": [
|
|
"# no_overlap_sample_sat"
|
|
]
|
|
},
|
|
{
|
|
"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/sat/no_overlap_sample_sat.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/ortools/sat/samples/no_overlap_sample_sat.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": "markdown",
|
|
"id": "description",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"Code sample to demonstrate how to build a NoOverlap constraint.\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "code",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from ortools.sat.python import cp_model\n",
|
|
"\n",
|
|
"\n",
|
|
"def no_overlap_sample_sat():\n",
|
|
" \"\"\"No overlap sample with fixed activities.\"\"\"\n",
|
|
" model = cp_model.CpModel()\n",
|
|
" horizon = 21 # 3 weeks.\n",
|
|
"\n",
|
|
" # Task 0, duration 2.\n",
|
|
" start_0 = model.new_int_var(0, horizon, \"start_0\")\n",
|
|
" duration_0 = 2 # Python cp/sat code accepts integer variables or constants.\n",
|
|
" end_0 = model.new_int_var(0, horizon, \"end_0\")\n",
|
|
" task_0 = model.new_interval_var(start_0, duration_0, end_0, \"task_0\")\n",
|
|
" # Task 1, duration 4.\n",
|
|
" start_1 = model.new_int_var(0, horizon, \"start_1\")\n",
|
|
" duration_1 = 4 # Python cp/sat code accepts integer variables or constants.\n",
|
|
" end_1 = model.new_int_var(0, horizon, \"end_1\")\n",
|
|
" task_1 = model.new_interval_var(start_1, duration_1, end_1, \"task_1\")\n",
|
|
"\n",
|
|
" # Task 2, duration 3.\n",
|
|
" start_2 = model.new_int_var(0, horizon, \"start_2\")\n",
|
|
" duration_2 = 3 # Python cp/sat code accepts integer variables or constants.\n",
|
|
" end_2 = model.new_int_var(0, horizon, \"end_2\")\n",
|
|
" task_2 = model.new_interval_var(start_2, duration_2, end_2, \"task_2\")\n",
|
|
"\n",
|
|
" # Weekends.\n",
|
|
" weekend_0 = model.new_interval_var(5, 2, 7, \"weekend_0\")\n",
|
|
" weekend_1 = model.new_interval_var(12, 2, 14, \"weekend_1\")\n",
|
|
" weekend_2 = model.new_interval_var(19, 2, 21, \"weekend_2\")\n",
|
|
"\n",
|
|
" # No Overlap constraint.\n",
|
|
" model.add_no_overlap([task_0, task_1, task_2, weekend_0, weekend_1, weekend_2])\n",
|
|
"\n",
|
|
" # Makespan objective.\n",
|
|
" obj = model.new_int_var(0, horizon, \"makespan\")\n",
|
|
" model.add_max_equality(obj, [end_0, end_1, end_2])\n",
|
|
" model.minimize(obj)\n",
|
|
"\n",
|
|
" # Solve model.\n",
|
|
" solver = cp_model.CpSolver()\n",
|
|
" status = solver.solve(model)\n",
|
|
"\n",
|
|
" if status == cp_model.OPTIMAL:\n",
|
|
" # Print out makespan and the start times for all tasks.\n",
|
|
" print(f\"Optimal Schedule Length: {solver.objective_value}\")\n",
|
|
" print(f\"Task 0 starts at {solver.value(start_0)}\")\n",
|
|
" print(f\"Task 1 starts at {solver.value(start_1)}\")\n",
|
|
" print(f\"Task 2 starts at {solver.value(start_2)}\")\n",
|
|
" else:\n",
|
|
" print(f\"Solver exited with nonoptimal status: {status}\")\n",
|
|
"\n",
|
|
"\n",
|
|
"no_overlap_sample_sat()\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"language_info": {
|
|
"name": "python"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|