-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
132 lines (114 loc) · 4.94 KB
/
run.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
import sys
import glob
import random
import multiprocessing
import time
import dpll
import walk
import sa
# Parse symbols and clauses from CNF INPUT
def get_input(filename):
with open(filename , 'r') as file:
user_input = file.readline()
while(user_input[0] == 'c'):
user_input = file.readline()
_, _, nbvar, nbclauses = user_input.split()
nbvar = int(nbvar)
nbclauses = int(nbclauses)
symbols = [i for i in range(1, nbvar+1)]
assignments = [None] * nbvar
clauses = [None] * nbclauses
for i in range(nbclauses):
user_input = file.readline()
clauses[i] = [int(i) for i in user_input.split()[:-1]]
return symbols, assignments, clauses
# Test driver for DPLL
def run_dpll(formula):
symbols, assignments, clauses = get_input(formula)
# Special thanks to Stack Abuse for teaching me the stdout swap technique
# Jacob Stopak, https://stackabuse.com/writing-to-a-file-with-pythons-print-function/
original_stdout = sys.stdout
with open("./logs/dpll/"+str(formula).split("/")[-1][:-4]+".log" , 'w') as log:
sys.stdout = log
max_recursion_depth = 0
max_clauses_sat = 0
max_list = [max_clauses_sat, max_recursion_depth]
start_time = time.time()
if(dpll.dpll(symbols, assignments, clauses, 0, max_list)):
duration = time.time() - start_time
sys.stdout = original_stdout
print(str(formula) + ", " + str(len(symbols)) + ", " +
str(len(clauses)) + ", SATISFIABLE, " + str(max_list[0]) +
", " + str(max_list[1]) + ", " + str(duration))
else:
duration = time.time() - start_time
sys.stdout = original_stdout
print(str(formula) + ", " + str(len(symbols)) + ", " +
str(len(clauses)) + ", UNSATISFIABLE, " + str(max_list[0]) +
", " + str(max_list[1]) + ", " + str(duration))
# Test driver for WalkSAT
def run_walk(formula):
symbols, assignments, clauses = get_input(formula)
# Special thanks to Stack Abuse for teaching me the stdout swap technique
# Jacob Stopak, https://stackabuse.com/writing-to-a-file-with-pythons-print-function/
original_stdout = sys.stdout
with open("./logs/walk/"+str(formula).split("/")[-1][:-4]+".log" , 'w') as log:
sys.stdout = log
start_time = time.time()
sat, assignments, max_clauses, flips = walk.walk(symbols, clauses, p, max_flips)
duration = time.time() - start_time
if(sat):
sys.stdout = original_stdout
print(str(formula) + ", " + str(len(symbols)) + ", " + str(len(clauses)) +
", SATISFIABLE, " + str(max_clauses) +
", " + str(flips) + ", " + str(duration))
else:
sys.stdout = original_stdout
print(str(formula) + ", " + str(len(symbols)) + ", " + str(len(clauses)) +
", CANNOT SATISFY IN MAX FLIPS, " + str(max_clauses) +
", " + str(flips) + ", " + str(duration) )
# Test driver for Simulated Annealing
def run_sa(formula):
symbols, assignments, clauses = get_input(formula)
# Special thanks to Stack Abuse for teaching me the stdout swap technique
# Jacob Stopak, https://stackabuse.com/writing-to-a-file-with-pythons-print-function/
original_stdout = sys.stdout
with open("./logs/sa/"+str(formula).split("/")[-1][:-4]+".log" , 'w') as log:
sys.stdout = log
start_time = time.time()
sat, assignments, max_clauses, flips = sa.sa(symbols, clauses, max_flips, cool_rate)
duration = time.time() - start_time
if(sat):
sys.stdout = original_stdout
print(str(formula) + ", " + str(len(symbols)) + ", " + str(len(clauses)) +
", SATISFIABLE, " + str(max_clauses) +
", " + str(flips) + ", " + str(duration))
else:
sys.stdout = original_stdout
print(str(formula) + ", " + str(len(symbols)) + ", " + str(len(clauses)) +
", CANNOT SATISFY IN MAX FLIPS, " + str(max_clauses) +
", " + str(flips) + ", " + str(duration) )
# Main test driver
if __name__ == '__main__':
RUN_DPLL = False
RUN_WALK = False
RUN_SA = True
if len(sys.argv) != 2:
print("Usage: run.py dir")
print("`dir/` should be a directory containing properly formatted .cnf formula files")
quit()
# Set random probability for WalkSAT
p = 0.5
max_flips = 50000
# Set cooling rate for Simulated Annealing
cool_rate = 0.999
formulas_to_solve = glob.glob(sys.argv[1]+"/*.cnf")
with multiprocessing.Pool() as pool:
if RUN_DPLL:
pool.map(run_dpll, formulas_to_solve)
if RUN_WALK:
for _ in range(10):
pool.map(run_walk, formulas_to_solve)
if RUN_SA:
for _ in range(10):
pool.map(run_sa, formulas_to_solve)