-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathharness.py
executable file
·190 lines (162 loc) · 5.34 KB
/
harness.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import os
from cg_generator import CGGenerator
from naive_ldfi import NaiveLDFI
from injectors import FaultInjector, RandomFaultInjector, HeuristicFaultInjector
import json
import sys
# LOLOLOLOLOL
sys.setrecursionlimit(1000000)
MAXWIDTH = 5
MAXDEPTH = 4
MAXALTS = 3
RANDOM_TARGET = 2
maxiterations = 10000
class Result():
def __init__(self, solution=None, iterations=None):
self.solution = solution
self.iterations = iterations
def __str__(self):
#print "IN STR"
return str(self.solution) + " in " + str(self.iterations) + " attempts"
def do_ldfi(g):
ldfi = NaiveLDFI()
ldfi.add_graph(g)
sugg = ldfi.suggestions()
try:
soln = next(sugg)
except StopIteration:
return Result(None, iterations)
iterations = 0
while soln:
iterations += 1
print( iterations )
faultset = list(map(lambda x: str(x), soln))
#print(faultset)
if len(faultset)==0:
return Result(None, iterations)
if g.label in faultset:
if iterations > maxiterations:
return Result(None, iterations)
#print "WOOOO"
try:
soln = next(sugg)
except StopIteration:
return(None, iterations)
print("newSOLN " + str(soln))
continue
ret = g.inject_new(faultset)
if ret is None:
return Result(faultset, iterations)
else:
if iterations > maxiterations:
return Result(None, iterations)
# if we are down to just suggesting" FAIL NODE 1! then we are done"
if ret.children == set():
return Result(None, iterations)
else:
ldfi.add_graph(ret)
#print "CURFORM: " + str(ldfi.current_formula())
sugg = ldfi.suggestions()
try:
soln = next(sugg)
except StopIteration:
return(None, iterations)
def do_bruteforce(g):
ft = FaultInjector(g)
iterations = 0
#print "all: " + str(ft.all_faults_cnt())
for fault in ft.all_faults():
iterations += 1
#print iterations
if g.label in fault:
if iterations > maxiterations:
return Result(None, iterations)
continue
ret = g.inject_new(fault)
if ret is None:
return Result(fault, iterations)
else:
if iterations > maxiterations:
return Result(None, iterations)
return Result(None, iterations)
def do_random(g):
ft = RandomFaultInjector(g)
iterations = 0
while True:
iterations += 1
#print iterations
fault = ft.next_fault(RANDOM_TARGET)
if g.label in fault:
if iterations > maxiterations:
return Result(None, iterations)
continue
ret = g.inject_new(fault)
if ret is None:
return Result(fault, iterations)
else:
if iterations > maxiterations:
return Result(None, iterations)
def do_heuristic(g):
ft = HeuristicFaultInjector(g)
iterations = 0
while True:
iterations += 1
#print iterations
sugg = ft.next_fault()
try:
prev_faults = ft.get_faults_injected()
fault = next(sugg)
while fault in prev_faults:
fault = next(sugg)
except StopIteration:
return Result(None, iterations)
ft.add_faults_injected(fault)
ret = g.inject_new(fault)
if ret is None:
return Result(fault, iterations)
else:
if iterations > maxiterations:
return Resut(None, iterations)
ft.update_heuristic(ret)
output_directory = 'out'
op_file = os.path.join(output_directory, sys.argv[1])
print("Nodes, edges, ldfi, random, bruteforce")
res_categories = {}
for j in range(1):
#i = j + 64
i = 35
cg = CGGenerator(MAXWIDTH, i)
g = cg.new_graph(MAXDEPTH, MAXALTS)
graph_output_file = os.path.join(output_directory, str(i))
g.to_dot(alternates=True).render(graph_output_file)
print("GRAPH " + str(i) + ":" + str(len(g.nodeset())) + " nodes, " + str(len(g.edgeset())) + " edges")
print("depth " + str(g.depth()))
print("bottom " + str(g.bottom()))
if g.bottom() == 0:
continue
print("Finding LDFI solution")
l = do_ldfi(g)
print(l.solution,l.iterations)
exit(0)
print("Finding random solution")
r = do_random(g)
print("Finding bruteforce solution")
b = do_bruteforce(g)
print("Finding heuristic solution")
h = do_heuristic(g)
print("|".join(map(lambda x: str(x), [j, len(g.nodeset()), len(g.edgeset(alternates=True)), l.solution, l.iterations, r.solution, r.iterations, b.solution, b.iterations, h.solution, h.iterations])))
soln_len = g.min_failure_scenario_size()
if soln_len not in res_categories:
res_categories[soln_len] = []
tmp = {}
tmp['l_iter'] = l.iterations
tmp['b_iter'] = b.iterations
tmp['r_iter'] = r.iterations
tmp['h_iter'] = h.iterations
tmp['alternates'] = MAXALTS
tmp['nodes'] = len(g.nodeset())
tmp['edges'] = len(g.edgeset(alternates=True))
res_categories[soln_len].append(tmp)
f = open(op_file, "w")
json.dump(res_categories, f, indent=0)
f.close()