-
Notifications
You must be signed in to change notification settings - Fork 6
/
program_executions_for_solutions.py
executable file
·318 lines (253 loc) · 10.9 KB
/
program_executions_for_solutions.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/usr/bin/python
## Can take 0, 1, or 2 command line arguments.
## If 0 arguments, uses variable set to outputDirectory as the location of the output files.
## First argument is the location of the output files, and overrides a variable defined below.
## Second argument can be "brief" in order to not output individual run success/fail, and only output aggregate statistics
## Second argument can alternatively be "csv" in order to print one line per problem, ready to paste into a spreadsheet
import os, sys
from sys import maxint
verbose = True
if (len(sys.argv) >= 2 and sys.argv[1] == "brief") or \
(len(sys.argv) >= 3 and sys.argv[2] == "brief"):
verbose = False
csv = False
# Set these before running:
#outputDirectory = "Results/wc-new-experiments/UMAD/wc"
outputDirectory = "Results/wc-new-experiments/old-atom-gens/UMAD/wc"
# This allows this script to take a command line argument for outputDirectory
if len(sys.argv) > 1 and sys.argv[1] != "brief" and sys.argv[1] != "csv":
outputDirectory = sys.argv[1]
outputFilePrefix = "log"
outputFileSuffix = ".txt"
errorType = "float"
# Some functions
def median(lst):
if len(lst) <= 0:
return False
sorts = sorted(lst)
length = len(lst)
if not length % 2:
return (sorts[length / 2] + sorts[length / 2 - 1]) / 2.0
return sorts[length / 2]
def kth_percent_smallest_number(lst, percent):
"""Finds the k = len(lst) * percent number in the lst."""
if len(lst) <= 0:
return False
sorts = sorted(lst)
length = len(lst)
k = int(length * percent)
return sorts[k]
def mean(nums):
if len(nums) <= 0:
return False
return sum(nums) / float(len(nums))
def reverse_readline(filename, buf_size=8192):
"""a generator that returns the lines of a file in reverse order
From: https://stackoverflow.com/questions/2301789/read-a-file-in-reverse-order-using-python"""
with open(filename) as fh:
segment = None
offset = 0
fh.seek(0, os.SEEK_END)
total_size = remaining_size = fh.tell()
while remaining_size > 0:
offset = min(total_size, offset + buf_size)
fh.seek(-offset, os.SEEK_END)
buffer = fh.read(min(remaining_size, buf_size))
remaining_size -= buf_size
lines = buffer.split('\n')
# the first line of the buffer is probably not a complete line so
# we'll save it and append it to the last line of the next buffer
# we read
if segment is not None:
# if the previous chunk starts right from the beginning of line
# do not concact the segment to the last line of new chunk
# instead, yield the segment first
if buffer[-1] is not '\n':
lines[-1] += segment
else:
yield segment
segment = lines[0]
for index in range(len(lines) - 1, 0, -1):
if len(lines[index]):
yield lines[index]
yield segment
# Main area
i = 0
if outputDirectory[-1] != '/':
outputDirectory += '/'
dirList = os.listdir(outputDirectory)
if not csv:
print
print " Directory of results:"
print outputDirectory
bestFitnessesOfRuns = []
testFitnessOfBest = []
testFitnessOfSimplifiedBest = []
numExecutionsList = []
errorThreshold = maxint
errorThresholdPerCase = maxint
numErrors = maxint
fileName0 = (outputFilePrefix + str(i) + outputFileSuffix)
f0 = open(outputDirectory + fileName0)
for line in f0:
if line.startswith("error-threshold"):
try:
errorThreshold = int(line.split()[-1])
except ValueError, e:
errorThreshold = float(line.split()[-1])
if errorThreshold == 0:
errorThresholdPerCase = 0
if errorThresholdPerCase == maxint and line.startswith("Errors:"):
numErrors = len(line.split()) - 1
errorThresholdPerCase = float(errorThreshold) / numErrors
if errorThresholdPerCase != maxint and numErrors != maxint:
break
while (outputFilePrefix + str(i) + outputFileSuffix) in dirList:
if not csv:
sys.stdout.write("%4i" % i)
sys.stdout.flush()
if i % 25 == 24:
print
runs = i + 1 # After this loop ends, runs should be correct
fileName = (outputFilePrefix + str(i) + outputFileSuffix)
# f = open(outputDirectory + fileName)
#final = False
gen = 0
best_mean_error = maxint
done = False
bestTest = maxint
simpBestTest = maxint
numExecutions = maxint
if os.path.getsize(outputDirectory + fileName) == 0:
bestFitnessesOfRuns.append((gen, best_mean_error, done))
testFitnessOfBest.append(bestTest)
testFitnessOfSimplifiedBest.append(simpBestTest)
i += 1
continue
for line in reverse_readline(outputDirectory + fileName):
if line.startswith(";; -*- Report") and gen == 0:
gen = int(line.split()[-1])
if gen != 0 and best_mean_error < maxint and numExecutions != maxint:
break
if line.startswith("SUCCESS"):
done = "SUCCESS"
if line.startswith("FAILURE"):
done = "FAILURE"
if line.startswith("Mean:"):
gen_best_error = -1
if errorType == "float":
gen_best_error = float(line.split()[-1])
elif errorType == "int" or errorType == "integer":
gen_best_error = int(line.split()[-1])
else:
raise Exception("errorType of %s is not recognized" % errorType)
if gen_best_error < best_mean_error:
best_mean_error = gen_best_error
if line.startswith("Test total error for best:") and done:
try:
bestTest = int(line.split()[-1].strip("Nn"))
except ValueError, e:
bestTest = float(line.split()[-1].strip("Nn"))
if line.startswith("Test total error for best:") and not done:
try:
simpBestTest = int(line.split()[-1].strip("Nn"))
except ValueError, e:
simpBestTest = float(line.split()[-1].strip("Nn"))
if line.startswith("Number of program executions") and numExecutions == maxint:
numExecutions = int(line.split()[-1].strip("Nn"))
bestFitnessesOfRuns.append((gen, best_mean_error, done))
testFitnessOfBest.append(bestTest)
testFitnessOfSimplifiedBest.append(simpBestTest)
numExecutionsList.append(numExecutions)
i += 1
if not csv:
print
not_done = []
if verbose:
print "Error threshold per case:", errorThresholdPerCase
print "-------------------------------------------------"
for i, (gen, fitness, done) in enumerate(bestFitnessesOfRuns):
doneSym = ""
if done == "SUCCESS":
doneSym = " <- suc"
if not done:
doneSym += "$$$$$$ ERROR $$$$$$" #Should never get here
if len(testFitnessOfBest) > i and testFitnessOfBest[i] < maxint:
if isinstance(testFitnessOfBest[i], int):
doneSym += " | test = %i" % testFitnessOfBest[i]
else:
doneSym += " | test = %.3f" % testFitnessOfBest[i]
if len(testFitnessOfSimplifiedBest) > i and testFitnessOfSimplifiedBest[i] < maxint:
if isinstance(testFitnessOfSimplifiedBest[i], int):
doneSym += " | test on simplified = %i" % testFitnessOfSimplifiedBest[i]
else:
doneSym += " | test on simplified = %.4f" % testFitnessOfSimplifiedBest[i] #TMH this line changed
elif not done:
doneSym = " -- not done"
not_done.append(i)
if fitness >= 0.001 or fitness == 0.0:
print "Run: %3i | Gen: %5i | Best Fitness (mean) = %8.4f%s | Program Executions = %i" % (i, gen, fitness, doneSym, numExecutionsList[i])
else:
print "Run: %3i | Gen: %5i | Best Fitness (mean) = %.4e%s | Program Executions = %i" % (i, gen, fitness, doneSym, numExecutionsList[i])
totalFitness = 0
inds = 0
perfectSolutions = 0
perfectOnTestSet = 0
simpPerfectOnTestSet = 0
trainSolutionGens = []
testSolutionGens = []
testSolutionProgramExecutions = []
for i, (gen, fitness, done) in enumerate(bestFitnessesOfRuns):
if done:
totalFitness += fitness
inds += 1
if done == "SUCCESS":
perfectSolutions += 1
trainSolutionGens.append(gen)
if len(testFitnessOfBest) > i:
if testFitnessOfBest[i] <= errorThresholdPerCase:
perfectOnTestSet += 1
testSolutionGens.append(gen)
testSolutionProgramExecutions.append(numExecutionsList[i])
if len(testFitnessOfSimplifiedBest) > i:
if testFitnessOfSimplifiedBest[i] <= errorThresholdPerCase:
simpPerfectOnTestSet += 1
if verbose and len(trainSolutionGens) > 0:
print "------------------------------------------------------------"
print "Training Solution Generations:"
print "Mean: ", mean(trainSolutionGens)
print "Minimum: ", min(trainSolutionGens)
print "Median: ", median(trainSolutionGens)
print "Maximum: ", max(trainSolutionGens)
if len(testSolutionGens) > 0:
print "--------------------------"
print "Test Solution Generations (not simplified):"
print "Mean: ", mean(testSolutionGens)
print "Minimum: ", min(testSolutionGens)
print "Median: ", median(testSolutionGens)
print "Maximum: ", max(testSolutionGens)
print "--------------------------"
print "Test solution program executions (not simplified):"
print "Mean: ", mean(testSolutionProgramExecutions)
print "Minimum: ", min(testSolutionProgramExecutions)
print "25%: ", kth_percent_smallest_number(testSolutionProgramExecutions, 0.25)
print "Median: ", median(testSolutionProgramExecutions)
print "75%: ", kth_percent_smallest_number(testSolutionProgramExecutions, 0.75)
print "Maximum: ", max(testSolutionProgramExecutions)
# print testSolutionGens
if csv:
print "%s,%i,%i,%i,%i" % (outputDirectory, inds, perfectSolutions, perfectOnTestSet, simpPerfectOnTestSet)
else:
print "------------------------------------------------------------"
print "Number of finished runs: %4i" % inds
print "Solutions found: %4i" % (perfectSolutions)
print "Zero error on test set: %4i" % perfectOnTestSet
print "Simplified zero error on test set: %4i" % simpPerfectOnTestSet
print "------------------------------------------------------------"
print "Not done yet: ",
for run_i in not_done:
sys.stdout.write("%i," % run_i)
print
print "------------------------------------------------------------"
if inds > 0:
print "MBF: %.5f" % (totalFitness / float(inds))