-
Notifications
You must be signed in to change notification settings - Fork 0
/
Reporter.py
39 lines (34 loc) · 1.38 KB
/
Reporter.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
import sys
import time
# Class to report basic results of an evolutionary algorithm
class Reporter:
def __init__(self, filename):
self.allowedTime = 300
self.numIterations = 0
self.filename = filename + ".csv"
self.delimiter = ','
self.startTime = time.time()
self.writingTime = 0
outFile = open(self.filename, "w")
outFile.write("# Student number: " + filename + "\n")
outFile.write("# Iteration, Elapsed time, Mean value, Best value, Cycle\n")
outFile.close()
# Append the reported mean objective value, best objective value, and the best tour
# to the reporting file.
#
# Returns the time that is left in seconds as a floating-point number.
def report(self, meanObjective, bestObjective, bestSolution):
if (time.time() - self.startTime < self.allowedTime + self.writingTime):
start = time.time()
outFile = open(self.filename, "a")
outFile.write(str(self.numIterations) + self.delimiter)
outFile.write(str(start - self.startTime - self.writingTime) + self.delimiter)
outFile.write(str(meanObjective) + self.delimiter)
outFile.write(str(bestObjective) + self.delimiter)
for i in range(bestSolution.size):
outFile.write(str(bestSolution[i]) + self.delimiter)
outFile.write('\n')
outFile.close()
self.numIterations += 1
self.writingTime += time.time() - start
return (self.allowedTime + self.writingTime) - (time.time() - self.startTime)