差分進化算法(Differential Evolution)是演化算法(evolutionary algorithms,簡稱EAs)的成員之一。EAs的成員還包括著名的遺傳算法(Genetic Algorithm)等。
DE在某些問題上表現非常良好,值得一試。
這裏演示用 scikit-opt 實現差分進化算法
Step1:定義你的問題,
這裏定義了一個有約束優化問題
s.t.
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)