2022-04-14 14:07:58 +02:00
|
|
|
#!/usr/bin/env python3
|
2022-06-17 08:40:20 +02:00
|
|
|
# Copyright 2010-2022 Google LLC
|
2022-04-14 14:07:58 +02:00
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
|
#
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
#
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
|
# limitations under the License.
|
2023-07-01 06:06:53 +02:00
|
|
|
|
2022-04-14 14:07:58 +02:00
|
|
|
"""This is the Golomb ruler problem.
|
|
|
|
|
|
|
|
|
|
This model aims at maximizing radar interferences in a minimum space.
|
|
|
|
|
It is known as the Golomb Ruler problem.
|
|
|
|
|
|
|
|
|
|
The idea is to put marks on a rule such that all differences
|
|
|
|
|
between all marks are all different. The objective is to minimize the length
|
|
|
|
|
of the rule.
|
|
|
|
|
see: https://en.wikipedia.org/wiki/Golomb_ruler
|
|
|
|
|
"""
|
|
|
|
|
|
2023-01-29 21:20:58 +01:00
|
|
|
from typing import Sequence
|
2022-04-14 14:07:58 +02:00
|
|
|
from absl import app
|
|
|
|
|
from absl import flags
|
|
|
|
|
|
|
|
|
|
from google.protobuf import text_format
|
|
|
|
|
from ortools.sat.python import cp_model
|
|
|
|
|
|
2023-07-01 06:06:53 +02:00
|
|
|
_ORDER = flags.DEFINE_integer("order", 8, "Order of the ruler.")
|
2023-01-29 21:20:58 +01:00
|
|
|
_PARAMS = flags.DEFINE_string(
|
2023-07-01 06:06:53 +02:00
|
|
|
"params",
|
|
|
|
|
"num_search_workers:16,log_search_progress:true,max_time_in_seconds:45",
|
|
|
|
|
"Sat solver parameters.",
|
|
|
|
|
)
|
2022-04-14 14:07:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def solve_golomb_ruler(order, params):
|
2023-01-29 21:20:58 +01:00
|
|
|
"""Solve the Golomb ruler problem."""
|
2022-04-14 14:07:58 +02:00
|
|
|
# Create the model.
|
|
|
|
|
model = cp_model.CpModel()
|
|
|
|
|
|
|
|
|
|
var_max = order * order
|
|
|
|
|
all_vars = list(range(0, order))
|
|
|
|
|
|
2023-07-01 06:06:53 +02:00
|
|
|
marks = [model.NewIntVar(0, var_max, f"marks_{i}") for i in all_vars]
|
2022-04-14 14:07:58 +02:00
|
|
|
|
|
|
|
|
model.Add(marks[0] == 0)
|
|
|
|
|
for i in range(order - 2):
|
|
|
|
|
model.Add(marks[i + 1] > marks[i])
|
|
|
|
|
|
|
|
|
|
diffs = []
|
|
|
|
|
for i in range(order - 1):
|
|
|
|
|
for j in range(i + 1, order):
|
2023-07-01 06:06:53 +02:00
|
|
|
diff = model.NewIntVar(0, var_max, f"diff [{j},{i}]")
|
2022-04-14 14:07:58 +02:00
|
|
|
model.Add(diff == marks[j] - marks[i])
|
|
|
|
|
diffs.append(diff)
|
|
|
|
|
model.AddAllDifferent(diffs)
|
|
|
|
|
|
|
|
|
|
# symmetry breaking
|
|
|
|
|
if order > 2:
|
|
|
|
|
model.Add(marks[order - 1] - marks[order - 2] > marks[1] - marks[0])
|
|
|
|
|
|
|
|
|
|
# Objective
|
|
|
|
|
model.Minimize(marks[order - 1])
|
|
|
|
|
|
|
|
|
|
# Solve the model.
|
|
|
|
|
solver = cp_model.CpSolver()
|
|
|
|
|
if params:
|
|
|
|
|
text_format.Parse(params, solver.parameters)
|
|
|
|
|
solution_printer = cp_model.ObjectiveSolutionPrinter()
|
2023-07-01 06:06:53 +02:00
|
|
|
print(f"Golomb ruler(order={order})")
|
2022-04-14 14:07:58 +02:00
|
|
|
status = solver.Solve(model, solution_printer)
|
|
|
|
|
|
|
|
|
|
# Print solution.
|
2023-07-01 06:06:53 +02:00
|
|
|
print(f"status: {solver.StatusName(status)}")
|
2022-04-14 14:07:58 +02:00
|
|
|
if status in (cp_model.OPTIMAL, cp_model.FEASIBLE):
|
|
|
|
|
for idx, var in enumerate(marks):
|
2023-07-01 06:06:53 +02:00
|
|
|
print(f"mark[{idx}]: {solver.Value(var)}")
|
2022-04-14 14:07:58 +02:00
|
|
|
intervals = [solver.Value(diff) for diff in diffs]
|
|
|
|
|
intervals.sort()
|
2023-07-01 06:06:53 +02:00
|
|
|
print(f"intervals: {intervals}")
|
2022-04-14 14:07:58 +02:00
|
|
|
|
2023-07-01 06:06:53 +02:00
|
|
|
print("Statistics:")
|
|
|
|
|
print(f"- conflicts: {solver.NumConflicts()}")
|
|
|
|
|
print(f"- branches : {solver.NumBranches()}")
|
|
|
|
|
print(f"- wall time: {solver.WallTime()}s\n")
|
2022-04-14 14:07:58 +02:00
|
|
|
|
|
|
|
|
|
2023-01-29 21:20:58 +01:00
|
|
|
def main(argv: Sequence[str]) -> None:
|
|
|
|
|
if len(argv) > 1:
|
2023-07-01 06:06:53 +02:00
|
|
|
raise app.UsageError("Too many command-line arguments.")
|
2023-01-29 21:20:58 +01:00
|
|
|
solve_golomb_ruler(_ORDER.value, _PARAMS.value)
|
2022-04-14 14:07:58 +02:00
|
|
|
|
|
|
|
|
|
2023-07-01 06:06:53 +02:00
|
|
|
if __name__ == "__main__":
|
2022-04-14 14:07:58 +02:00
|
|
|
app.run(main)
|