遺傳演算法解簡單的函式極值問題

2022-07-29 04:36:12 字數 4419 閱讀 1262

1 import

numpy

2import

matplotlib.pyplot as plt

3import

random

4import

operator

56 x_label=

7 y_label=#

將每一步迭代的結果儲存到列表中,便於畫圖

8class

ga(object):

9def

__init__

(self,length,number,lower_boundary,upper_boundary,iter_number):

10 self.length = length#

確定染色體編碼長度

11 self.number = number#

確定初始化種群數量

12 self.lower_boundary = lower_boundary#

定義域下界

13 self.upper_boundary = upper_boundary#

定義域上屆

14 self.population =self.initial_population(length,number)

15self.iteration(iter_number)

16def initial_population(self,length,number):#

初始化種群

17return [self.initial_chromosome(length) for i in

range(number)]

18def initial_chromosome(self,length):#

編碼19

"""20

隨機生成長度為length的染色體,每個基因的取值是0或1

21這裡用乙個bit表示乙個基因

22"""

23 chromosome =0

24for i in

range(self.length):

25 chromosome |= (1 << i) * random.randint(0, 1)26#

print(chromosome)27#

print(num)

28return

chromosome

29def decode(self,chromosome):#

解碼30 x = chromosome*(self.upper_boundary-self.lower_boundary)/(2**self.length-1)

31returnx32

def fitness_function(self,chromosome):#

適應度函式

33 x =self.decode(chromosome)

34 y = x + 10 * numpy.sin(5 * x) + 7 * numpy.cos(4 *x)

35returny36

def evolution(self, retain_rate=0.2, random_select_rate=0.5, mutation_rate=0.01):

37"""

38對當前種群依次進行選擇、交叉並生成新一代種群,然後對新一代種群進行變異

39"""

40 parents =self.selection(retain_rate, random_select_rate)

41self.crossover(parents)

42self.mutation(mutation_rate)

43def

selection(self,retain_rate, random_select_rate):

44 graded = [(self.fitness_function(chromosome), chromosome) for chromosome in

self.population]

45 sort = [x[1] for x in sorted(graded, reverse=true)]

46 retain_length=int(len(sort)*retain_rate)47#

選出適應性強的個體,精英選擇

48 parents=sort[:retain_length]

49for chromosome in

sort[retain_length:]:

50if random.random()

5152

return

parents

53def crossover(self,parents):#

交叉54 children=55#

需要繁殖的子代數量

56 target_number=len(self.population)-len(parents)57#

開始繁殖

58while len(children)

59 father = random.randint(0, len(parents) - 1)

60 mother = random.randint(0, len(parents) - 1)

61if father !=mother:62#

隨機選取交叉點

63 cross_point =random.randint(0, self.length)64#

生成掩碼,方便位操作

65 mark =0

66for i in

range(cross_point):

67 mark |= (1 <

68 father =parents[father]

69 mother =parents[mother]70#

孩子將獲得父親在交叉點前的基因和母親在交叉點後(包括交叉點)的基因

71 child = ((father & mark) | (mother & ~mark)) & ((1 << self.length) - 1)

7273

#經過繁殖後,孩子和父母的數量與原始種群數量相等,在這裡可以更新種群。

74 self.population = parents +children

75def

mutation(self,rate):

76for i in

range(len(self.population)):

77if random.random()

78 j = random.randint(0, self.length - 1)#

s隨機產生變異位置

79 self.population[i] ^= 1 << j #

產生變異

80def

result(self):

81'''

82獲得近優值

83'''

84 graded = [(self.fitness_function(chromosome), chromosome) for chromosome in

self.population]

85 graded = [x[1] for x in sorted(graded, reverse=true)]86#

print('近優的x值為:', self.decode(graded[0]),87#

'近優的y值為:', self.fitness_function(graded[0]))88#

將每一步迭代的結果儲存到列表中

8990

return

self.decode(graded[0])

91def

iteration(self,iter_number):

92for i in

range(iter_number):

93self.evolution()

94self.result()

9596 g1=ga(17,300,0,9,30)#

建立乙個例項

簡單遺傳演算法求函式極值

引言 遺傳演算法求函式極值算是遺傳演算法的一種最簡單的應用,這裡就介紹一種簡單的,全文基本翻譯自codeproject的一篇文章,作者為luay al wesi,軟體工程師。例子中的函式為y x2 5 大家可以將其改為其他複雜一些的函式,比如說f x 10sin 5x 7cos 4x 等。本篇文章適...

python 遺傳演算法求函式極值問題

遺傳演算法實現求函式極大值 zjh import numpy as np import random import matplotlib.pyplot as plt class ga 求出二進位制編碼的長度 def init self self.boundsbegin 2 self.boundsen...

遺傳演算法求函式最優解

前兩天寫了乙個遺傳演算法求最優解的程式,今天拿出來簡單整理一下。最優解問題 四個變數取何值,該函式取最大值時?取值範圍 5到5 顯然,當四個未知數都為零的時候,函式取最大值,但如何利用遺傳演算法來實現,才是要研究的問題。1.染色體結構定義define.h struct chromosome 基因 d...