最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python實現(xiàn)遺傳算法(二進制編碼)求函數(shù)最優(yōu)值方式

 更新時間:2020年02月11日 09:02:34   作者:Mr_Leeeee  
今天小編就為大家分享一篇Python實現(xiàn)遺傳算法(二進制編碼)求函數(shù)最優(yōu)值方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

目標(biāo)函數(shù)

編碼方式

本程序采用的是二進制編碼精確到小數(shù)點后五位,經(jīng)過計算可知對于 其編碼長度為18,對于 其編碼長度為15,因此每個基于的長度為33。

參數(shù)設(shè)置

算法步驟

設(shè)計的程序主要分為以下步驟:1、參數(shù)設(shè)置;2、種群初始化;3、用輪盤賭方法選擇其中一半較好的個體作為父代;4、交叉和變異;5、更新最優(yōu)解;6、對最有個體進行自學(xué)習(xí)操作;7結(jié)果輸出。其算法流程圖為:

算法結(jié)果

由程序輸出可知其最終優(yōu)化結(jié)果為38.85029,

輸出基因編碼為[1 1 0 0 1 0 1 1 1 1 1 1 1 0 1 1 1 1 0 1 0 1 1 0 1 0 0 1 0 1 1 1 1]。

代碼

import numpy as np
import random
import math
import copy

class Ind():
 def __init__(self):
  self.fitness = 0
  self.x = np.zeros(33)
  self.place = 0
  self.x1 = 0
  self.x2 = 0

def Cal_fit(x, upper, lower): #計算適應(yīng)度值函數(shù)
 Temp1 = 0
 for i in range(18):
  Temp1 += x[i] * math.pow(2, i)
 Temp2 = 0
 for i in range(18, 33, 1):
  Temp2 += math.pow(2, i - 18) * x[i]
 x1 = lower[0] + Temp1 * (upper[0] - lower[0])/(math.pow(2, 18) - 1)
 x2 = lower[1] + Temp2 * (upper[1] - lower[1])/(math.pow(2, 15) - 1)
 if x1 > upper[0]:
  x1 = random.uniform(lower[0], upper[0])
 if x2 > upper[1]:
  x2 = random.uniform(lower[1], upper[1])
 return 21.5 + x1 * math.sin(4 * math.pi * (x1)) + x2 * math.sin(20 * math.pi * x2)
def Init(G, upper, lower, Pop): #初始化函數(shù)
 for i in range(Pop):
  for j in range(33):
   G[i].x[j] = random.randint(0, 1)
  G[i].fitness = Cal_fit(G[i].x, upper, lower)
  G[i].place = i
def Find_Best(G, Pop):
 Temp = copy.deepcopy(G[0])
 for i in range(1, Pop, 1):
  if G[i].fitness > Temp.fitness:
   Temp = copy.deepcopy(G[i])
 return Temp

def Selection(G, Gparent, Pop, Ppool): #選擇函數(shù)
 fit_sum = np.zeros(Pop)
 fit_sum[0] = G[0].fitness
 for i in range(1, Pop, 1):
  fit_sum[i] = G[i].fitness + fit_sum[i - 1]
 fit_sum = fit_sum/fit_sum.max()
 for i in range(Ppool):
  rate = random.random()
  Gparent[i] = copy.deepcopy(G[np.where(fit_sum > rate)[0][0]])

