ECF 1.7
convergence.py
1import math
2import os.path
3from os import path
4
6
7 def __init__(self):
8 self.generations = []
9 self.times = []
10 self.evals = []
11 self.mins = []
12 self.maxs = []
13 self.avgs = []
14
15 def readLogFile(self, fileName):
16 try:
17 logFile = open(fileName, 'r')
18 except IOError:
19 print("Couldn't open the file ", fileName)
20 quit()
21
22 generation = []
23 eval = []
24 time = []
25 max = []
26 min = []
27 avg = []
28 std = []
29 inGeneration = False
30 readMax = readMin = readAvg = readStd = False
31 readEval = readTime = False
32
33 for line in logFile:
34 items = [word for word in line.split()]
35 if(len(items) == 0):
36 continue
37 if(items[0] == "Generation:"):
38 generation.append(int(items[1]))
39 inGeneration = True
40 if(not readEval and items[0] == "Evaluations:"):
41 eval.append(int(items[1]))
42 readEval = True
43 if(not readTime and items[0] == "Elapsed"):
44 time.append(int(items[2]))
45 readTime = True
46 if(not readMax and items[0] == "max:"):
47 max.append(float(items[1]))
48 readMax = True
49 if(not readMin and items[0] == "min:"):
50 min.append(float(items[1]))
51 readMin = True
52 if(not readAvg and items[0] == "avg:"):
53 avg.append(float(items[1]))
54 readAvg = True
55 if(not readStd and items[0] == "stdev:"):
56 std.append(float(items[1]))
57 readStd = True
58 if(inGeneration and readMax and readMin and readAvg and readStd):
59 inGeneration = readMax = readMin = readAvg = readStd = False
60 readEval = readTime = False
61
62 logFile.close()
63 self.generations.append(generation)
64 self.times.append(time)
65 self.evals.append(eval)
66 self.mins.append(min)
67 self.maxs.append(max)
68 self.avgs.append(avg)
69# print("eval:", eval)
70# print("time:", time)
71# print("max:", max)
72# print("min:", min)
73# print(len(self.evals))
74
75
76 def getPartialLog(self, percentage):
77 stats = []
78 for i in range(len(self.generations)):
79 logNo = int((len(self.generations[i]) - 1) * percentage);
80 stat = []
81 stat.append(self.mins[i][logNo])
82 stat.append(self.maxs[i][logNo]);
83 stat.append(self.avgs[i][logNo]);
84 stat.append(self.generations[i][logNo]);
85 stat.append(self.evals[i][logNo]);
86 stat.append(self.times[i][logNo]);
87 stats.append(stat);
88 return stats;
89
90 def getMean(self, values):
91 sum = float(0)
92 for i in range(len(values)):
93 sum += values[i]
94 return sum / len(values)
95
96
97 def getStdDev(self, values):
98 if len(values) == 0:
99 return 0
100 numerator = float(0)
101 average = self.getMean(values)
102 for i in range(len(values)):
103 numerator += ((values[i] - average) * (values[i] - average));
104 denominator = (len(values) - 1)
105 return math.sqrt(numerator / denominator)
106
107
108 def getMin(self, values):
109 min = values[0]
110 for i in range(len(values)):
111 if min > values[i]:
112 min = values[i]
113 return min
114
115
116 def getMax(self, values):
117 max = values[0]
118 for i in range(len(values)):
119 if max < values[i]:
120 max = values[i]
121 return max
122
123
124 def getMedian(self, values):
125 values.sort()
126 if len(values) % 2 == 0:
127 return (values[int(len(values) / 2) - 1] + values[int(len(values) / 2)]) / 2
128 else:
129 return values[int(len(values) / 2)]
130
131
132def printStr(FloatNumber, Precision):
133 return "%0.*f" % (Precision, FloatNumber)
134
135
136
137
138# main
139
140# default vrijednosti
141logBase = "log_";
142logExt = ".txt";
143outFile = "convergence.txt";
144logStartNum = 1;
145logEndNum = 0;
146nSamples = 50;
147nDigits = 2;
148
149line = input(" -- Log filename base (enter - \"" + logBase + "\") : ")
150if line != "":
151 logBase = line
152
153line = input(" -- Starting log number (enter - " + str(logStartNum) + ") : ")
154if line != "":
155 logStartNum = int(line)
156
157line = input(" -- End log number (enter - read all): ")
158if line != "":
159 logEndNum = int(line)
160
161line = input(" -- Log filename num of digits (enter - 2) : ")
162if line != "":
163 nDigits = int(line)
164
165line = input(" -- Extension (enter - \"" + logExt + "\") : ")
166if line != "":
167 logExt = line
168
169line = input(" -- Output file (enter - \"" + outFile + "\") : ")
170if line != "":
171 outFile = line
172
173line = input(" -- Number of samples (enter - " + str(nSamples) + ") : ")
174if line != "":
175 nSamples = int(line)
176
177
178logNum = logStartNum
179rs = ReadStats()
180
181while True:
182 fileName = ""
183 fileName += logBase
184 fileName += str(logNum).zfill(nDigits)
185 fileName += logExt
186
187 print("Log filename: ", fileName)
188 opened = False
189 if path.exists(fileName):
190 print("Found!")
191 opened = True
192 rs.readLogFile(fileName)
193 else:
194 print("File not found!")
195
196 logNum += 1
197 cont = False
198 if logEndNum >= logNum or opened == True:
199 cont = True
200 if not cont:
201 break
202
203#rs.readLogFile(fileName)
204
205vAvgMin, vAvgMax, vMedMin, vMedMax, vMaxMin, vMinMax, vMinMin, vMaxMax, vAvgAvg = ([] for i in range(9))
206vHits = []
207vGens = []
208vEvals = []
209sample = 0
210nSamples = 10
211while sample < nSamples:
212 percentage = 1. * sample / nSamples
213 stats = rs.getPartialLog(percentage)
214 nRuns = len(stats)
215 avg_min = 0.
216 avg_max = 0.
217 mins = []
218 maxs = [] # vektor minimuma i maksimuma u ovom trenutku
219 avgs = [] # vektor srednjih vrijednosti u ovom trenutku
220 for runId in range(nRuns):
221 curr_min = stats[runId][0]
222 curr_max = stats[runId][1]
223 mins.append(stats[runId][0])
224 maxs.append(stats[runId][1])
225 avgs.append(stats[runId][2])
226
227 vAvgMin.append(rs.getMean(mins))
228 vAvgMax.append(rs.getMean(maxs))
229 vMedMin.append(rs.getMedian(mins))
230 vMedMax.append(rs.getMedian(maxs))
231 vMinMin.append(rs.getMin(mins))
232 vMaxMin.append(rs.getMax(mins))
233 vMinMax.append(rs.getMin(maxs))
234 vMaxMax.append(rs.getMax(maxs))
235 vAvgAvg.append(rs.getMean(avgs))
236 vHits.append(0)
237 vGens.append(stats[0][3])
238 vEvals.append(stats[0][4])
239 sample += 1
240
241
242file = open(outFile,"w")
243file.write("recId\tgen\teval\tmin_min\tavg_min\tmed_min\tmax_min\tmin_max\tavg_max\tmed_max\tmax_max\thits\tavg_avg\n")
244for sample in range(nSamples):
245 output = str(sample) + "\t" + str(vGens[sample]) + "\t" + str(vEvals[sample]) + "\t"
246 output += str(vMinMin[sample]) + "\t" + str(vAvgMin[sample]) + "\t" + str(vMedMin[sample]) + "\t" + str(vMaxMin[sample]) + "\t"
247 output += str(vMinMax[sample]) + "\t" + str(vAvgMax[sample]) + "\t" + str(vMedMax[sample]) + "\t" + str(vMaxMax[sample]) + "\t"
248 output += str(vHits[sample]) + "\t" + str(vAvgAvg[sample]) + "\n"
249 file.write(output)
250file.close()
getMean(self, values)