cnf_simplify.py
cnf_simplify.py
Functions for simplifying Cnfs, particularly (a∨b∨c) ∧ (a∨b∨¬c) to (a∨b).
This simplification is traditionally handled by the Quine–McCluskey algorithm. But for our simple use-case, this might be over-engineered.
Instead we implement our own functions for making this simplification.
absolute_literals_of_clause(clause)
Return the set of all absolute-values of all literals in a clause, .
absolute_literals_of_clause = set ∘ map(cnf.absolute_value, __)
Source code in normal_form/cnf_simplify.py
15 16 17 18 19 20 21 |
|
differing_lits(clause1, clause2)
Give a set of literals that two clauses differ on.
This returns a set that can give us twice the Hamming distance between
clauses. Assume that the clauses have the same image under hedge_of_clause
. If
not, raise an AssertionError.
Quick way to compute set of distinct lits is to calculate the symmetric difference between them (using the ^ operator).
∀ c : cnf.Clause, differing_lits(c, c) = ∅ ∀ c d : cnf.Clause, differing_lits(c, d) = differing_lits(d, c)
Source code in normal_form/cnf_simplify.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
equivalent_smaller_clause(clause1, clause2)
Given clauses that are distance 1 apart, return smaller equiv. clause.
The smaller equivalent clause is given by the intersection of the two clauses.
This is computed using the &
operator.
Source code in normal_form/cnf_simplify.py
43 44 45 46 47 48 49 50 51 52 53 54 |
|
reduce_cnf(cnf_instance)
Reduce distance=1 as well as subclauses.
Source code in normal_form/cnf_simplify.py
92 93 94 |
|
reduce_distance_one_clauses(cnf_instance)
Reduce all clauses in the the
The problem this function is trying to solve is known to be NP-hard. This is an inefficient solution to this problem. Do not apply it to Cnfs with more than 5 variables.
reduce_cnf = ungroup_clauses_to_cnf ∘ reduce_all_groups ∘ group_clauses_by_edge
Source code in normal_form/cnf_simplify.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
|
subclause_reduction(cnf_instance)
Replace the conjunction of two clauses with the smaller clause.
If c₁ c₂ : cnf.Clause, we sat the c₁ ≪ c₂ if every literal of c₁ is in c₂.
Replace c₁ ∧ c₂ with c₁ whenever c₁ ≪ c₂. Perform action recursively.
Source code in normal_form/cnf_simplify.py
77 78 79 80 81 82 83 84 85 86 87 88 89 |
|