- \(\color{darkred}x_i \in \{0,1,2,\dots\}\),
- \(\color{darkred}x_i\) is equal to the number of times the value \(i\) occurs in the series. In mathematical terms, one could say: \[\color{darkred}x_i = |\{j:\color{darkred}x_j=i\}|\] (here \(|S|\) is the cardinality of the set \(S\)).
index 0 1 2 3 4value 2 1 2 0 0
for i in I:mdl.add(x[i] == sum(x[j] == i for j in I))
In a MIP model, when we need to do some counting, use binary variables
We therefore introduce binary variables \(\color{darkred}y_{i,j}\in \{0,1\}\), defined by \[\color{darkred}y_{i,j}=1 \iff\color{darkred}x_i=j\] We can do this by: \[\begin{align}&\color{darkred}x_i = \sum_j j\cdot\color{darkred}y_{i,j} \\ & \sum_j \color{darkred}y_{i,j} = 1\end{align}\] The counting of occurrences can now be stated as: \[\color{darkred}x_i = \sum_j \color{darkred}y_{j,i}\] These three linear constraints now form our MIP model. Note that the variables \(\color{darkred}x_i\) can be substituted out (they can be recovered afterward from the optimal levels of \(\color{darkred}y_{i,j}\)). So, the remaining linear model is:
| Linear MIP constraints |
|---|
| \[\begin{align}& \sum_j j\cdot\color{darkred}y_{i,j} = \sum_j \color{darkred}y_{j,i} && \forall i \\ & \sum_j \color{darkred}y_{i,j} = 1 && \forall i \\ & \color{darkred}y_{i,j}\in \{0,1\} \end{align}\] |
The solution looks like:
---- 30 SET isize of problem
i0, i1, i2, i3, i4
---- 30 VARIABLE y.L
i0 i1 i2
i0 1.000
i1 1.000
i2 1.000
i3 1.000
i4 1.000
---- 30 PARAMETER x
i0 2.000, i1 1.000, i2 2.000
- Not all \(n\) yield a feasible solution. E.g. \(n=0,1,2,5\) are infeasible.
- But a feasible solution for any \(n\ge 6\) is:
index 0 1 2 n-3
value n-3 2 1 1
The zero values are not printed here. We see that the solution is very sparse. Of course, this general solution makes the problem slightly less interesting. - The solutions seem unique. A no-good cut that forbids a previously found solution \(\color{darkblue}y^*_{i,j}\) is \[\sum_{i,j} \color{darkblue}y^*_{i,j}\cdot \color{darkred}y_{i,j} \le \color{darkblue}n\] When running with this additional constraint, the model becomes infeasible, indicating there is no other solution.
Conclusion
References
- Simple Integer Optimization Problem: docplex CP model works but equivalent PuLP+CBC model is infeasible?, https://or.stackexchange.com/questions/9328/simple-integer-optimization-problem-docplex-cp-model-works-but-equivalent-pulp
Appendix: GAMS model
set |