Skip to content

Commit 0c4bf3c

Browse files
committed
bug fix in propositional logic
1 parent 2a0b37a commit 0c4bf3c

File tree

3 files changed

+34
-24
lines changed

3 files changed

+34
-24
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "DisjunctiveProgramming"
22
uuid = "0d27d021-0159-4c7d-b4a7-9ccb5d9366cf"
33
authors = ["hdavid16 <[email protected]>"]
4-
version = "0.2.1"
4+
version = "0.3.0"
55

66
[deps]
77
IntervalArithmetic = "d1acc4aa-44c8-5952-acd4-ba5d80a2a253"

src/logic.jl

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1+
"""
2+
3+
"""
4+
function select()
5+
6+
end
7+
18
"""
29
to_cnf!(m::Model, expr::Expr)
310
411
Convert logical proposition expression into conjunctive normal form.
512
"""
613
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
815
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)
1517
#replace symbolic variables with JuMP variables and boolean operators with their algebraic counterparts
1618
for clause in clause_list
1719
replace_JuMPvars!(clause, m)
@@ -29,6 +31,22 @@ function to_cnf!(m::Model, expr::Expr)
2931
end
3032
end
3133

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+
3250
"""
3351
check_logical_proposition(expr::Expr)
3452
@@ -53,11 +71,13 @@ function eliminate_equivalence!(expr)
5371
if expr isa Expr
5472
if expr.args[1] == :
5573
@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
5878
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)
6181
end
6282
for i in eachindex(expr.args)
6383
expr.args[i] = eliminate_equivalence!(expr.args[i])

src/macros.jl

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -135,17 +135,7 @@ Add logical proposition macro.
135135
macro proposition(m, expr)
136136
#get args
137137
expr = QuoteNode(expr)
138-
code = :(add_proposition!($m, $expr))
139-
138+
code = :(DisjunctiveProgramming.to_cnf!($m, $expr))
139+
140140
return esc(code)
141141
end
142-
143-
"""
144-
add_proposition!(m::Model, expr::Expr)
145-
146-
Add logical proposition expression to a JuMP model.
147-
"""
148-
function add_proposition!(m::Model, expr::Expr)
149-
@assert m isa Model "A valid JuMP Model must be provided."
150-
to_cnf!(m, expr)
151-
end

0 commit comments

Comments
 (0)