差分进化算法(Differential Evolution)是演化算法(evolutionary algorithms,简称EAs)的成员之一。EAs的成员还包括著名的遗传算法(Genetic Algorithm)等。
DE在某些问题上表现非常良好,值得一试。
这里演示用 scikit-opt 实现差分进化算法
Step1:定义你的问题,
这里定义了一个有约束优化问题
m
i
n
f
(
x
1
,
x
2
,
x
3
)
=
x
1
2
+
x
2
2
+
x
3
2
min f(x1, x2, x3) = x1^2 + x2^2 + x3^2
minf(x1,x2,x3)=x12+x22+x32
s.t.
x
1
x
2
>
=
1
x1x2 >= 1
x1x2>=1
x
1
x
2
<
=
5
x1x2 <= 5
x1x2<=5
x
2
+
x
3
=
1
x2+x3 = 1
x2+x3=1
0
<
=
x
1
,
x
2
,
x
3
<
=
5
0 <= x1, x2, x3 <= 5
0<=x1,x2,x3<=5
def obj_func(p):
x1, x2, x3 = p
return x1 ** 2 + x2 ** 2 + x3 ** 2
constraint_eq = [
lambda x: 1 - x[1] - x[2]
]
constraint_ueq = [
lambda x: 1 - x[0] * x[1],
lambda x: x[0] * x[1] - 5
]
Step2: 做差分进化算法
from sko.DE import DE
de = DE(func=obj_func, n_dim=3, size_pop=50, max_iter=800, lb=[0, 0, 0], ub=[5, 5, 5],
constraint_eq=constraint_eq, constraint_ueq=constraint_ueq)
best_x, best_y = de.run()
print('best_x:', best_x, '\n', 'best_y:', best_y)
打印的结果非常接近真实最优值(1,1,0)