想要學習啓發式算法?推薦你看看這個價值極高的開源項目

許多學習算法的開發者在刷題或者練習的過程當中都會遇到啓發式算法,若是你剛好也正在學習算法,那麼今天 Gitee 介紹的這款開源項目必定能對你的學習過程有所幫助,幫你更好的理解啓發式算法。git

啓發式算法(heuristic algorithm)是相對於最優化算法提出的。一個問題的最優算法求得該問題每一個實例的 最優解 。啓發式算法能夠這樣定義:一個基於直觀或經驗構造的算法,在可接受的花費(指計算時間和空間)下給出待解決組合優化問題每個實例的一個可行解,該可行解與最優解的偏離程度通常不能被預計。

項目名稱: scikit-opt算法

項目做者: guofei9987函數

開源許可協議: MIT學習

項目地址:https://gitee.com/guofei9987/scikit-opt優化

項目簡介

該項目是一個封裝了7種啓發式算法的 Python 代碼庫,包含了差分進化算法、遺傳算法、粒子羣算法、模擬退火算法、蟻羣算法、魚羣算法、免疫優化算法。spa

特性

  • UDF(用戶自定義算子)
  • GPU 加速
  • 斷點繼續運行

算法演示

差分進化算法

1.定義你的問題code

'''
min f(x1, x2, x3) = x1^2 + x2^2 + x3^2
s.t.
    x1\*x2 >= 1
    x1\*x2 <= 5
    x2 + x3 = 1
    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\] -

2.作差分進化算法blog

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.定義你的問題ci

import numpy as np
def schaffer(p):
    '''
    This function has plenty of local minimum, with strong shocks
    global minimum at (0,0) with value 0
    '''
    x1, x2 = p
    x = np.square(x1) + np.square(x2)
    return 0.5 + (np.sin(x) - 0.5) / np.square(1 + 0.001 \* x

2.運行遺傳算法開發

from sko.GA import GA

ga = GA(func=schaffer, n\_dim=2, size\_pop=50, max\_iter=800, lb=\[-1, -1\], ub=\[1, 1\], precision=1e-7)
best\_x, best\_y = ga.run()
print('best\_x:', best\_x, '\\n', 'best\_y:', best\_y)

3.用 matplotlib 畫出結果

import pandas as pd
import matplotlib.pyplot as plt
Y\_history = pd.DataFrame(ga.all\_history\_Y)
fig, ax = plt.subplots(2, 1)
ax\[0\].plot(Y\_history.index, Y\_history.values, '.', color='red')
Y\_history.min(axis=1).cummin().plot(kind='line')
plt.show()

粒子羣算法

1.定義問題

def demo\_func(x):
    x1, x2, x3 = x
    return x1 \*\* 2 + (x2 - 0.05) \*\* 2 + x3 \*\* 2

2.作粒子羣算法

from sko.PSO import PSO
pso = PSO(func=demo\_func, dim=3, pop=40, max\_iter=150, lb=\[0, -1, 0.5\], ub=\[1, 1, 1\], w=0.8, c1=0.5, c2=0.5)
pso.run()
print('best\_x is ', pso.gbest\_x, 'best\_y is', pso.gbest\_y)

3.畫出結果

import matplotlib.pyplot as plt

plt.plot(pso.gbest\_y\_hist)
plt.show()

image.png

模擬退火算法

模擬退火算法用於多元函數優化

1.定義問題

demo\_func = lambda x: x\[0\] \*\* 2 + (x\[1\] - 0.05) \*\* 2 + x\[2\] \*\* 2

2.運行模擬退火算法

from sko.SA import SA
sa = SA(func=demo\_func, x0=\[1, 1, 1\], T\_max=1, T\_min=1e-9, L=300, max\_stay\_counter=150)
best\_x, best\_y = sa.run()
print('best\_x:', best\_x, 'best\_y', best\_y)

3.畫出結果

import matplotlib.pyplot as plt
import pandas as pd
plt.plot(pd.DataFrame(sa.best\_y\_history).cummin(axis=0))
plt.show()

蟻羣算法

蟻羣算法(ACA, Ant Colony Algorithm)解決TSP問題

from sko.ACA import ACA\_TSP
aca = ACA\_TSP(func=cal\_total\_distance, n\_dim=num\_points,
              size\_pop=50, max\_iter=200,
              distance\_matrix=distance\_matrix)
best\_x, best\_y = aca.run(

免疫優化算法

from sko.IA import IA\_TSP

ia\_tsp = IA\_TSP(func=cal\_total\_distance, n\_dim=num\_points, size\_pop=500, max\_iter=800, prob\_mut=0.2,
                T=0.7, alpha=0.95)
best\_points, best\_distance = ia\_tsp.run()
print('best routine:', best\_points, 'best\_distance:', best\_distance)

人工魚羣算法

def func(x):
    x1, x2 = x
    return 1 / x1 \*\* 2 + x1 \*\* 2 + 1 / x2 \*\* 2 + x2 \*\* 2
from sko.AFSA import AFSA
afsa = AFSA(func, n\_dim=2, size\_pop=50, max\_iter=300,
            max\_try\_num=100, step=0.5, visual=0.3,
            q=0.98, delta=0.5)
best\_x, best\_y = afsa.run()
print(best\_x, best\_

想要學習更多算法類開源項目?點擊後面的連接前往 Gitee 看看:https://gitee.com/explore/mathlibs

相關文章
相關標籤/搜索