Cardinality Constraints X37043


Statement
 

optilog

pdf   zip

html

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

A set of cardinality constraints is a set of inequalities like:

x1 + x2 ≥ 1
x2 + x3 + x4 ≥ 2
x1 + x2 + x3 + x4 ≤ 2

They can be translated into SAT using sequential counters:

S11 ↔ x1
Sii ↔ Si−1i−1 ∧ xii=2,…,k
Si1 ↔ Si−11 ∨ xii=2,…,n
Sij ↔ Si−1j ∨ (xi ∧ Si−1j−1)i=3,…,n, j=2,…,min{i−1,k}

The clause Snk plus the clauses defining the right implications of the previous equivalences enforce x1+⋯+xnk. In the last equivalence, for instance, the clauses are:

Sij → Si−1j ∨ (xi ∧ Si−1j−1) ≡ 





Sij
∨ Si−1j∨ Si−1j−1 ≡ 
Sij
∨ Si−1j−1
Sij
∨ Si−1j∨ xi

The constraint x1 + ⋯ + xnk is interpreted as: the number of variables assigned to true is at least k. The minus sign is interpreted as negation. Therefore, x1x2 ≥ 2 is interpreted as: both x1 and x2 are both true. Therefore, as inequality, it is in fact x1 + (1− x2) ≥ 2 that has a unique solution x1=1, x2= 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