# Modelling Module

OptiLog provides a high level API for modelling non-CNF formulas.

This module provides functionality for:

• Converting Non-CNF formulas to CNF formulas

• Representing Formulas with varying levels of parenthesis

• Encoding non normalized Pseudo Boolean expressions and normalizing them

• Representing truth tables of multiple problems with common variables

• Encoding (decoding) to (from) DIMACS

• Evaluate formulas with a high level interpretation

• Other features…

## Basic Problem Modelling

For simplicity, the modelling module is usually imported with the star operator like to import everything, like so:

1>>> from optilog.modelling import *

This allows us to instantiate boolean variables:

1>>> a = Bool('a')
2>>> b = Bool('b')
3>>> c = Bool('c')
4>>> z = Bool('z')

And then use these variables to model expressions:

1>>> expr1 = ~a & b & c | z
2>>> print(expr1) # ~a & b & c | z

We can also combine expressions

1>>> e1 = a | b
2>>> e2 = c & e1
3>>> e2
4c & (a | b)

These expressions can also be represented by forcing parenthesis.

1>>> print(expr1.represent(parenthesis='force')) # ((~a & b & c) | z)

This functionality is very useful for undergraduate Computer Science courses that introduce propositional logic. By enforcing parentheses representation, teachers and students can check the correct operator precedence of the formulas.

The next possible step is to model Problems. Problems are a collection of expressions as constraints. In this example we will add the previously created expression and a new one:

1>>> p = Problem()
3~a & b & c | z
5a | b

Once the problem has been instantiated, we can visualize its TruthTable:

1>>> t = TruthTable(p)
2>>> t.print()
3| z | b | c | a | (~a & b & c | z) & (a | b) |
4+---+---+---+---+----------------------------+
5| 0 | 0 | 0 | 0 |             0              |
6| 0 | 0 | 0 | 1 |             0              |
7| 0 | 0 | 1 | 0 |             0              |
8| 0 | 0 | 1 | 1 |             0              |
9| 0 | 1 | 0 | 0 |             0              |
10| 0 | 1 | 0 | 1 |             0              |
11| 0 | 1 | 1 | 0 |             1              |
12| 0 | 1 | 1 | 1 |             0              |
13| 1 | 0 | 0 | 0 |             0              |
14| 1 | 0 | 0 | 1 |             1              |
15| 1 | 0 | 1 | 0 |             0              |
16| 1 | 0 | 1 | 1 |             1              |
17| 1 | 1 | 0 | 0 |             1              |
18| 1 | 1 | 0 | 1 |             1              |
19| 1 | 1 | 1 | 0 |             1              |
20| 1 | 1 | 1 | 1 |             1              |

The order of the variables of the TruthTable can also be configured:

1>>> t.print(variable_order=[a,b,c,z])

We also could have evaluated the problem manually:

1>>> res = p.evaluate({
2>>>     'a': True,
3>>>     'b': False,
4>>>     'c': False,
5>>>     'z': False
6>>> })
7>>> print(res) # False

## Operators and parsing options

The following operators (and symbols) are provided:

• Logic And (&)

• Logic Or (|)

• Logic Xor (^)

• Logic Not (~)

• Logic If

• Logic Iff

• Comparison Constraints such as Equality, LessThan, GreaterThan, LessOrEqual, GreaterOrEqual

• Other Integer operators like Add, Substract, Multiply, Power and Minus

Warning

Currently XOR is not supported when translating to CNF. This limitation will be lifted in the future.

Expressions can be crafted with the symbols or with the underlying classes. In the following example all of the expressions are equivalent:

1>>> expr = ~a & b & c | z
2>>> expr = And(~a,b,c) | z
3>>> expr = Or(And(~a,b,c),z)
4>>> expr = Or(And(Not(a),b,c),z)

OptiLog also supports If and Iff Operators, without symbol overloading like so:

1>>> expr = If(a, b) # a -> b
2>>> expr = Iff(a, b) # a <-> b

In order to ease the modelling of complex formulas, a parsing option is available.

1>>> expr = Bool.parse('a -> b & c | d')
2>>> print(expr) # a -> b & c | d
3>>> print(expr.represent(parenthesis='force')) # (a -> ((b & c) | d))

This way of generating expressions preserves parentheses by default. Notice the following code prints a different expression:

1>>> expr = Bool.parse('a -> ((b & c) | d)')
2>>> print(expr) # a -> ((b & c) | d)

But it is equivalent:

1>>> print(expr.represent(parenthesis='simplify')) # a -> b & c | d

## Equivalence of Python operators, Expression classes, and textual description

Using the modelling module, you can describe problems in three possible ways: creating Bool variables and applying python operators, using the Expression classes (such as And, If…), or using a textual description of the encoding as a string and calling Bool.parse. The following table shows the equivalence between the three variants:

Equivalence

Operation

Python operator

Expression class

Textual string

Negation of a literal a

~a

