-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallbacks.py
138 lines (100 loc) · 4.92 KB
/
callbacks.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
import numpy as np
from .core import GeneticAlgorithm, CallbackBase
import matplotlib.pyplot as plt
class GAStatus(CallbackBase):
def __init__(self, fig: plt.Figure):
super(GAStatus, self).__init__()
self._fig = fig
def __call__(self, ga: GeneticAlgorithm) -> None:
self._fig.canvas.set_window_title('Current generation: {}'.format(ga.current_generation))
self._fig.canvas.draw()
class MultiObjectiveReport(CallbackBase):
def __init__(self, ax: plt.Axes):
super(MultiObjectiveReport, self).__init__()
self._ax = ax
def __call__(self, ga: 'GeneticAlgorithm') -> None:
self._ax.lines = []
self._ax.plot(*ga.capture(-1).objectives.transpose(), 'r+')
class BestReport(CallbackBase):
def __call__(self, ga: GeneticAlgorithm) -> None:
offspring = ga.capture(-1)
print('{gen_i:3d}: {ind}'.format(
gen_i=ga.current_generation,
ind=offspring.genes[offspring.fitnesses.argmax()]
))
class BestImgReport(CallbackBase):
def __init__(self, ax: plt.Axes = None):
super(BestImgReport, self).__init__()
if ax is None:
self._fig, self._ax = plt.subplots(1, 1)
else:
self._fig, self._ax = ax.figure, ax
def __call__(self, ga: GeneticAlgorithm) -> None:
offspring = ga.capture(-1)
best_i = offspring.fitnesses.argmax()
self._ax.imshow(offspring.genes[best_i], cmap='gray')
class HistoryReport(CallbackBase):
def __init__(self, fitness_axes: plt.Axes = None, *objective_axes: plt.Axes):
super(HistoryReport, self).__init__()
if fitness_axes is None:
_, self._fitness_axes = plt.subplots(1, 1)
else:
self._fitness_axes = fitness_axes
self._objective_axes = objective_axes if (len(objective_axes) > 0) else None
def __call__(self, ga: GeneticAlgorithm):
self._fitness_axes.clear()
self._fitness_axes.plot(ga.fitness_history)
self._fitness_axes.set_title(
'Current best fitness: {:.6f}'.format(ga.fitness_history[-1])
)
if self._objective_axes is not None:
for column in range(ga.objectives_history.shape[1]):
curr_axes = self._objective_axes[column if (len(self._objective_axes) > 1) else 0]
curr_axes.clear()
curr_axes.plot(ga.objectives_history[:, column])
class PolishedHistoryReport(CallbackBase):
def __init__(self, quantile: float = None, fitness_axes: plt.Axes = None, *objective_axes: plt.Axes):
super(HistoryReport, self).__init__()
self._quantile_fitness = None
self._quantile_objective = None
self._quantile = quantile
if fitness_axes is None:
_, self._fitness_axes = plt.subplots(1, 1)
else:
self._fitness_axes = fitness_axes
self._objective_axes = objective_axes if (len(objective_axes) > 0) else None
def __call__(self, ga: GeneticAlgorithm):
if self._quantile is not None:
if ga.current_generation == 0:
self._quantile_fitness = np.empty(shape=ga.generation_cap, dtype=np.float)
if self._objective_axes is not None:
if (
(len(self._objective_axes) > 1) and
(len(self._objective_axes != ga.objectives_history.shape[1]))
):
raise ValueError('Objectives count does not match objective axes provided in constructor')
self._quantile_objective = np.empty(
shape=(ga.generation_cap,) + ga.objectives_history.shape[1:],
dtype=ga.objectives_history.dtype
)
offspring = ga.capture(-1)
quantile_i = np.argpartition(offspring.fitnesses, int(self._quantile * offspring.size + 0.5))
self._quantile_fitness[ga.current_generation] = offspring.fitnesses[quantile_i]
if self._objective_axes is not None:
self._quantile_objective[ga.current_generation] = offspring.objectives[quantile_i]
self._fitness_axes.clear()
self._fitness_axes.plot(ga.fitness_history)
if self._quantile is not None:
plt.fill_between(
range(ga.current_generation), ga.fitness_history,
self._quantile_fitness[:ga.current_generation], alpha=0.5
)
if self._objective_axes is not None:
for column in range(ga.objectives_history.shape[1]):
curr_axes = self._objective_axes[column if (len(self._objective_axes) > 1) else 0]
curr_axes.clear()
curr_axes.plot(ga.objectives_history[:, column])
curr_axes.fill_between(
range(ga.current_generation), ga.objectives_history[:, column],
self._quantile_objective[:ga.current_generation, column], alpha=0.5
)