1. 程式人生 > 實用技巧 >遺傳演算法 初步認識(一)

遺傳演算法 初步認識(一)

簡介

對什麼蟻群演算法啦,遺傳演算法啦...... 很感興趣,遂學習一波
參考文章 核心連結 連結 https://www.zhihu.com/search?q=遺傳演算法&utm_content=search_history&type=content
參考文章附帶的程式碼 https://github.com/yanshengjia/artificial-intelligence/blob/master/genetic-algorithm-for-functional-maximum-problem/matlab-ga

開始個人理解

首先 個人用python 重寫了 matlab

目標函式

求解函式 f(x) = x + 10*sin(5*x) + 7*cos(4*x) 在區間[0,9]的最大值

先來看看 目標函式的的校驗吧 看看 在 0 - 9的區間上的值分佈

測試程式碼

    def testTarget(self):
        plt.figure(1)
        x = np.linspace(0, 9, 10000)
        x = x.astype('float')
        y = []
        for i in range(len(x)):
            y.append(self.targetFun(x[i]))
        print(x)
        print(y)
        plt.plot(x, y)
        plt.show()

result

可以看出在8附近達到最大值為25
如果我遺傳演算法最後的結果在這個區間內的話,那麼就可以說是正確答案