def Cross_and_Mutation(Gparent, Gchild, Pc, Pm, upper, lower, Pop, Ppool): #交叉和變異
 for i in range(Ppool):
  place = random.sample([_ for _ in range(Ppool)], 2)
  parent1 = copy.deepcopy(Gparent[place[0]])
  parent2 = copy.deepcopy(Gparent[place[1]])
  parent3 = copy.deepcopy(parent2)
  if random.random() < Pc:
   num = random.sample([_ for _ in range(1, 32, 1)], 2)
   num.sort()
   if random.random() < 0.5:
    for j in range(num[0], num[1], 1):
     parent2.x[j] = parent1.x[j]
   else:
    for j in range(0, num[0], 1):
     parent2.x[j] = parent1.x[j]
    for j in range(num[1], 33, 1):
     parent2.x[j] = parent1.x[j]
   num = random.sample([_ for _ in range(1, 32, 1)], 2)
   num.sort()
   num.sort()
   if random.random() < 0.5:
    for j in range(num[0], num[1], 1):
     parent1.x[j] = parent3.x[j]
   else:
    for j in range(0, num[0], 1):
     parent1.x[j] = parent3.x[j]
    for j in range(num[1], 33, 1):
     parent1.x[j] = parent3.x[j]
  for j in range(33):
   if random.random() < Pm:
    parent1.x[j] = (parent1.x[j] + 1) % 2
   if random.random() < Pm:
    parent2.x[j] = (parent2.x[j] + 1) % 2

  parent1.fitness = Cal_fit(parent1.x, upper, lower)
  parent2.fitness = Cal_fit(parent2.x, upper, lower)
  Gchild[2 * i] = copy.deepcopy(parent1)
  Gchild[2 * i + 1] = copy.deepcopy(parent2)

def Choose_next(G, Gchild, Gsum, Pop): #選擇下一代函數(shù)
 for i in range(Pop):
  Gsum[i] = copy.deepcopy(G[i])
  Gsum[2 * i + 1] = copy.deepcopy(Gchild[i])
 Gsum = sorted(Gsum, key = lambda x: x.fitness, reverse = True)
 for i in range(Pop):
  G[i] = copy.deepcopy(Gsum[i])
  G[i].place = i

def Decode(x):   #解碼函數(shù)
 Temp1 = 0
 for i in range(18):
  Temp1 += x[i] * math.pow(2, i)
 Temp2 = 0
 for i in range(18, 33, 1):
  Temp2 += math.pow(2, i - 18) * x[i]
 x1 = lower[0] + Temp1 * (upper[0] - lower[0]) / (math.pow(2, 18) - 1)
 x2 = lower[1] + Temp2 * (upper[1] - lower[1]) / (math.pow(2, 15) - 1)
 if x1 > upper[0]:
  x1 = random.uniform(lower[0], upper[0])
 if x2 > upper[1]:
  x2 = random.uniform(lower[1], upper[1])
 return x1, x2

def Self_Learn(Best, upper, lower, sPm, sLearn): #自學(xué)習(xí)操作
 num = 0
 Temp = copy.deepcopy(Best)
 while True:
  num += 1
  for j in range(33):
   if random.random() < sPm:
    Temp.x[j] = (Temp.x[j] + 1)%2
  Temp.fitness = Cal_fit(Temp.x, upper, lower)
  if Temp.fitness > Best.fitness:
   Best = copy.deepcopy(Temp)
   num = 0
  if num > sLearn:
   break
 return Best

if __name__ == '__main__':
 upper = [12.1, 5.8]
 lower = [-3, 4.1]
 Pop = 100
 Ppool = 50
 G_max = 300
 Pc = 0.8
 Pm = 0.1
 sPm = 0.05
 sLearn = 20
 G = np.array([Ind() for _ in range(Pop)])
 Gparent = np.array([Ind() for _ in range(Ppool)])
 Gchild = np.array([Ind() for _ in range(Pop)])
 Gsum = np.array([Ind() for _ in range(Pop * 2)])
 Init(G, upper, lower, Pop)  #初始化
 Best = Find_Best(G, Pop)
 for k in range(G_max):
  Selection(G, Gparent, Pop, Ppool)  #使用輪盤賭方法選擇其中50%為父代
  Cross_and_Mutation(Gparent, Gchild, Pc, Pm, upper, lower, Pop, Ppool) #交叉和變異生成子代
  Choose_next(G, Gchild, Gsum, Pop)  #選擇出父代和子代中較優(yōu)秀的個體
  Cbest = Find_Best(G, Pop)
  if Best.fitness < Cbest.fitness:
   Best = copy.deepcopy(Cbest)  #跟新最優(yōu)解
  else:
   G[Cbest.place] = copy.deepcopy(Best)
  Best = Self_Learn(Best, upper, lower, sPm, sLearn)
  print(Best.fitness)
 x1, x2 = Decode(Best.x)
 print(Best.x)
 print([x1, x2])

