一、遗传算法介绍

遗传算法是通过模拟大自然中生物进化的历程,来解决问题的。大自然中一个种群经历过若干代的自然选择后,剩下的种群必定是适应环境的。把一个问题所有的解看做一个种群,经历过若干次的自然选择以后,剩下的解中是有问题的最优解的。当然,只能说有最优解的概率很大。这里,我们用遗传算法求一个函数的最大值。
        f(x) = 10 * sin( 5x ) + 7 * cos( 4x ),    0 <=  x <= 10

1、将自变量x进行编码

取基因片段的长度为10, 则10位二进制位可以表示的范围是0到1023。基因与自变量转变的公式是x = b2d(individual) * 10 / 1023。构造初始的种群pop。每个个体的基因初始值是[0, 1, 0, 1, 0, 1, 0, 1, 0, 1]

2、计算目标函数值

根据自变量与基因的转化关系式,求出每个个体的基因对应的自变量,然后将自变量代入函数f(x),求出每个个体的目标函数值。

3、适应度函数

适应度函数是用来评估个体适应环境的能力,是进行自然选择的依据。本题的适应度函数直接将目标函数值中的负值变成0. 因为我们求的是最大值,所以要使目标函数值是负数的个体不适应环境,使其繁殖后代的能力为0.适应度函数的作用将在自然选择中体现。

4、自然选择

自然选择的思想不再赘述,操作使用轮盘赌算法。其具体步骤:

假设种群中共5个个体,适应度函数计算出来的个体适应性列表是fitvalue = [1 ,3, 0, 2, 4] ,totalvalue = 10 , 如果将fitvalue画到圆盘上,值的大小表示在圆盘上的面积。在转动轮盘的过程中,单个模块的面积越大则被选中的概率越大。选择的方法是将fitvalue转化为[1 , 4 ,4 , 6 ,10], fitvalue / totalvalue = [0.1 , 0.4 , 0.4 , 0.6 , 1.0] . 然后产生5个0-1之间的随机数,将随机数从小到大排序,假如是[0.05 , 0.2 , 0.7 , 0.8 ,0.9],则将0号个体、1号个体、4号个体、4号个体、4号个体拷贝到新种群中。自然选择的结果使种群更符合条件了。

5、繁殖

假设个体a、b的基因是

a = [1, 0, 0, 0, 0, 1, 1, 1, 0, 0]

b = [0, 0, 0, 1, 1, 0, 1, 1, 1, 1]

这两个个体发生基因交换的概率pc = 0.6.如果要发生基因交换,则产生一个随机数point表示基因交换的位置,假设point = 4,则:

a = [1, 0, 0, 0, 0, 1, 1, 1, 0, 0]

b = [0, 0, 0, 1,1, 0, 1, 1, 1, 1]

交换后为:

a = [1, 0, 0, 0, 1, 0, 1, 1, 1, 1]

b = [0, 0, 0, 1, 0, 1, 1, 1, 0, 0]

6、突变

遍历每一个个体,基因的每一位发生突变(0变为1,1变为0)的概率为0.001.突变可以增加解空间

二、代码

