2014-07-09 11:17:29 +00:00
|
|
|
# Copyright 2010-2014 Google
|
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
|
|
|
|
|
#
|
2010-10-06 19:46:05 +00:00
|
|
|
# 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.
|
|
|
|
|
|
|
|
|
|
"""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.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
2014-06-13 10:03:03 +00:00
|
|
|
|
2010-10-11 17:39:39 +00:00
|
|
|
from google.apputils import app
|
|
|
|
|
import gflags
|
2013-12-24 11:35:01 +00:00
|
|
|
from ortools.constraint_solver import pywrapcp
|
2010-09-15 12:42:33 +00:00
|
|
|
|
2010-10-11 17:39:39 +00:00
|
|
|
FLAGS = gflags.FLAGS
|
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)
|
2013-10-17 08:58:26 +00:00
|
|
|
# pylint: disable=g-explicit-bool-comparison
|
2010-09-15 12:42:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def main(unused_argv):
|
|
|
|
|
# Create the solver.
|
|
|
|
|
solver = pywrapcp.Solver('golomb ruler')
|
|
|
|
|
|
|
|
|
|
size = 8
|
|
|
|
|
var_max = size * size
|
|
|
|
|
all_vars = range(0, size)
|
|
|
|
|
|
|
|
|
|
marks = [solver.IntVar(0, var_max, 'marks_%d' % i) for i in all_vars]
|
|
|
|
|
|
|
|
|
|
objective = solver.Minimize(marks[size - 1], 1)
|
|
|
|
|
|
|
|
|
|
solver.Add(marks[0] == 0)
|
|
|
|
|
solver.Add(solver.AllDifferent([marks[j] - marks[i]
|
|
|
|
|
for i in range(0, size - 1)
|
2014-05-22 20:13:16 +00:00
|
|
|
for j in range(i + 1, size)]))
|
2010-09-15 12:42:33 +00:00
|
|
|
|
|
|
|
|
solver.Add(marks[size - 1] - marks[size - 2] > marks[1] - marks[0])
|
|
|
|
|
for i in range(0, size - 2):
|
|
|
|
|
solver.Add(marks[i + 1] > marks[i])
|
|
|
|
|
|
|
|
|
|
solution = solver.Assignment()
|
|
|
|
|
solution.Add(marks[size - 1])
|
|
|
|
|
collector = solver.AllSolutionCollector(solution)
|
|
|
|
|
|
|
|
|
|
solver.Solve(solver.Phase(marks,
|
|
|
|
|
solver.CHOOSE_FIRST_UNBOUND,
|
|
|
|
|
solver.ASSIGN_MIN_VALUE),
|
|
|
|
|
[objective, collector])
|
2012-01-21 17:15:03 +00:00
|
|
|
for i in range(0, collector.SolutionCount()):
|
2014-05-22 20:13:16 +00:00
|
|
|
obj_value = collector.Value(i, marks[size - 1])
|
2012-01-21 17:15:03 +00:00
|
|
|
time = collector.WallTime(i)
|
2012-01-21 17:51:53 +00:00
|
|
|
branches = collector.Branches(i)
|
|
|
|
|
failures = collector.Failures(i)
|
2010-09-15 12:42:33 +00:00
|
|
|
print ('Solution #%i: value = %i, failures = %i, branches = %i,'
|
|
|
|
|
'time = %i ms') % (i, obj_value, failures, branches, time)
|
2012-01-21 17:15:03 +00:00
|
|
|
time = solver.WallTime()
|
|
|
|
|
branches = solver.Branches()
|
|
|
|
|
failures = solver.Failures()
|
2010-09-15 12:42:33 +00:00
|
|
|
print ('Total run : failures = %i, branches = %i, time = %i ms' %
|
|
|
|
|
(failures, branches, time))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2010-10-11 17:39:39 +00:00
|
|
|
app.run()
|