以上這篇Python實現(xiàn)遺傳算法(二進制編碼)求函數(shù)最優(yōu)值方式就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • AI與Python人工智能遺傳算法

    AI與Python人工智能遺傳算法

    這篇文章主要為大家介紹了AI與Python人工智能遺傳算法的詳解教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-05-05
  • Python之lxml安裝失敗的解決

    Python之lxml安裝失敗的解決

    這篇文章主要介紹了Python之lxml安裝失敗的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • pip install 使用國內(nèi)鏡像的方法示例

    pip install 使用國內(nèi)鏡像的方法示例

    這篇文章主要介紹了pip install 使用國內(nèi)鏡像的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • Python中的Function定義方法

    Python中的Function定義方法

    Python中,函數(shù)是可被重用的程序段。對于函數(shù)的定義,可以使用def關(guān)鍵字。
    2009-09-09
  • 使用SQLAlchemy操作數(shù)據(jù)庫表過程解析

    使用SQLAlchemy操作數(shù)據(jù)庫表過程解析

    這篇文章主要介紹了使用SQLAlchemy操作數(shù)據(jù)庫表過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-06-06
  • 解決python3 requests headers參數(shù)不能有中文的問題

    解決python3 requests headers參數(shù)不能有中文的問題

    今天小編就為大家分享一篇解決python3 requests headers參數(shù)不能有中文的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • python中telnetlib模塊的使用方式

    python中telnetlib模塊的使用方式

    這篇文章主要介紹了python中telnetlib模塊的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • python目標(biāo)檢測給圖畫框,bbox畫到圖上并保存案例

    python目標(biāo)檢測給圖畫框,bbox畫到圖上并保存案例

    這篇文章主要介紹了python目標(biāo)檢測給圖畫框,bbox畫到圖上并保存案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • Python實現(xiàn)求兩個數(shù)組交集的方法示例

    Python實現(xiàn)求兩個數(shù)組交集的方法示例

    這篇文章主要介紹了Python實現(xiàn)求兩個數(shù)組交集的方法,涉及Python數(shù)組遍歷、排序、判斷、追加等相關(guān)操作技巧,需要的朋友可以參考下
    2019-02-02
  • Python 之pandas庫的安裝及庫安裝方法小結(jié)

    Python 之pandas庫的安裝及庫安裝方法小結(jié)

    Pandas 是一種開源的、易于使用的數(shù)據(jù)結(jié)構(gòu)和Python編程語言的數(shù)據(jù)分析工具,它與 Scikit-learn 兩個模塊幾乎提供了數(shù)據(jù)科學(xué)家所需的全部工具,今天通過本文給大家介紹Python 之pandas庫的安裝及庫安裝方法小結(jié),感興趣的朋友跟隨小編一起看看吧
    2022-11-11

最新評論

寿阳县| 定州市| 团风县| 台南市| 吴川市| 淮南市| 盐津县| 黑河市| 郑州市| 汤阴县| 赤水市| 玉环县| 呼玛县| 和龙市| 普安县| 潼南县| 韶山市| 三门峡市| 浪卡子县| 凭祥市| 灵璧县| 仪征市| 台中市| 贵德县| 南漳县| 灌南县| 达尔| 佛山市| 喀喇| 平阳县| 仪陇县| 罗城| 招远市| 杂多县| 龙游县| 清原| 襄樊市| 拉萨市| 汶川县| 阳春市| 克拉玛依市|