<a target=_blank id="L1" href="http://blog.csdn.net/u010902721/article/details/23531359#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;">  1</a>
<a target=_blank id="L2" href="http://blog.csdn.net/u010902721/article/details/23531359#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;">  2</a>
<a target=_blank id="L3" href="http://blog.csdn.net/u010902721/article/details/23531359#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;">  3</a>
<a target=_blank id="L4" href="http://blog.csdn.net/u010902721/article/details/23531359#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;">  4</a>
<a target=_blank id="L5" href="http://blog.csdn.net/u010902721/article/details/23531359#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;">  5</a>
<a target=_blank id="L6" href="http://blog.csdn.net/u010902721/article/details/23531359#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;">  6</a>
<a target=_blank id="L7" href="http://blog.csdn.net/u010902721/article/details/23531359#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;">  7</a>
<a target=_blank id="L8" href="http://blog.csdn.net/u010902721/article/details/23531359#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;">  8</a>
<a target=_blank id="L9" href="http://blog.csdn.net/u010902721/article/details/23531359#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;">  9</a>
<a target=_blank id="L10" href="http://blog.csdn.net/u010902721/article/details/23531359#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;"> 10</a>
<a target=_blank id="L11" href="http://blog.csdn.net/u010902721/article/details/23531359#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;"> 11</a>
<a target=_blank id="L12" href="http://blog.csdn.net/u010902721/article/details/23531359#L12" rel="#L12" style="color: rgb(102, 102, 102); text-decoration: none;"> 12</a>
<a target=_blank id="L13" href="http://blog.csdn.net/u010902721/article/details/23531359#L13" rel="#L13" style="color: rgb(102, 102, 102); text-decoration: none;"> 13</a>
<a target=_blank id="L14" href="http://blog.csdn.net/u010902721/article/details/23531359#L14" rel="#L14" style="color: rgb(102, 102, 102); text-decoration: none;"> 14</a>
<a target=_blank id="L15" href="http://blog.csdn.net/u010902721/article/details/23531359#L15" rel="#L15" style="color: rgb(102, 102, 102); text-decoration: none;"> 15</a>
<a target=_blank id="L16" href="http://blog.csdn.net/u010902721/article/details/23531359#L16" rel="#L16" style="color: rgb(102, 102, 102); text-decoration: none;"> 16</a>
<a target=_blank id="L17" href="http://blog.csdn.net/u010902721/article/details/23531359#L17" rel="#L17" style="color: rgb(102, 102, 102); text-decoration: none;"> 17</a>
<a target=_blank id="L18" href="http://blog.csdn.net/u010902721/article/details/23531359#L18" rel="#L18" style="color: rgb(102, 102, 102); text-decoration: none;"> 18</a>
<a target=_blank id="L19" href="http://blog.csdn.net/u010902721/article/details/23531359#L19" rel="#L19" style="color: rgb(102, 102, 102); text-decoration: none;"> 19</a>
<a target=_blank id="L20" href="http://blog.csdn.net/u010902721/article/details/23531359#L20" rel="#L20" style="color: rgb(102, 102, 102); text-decoration: none;"> 20</a>
<a target=_blank id="L21" href="http://blog.csdn.net/u010902721/article/details/23531359#L21" rel="#L21" style="color: rgb(102, 102, 102); text-decoration: none;"> 21</a>
<a target=_blank id="L22" href="http://blog.csdn.net/u010902721/article/details/23531359#L22" rel="#L22" style="color: rgb(102, 102, 102); text-decoration: none;"> 22</a>
<a target=_blank id="L23" href="http://blog.csdn.net/u010902721/article/details/23531359#L23" rel="#L23" style="color: rgb(102, 102, 102); text-decoration: none;"> 23</a>
<a target=_blank id="L24" href="http://blog.csdn.net/u010902721/article/details/23531359#L24" rel="#L24" style="color: rgb(102, 102, 102); text-decoration: none;"> 24</a>
<a target=_blank id="L25" href="http://blog.csdn.net/u010902721/article/details/23531359#L25" rel="#L25" style="color: rgb(102, 102, 102); text-decoration: none;"> 25</a>
<a target=_blank id="L26" href="http://blog.csdn.net/u010902721/article/details/23531359#L26" rel="#L26" style="color: rgb(102, 102, 102); text-decoration: none;"> 26</a>
<a target=_blank id="L27" href="http://blog.csdn.net/u010902721/article/details/23531359#L27" rel="#L27" style="color: rgb(102, 102, 102); text-decoration: none;"> 27</a>
<a target=_blank id="L28" href="http://blog.csdn.net/u010902721/article/details/23531359#L28" rel="#L28" style="color: rgb(102, 102, 102); text-decoration: none;"> 28</a>
<a target=_blank id="L29" href="http://blog.csdn.net/u010902721/article/details/23531359#L29" rel="#L29" style="color: rgb(102, 102, 102); text-decoration: none;"> 29</a>
<a target=_blank id="L30" href="http://blog.csdn.net/u010902721/article/details/23531359#L30" rel="#L30" style="color: rgb(102, 102, 102); text-decoration: none;"> 30</a>
<a target=_blank id="L31" href="http://blog.csdn.net/u010902721/article/details/23531359#L31" rel="#L31" style="color: rgb(102, 102, 102); text-decoration: none;"> 31</a>
<a target=_blank id="L32" href="http://blog.csdn.net/u010902721/article/details/23531359#L32" rel="#L32" style="color: rgb(102, 102, 102); text-decoration: none;"> 32</a>
def b2d(b): #将二进制转化为十进制 x∈[0,10]
t = 0
for j in range(len(b)):
t += b[j] * (math.pow(2, j))
t = t * 10 / 1023
return t
popsize = 50 #种群的大小
#用遗传算法求函数最大值:
#f(x)=10*sin(5x)+7*cos(4x) x∈[0,10]
chromlength = 10 #基因片段的长度
pc = 0.6 #两个个体交叉的概率
pm = 0.001; #基因突变的概率
results = [[]]
bestindividual = []
bestfit = 0
fitvalue = []
tempop = [[]]
pop = [[0, 1, 0, 1, 0, 1, 0, 1, 0, 1] for i in range(popsize)]
for i in range(100): #繁殖100代
objvalue = calobjvalue(pop) #计算目标函数值
fitvalue = calfitvalue(objvalue); #计算个体的适应值
[bestindividual, bestfit] = best(pop, fitvalue) #选出最好的个体和最好的函数值
results.append([bestfit,b2d(bestindividual)]) #每次繁殖,将最好的结果记录下来
selection(pop, fitvalue) #自然选择,淘汰掉一部分适应性低的个体
crossover(pop, pc) #交叉繁殖
mutation(pop, pc) #基因突变
results.sort()
print(results[-1]) #打印函数最大值和对应的

