Magic sequences
Description: Solving magic sequences through reinforced formulations and constrained programming. Some comparison between models and solvers is done, and we look into the “Another solution” problem for these sequences.
Tags: constraint-programming, educational, mp, sequences, arithmetic, reinforced-formulations, highs, gecode, cbc, mip
Notebook author: Marcos Dominguez Velad <marcos@ampl.com>
Description
A Magic sequence is a sequence of length $n$ is a sequence of integers $s_0 … s_{n-1}$ such that for all $i$ in 0 to $n-1$, the number $i$ occurs exactly $s_i$ times in the sequence. For instance, 6,2,1,0,0,0,1,0,0,0 is a magic sequence since 0 occurs 6 times in it, 1 occurs twice, etc.
This problem was first proposed in CSPLib by Toby Walsh (https://www.csplib.org/Problems/prob019/), and modelled in AMPL by Hakank (check also his amazing repository of models! http://www.hakank.org/ampl/). Sequences that are self-defined are also known as “autograms”.
In this notebook, we are reviewing the model for the magic sequences problem, how to enhance the model to solve larger instances. Finally, we are trying to reply computationally to the “Another Solution Problem” looking for alternatives sequences for a given length.
Better models
Whilst in the previous section we proposed a one-constraint formulation to solve the model, adding more constraints could be helpful in order to speedup the solving process.
Although these constraints are redundant for us, they are not for the solvers. It can be proven that given a magic sequence of n elements, there a couple properties it satisfies:
$$\sum \limits_{i = 0}^{n-1} s_i = n$$
Moreover,
$$\sum \limits_{i = 0}^{n-1} i \cdot s_i = n$$
(Remember that for we start counting from zero)
Then, we propose the reinforced formulation:
Comparing times
There is timeit
from python to easily compare the time between 2 functions or snippets of code. seq0.mod
is the basic model to solve the problem. seqr.mod
is the reinforced formulation.
33.5 ms ± 8.3 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
17.5 ms ± 2.09 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
Let’s plot the time difference.
Testing for bigger values of n
, it is notable how the basic model struggles, so we only run it up to n=30
(feel free to tune and test this values).
Alternative solutions?
In Computer Science, it is common to look for alternative solutions of the same problem. Given n
, is there more than one magic sequence of n
elements?
We should proof this formally, but software can be helpful to make an idea of the answer.
When solving the problem, we are looking for any feasible solution in the search space regardless an objective (there is no objective!). We could write an artificial objective function to navigate the search space looking for solutions following a certain pattern in order to solve the “Another solution problem”.
In this case we can write a natural objective. As we have already found a solution, we can look for another one that is completely different. If $s0$ is a solution for the problem, we could search for $s$ such that:
$$\max \quad \sum \limits_{i = 0}^{n-1} |s_i-s0_i|$$
(in AMPL)
maximize f: sum{i in 0..n-1} abs(s[i]-s0[i]);
Where $|\cdot|$ is the absolute value (other vector norms could be used). The absolute value becomes linearize so MIP and CP solvers can solve the problem.
the only feasible solution with optimal value 0 is the current solucion $s0$, so another solution will give a greater optimal value.
seq_alt.mod
contains the new model to found alternative solutions given a magic sequence.
Looking for alternative solutions up to $n=25$.
No new solutions for n = 1
No new solutions for n = 2
No new solutions for n = 3
n = 4 ==> 2 0 2 0
n = 4 ==> 1 2 1 0
No new solutions for n = 5
No new solutions for n = 6
No new solutions for n = 7
No new solutions for n = 8
No new solutions for n = 9
No new solutions for n = 10
No new solutions for n = 11
No new solutions for n = 12
No new solutions for n = 13
No new solutions for n = 14
No new solutions for n = 15
No new solutions for n = 16
No new solutions for n = 17
No new solutions for n = 18
No new solutions for n = 19
No new solutions for n = 20
No new solutions for n = 21
No new solutions for n = 22
No new solutions for n = 23
No new solutions for n = 24
It seems that there is no other solution for any value of n
(except of $n=4$).