175 lines
5.5 KiB
Plaintext
175 lines
5.5 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "google",
|
|
"metadata": {},
|
|
"source": [
|
|
"##### Copyright 2023 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": [
|
|
"# golomb_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/examples/golomb_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/examples/python/golomb_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",
|
|
"This is the Golomb ruler problem.\n",
|
|
"\n",
|
|
"This model aims at maximizing radar interferences in a minimum space.\n",
|
|
"It is known as the Golomb Ruler problem.\n",
|
|
"\n",
|
|
"The idea is to put marks on a rule such that all differences\n",
|
|
"between all marks are all different. The objective is to minimize the length\n",
|
|
"of the rule.\n",
|
|
"see: https://en.wikipedia.org/wiki/Golomb_ruler\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "code",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from typing import Sequence\n",
|
|
"from ortools.sat.colab import flags\n",
|
|
"from google.protobuf import text_format\n",
|
|
"from ortools.sat.python import cp_model\n",
|
|
"\n",
|
|
"_ORDER = flags.define_integer(\"order\", 8, \"Order of the ruler.\")\n",
|
|
"_PARAMS = flags.define_string(\n",
|
|
" \"params\",\n",
|
|
" \"num_search_workers:16,log_search_progress:true,max_time_in_seconds:45\",\n",
|
|
" \"Sat solver parameters.\",\n",
|
|
")\n",
|
|
"\n",
|
|
"\n",
|
|
"def solve_golomb_ruler(order: int, params: str):\n",
|
|
" \"\"\"Solve the Golomb ruler problem.\"\"\"\n",
|
|
" # Create the model.\n",
|
|
" model = cp_model.CpModel()\n",
|
|
"\n",
|
|
" var_max = order * order\n",
|
|
" all_vars = list(range(0, order))\n",
|
|
"\n",
|
|
" marks = [model.new_int_var(0, var_max, f\"marks_{i}\") for i in all_vars]\n",
|
|
"\n",
|
|
" model.add(marks[0] == 0)\n",
|
|
" for i in range(order - 2):\n",
|
|
" model.add(marks[i + 1] > marks[i])\n",
|
|
"\n",
|
|
" diffs = []\n",
|
|
" for i in range(order - 1):\n",
|
|
" for j in range(i + 1, order):\n",
|
|
" diff = model.new_int_var(0, var_max, f\"diff [{j},{i}]\")\n",
|
|
" model.add(diff == marks[j] - marks[i])\n",
|
|
" diffs.append(diff)\n",
|
|
" model.add_all_different(diffs)\n",
|
|
"\n",
|
|
" # symmetry breaking\n",
|
|
" if order > 2:\n",
|
|
" model.add(marks[order - 1] - marks[order - 2] > marks[1] - marks[0])\n",
|
|
"\n",
|
|
" # Objective\n",
|
|
" model.minimize(marks[order - 1])\n",
|
|
"\n",
|
|
" # Solve the model.\n",
|
|
" solver = cp_model.CpSolver()\n",
|
|
" if params:\n",
|
|
" text_format.Parse(params, solver.parameters)\n",
|
|
" solution_printer = cp_model.ObjectiveSolutionPrinter()\n",
|
|
" print(f\"Golomb ruler(order={order})\")\n",
|
|
" status = solver.solve(model, solution_printer)\n",
|
|
"\n",
|
|
" # Print solution.\n",
|
|
" print(f\"status: {solver.status_name(status)}\")\n",
|
|
" if status in (cp_model.OPTIMAL, cp_model.FEASIBLE):\n",
|
|
" for idx, var in enumerate(marks):\n",
|
|
" print(f\"mark[{idx}]: {solver.value(var)}\")\n",
|
|
" intervals = [solver.value(diff) for diff in diffs]\n",
|
|
" intervals.sort()\n",
|
|
" print(f\"intervals: {intervals}\")\n",
|
|
"\n",
|
|
" print(\"Statistics:\")\n",
|
|
" print(f\"- conflicts: {solver.num_conflicts}\")\n",
|
|
" print(f\"- branches : {solver.num_branches}\")\n",
|
|
" print(f\"- wall time: {solver.wall_time}s\\n\")\n",
|
|
"\n",
|
|
"\n",
|
|
"def main(argv: Sequence[str]) -> None:\n",
|
|
" if len(argv) > 1:\n",
|
|
" raise app.UsageError(\"Too many command-line arguments.\")\n",
|
|
" solve_golomb_ruler(_ORDER.value, _PARAMS.value)\n",
|
|
"\n",
|
|
"\n",
|
|
"main()\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|