来自CODE的代码片
GA.py

<a target=_blank id="L1" href="http://blog.csdn.net/u010902721/article/details/23531359#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;"> 1</a>
<a target=_blank id="L2" href="http://blog.csdn.net/u010902721/article/details/23531359#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;"> 2</a>
<a target=_blank id="L3" href="http://blog.csdn.net/u010902721/article/details/23531359#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;"> 3</a>
<a target=_blank id="L4" href="http://blog.csdn.net/u010902721/article/details/23531359#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;"> 4</a>
<a target=_blank id="L5" href="http://blog.csdn.net/u010902721/article/details/23531359#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;"> 5</a>
<a target=_blank id="L6" href="http://blog.csdn.net/u010902721/article/details/23531359#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;"> 6</a>
<a target=_blank id="L7" href="http://blog.csdn.net/u010902721/article/details/23531359#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;"> 7</a>
<a target=_blank id="L8" href="http://blog.csdn.net/u010902721/article/details/23531359#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;"> 8</a>
<a target=_blank id="L9" href="http://blog.csdn.net/u010902721/article/details/23531359#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;"> 9</a>
def best(pop, fitvalue): #找出适应函数值中最大值,和对应的个体
px = len(pop)
bestindividual = []
bestfit = fitvalue[0]
for i in range(1,px):
if(fitvalue[i] > bestfit):
bestfit = fitvalue[i]
bestindividual = pop[i]
return [bestindividual, bestfit]

来自CODE的代码片
best.py

<a target=_blank id="L1" href="http://blog.csdn.net/u010902721/article/details/23531359#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;">  1</a>
<a target=_blank id="L2" href="http://blog.csdn.net/u010902721/article/details/23531359#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;">  2</a>
<a target=_blank id="L3" href="http://blog.csdn.net/u010902721/article/details/23531359#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;">  3</a>
<a target=_blank id="L4" href="http://blog.csdn.net/u010902721/article/details/23531359#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;">  4</a>
<a target=_blank id="L5" href="http://blog.csdn.net/u010902721/article/details/23531359#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;">  5</a>
<a target=_blank id="L6" href="http://blog.csdn.net/u010902721/article/details/23531359#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;">  6</a>
<a target=_blank id="L7" href="http://blog.csdn.net/u010902721/article/details/23531359#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;">  7</a>
<a target=_blank id="L8" href="http://blog.csdn.net/u010902721/article/details/23531359#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;">  8</a>
<a target=_blank id="L9" href="http://blog.csdn.net/u010902721/article/details/23531359#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;">  9</a>
<a target=_blank id="L10" href="http://blog.csdn.net/u010902721/article/details/23531359#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;"> 10</a>
<a target=_blank id="L11" href="http://blog.csdn.net/u010902721/article/details/23531359#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;"> 11</a>
def calfitvalue(objvalue):#转化为适应值,目标函数值越大越好,负值淘汰。
fitvalue = []
temp = 0.0
Cmin = 0;
for i in range(len(objvalue)):
if(objvalue[i] + Cmin > 0):
temp = Cmin + objvalue[i]
else:
temp = 0.0
fitvalue.append(temp)
return fitvalue

