-
Notifications
You must be signed in to change notification settings - Fork 22
/
report.py
72 lines (59 loc) · 2.45 KB
/
report.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
import ga_params
if ga_params.draw_plot:
import matplotlib.pyplot as plt
if ga_params.export_spreadsheet:
from openpyxl import Workbook
from datetime import datetime
import re
class Reporter:
plot_x_axis = []
plot_y_axis = []
sheet_wb = None
sheet_ws = None
sheet_dest = 'output'
sheet_dest_ext = '.xlsx'
def __init__(self, run_name: str):
if ga_params.draw_plot:
plt.ion()
self.fig, self.subplot = plt.subplots()
self.fig_node_connector, self.subplot2 = plt.subplots()
if ga_params.export_spreadsheet:
self.init_spreadsheet(run_name=run_name)
def plot_draw(self, x_axis: list, y_axis: list, latest_result):
y_axis_parsed = []
for y in y_axis:
try:
y_axis_parsed.append(y['best'])
except:
break
if y_axis_parsed:
y_axis = y_axis_parsed
plt.figure(1)
# self.subplot.set_xlim([self.plot_x_axis[0], self.plot_x_axis[-1]])
self.subplot.set_ylim([min(y_axis) - 5, max(y_axis) + 5])
plt.suptitle('Best solution so far: ' + re.sub("(.{64})", "\\1\n", str(latest_result), 0, re.DOTALL),
fontsize=10)
print(latest_result)
self.subplot.plot(x_axis, y_axis)
plt.figure(2)
for route_x, route_y in latest_result.plot_get_route_cords():
plt.plot(route_x, route_y)
plt.draw()
self.fig.savefig("plot-output.png")
self.fig_node_connector.savefig("plot2-output.png")
plt.pause(0.000001)
def init_spreadsheet(self, run_name: str):
datetime_str = datetime.now().strftime('%b%d-%H:%M')
# datetime_str = '1'
self.sheet_dest = self.sheet_dest + '_' + run_name + '_' + datetime_str + self.sheet_dest_ext
self.sheet_wb = Workbook()
self.sheet_ws = self.sheet_wb.active
self.sheet_ws.append(['gen_index', 'best', 'average', 'std', 'worst', 'created_time', 'computation_time',
'best_route'])
def export_spreadsheet(self, x_axis: list, y_axis: list):
datetime_str = str(datetime.now())
for x, y in zip(x_axis, y_axis):
self.sheet_ws.append([x, y['best'].value, y['average'], y['std'], y['worst'].value, datetime_str,
str(y['process_time']), str(y['best'].route)])
# Save the file
self.sheet_wb.save(filename=self.sheet_dest)