-
Notifications
You must be signed in to change notification settings - Fork 0
/
brute-MakennaGall
executable file
·141 lines (130 loc) · 4.91 KB
/
brute-MakennaGall
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
#!/usr/bin/env python3
# this file is for the brute force SAT Solver
#Team: Makenna Gall (team of 1)
import time
def main():
readIn("smallerTest.cnf")
def readIn(filename):
readFile = open(filename, 'r')
writeFile = open('output.cnf', 'w')
Lines = readFile.readlines()
clauseList = []
for line in Lines:
if line.startswith('c'):
if bool(clauseList):
varDict = {}
varDict = createDictionary(numVariables)
evaluation = allPossibilities(numVariables, varDict, clauseList)
returnedSatisfiability = evaluation[0]
numLiterals = evaluation[1]
workingString = evaluation[2]
timeTaken = evaluation[3]
if satisfiability == 'X':
agreement = 0
elif not returnedSatisfiability == satisfiability:
agreement = -1
else:
agreement = 1
finalList = [problemNumber, numVariables, numClauses, maxLiterals, numLiterals, returnedSatisfiability, agreement, timeTaken, workingString]
writeFile.write(','.join(str(e) for e in finalList))
writeFile.write("\n")
clauseList = []
cItems = line.split()
problemNumber = cItems[1]
maxLiterals = cItems[2]
if len(cItems) >= 4:
satisfiability = cItems[3]
else:
satisfiability = "X"
if satisfiability == 'U':
print("should be unsatisfiable")
else:
print("should be satisfiable")
#if the list of clauses is not empty, evaluate the clauses:
if(line.startswith('p')):
elements = line.split()
numClauses = int(elements[3])
numVariables = int(elements[2])
if not line.startswith('c') and not line.startswith('p'):
clauseList.append(line)
varDict = {}
varDict = createDictionary(numVariables)
evaluation = allPossibilities(numVariables, varDict, clauseList)
returnedSatisfiability = evaluation[0]
numLiterals = evaluation[1]
workingString = evaluation[2]
timeTaken = evaluation[3]
if satisfiability == 'X':
agreement = 0
elif not returnedSatisfiability == satisfiability:
agreement = -1
else:
agreement = 1
finalList = [problemNumber, numVariables, numClauses, maxLiterals, numLiterals, returnedSatisfiability, agreement, timeTaken, workingString]
writeFile.write(','.join(str(e) for e in finalList))
#test of createDictionary, allPossibilities and printDict:
def createDictionary(numVars):
#keys are numbers 1 to the number of vars (representing x1, x2 ... xnumVars)
varDict = {}
for i in range(1,numVars + 1):
#initialize all values to True
varDict[i] = True
return varDict
def allPossibilities(numVars, varDict, clauseList):
#range consists of numbers 0 through 2^numVars - 1
#iterates 2^numVar times
print("evaluating term")
startTime = time.time()
for i in range(1, pow(2, numVars)+1):
#print(i, " : ", end= "")
#iterates numVar times
for j in range(numVars):
if(i%pow(2,j) == 0):
if varDict[j+1]:
varDict[j+1] = False
else:
varDict[j+1] = True
#printVars(numVars, varDict)
currEval = evalClauses(numVars, varDict, clauseList)
if currEval[0]:
print("Satisfiable")
printVars(numVars, varDict)
endTime = time.time()
timeTaken = round((endTime - startTime) * pow(10, 6), 2)
return ['S', currEval[1], createReturnList(numVars, varDict), timeTaken]
print("Unsatisfiable")
endTime = time.time()
timeTaken = round((endTime - startTime) * pow(10, 6), 2)
return ['U', currEval[1], "", timeTaken]
def createReturnList(numVars, varDict):
returnString = ""
for i in range (1, numVars+1):
if varDict[i] == True:
returnString += "1"
else:
returnString += "0"
return returnString
def printVars (numVars, varDict):
for i in range(1, numVars + 1):
#print all values
print(i, varDict[i], end="; ")
print()
def evalClauses(numVars, varDict, clauseList):
literalCounter = 0
for clause in clauseList:
currClause = False
for var in clause.split(',')[:-1]:
literalCounter+=1
if int(var) < 0:
if not varDict[abs(int(var))]:
currClause = True
elif int(var) > 0:
if varDict[abs(int(var))]:
currClause = True
if not currClause:
#printVars(numVars, varDict)
return [False, literalCounter]
#printVars(numVars, varDict)
return [True, literalCounter]
if __name__ == "__main__":
main()