来自CODE的代码片
calfitvalue.py

<a target=_blank id="L1" href="http://blog.csdn.net/u010902721/article/details/23531359#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;">  1</a>
<a target=_blank id="L2" href="http://blog.csdn.net/u010902721/article/details/23531359#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;">  2</a>
<a target=_blank id="L3" href="http://blog.csdn.net/u010902721/article/details/23531359#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;">  3</a>
<a target=_blank id="L4" href="http://blog.csdn.net/u010902721/article/details/23531359#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;">  4</a>
<a target=_blank id="L5" href="http://blog.csdn.net/u010902721/article/details/23531359#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;">  5</a>
<a target=_blank id="L6" href="http://blog.csdn.net/u010902721/article/details/23531359#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;">  6</a>
<a target=_blank id="L7" href="http://blog.csdn.net/u010902721/article/details/23531359#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;">  7</a>
<a target=_blank id="L8" href="http://blog.csdn.net/u010902721/article/details/23531359#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;">  8</a>
<a target=_blank id="L9" href="http://blog.csdn.net/u010902721/article/details/23531359#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;">  9</a>
<a target=_blank id="L10" href="http://blog.csdn.net/u010902721/article/details/23531359#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;"> 10</a>
<a target=_blank id="L11" href="http://blog.csdn.net/u010902721/article/details/23531359#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;"> 11</a>
<a target=_blank id="L12" href="http://blog.csdn.net/u010902721/article/details/23531359#L12" rel="#L12" style="color: rgb(102, 102, 102); text-decoration: none;"> 12</a>
<a target=_blank id="L13" href="http://blog.csdn.net/u010902721/article/details/23531359#L13" rel="#L13" style="color: rgb(102, 102, 102); text-decoration: none;"> 13</a>
<a target=_blank id="L14" href="http://blog.csdn.net/u010902721/article/details/23531359#L14" rel="#L14" style="color: rgb(102, 102, 102); text-decoration: none;"> 14</a>
<a target=_blank id="L15" href="http://blog.csdn.net/u010902721/article/details/23531359#L15" rel="#L15" style="color: rgb(102, 102, 102); text-decoration: none;"> 15</a>
<a target=_blank id="L16" href="http://blog.csdn.net/u010902721/article/details/23531359#L16" rel="#L16" style="color: rgb(102, 102, 102); text-decoration: none;"> 16</a>
<a target=_blank id="L17" href="http://blog.csdn.net/u010902721/article/details/23531359#L17" rel="#L17" style="color: rgb(102, 102, 102); text-decoration: none;"> 17</a>
<a target=_blank id="L18" href="http://blog.csdn.net/u010902721/article/details/23531359#L18" rel="#L18" style="color: rgb(102, 102, 102); text-decoration: none;"> 18</a>
<a target=_blank id="L19" href="http://blog.csdn.net/u010902721/article/details/23531359#L19" rel="#L19" style="color: rgb(102, 102, 102); text-decoration: none;"> 19</a>
import math
def decodechrom(pop): #将种群的二进制基因转化为十进制(0,1023)
temp = [];
for i in range(len(pop)):
t = 0;
for j in range(10):
t += pop[i][j] * (math.pow(2, j))
temp.append(t)
return temp
def calobjvalue(pop): #计算目标函数值
temp1 = [];
objvalue = [];
temp1 = decodechrom(pop)
for i in range(len(temp1)):
x = temp1[i] * 10 / 1023 #(0,1023)转化为 (0,10)
objvalue.append(10 * math.sin(5 * x) + 7 * math.cos(4 * x))
return objvalue #目标函数值objvalue[m] 与个体基因 pop[m] 对应

来自CODE的代码片
calobjvalue.py

