Added argument parsing
@@ -15,7 +15,7 @@ interval appears. The problem of finding such a series can be easily
formulated as an instance of a more general arithmetic problem on Z_n,
the set of integer residues modulo n. Given n in N, find a vector
s = (s_1, ..., s_n), such that (i) s is a permutation of
-Z_n = {0,1,...,n-1}; and (ii) the interval vector
+Z_n = {1,2,...,n}; and (ii) the interval vector
v = (|s_2-s_1|, |s_3-s_2|, ... |s_n-s_{n-1}|) is a permutation of
Z_n-{0} = {1,2,...,n-1}. A vector v satisfying these conditions is
called an all-interval series of size n; the problem of finding such
@@ -29,11 +29,13 @@ See also my cpmpy page: http://www.hakank.org/cpmpy/
Modified by Ignace Bleukx
"""
-import sys
+import argparse
+
from cpmpy import *
import numpy as np
-def all_interval(n=12,num_sols=0):
+
+def all_interval(n=12):
# Create the solver.
model = Model()
@@ -57,17 +59,18 @@ def all_interval(n=12,num_sols=0):
def print_solution(x, diffs):
print(f"x: {x.value()}")
print(f"diffs: {diffs.value()}")
+ print()
if __name__ == "__main__":
- n = 12
- if len(sys.argv) > 1:
- n = int(sys.argv[1])
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument("-n", type=int,help="Length of array", default=12, required=False)
+ parser.add_argument("-num_sols", type=int, help="Number of solutions to find, find all by default", default=0, required=False)
- num_sols = 0 # find all solutions
+ args = parser.parse_args()
- model, (x, diffs) = all_interval(n)
- n = model.solveAll(solution_limit=num_sols,
+ model, (x, diffs) = all_interval(args.n)
+ found_n = model.solveAll(solution_limit=args.num_sols,
display=lambda: print_solution(x, diffs))
- print(f"Found {n} solutions")
\ No newline at end of file
+ print(f"Found {found_n} solutions")
\ No newline at end of file
