差分進化算法解決有約束優化問題(Python實現)

差分進化算法(Differential Evolution)是演化算法(evolutionary algorithms,簡稱EAs)的成員之一。EAs的成員還包括著名的遺傳算法(Genetic Algorithm)等。

DE在某些問題上表現非常良好,值得一試。

這裏演示用 scikit-opt 實現差分進化算法

Step1:定義你的問題,

這裏定義了一個有約束優化問題

minf(x1,x2,x3)=x12+x22+x32min f(x1, x2, x3) = x1^2 + x2^2 + x3^2
s.t.
x1x2>=1x1x2 >= 1
x1x2<=5x1x2 <= 5
x2+x3=1x2+x3 = 1
0<=x1,x2,x3<=50 <= 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)

以上代碼全部整理到了 GitHub

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章