-
Notifications
You must be signed in to change notification settings - Fork 1
/
visualize.py
41 lines (32 loc) · 1.21 KB
/
visualize.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
from __future__ import print_function
import copy
import warnings
import graphviz
import matplotlib.pyplot as plt
import numpy as np
def plot_stats(statistics, ylog=False, view=False, filename="avg_fitness.svg"):
"""Plots the population's average and best fitness."""
if plt is None:
warnings.warn(
"This display is not available due to a missing optional dependency (matplotlib)"
)
return
generation = range(len(statistics.most_fit_genomes))
best_fitness = [c.fitness for c in statistics.most_fit_genomes]
avg_fitness = np.array(statistics.get_fitness_mean())
stdev_fitness = np.array(statistics.get_fitness_stdev())
plt.plot(generation, avg_fitness, "b-", label="average")
plt.plot(generation, avg_fitness - stdev_fitness, "g-.", label="-1 sd")
plt.plot(generation, avg_fitness + stdev_fitness, "g-.", label="+1 sd")
plt.plot(generation, best_fitness, "r-", label="best")
plt.title("Population's average and best fitness")
plt.xlabel("Generations")
plt.ylabel("Fitness")
plt.grid()
plt.legend(loc="best")
if ylog:
plt.gca().set_yscale("symlog")
plt.savefig(filename)
if view:
plt.show()
plt.close()