-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot.py
89 lines (81 loc) · 3.01 KB
/
plot.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
import matplotlib.pyplot as plt
import numpy as np
from results import *
# # there are 504 different parameter settings.
parameter_list = []
for c1c2_temp in range(1, 41):
for w_temp in range(-9, 10):
c1c2 = c1c2_temp / 10
w = w_temp / 10
if c1c2 < (24 * (1 - np.square(w)) / (7 - 5 * w)):
parameter_list.append((w, c1c2))
def q1_plot():
cmap = plt.colormaps["plasma_r"]
fig, ax = plt.subplots()
x = [x for (_, x) in parameter_list]
y = [y for (y, _) in parameter_list]
area = [800 / a for (a, _, _) in results_rastrigin]
# normalize fitness values
fit_raw = np.array([fit for (_, fit, _) in results_rastrigin])
fit_range = max(fit_raw) - min(fit_raw)
fit_min = min(fit_raw)
fit = np.array([(f - fit_min) / fit_range for f in fit_raw])
sc = ax.scatter(x, y, s=area, c=fit, cmap=cmap)
plt.colorbar(sc)
ax.set_title('Rastrigin Function Parameters')
ax.set_xlabel('alpha1 + alpha2')
ax.set_ylabel('omega')
plt.show()
fig, ax = plt.subplots()
x = [x for (_, x) in parameter_list]
y = [y for (y, _) in parameter_list]
area = [800 / a for (a, _, _) in results_sphere]
# normalize fitness values
fit_raw = np.array([fit for (_, fit, _) in results_sphere])
fit_range = max(fit_raw) - min(fit_raw)
fit_min = min(fit_raw)
fit = np.array([(f - fit_min) / fit_range for f in fit_raw])
sc = ax.scatter(x, y, s=area, c=fit, cmap=cmap)
plt.colorbar(sc)
ax.set_title('Sphere Function Parameters')
ax.set_xlabel('alpha1 + alpha2')
ax.set_ylabel('omega')
plt.show()
def q2_plot():
fig, ax = plt.subplots()
print(np.array(q2_result_rastrigin).shape)
# array1 = np.array(q2_result_sphere)
all_data1 = []
for box in q2_result_sphere:
temp = []
for item in box:
temp.append(item) # / (array1.max() - array1.min()) + 1)
all_data1.append(temp)
n_list = [1, 5, 10, 20, 30, 50, 75, 100, 200, 500]
# plot violin plot
ax.violinplot(q2_result_sphere, showmeans=False, showmedians=True)
ax.set_title('Fitness vs. Number of Particle \n Sphere Function')
ax.yaxis.grid(True)
ax.set_xticks([y + 1 for y in range(len(all_data1))], labels=n_list)
ax.set_xlabel('Number of particles')
ax.set_ylabel('Fitness values')
plt.show()
fig, ax = plt.subplots()
# # generate some random test data
# array2 = np.array(q2_result_rastrigin)
all_data2 = []
for box in q2_result_rastrigin:
temp = []
for item in box:
temp.append(item) # / (array1.max() - array1.min()) + 1)
all_data2.append(temp)
n_list = [1, 5, 10, 20, 30, 50, 75, 100, 200, 500]
# plot violin plot
ax.violinplot(all_data2, showmeans=False, showmedians=True)
ax.set_title('Fitness vs. Number of Particle \n Rastrigin Function')
ax.yaxis.grid(True)
ax.set_xticks([y + 1 for y in range(len(all_data2))], labels=n_list)
ax.set_xlabel('Number of particles')
ax.set_ylabel('Fitness values')
plt.show()
q2_plot()