<a target=_blank id="L1" href="http://blog.csdn.net/u010902721/article/details/23531359#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;">  1</a>
<a target=_blank id="L2" href="http://blog.csdn.net/u010902721/article/details/23531359#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;">  2</a>
<a target=_blank id="L3" href="http://blog.csdn.net/u010902721/article/details/23531359#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;">  3</a>
<a target=_blank id="L4" href="http://blog.csdn.net/u010902721/article/details/23531359#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;">  4</a>
<a target=_blank id="L5" href="http://blog.csdn.net/u010902721/article/details/23531359#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;">  5</a>
<a target=_blank id="L6" href="http://blog.csdn.net/u010902721/article/details/23531359#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;">  6</a>
<a target=_blank id="L7" href="http://blog.csdn.net/u010902721/article/details/23531359#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;">  7</a>
<a target=_blank id="L8" href="http://blog.csdn.net/u010902721/article/details/23531359#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;">  8</a>
<a target=_blank id="L9" href="http://blog.csdn.net/u010902721/article/details/23531359#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;">  9</a>
<a target=_blank id="L10" href="http://blog.csdn.net/u010902721/article/details/23531359#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;"> 10</a>
<a target=_blank id="L11" href="http://blog.csdn.net/u010902721/article/details/23531359#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;"> 11</a>
<a target=_blank id="L12" href="http://blog.csdn.net/u010902721/article/details/23531359#L12" rel="#L12" style="color: rgb(102, 102, 102); text-decoration: none;"> 12</a>
<a target=_blank id="L13" href="http://blog.csdn.net/u010902721/article/details/23531359#L13" rel="#L13" style="color: rgb(102, 102, 102); text-decoration: none;"> 13</a>
<a target=_blank id="L14" href="http://blog.csdn.net/u010902721/article/details/23531359#L14" rel="#L14" style="color: rgb(102, 102, 102); text-decoration: none;"> 14</a>
<a target=_blank id="L15" href="http://blog.csdn.net/u010902721/article/details/23531359#L15" rel="#L15" style="color: rgb(102, 102, 102); text-decoration: none;"> 15</a>
import random
def crossover(pop, pc): #个体间交叉,实现基因交换
poplen = len(pop)
for i in range(poplen - 1):
if(random.random() < pc):
cpoint = random.randint(0,len(pop[0]))
temp1 = []
temp2 = []
temp1.extend(pop[i][0 : cpoint])
temp1.extend(pop[i+1][cpoint : len(pop[i])])
temp2.extend(pop[i+1][0 : cpoint])
temp2.extend(pop[i][cpoint : len(pop[i])])
pop[i] = temp1
pop[i+1] = temp2

来自CODE的代码片
crossover.py

<a target=_blank id="L1" href="http://blog.csdn.net/u010902721/article/details/23531359#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;">  1</a>
<a target=_blank id="L2" href="http://blog.csdn.net/u010902721/article/details/23531359#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;">  2</a>
<a target=_blank id="L3" href="http://blog.csdn.net/u010902721/article/details/23531359#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;">  3</a>
<a target=_blank id="L4" href="http://blog.csdn.net/u010902721/article/details/23531359#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;">  4</a>
<a target=_blank id="L5" href="http://blog.csdn.net/u010902721/article/details/23531359#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;">  5</a>
<a target=_blank id="L6" href="http://blog.csdn.net/u010902721/article/details/23531359#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;">  6</a>
<a target=_blank id="L7" href="http://blog.csdn.net/u010902721/article/details/23531359#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;">  7</a>
<a target=_blank id="L8" href="http://blog.csdn.net/u010902721/article/details/23531359#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;">  8</a>
<a target=_blank id="L9" href="http://blog.csdn.net/u010902721/article/details/23531359#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;">  9</a>
<a target=_blank id="L10" href="http://blog.csdn.net/u010902721/article/details/23531359#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;"> 10</a>
<a target=_blank id="L11" href="http://blog.csdn.net/u010902721/article/details/23531359#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;"> 11</a>
<a target=_blank id="L12" href="http://blog.csdn.net/u010902721/article/details/23531359#L12" rel="#L12" style="color: rgb(102, 102, 102); text-decoration: none;"> 12</a>
<a target=_blank id="L13" href="http://blog.csdn.net/u010902721/article/details/23531359#L13" rel="#L13" style="color: rgb(102, 102, 102); text-decoration: none;"> 13</a>
import random
def mutation(pop, pm): #基因突变
px = len(pop)
py = len(pop[0])
for i in range(px):
if(random.random() < pm):
mpoint = random.randint(0,py-1)
if(pop[i][mpoint] == 1):
pop[i][mpoint] = 0
else:
pop[i][mpoint] = 1

