1
+ """
2
+
3
+ """
4
+ function select ()
5
+
6
+ end
7
+
1
8
"""
2
9
to_cnf!(m::Model, expr::Expr)
3
10
4
11
Convert logical proposition expression into conjunctive normal form.
5
12
"""
6
13
function to_cnf! (m:: Model , expr:: Expr )
7
- expr_name = Symbol (expr) # get name to register reformulated logical proposition
14
+ expr_name = Symbol (" { $ expr} " ) # get name to register reformulated logical proposition
8
15
replace_Symvars! (expr, m; logical_proposition = true ) # replace all JuMP variables with Symbolic variables
9
- check_logical_proposition (expr) # check that valid boolean symbols and variables are used in the logical proposition
10
- eliminate_equivalence! (expr) # eliminate ⇔
11
- eliminate_implication! (expr) # eliminmate ⇒
12
- move_negations_inwards! (expr) # expand ¬
13
- clause_list = distribute_and_over_or_recursively! (expr) # distribute ∧ over ∨ recursively
14
- @assert ! isempty (clause_list) " Conversion to CNF failed."
16
+ clause_list = to_cnf! (expr)
15
17
# replace symbolic variables with JuMP variables and boolean operators with their algebraic counterparts
16
18
for clause in clause_list
17
19
replace_JuMPvars! (clause, m)
@@ -29,6 +31,22 @@ function to_cnf!(m::Model, expr::Expr)
29
31
end
30
32
end
31
33
34
+ """
35
+ to_cnf!(expr::Expr)
36
+
37
+ Convert an expression of symbolic Boolean variables and operators to CNF.
38
+ """
39
+ function to_cnf! (expr:: Expr )
40
+ check_logical_proposition (expr) # check that valid boolean symbols and variables are used in the logical proposition
41
+ eliminate_equivalence! (expr) # eliminate ⇔
42
+ eliminate_implication! (expr) # eliminmate ⇒
43
+ move_negations_inwards! (expr) # expand ¬
44
+ clause_list = distribute_and_over_or_recursively! (expr) # distribute ∧ over ∨ recursively
45
+ @assert ! isempty (clause_list) " Conversion to CNF failed."
46
+
47
+ return clause_list
48
+ end
49
+
32
50
"""
33
51
check_logical_proposition(expr::Expr)
34
52
@@ -53,11 +71,13 @@ function eliminate_equivalence!(expr)
53
71
if expr isa Expr
54
72
if expr. args[1 ] == :⇔
55
73
@assert length (expr. args) == 3 " Double implication cannot have more than two clauses."
56
- A = expr. args[2 ]
57
- B = expr. args[3 ]
74
+ A1 = expr. args[2 ]
75
+ B1 = expr. args[3 ]
76
+ A2 = A1 isa Expr ? copy (A1) : A1
77
+ B2 = B1 isa Expr ? copy (B1) : B1
58
78
expr. args[1 ] = :∧
59
- expr. args[2 ] = :($ A ⇒ $ B )
60
- expr. args[3 ] = :($ B ⇒ $ A )
79
+ expr. args[2 ] = :($ A1 ⇒ $ B1 )
80
+ expr. args[3 ] = :($ B2 ⇒ $ A2 )
61
81
end
62
82
for i in eachindex (expr. args)
63
83
expr. args[i] = eliminate_equivalence! (expr. args[i])
0 commit comments