python实现的遗传算法实例(一)

一、遗传算法介绍

        遗传算法是通过模拟大自然中生物进化的历程,来解决问题的。大自然中一个种群经历过若干代的自然选择后,剩下的种群必定是适应环境的。把一个问题所有的解看做一个种群,经历过若干次的自然选择以后,剩下的解中是有问题的最优解的。当然,只能说有最优解的概率很大。这里,我们用遗传算法求一个函数的最大值。
        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实现的遗传算法实例(一))