来自CODE的代码片
mutation.py

<a target=_blank id="L1" href="http://blog.csdn.net/u010902721/article/details/23531359#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;">  1</a>
<a target=_blank id="L2" href="http://blog.csdn.net/u010902721/article/details/23531359#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;">  2</a>
<a target=_blank id="L3" href="http://blog.csdn.net/u010902721/article/details/23531359#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;">  3</a>
<a target=_blank id="L4" href="http://blog.csdn.net/u010902721/article/details/23531359#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;">  4</a>
<a target=_blank id="L5" href="http://blog.csdn.net/u010902721/article/details/23531359#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;">  5</a>
<a target=_blank id="L6" href="http://blog.csdn.net/u010902721/article/details/23531359#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;">  6</a>
<a target=_blank id="L7" href="http://blog.csdn.net/u010902721/article/details/23531359#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;">  7</a>
<a target=_blank id="L8" href="http://blog.csdn.net/u010902721/article/details/23531359#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;">  8</a>
<a target=_blank id="L9" href="http://blog.csdn.net/u010902721/article/details/23531359#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;">  9</a>
<a target=_blank id="L10" href="http://blog.csdn.net/u010902721/article/details/23531359#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;"> 10</a>
<a target=_blank id="L11" href="http://blog.csdn.net/u010902721/article/details/23531359#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;"> 11</a>
<a target=_blank id="L12" href="http://blog.csdn.net/u010902721/article/details/23531359#L12" rel="#L12" style="color: rgb(102, 102, 102); text-decoration: none;"> 12</a>
<a target=_blank id="L13" href="http://blog.csdn.net/u010902721/article/details/23531359#L13" rel="#L13" style="color: rgb(102, 102, 102); text-decoration: none;"> 13</a>
<a target=_blank id="L14" href="http://blog.csdn.net/u010902721/article/details/23531359#L14" rel="#L14" style="color: rgb(102, 102, 102); text-decoration: none;"> 14</a>
<a target=_blank id="L15" href="http://blog.csdn.net/u010902721/article/details/23531359#L15" rel="#L15" style="color: rgb(102, 102, 102); text-decoration: none;"> 15</a>
<a target=_blank id="L16" href="http://blog.csdn.net/u010902721/article/details/23531359#L16" rel="#L16" style="color: rgb(102, 102, 102); text-decoration: none;"> 16</a>
<a target=_blank id="L17" href="http://blog.csdn.net/u010902721/article/details/23531359#L17" rel="#L17" style="color: rgb(102, 102, 102); text-decoration: none;"> 17</a>
<a target=_blank id="L18" href="http://blog.csdn.net/u010902721/article/details/23531359#L18" rel="#L18" style="color: rgb(102, 102, 102); text-decoration: none;"> 18</a>
<a target=_blank id="L19" href="http://blog.csdn.net/u010902721/article/details/23531359#L19" rel="#L19" style="color: rgb(102, 102, 102); text-decoration: none;"> 19</a>
<a target=_blank id="L20" href="http://blog.csdn.net/u010902721/article/details/23531359#L20" rel="#L20" style="color: rgb(102, 102, 102); text-decoration: none;"> 20</a>
<a target=_blank id="L21" href="http://blog.csdn.net/u010902721/article/details/23531359#L21" rel="#L21" style="color: rgb(102, 102, 102); text-decoration: none;"> 21</a>
<a target=_blank id="L22" href="http://blog.csdn.net/u010902721/article/details/23531359#L22" rel="#L22" style="color: rgb(102, 102, 102); text-decoration: none;"> 22</a>
<a target=_blank id="L23" href="http://blog.csdn.net/u010902721/article/details/23531359#L23" rel="#L23" style="color: rgb(102, 102, 102); text-decoration: none;"> 23</a>
<a target=_blank id="L24" href="http://blog.csdn.net/u010902721/article/details/23531359#L24" rel="#L24" style="color: rgb(102, 102, 102); text-decoration: none;"> 24</a>
<a target=_blank id="L25" href="http://blog.csdn.net/u010902721/article/details/23531359#L25" rel="#L25" style="color: rgb(102, 102, 102); text-decoration: none;"> 25</a>
<a target=_blank id="L26" href="http://blog.csdn.net/u010902721/article/details/23531359#L26" rel="#L26" style="color: rgb(102, 102, 102); text-decoration: none;"> 26</a>
<a target=_blank id="L27" href="http://blog.csdn.net/u010902721/article/details/23531359#L27" rel="#L27" style="color: rgb(102, 102, 102); text-decoration: none;"> 27</a>
<a target=_blank id="L28" href="http://blog.csdn.net/u010902721/article/details/23531359#L28" rel="#L28" style="color: rgb(102, 102, 102); text-decoration: none;"> 28</a>
<a target=_blank id="L29" href="http://blog.csdn.net/u010902721/article/details/23531359#L29" rel="#L29" style="color: rgb(102, 102, 102); text-decoration: none;"> 29</a>
<a target=_blank id="L30" href="http://blog.csdn.net/u010902721/article/details/23531359#L30" rel="#L30" style="color: rgb(102, 102, 102); text-decoration: none;"> 30</a>
<a target=_blank id="L31" href="http://blog.csdn.net/u010902721/article/details/23531359#L31" rel="#L31" style="color: rgb(102, 102, 102); text-decoration: none;"> 31</a>
<a target=_blank id="L32" href="http://blog.csdn.net/u010902721/article/details/23531359#L32" rel="#L32" style="color: rgb(102, 102, 102); text-decoration: none;"> 32</a>
<a target=_blank id="L33" href="http://blog.csdn.net/u010902721/article/details/23531359#L33" rel="#L33" style="color: rgb(102, 102, 102); text-decoration: none;"> 33</a>
<a target=_blank id="L34" href="http://blog.csdn.net/u010902721/article/details/23531359#L34" rel="#L34" style="color: rgb(102, 102, 102); text-decoration: none;"> 34</a>
<a target=_blank id="L35" href="http://blog.csdn.net/u010902721/article/details/23531359#L35" rel="#L35" style="color: rgb(102, 102, 102); text-decoration: none;"> 35</a>
<a target=_blank id="L36" href="http://blog.csdn.net/u010902721/article/details/23531359#L36" rel="#L36" style="color: rgb(102, 102, 102); text-decoration: none;"> 36</a>
<a target=_blank id="L37" href="http://blog.csdn.net/u010902721/article/details/23531359#L37" rel="#L37" style="color: rgb(102, 102, 102); text-decoration: none;"> 37</a>
<a target=_blank id="L38" href="http://blog.csdn.net/u010902721/article/details/23531359#L38" rel="#L38" style="color: rgb(102, 102, 102); text-decoration: none;"> 38</a>
import random
def sum(fitvalue):
total = 0
for i in range(len(fitvalue)):
total += fitvalue[i]
return total
def cumsum(fitvalue):
for i in range(len(fitvalue)):
t = 0;
j = 0;
while(j <= i):
t += fitvalue[j]
j = j + 1
fitvalue[i] = t;
def selection(pop, fitvalue): #自然选择(轮盘赌算法)
newfitvalue = []
totalfit = sum(fitvalue)
for i in range(len(fitvalue)):
newfitvalue.append(fitvalue[i] / totalfit)
cumsum(newfitvalue)
ms = [];
poplen = len(pop)
for i in range(poplen):
ms.append(random.random()) #random float list ms
ms.sort()
fitin = 0
newin = 0
newpop = pop
while newin < poplen:
if(ms[newin] < newfitvalue[fitin]):
newpop[newin] = pop[fitin]
newin = newin + 1
else:
fitin = fitin + 1
pop = newpop

