forked from GuilhermeCaeiro/trabmh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.py
More file actions
65 lines (53 loc) · 2.63 KB
/
plot.py
File metadata and controls
65 lines (53 loc) · 2.63 KB
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
import matplotlib.pyplot as plt
import numpy as np
import os
class Plot:
def __init__(self):
pass
@classmethod
def draw_plots(self, environment):
if not environment.generate_plots:
return
self.textstr = '\n'.join((
r'Encoding method= %s' %(environment.encoding,),
r'Selection method= %s' %(environment.selection_method,),
r'Mutation method= %s' %(environment.mutation_method,),
r'Crossover method= %s' %(environment.crossover_method,),
r'Stopping criteria= %s' %(environment.stop_message,),
))
self.plot_time_to_best_sol(environment)
self.plot_best_sol_tracking(environment)
@classmethod
def plot_time_to_best_sol(self, environment):
times= np.array(environment.best_sol_change_times) - environment.start_time
sols = np.unique(environment.best_sol_changes)
plt.plot(times, sols, color='tab:blue')
plt.xlabel("Time (s)")
plt.ylabel("Best solution")
plt.title("Exp. {} - Time to best solution found".format(str(environment.experiment_id)))
plt.axhline(y=environment.best_known_result, color='tab:red', linestyle='-')
# place a text box in bottom right in axes coords
ax = plt.gca()
ax.text(0.4, 0.05, self.textstr, fontsize=10,
verticalalignment='bottom', horizontalalignment='left',
transform=ax.transAxes, bbox=dict(boxstyle='round', alpha=0.4))
file_name = "{}_time_to_best_sol_.png".format(str(environment.experiment_id))
plots_file = os.path.join(environment.plots_dir, file_name)
plt.savefig(plots_file)
plt.close()
@classmethod
def plot_best_sol_tracking(self, environment):
plt.plot([i for i in range(len(environment.best_sol_tracking))], environment.best_sol_tracking, color='tab:blue')
plt.xlabel("Generation")
plt.ylabel("Best solution")
plt.title("Exp. {} - Evolution of Best solution found so far".format(str(environment.experiment_id)))
plt.axhline(y=environment.best_known_result, color='tab:red', linestyle='-')
# place a text box in bottom right in axes coords
ax = plt.gca()
ax.text(0.4, 0.05, self.textstr, fontsize=10,
verticalalignment='bottom', horizontalalignment='left',
transform=ax.transAxes, bbox=dict(boxstyle='round', alpha=0.4))
file_name = "{}_best_sol_tracking_.png".format(str(environment.experiment_id))
plots_file = os.path.join(environment.plots_dir, file_name)
plt.savefig(plots_file)
plt.close()