Skip to content

Commit e3decc4

Browse files
Add files via upload
1 parent 550138b commit e3decc4

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
"""该绘图程序被段琦琦的博士论文(哈工大与南科大联合培养)所使用。
2+
"""
3+
import numpy as np
4+
import matplotlib
5+
import matplotlib.pyplot as plt
6+
7+
from pypop7.benchmarks.utils import generate_xyz
8+
# abstract class for all evolution Strategies
9+
from pypop7.optimizers.es.es import ES
10+
# Matrix Adaptation Evolution Strategy
11+
from pypop7.optimizers.es.maes import MAES
12+
13+
14+
matplotlib.rcParams['font.family'] = 'sans-serif'
15+
matplotlib.rcParams['font.sans-serif'] = 'SimSun'
16+
# matplotlib.rcParams['font.size'] = 10 #
17+
18+
19+
def cd(x): # from https://arxiv.org/pdf/1610.00040v1.pdf
20+
return 7.0 * (x[0] ** 2) + 6.0 * x[0] * x[1] + 8.0 * (x[1] ** 2)
21+
22+
23+
# helper function for 2D-plotting
24+
def plot_contour(func, x, y):
25+
x, y, z = generate_xyz(func, x, y, 200)
26+
plt.contourf(x, y, z, cmap='bone')
27+
plt.contour(x, y, z, colors='white', alpha=0.5)
28+
29+
30+
def plot(xs, means, bound=[-10.0, 10.0]):
31+
for i in range(len(xs)):
32+
plt.figure(figsize=(2.5, 2.5))
33+
plt.title('不可分函数', fontsize=10)
34+
plt.xlim(bound)
35+
plt.ylim(bound)
36+
plt.xticks(fontsize=10)
37+
plt.yticks(fontsize=10)
38+
plot_contour(cd, bound, bound)
39+
plt.scatter(xs[i][:, 0], xs[i][:, 1], color='green')
40+
plt.scatter(means[i][0], means[i][1], color='red')
41+
plt.xlabel('维度1', fontsize=10)
42+
plt.ylabel('维度2', fontsize=10, labelpad=-1)
43+
plt.savefig(str(i) + '.png', dpi=700, bbox_inches='tight')
44+
plt.show()
45+
46+
47+
class PlotMaes(MAES):
48+
def optimize(self, fitness_function=None, args=None):
49+
fitness = ES.optimize(self, fitness_function)
50+
z, d, mean, s, tm, y = self.initialize()
51+
xs, means = [], [] # only for plotting
52+
while not self._check_terminations():
53+
z, d, y = self.iterate(z, d, mean, tm, y, args)
54+
if self.saving_fitness and (not self._n_generations % self.saving_fitness):
55+
xs.append(self.sigma * d + mean) # only for plotting
56+
means.append(mean.copy()) # only for plotting
57+
mean, s, tm = self._update_distribution(z, d, mean, s, tm, y)
58+
self._print_verbose_info(fitness, y)
59+
self._n_generations += 1
60+
res = self._collect(fitness, y, mean)
61+
return res, xs, means
62+
63+
64+
if __name__ == '__main__':
65+
ndim_problem = 2
66+
problem = {'fitness_function': cd,
67+
'ndim_problem': ndim_problem,
68+
'lower_boundary': -10.0 * np.ones((ndim_problem,)),
69+
'upper_boundary': 10.0 * np.ones((ndim_problem,))}
70+
options = {'max_function_evaluations': 3e3,
71+
'n_individuals': 200,
72+
'seed_rng': 2022,
73+
'x': (7.0, -7.0),
74+
'sigma': 0.05,
75+
# to record best-so-far fitness every 50 function evaluations
76+
'saving_fitness': 4,
77+
'is_restart': False}
78+
_, xs, means = PlotMaes(problem, options).optimize()
79+
plot(xs, means)

0 commit comments

Comments
 (0)