-
-
Notifications
You must be signed in to change notification settings - Fork 152
/
plot_operators.py
78 lines (59 loc) · 3 KB
/
plot_operators.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
# Generates a plot showing the distributions of six real-valued operators.
import matplotlib.pyplot as plt
from platypus import (PCX, PM, SBX, SPX, UM, UNDX, DifferentialEvolution,
GAOperator, Problem, Real, Solution)
problem = Problem(2, 0)
problem.types[:] = Real(-1, 1)
solution1 = Solution(problem)
solution1.variables[:] = [-0.25, -0.25]
solution2 = Solution(problem)
solution2.variables[:] = [0.25, -0.25]
solution3 = Solution(problem)
solution3.variables[:] = [0.0, 0.25]
def generate(variator, parents):
result = []
while len(result) < 10000:
result.extend(variator.evolve(parents))
return to_points(result)
def to_points(solutions):
return [s.variables[0] for s in solutions], [s.variables[1] for s in solutions]
fig, axarr = plt.subplots(2, 3)
options = {"s": 0.1, "alpha": 0.5}
parent_options = {"s": 25, "color": "b"}
axarr[0, 0].scatter(*generate(GAOperator(SBX(1.0, 20.0), PM(0.5, 250.0)), [solution1, solution3]), **options)
axarr[0, 0].scatter(*to_points([solution1, solution3]), **parent_options)
axarr[0, 0].set_title("SBX")
axarr[0, 1].scatter(*generate(GAOperator(DifferentialEvolution(1.0, 1.0), PM(0.5, 100.0)), [solution3, solution2, solution1, solution3]), **options)
axarr[0, 1].scatter(*to_points([solution3, solution2, solution1, solution3]), **parent_options)
axarr[0, 1].set_title("DE")
axarr[0, 2].scatter(*generate(UM(0.5), [solution1]), **options)
axarr[0, 2].scatter(*to_points([solution1]), **parent_options)
axarr[0, 2].set_title("UM")
axarr[1, 0].scatter(*generate(PCX(3, 2), [solution1, solution2, solution3]), **options)
axarr[1, 0].scatter(*to_points([solution1, solution2, solution3]), **parent_options)
axarr[1, 0].set_title("PCX")
axarr[1, 1].scatter(*generate(UNDX(3, 2), [solution1, solution2, solution3]), **options)
axarr[1, 1].scatter(*to_points([solution1, solution2, solution3]), **parent_options)
axarr[1, 1].set_title("UNDX")
axarr[1, 2].scatter(*generate(SPX(3, 2), [solution1, solution2, solution3]), **options)
axarr[1, 2].scatter(*to_points([solution1, solution2, solution3]), **parent_options)
axarr[1, 2].set_title("SPX")
# add arrow annotations to DE
axarr[0, 1].annotate("",
xy=solution3.variables, xycoords='data',
xytext=solution1.variables, textcoords='data',
arrowprops=dict(arrowstyle="fancy",
connectionstyle="arc3",
color="0.75"))
axarr[0, 1].annotate("",
xy=[solution2.variables[i] + (solution3.variables[i] - solution1.variables[i]) for i in range(2)], xycoords='data',
xytext=solution2.variables, textcoords='data',
arrowprops=dict(arrowstyle="fancy",
connectionstyle="arc3",
color="0.75"))
for i in range(1):
for j in range(3):
axarr[i, j].set_xticklabels([])
axarr[i, j].set_yticklabels([])
axarr[i, j].autoscale(tight=True)
plt.show()