来自CODE的代码片
selection.py

python实现的遗传算法实例(一)相关推荐

  1. python实现遗传算法实例_基于Python的遗传算法特征约简(附代码)

    作者:Ahmed Gad 翻译:张睿毅 校对:丁楠雅 本文4700字,建议阅读15分钟. 本教程主要使用numpy和sklearn来讨论如何使用遗传算法(genetic algorithm,GA)来减 ...

  2. python键盘输入代码,python监控键盘输入实例代码

    本文研究的主要是python监控键盘输入的相关代码,用到了os,sys,time等,具体实现代码如下: #!/usr/bin/env python # -*- coding: utf-8 -*- im ...

  3. python 爬虫实例-Python 爬虫:Scrapy 实例(二)

    原标题:Python 爬虫:Scrapy 实例(二) 稍微增加点难度,做个所需项目多一点的,并将的结果以多种形式保存起来.我们就从网络天气预报开始. 首先要做的是确定网络天气数据的来源.打开百度,搜索 ...

  4. 用python绘制漂亮的图形-用python绘制图形的实例详解

    1.环境系统:windows10 python版本:python3.6.1 使用的库:matplotlib,numpy 2.numpy库产生随机数几种方法import numpy as npnumpy ...

  5. python画直方图成绩分析-python plotly绘制直方图实例详解

    计算数值出现的次数 import cufflinks as cf cf.go_offline() import numpy as np import pandas as pd set_slippage ...

  6. python简单编程例子-中文方便就用中文编程!Python图形界面开发实例

    原标题:中文方便就用中文编程!Python图形界面开发实例 之前做的一个Python小程序,功能很简单,面对用户群也很窄,是五笔编码编.校人员使用的五笔编码编辑器. 这样的"周末" ...

  7. python编程入门经典实例-总算明了python编程入门经典实例

    跟Java语言一样,python语言也有类的概念,直接使用class关键字定义python类.在python类,定义类的方法.然后直接使用类的初始化调用自身,获取相应的属性.以下是小编为你整理的pyt ...

  8. python装饰器实例-python装饰器使用实例详解

    这篇文章主要介绍了python装饰器使用实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 python装饰器的作用就是在不想改变原函数代码的情 ...

  9. 用C语言扩展Python的功能的实例

    用C语言扩展Python的功能的实例 分类: C/C++ 编程技巧 Programes 2008-04-23 09:31 1232人阅读 评论(0)收藏 举报 python扩展语言cmethodsnu ...

