Cardinality Constraints X37043


Statement
 

optilog

pdf   zip

Write a program in Python that, using the optilog library, finds all solutions of a set of cardinality constraints.

image

A set of cardinality constraints is a set of inequalities like: x1+x21x2+x3+x42x1+x2+x3+x42\begin{array}{l} x_1 + x_2 \geq 1\\ x_2 + x_3 + x_4 \geq 2\\ x_1 + x_2 + x_3 + x_4 \leq 2 \end{array}

They can be translated into SAT using sequential counters: S11x1SiiSi1i1xii=2,,kSi1Si11xii=2,,nSijSi1j(xiSi1j1)i=3,,n,j=2,,min{i1,k}\begin{array}{ll} S_1^1 \leftrightarrow x_1\\ S_i^i \leftrightarrow S_{i-1}^{i-1} \wedge x_i& i=2,\dots,k\\ S_i^1 \leftrightarrow S_{i-1}^1 \vee x_i& i=2,\dots,n\\ S_i^j \leftrightarrow S_{i-1}^j \vee (x_i \wedge S_{i-1}^{j-1})& i=3,\dots,n, j=2,\dots,\min\{i-1,k\} \end{array}

The clause SnkS_n^k plus the clauses defining the right implications of the previous equivalences enforce x1++xnkx_1+\cdots+x_n\geq k. In the last equivalence, for instance, the clauses are: SijSi1j(xiSi1j1){Sij¯Si1jSi1j1Sij¯Si1j1Sij¯Si1jxiS_i^j \to S_{i-1}^j \vee (x_i \wedge S_{i-1}^{j-1})\ \equiv\ \left\{ \begin{array}{l} \overline{S_i^j}\vee S_{i-1}^j\vee S_{i-1}^{j-1}\ \equiv\ \overline{S_i^j}\vee S_{i-1}^{j-1}\\ \overline{S_i^j}\vee S_{i-1}^j\vee x_i \end{array} \right.

The constraint x1++xnkx_1 + \cdots + x_n \geq k is interpreted as: the number of variables assigned to true is at least kk. The minus sign is interpreted as negation. Therefore, x1x22x_1 - x_2 \geq 2 is interpreted as: both x1x_1 and x2¯\overline{x_2} are both true. Therefore, as inequality, it is in fact x1+(1x2)2x_1 + (1- x_2) \geq 2 that has a unique solution x1=1,x2=0x_1=1, x_2= 0.

Input

The input is a text (in the stdin) with pairs of connected nodes. For instance, the following text in the case of our example:

x1 + x2 > 1
x2 + x3 + x4 > 2
x1 + x2 + x3 + x4 < 2

Output

The output is also a text (in the stdout) where in every line there is a variable assignment (variables assigned to 1 (true) as possitive, and those assigned to 0 (false) as negative. In this example:

{ -x1 x2 x3 -x4 }
{ -x1 x2 -x3 x4 }

Scoring

Public test cases
  • Input

    x1 + x2 + x3 > 2
    x1 + x2 + x3 < 2
    

    Output

    { -x1 x2 x3 }
    { x1 -x2 x3 }
    { x1 x2 -x3 }
    
  • Input

    x1 + x2 > 1
    x2 + x3 + x4 > 2
    x1 + x2 + x3 + x4 < 2
    

    Output

    { -x1 x2 x3 -x4 }
    { -x1 x2 -x3 x4 }
    
  • Input

    x1 - x2 > 2
    

    Output

    { x1 -x2 }
    
  • Information
    Author
    Jordi Levy
    Language
    English
    Official solutions
    Python
    User solutions
    Python