2021-05-03 12:11:39 +02:00
|
|
|
#!/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
|
|
|
|
|
#
|
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.
|
2023-07-01 06:06:53 +02:00
|
|
|
|
2010-09-15 12:42:33 +00:00
|
|
|
"""Magic sequence problem.
|
|
|
|
|
|
|
|
|
|
This models aims at building a sequence of numbers such that the number of
|
|
|
|
|
occurrences of i in this sequence is equal to the value of the ith number.
|
|
|
|
|
It uses an aggregated formulation of the count expression called
|
|
|
|
|
distribute().
|
2020-11-18 10:50:14 +01:00
|
|
|
|
|
|
|
|
Usage: python magic_sequence_distribute.py NUMBER
|
2010-09-15 12:42:33 +00:00
|
|
|
"""
|
|
|
|
|
|
2020-11-18 10:50:14 +01:00
|
|
|
from absl import app
|
|
|
|
|
from absl import flags
|
2013-12-24 11:35:01 +00:00
|
|
|
from ortools.constraint_solver import pywrapcp
|
2010-09-15 12:42:33 +00:00
|
|
|
|
2020-11-18 10:50:14 +01:00
|
|
|
FLAGS = flags.FLAGS
|
|
|
|
|
|
2010-09-15 12:42:33 +00:00
|
|
|
|
2020-11-18 10:50:14 +01:00
|
|
|
def main(argv):
|
2018-11-11 09:39:59 +01:00
|
|
|
# Create the solver.
|
2023-07-01 06:06:53 +02:00
|
|
|
solver = pywrapcp.Solver("magic sequence")
|
2010-09-15 12:42:33 +00:00
|
|
|
|
2020-11-18 10:50:14 +01:00
|
|
|
# Create an array of IntVars to hold the answers.
|
|
|
|
|
size = int(argv[1]) if len(argv) > 1 else 100
|
2018-11-11 09:39:59 +01:00
|
|
|
all_values = list(range(0, size))
|
2023-07-01 06:06:53 +02:00
|
|
|
all_vars = [solver.IntVar(0, size, "vars_%d" % i) for i in all_values]
|
2010-09-15 12:42:33 +00:00
|
|
|
|
2020-11-18 10:50:14 +01:00
|
|
|
# The number of variables equal to j shall be the value of all_vars[j].
|
2018-11-11 09:39:59 +01:00
|
|
|
solver.Add(solver.Distribute(all_vars, all_values, all_vars))
|
2020-11-18 10:50:14 +01:00
|
|
|
|
|
|
|
|
# The sum of all the values shall be equal to the size.
|
|
|
|
|
# (This constraint is redundant, but speeds up the search.)
|
2018-11-11 09:39:59 +01:00
|
|
|
solver.Add(solver.Sum(all_vars) == size)
|
2010-09-15 12:42:33 +00:00
|
|
|
|
2018-11-11 09:39:59 +01:00
|
|
|
solver.NewSearch(
|
2023-07-01 06:06:53 +02:00
|
|
|
solver.Phase(all_vars, solver.CHOOSE_FIRST_UNBOUND, solver.ASSIGN_MIN_VALUE)
|
|
|
|
|
)
|
2018-11-11 09:39:59 +01:00
|
|
|
solver.NextSolution()
|
|
|
|
|
print(all_vars)
|
|
|
|
|
solver.EndSearch()
|
2010-09-15 12:42:33 +00:00
|
|
|
|
|
|
|
|
|
2023-07-01 06:06:53 +02:00
|
|
|
if __name__ == "__main__":
|
2020-11-18 10:50:14 +01:00
|
|
|
app.run(main)
|