最新文章

  1. linux 多线程的基础 交通信号灯学习笔记 :信号详解
  2. coursera—吴恩达Machine Learning笔记(1-3周)
  3. DC课程笔记-数字逻辑综合工具-DC Synthesis Optimization Techniques
  4. 大四 PHP《上传文件》
  5. inline-block空白间隙
  6. selenium 定位方式3-css_selector
  7. securecrt 中文横着显示解决
  8. JAVA:泛型通配符T,E,K,V区别,T以及Class,Class的区别
  9. Kinetics-600数据集介绍
  10. python之Character string
  11. python学习笔记之初识Python
  12. 智能驾驶场景库设计方法-V2X
  13. 通过子网掩码和ip地址计算网络地址和广播地址
  14. xxxxxxxxxxxxxxxxxxxxxxxxxxxxxcccccccccccc
  15. 如何组建权责明确、运营高效的数据团队
  16. 1335:【例2-4】连通块
  17. [转]绝对地址和相对地址的区别,为什么要采用绝对地址?
  18. 2019年DigitalOcean最新优惠券赠送100美元
  19. 使用Flink对hudi MOR表进行离线压缩
  20. 最强PostMan使用教程(6)- 使用Postman导入swagger OPEN API

热门文章

  1. 屌丝逆袭,通过相亲实现阶层跃迁的秘诀
  2. 量化交易很好,但是也存在问题
  3. 编译原理词法分析实验
  4. 情景喜剧消亡史:人人都爱,没人敢拍
  5. 5799元!OPPO Find X5 Pro天玑版即将开卖:性能比肩骁龙8
  6. 支付宝五福活动抢先开始了!原来今年可以提前集
  7. 微拍堂推出“正义联盟计划” 助力文玩行业高质量发展
  8. 大动作!今日头条、西瓜视频并入抖音,字节梁汝波正式接任CEO
  9. 英国反垄断监管机构将对音乐流媒体市场展开调查
  10. 马斯克终于承认售出比特币:卖了10%