-
Notifications
You must be signed in to change notification settings - Fork 0
/
DPLL.py
executable file
·157 lines (122 loc) · 3.87 KB
/
DPLL.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/python3
import sys
from enum import Enum, auto
#We will define variables in lowercase as variables with no negation and characters in uppercase as variables in negation
# -a = A
# a = a
#Definitions--------------------------------------------------------
#Enum to cover the case:
#FALSE: The DPLL recursion should go on with false
#TRUE: The DPLL recursion should go on with true
#BOTH: The DPLL recursion should go on with both branches
class RecType(Enum):
FALSE = auto()
TRUE = auto()
BOTH = auto()
#Defining type of recursion.
#var: defines which variable will be used next
#type: defines which branches the recursion will take
class NextRec:
def __init__(self, var, recType):
self.var = var
self.type = recType
#checks whether a character c exists in character list varlist
#return True: c doesn't exist in varlist
#return False: c does exist in varlist
def setCheck(c, varList):
for c_ in varList:
if(c==c_):
return False
return True
#Gets the next variable to use. If no heuristic it just uses the order of
# variables in vars to determine which variable is next.
#If a heuristic is applicable, that heuristic is used.
def getNextRec(clause, vars):
#TODO code for OLR/PLR heuristic
c = '?'
clauseVars = ""
for lit in clause:
clauseVars += lit
clauseVars = clauseVars.lower()
for i in range(len(vars)):
if(not setCheck(vars[i], clauseVars)):
c = vars[i]
break
#print("Char: " + c)
return NextRec(c, RecType.BOTH)
def genBranch(clause, nextRec, bool):
branch = []
if(bool):
#Generation of trueBranch
for lit in clause:
if(not setCheck(nextRec.var, lit)):
continue
temp = lit.replace(nextRec.var.upper(), "")
if(setCheck(temp, branch)):
branch.append(temp)
else:
#Generation of falseBranch
for lit in clause:
if(not setCheck(nextRec.var.upper(), lit)):
continue
temp = lit.replace(nextRec.var, "")
if(setCheck(temp, branch)):
branch.append(temp)
return branch
#Sets the interpretation of var with the value of bool. The value of bool is True, False
#but inter is an array of 0 and 1
def setInter(inter, vars, var, bool):
for i in range(len(vars)):
if(vars[i] == var):
inter[i] = 1 if bool else 0
#Base DPLL
#clause: a list of strings
#vars: list of variables (just so that no global vars are used)
#inter: Interpretation - Belegung in german
def base_DPLL(clause, vars, inter):
#Checks whether to end Recursion
if(len(clause)==0):
print(vars)
print(inter)
return True
else:
for lit in clause:
if(lit == ""):
return False
#print(vars)
#print(clause)
nextRec = getNextRec(clause, vars)
if(nextRec.type == RecType.TRUE or nextRec.type == RecType.BOTH):
#Generation of trueBranch
trueBranch = genBranch(clause, nextRec, True)
setInter(inter, vars, nextRec.var, True)
if(base_DPLL(trueBranch, vars, inter)):
return True
if(nextRec.type == RecType.FALSE or nextRec.type == RecType.BOTH):
#Generation of falseBranch
falseBranch = genBranch(clause, nextRec, False)
setInter(inter, vars, nextRec.var, False)
if(base_DPLL(falseBranch, vars, inter)):
return True
return False
#Initializing function
def DPLL(clause, vars):
inter = []
for v in vars:
inter.append(-1) #-1 if an interpretation isnt assigned
base_DPLL(clause, vars, inter)
return
#Global Variables---------------------------------------------------
#CLI Arguments
g_args = sys.argv[1:]
#Later will be the list of variables
g_vars = []
#Code---------------------------------------------------------------
#Generation of variable list
for arg in g_args:
temp = arg.lower()
for c in temp:
if(setCheck(c, g_vars)):
g_vars.append(c)
g_vars.sort()
DPLL(g_args, g_vars)