-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplot.py
More file actions
116 lines (95 loc) · 2.36 KB
/
plot.py
File metadata and controls
116 lines (95 loc) · 2.36 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
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
from pandas import DataFrame
import seaborn
from deap import tools
from matplotlib import pyplot as plt
from pprint import pprint
from itertools import chain
from toolboxes import nsgaToolbox, singleObjToolbox
from algorithms import runGA, runRandom
ITERATIONS = 3
GENERATIONS = 50
# Make a plot figure
fig, ax = plt.subplots()
plot_colors = seaborn.color_palette("Set1", n_colors=20)
randomScores = []
randomCosts = []
fronts = []
singleObjHallOfFame = tools.HallOfFame(GENERATIONS * ITERATIONS)
for i in range(ITERATIONS):
# Run the random algorithm
randomGenerations, fitnessesPerGen = runRandom(maxGen = GENERATIONS)
for gen in fitnessesPerGen:
for fitness in gen:
randomScores.append(fitness[0])
randomCosts.append(fitness[1])
# Run single objective
singleResult, singleLogbook = runGA(
singleObjToolbox,
maxGen = GENERATIONS,
hallOfFame = singleObjHallOfFame
)
# Run NSGA2
nsgaResult, nsgaLogbook = runGA(nsgaToolbox, maxGen = GENERATIONS)
front = tools.emo.sortLogNondominated(
nsgaResult,
len(nsgaResult),
first_front_only = True
)
# Fitness for every front
fronts.append(list(nsgaToolbox.evaluate(ind) for ind in front))
# Plot results from random
seaborn.kdeplot(
randomScores,
randomCosts,
ax = ax,
cmap = 'Blues',
)
plt.scatter(
randomScores,
randomCosts,
label = '(blue) Random',
s = 3,
color = 'b',
alpha = .5 / ITERATIONS
)
# Plot fronts from NSGA2
flatNsgaFronts = list(chain(*fronts))
nsgaDf = DataFrame(flatNsgaFronts)
seaborn.kdeplot(
nsgaDf[nsgaDf.columns[0]],
nsgaDf[nsgaDf.columns[1]],
ax = ax,
cmap = 'Reds',
)
plt.scatter(
nsgaDf[nsgaDf.columns[0]],
nsgaDf[nsgaDf.columns[1]],
label = '(red) NSGA2 Last Fronts'.format(ITERATIONS),
s = 3,
color = 'r',
alpha = .3
)
# Get score and cost of single obj
singleFits = list(map(nsgaToolbox.evaluate, singleObjHallOfFame))
singleFitsDf = DataFrame(singleFits)
print(singleFits)
# Plot single objective
seaborn.kdeplot(
singleFitsDf[singleFitsDf.columns[0]],
singleFitsDf[singleFitsDf.columns[1]],
ax = ax,
cmap = 'Greens',
)
plt.scatter(
singleFitsDf[singleFitsDf.columns[0]],
singleFitsDf[singleFitsDf.columns[1]],
label = '(green) Single Objective Hall Of Fame',
s = 3,
color = 'g',
alpha = 1.0 / ITERATIONS
)
plt.xlabel('Score')
plt.ylabel('Cost')
plt.title('Results for {} iterations, {} generations.'.format(ITERATIONS, GENERATIONS))
plt.legend()
plt.show()