Files
ortools-clone/examples/python/golomb8.py

88 lines
2.7 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/env python3
2025-01-10 11:35:44 +01:00
# Copyright 2010-2025 Google LLC
2010-09-15 12:42:33 +00: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
2010-09-15 12:42:33 +00:00
#
# 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
2010-09-15 12:42:33 +00: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.
"""
from absl import app
from ortools.constraint_solver import pywrapcp
2010-09-15 12:42:33 +00:00
# We disable the following warning because it is a false positive on constraints
# like: solver.Add(x == 0)
# pylint: disable=g-explicit-bool-comparison
2010-09-15 12:42:33 +00:00
def main(_) -> None:
# Create the solver.
2023-07-01 06:06:53 +02:00
solver = pywrapcp.Solver("golomb ruler")
2010-09-15 12:42:33 +00:00
size = 8
var_max = size * size
all_vars = list(range(0, size))
2023-07-01 06:06:53 +02:00
marks = [solver.IntVar(0, var_max, "marks_%d" % i) for i in all_vars]
2010-09-15 12:42:33 +00:00
objective = solver.Minimize(marks[size - 1], 1)
2010-09-15 12:42:33 +00:00
solver.Add(marks[0] == 0)
# We expand the creation of the diff array to avoid a pylint warning.
diffs = []
for i in range(size - 1):
for j in range(i + 1, size):
diffs.append(marks[j] - marks[i])
solver.Add(solver.AllDifferent(diffs))
2010-09-15 12:42:33 +00:00
solver.Add(marks[size - 1] - marks[size - 2] > marks[1] - marks[0])
for i in range(size - 2):
solver.Add(marks[i + 1] > marks[i])
2010-09-15 12:42:33 +00:00
solution = solver.Assignment()
solution.Add(marks[size - 1])
collector = solver.AllSolutionCollector(solution)
2010-09-15 12:42:33 +00:00
solver.Solve(
2023-07-01 06:06:53 +02:00
solver.Phase(marks, solver.CHOOSE_FIRST_UNBOUND, solver.ASSIGN_MIN_VALUE),
[objective, collector],
)
for i in range(0, collector.SolutionCount()):
obj_value = collector.Value(i, marks[size - 1])
time = collector.WallTime(i)
branches = collector.Branches(i)
failures = collector.Failures(i)
2023-07-01 06:06:53 +02:00
print(
2025-07-23 17:38:49 +02:00
"Solution #%i: value = %i, failures = %i, branches = %i,time = %i ms"
2023-07-01 06:06:53 +02:00
% (i, obj_value, failures, branches, time)
)
time = solver.WallTime()
branches = solver.Branches()
failures = solver.Failures()
2023-07-01 06:06:53 +02:00
print(
(
"Total run : failures = %i, branches = %i, time = %i ms"
% (failures, branches, time)
)
)
2010-09-15 12:42:33 +00:00
2023-07-01 06:06:53 +02:00
if __name__ == "__main__":
app.run(main)