Repository navigation
Expand file tree
/
Copy pathgolomb8.py
More file actions
executable file
·92 lines (74 loc) · 2.82 KB
/
Copy pathgolomb8.py
File metadata and controls
executable file
·92 lines (74 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python3
# Copyright 2010-2025 Google LLC
# 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.
"""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.python import constraint_solver as cp
# We disable the following warning because it is a false positive on constraints
# like: solver.add(x == 0)
# pylint: disable=g-explicit-bool-comparison
def main(_) -> None:
# Create the solver.
solver = cp.Solver("golomb ruler")
size = 8
var_max = size * size
all_vars = list(range(0, size))
marks = [solver.new_int_var(0, var_max, "marks_%d" % i) for i in all_vars]
objective = solver.minimize(marks[size - 1], 1)
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_all_different(diffs)
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])
solution = solver.assignment()
solution.add(marks[size - 1])
collector = solver.all_solution_collector(solution)
solver.solve(
solver.phase(
marks,
cp.IntVarStrategy.CHOOSE_FIRST_UNBOUND,
cp.IntValueStrategy.ASSIGN_MIN_VALUE,
),
[objective, collector],
)
for i in range(0, collector.solution_count):
obj_value = collector.value(i, marks[size - 1])
time = collector.wall_time_ms(i)
branches = collector.branches(i)
failures = collector.failures(i)
print(
"Solution #%i: value = %i, failures = %i, branches = %i,time = %i ms"
% (i, obj_value, failures, branches, time)
)
time = solver.wall_time_ms
branches = solver.num_branches
failures = solver.num_failures
print(
(
"Total run : failures = %i, branches = %i, time = %i ms"
% (failures, branches, time)
)
)
if __name__ == "__main__":
app.run(main)