Not(a)

“~a”

Conjunction of two literals a, b, c

a & b

And(a, b) 1

“a & b”

Disjunction of two literals a, b

a | b

Or(a, b) 1

“a | b”

a implies b

Not available

If(a, b)

“a -> b”

Double implication between a and b

Not available

Iff(a, b)

“a <-> b”

Xor between a and b

a ^ b

Xor(a, b) 1

“a ^ b”

a + b

“a + b”

Minus (unary)

-a

Minus(a)

“-a”

Subtraction

a - b

Substract(a, b)

“a - b”

Multiplication

a * b

Multiply(a, b) 1

“a * b”

Division

Not available

Not available

Not available

Power

a ** b

Power(a, b)

“a ** b”

Equivalence: $$a + b = c$$

(a + b) == c

Equality(a + b, c)

“(a + b) == c”

Less than: $$a + b < c$$

(a + b) < c

LessThan(a + b, c)

“(a + b) < c”

Greater than: $$a + b > c$$

(a + b) > c

GreaterThan(a + b, c)

“(a + b) > c”

Less or equal than: $$a + b <= c$$

a + b <= c

LessOrEqual(a + b, c)

“(a + b) <= c”

Greater or equal than: $$a + b >= c$$

a + b >= c

GreaterOrEqual(a + b, c)

“(a + b) >= c”

You can nest and combine different operations at once. The priority levels that will be applied are:

1. Parenthesis

2. Power

3. Minus (unary), Negation

4. Multiplication

6. Conjunction

7. Xor

8. Disjunction

9. Implication and double implication

## Solving a Problem with a SAT solver

We can leverage this modelling module to solve non-CNF formulas with the standard SAT solvers integrated into OptiLog.

The first thing we need to do is define a problem and convert it to DIMACS:

1>>> p = Problem()
2>>> p.add_constr(~a & b & c | z)
4>>> cnf = p.to_cnf_dimacs()

Finally, we can solve this cnf encoded instance with a solver such as Glucose41:

1>>> from optilog.solvers.sat import Glucose41
2>>> s = Glucose41()
4>>> s.solve()
5>>> interpretation = cnf.decode_dimacs(s.model())
6>>> print('Interpretation:', interpretation) # [~z, b, c, ~a]

Notice the call to the method decode_dimacs that translates the DIMACS encoded solution into a higher level interpretation.

## Encoding Pseudo Boolean Expressions

OptiLog has support for encoding non normalized Pseudo Boolean expressions.

The following example encodes a cardinality constraint. The constraint is interpreted as an expression and is then instantiated as a Problem:

1>>> a = Bool('a')
2>>> b = Bool('b')
3>>> c = Bool('c')
4>>> p = Problem(a + b + c < 2, name='cardinality')
5>>> t = TruthTable(p)
6>>> t.print()
7| a | b | c | cardinality |
8+---+---+---+-------------+
9| 0 | 0 | 0 |      1      |
10| 0 | 0 | 1 |      1      |
11| 0 | 1 | 0 |      1      |
12| 0 | 1 | 1 |      0      |
13| 1 | 0 | 0 |      1      |
14| 1 | 0 | 1 |      0      |
15| 1 | 1 | 0 |      0      |
16| 1 | 1 | 1 |      0      |

More complex Pseudo Boolean expressions are also supported and are automatically normalized. Example:

1>>> p = Problem(a + b * 2 + c <= ~b + 3)
2>>> t = TruthTable(p)
3>>> t.print()
4| a | c | b | a + b * 2 + c <= ~b + 3 |
5+---+---+---+-------------------------+
6| 0 | 0 | 0 |            1            |
7| 0 | 0 | 1 |            1            |
8| 0 | 1 | 0 |            1            |
9| 0 | 1 | 1 |            1            |
10| 1 | 0 | 0 |            1            |
11| 1 | 0 | 1 |            1            |
12| 1 | 1 | 0 |            1            |
13| 1 | 1 | 1 |            0            |

PB Normalization is also supported when translating to CNF:

1>>> from optilog.modelling import *
2>>> problem = Problem(a + b * 2 + c <= ~ b + 3)
3>>> problem.to_cnf_dimacs()
4p cnf 12 25
5-3 -4 0
63 4 0
7-1 -5 0
81 5 0
9-3 -6 0
103 6 0
11-2 -7 0
122 7 0
13-8 -6 0
148 6 0
15-9 -7 -4 0
169 7 0
179 4 0
18-10 -7 -5 0
1910 7 0
2010 5 0
21-11 -4 -5 0
2211 4 0
2311 5 0
2412 -8 -9 -10 -11 0
25-12 8 0
26-12 9 0
27-12 10 0
28-12 11 0
2912 0

PB Normalization does not apply the distributive rule, coefficients must multiply only a single variable.

Footnotes

1(1,2,3,4,5)

This Expression class accepts a variable amount of operands: Class(a, b, c, d…)