-
Notifications
You must be signed in to change notification settings - Fork 6
/
cummulative_success_counts_over_generations.py
executable file
·208 lines (167 loc) · 6.77 KB
/
cummulative_success_counts_over_generations.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
#!/usr/bin/python
import os, sys
from sys import maxint
# Set these before running:
#outputDirectory = "Results/bench-prog-synth/collatz-numbers/parent-selection/lexicase/"
# This allows this script to take a command line argument for outputDirectory
if len(sys.argv) > 1 and sys.argv[1] != "brief":
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 mean(nums):
if len(nums) <= 0:
return False
return sum(nums) / float(len(nums))
# Main area
i = 0
if outputDirectory[-1] != '/':
outputDirectory += '/'
dirList = os.listdir(outputDirectory)
print
print " Directory of results:"
print outputDirectory
bestFitnessesOfRuns = []
testFitnessOfBest = []
testFitnessOfSimplifiedBest = []
errorThreshold = maxint
errorThresholdPerCase = maxint
while (outputFilePrefix + str(i) + outputFileSuffix) in dirList:
sys.stdout.write(str(i))
sys.stdout.write(" ")
sys.stdout.flush()
if i % 50 == 49:
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
for line in f:
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:"):
numCases = len(line.split()) - 1
errorThresholdPerCase = float(errorThreshold) / numCases
if line.startswith(";; -*- Report"):
gen = int(line.split()[-1])
if line.startswith("SUCCESS"):
done = "SUCCESS"
if line.startswith("FAILURE"):
done = "FAILURE"
break
if line.startswith("Mean:") and not done:
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 not 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 done:
try:
simpBestTest = int(line.split()[-1].strip("Nn"))
except ValueError, e:
simpBestTest = float(line.split()[-1].strip("Nn"))
bestFitnessesOfRuns.append((gen, best_mean_error, done))
testFitnessOfBest.append(bestTest)
testFitnessOfSimplifiedBest.append(simpBestTest)
i += 1
print "Error threshold per case:", errorThresholdPerCase
print "-------------------------------------------------"
for i, (gen, fitness, done) in enumerate(bestFitnessesOfRuns):
doneSym = ""
if fitness <= errorThresholdPerCase:
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"
if fitness >= 0.001 or fitness == 0.0:
print "Run: %3i | Gen: %5i | Best Fitness (mean) = %8.4f%s" % (i, gen, fitness, doneSym)
else:
print "Run: %3i | Gen: %5i | Best Fitness (mean) = %.4e%s" % (i, gen, fitness, doneSym)
totalFitness = 0
inds = 0
perfectSolutions = 0
perfectOnTestSet = 0
simpPerfectOnTestSet = 0
trainSolutionGens = []
testSolutionGens = []
simpSolutionGens = []
max_gens = 0
for i, (gen, fitness, done) in enumerate(bestFitnessesOfRuns):
if gen > max_gens:
max_gens = gen
if done:
totalFitness += fitness
inds += 1
if fitness <= errorThresholdPerCase:
perfectSolutions += 1
trainSolutionGens.append(gen)
if len(testFitnessOfBest) > i:
if testFitnessOfBest[i] <= errorThresholdPerCase:
perfectOnTestSet += 1
testSolutionGens.append(gen)
if len(testFitnessOfSimplifiedBest) > i:
if testFitnessOfSimplifiedBest[i] <= errorThresholdPerCase:
simpPerfectOnTestSet += 1
simpSolutionGens.append(gen)
if 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:"
print "Mean: ", mean(testSolutionGens)
print "Minimum: ", min(testSolutionGens)
print "Median: ", median(testSolutionGens)
print "Maximum: ", max(testSolutionGens)
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 "------------------------------------------------------------"
for gen in range(max_gens + 1):
cummulative_successes = len([x for x in simpSolutionGens if x <= gen])
print "successes up to generation %4d: %4d" % (gen, cummulative_successes)