遗传算法入门例子和总结

来源:互联网 发布:淘宝旺旺自动回复链接 编辑:程序博客网 时间:2024/06/17 19:16

遗传算法的手工模拟计算示例

为更好地理解遗传算法的运算过程,下面用手工计算来简单地模拟遗传算法的各
    个主要执行步骤。
  
     例:求下述二元函数的最大值:

    (1) 个体编码
           遗传算法的运算对象是表示个体的符号串,所以必须把变量 x1, x2 编码为一种
       符号串。本题中,用无符号二进制整数来表示。
           因 x1, x2 为 0 ~ 7之间的整数,所以分别用3位无符号二进制整数来表示,将它
       们连接在一起所组成的6位无符号二进制数就形成了个体的基因型,表示一个可
       行解。
           例如,基因型 X=101110 所对应的表现型是:x=[ 5,6 ]。
           个体的表现型x和基因型X之间可通过编码和解码程序相互转换。

(2) 初始群体的产生
          遗传算法是对群体进行的进化操作,需要给其淮备一些表示起始搜索点的初始
      群体数据。
         本例中,群体规模的大小取为4,即群体由4个个体组成,每个个体可通过随机
     方法产生。
          如:011101,101011,011100,111001
         
 (3) 适应度汁算
          遗传算法中以个体适应度的大小来评定各个个体的优劣程度,从而决定其遗传
       机会的大小。
          本例中,目标函数总取非负值,并且是以求函数最大值为优化目标,故可直接
       利用目标函数值作为个体的适应度。

 (4)  选择运算
          选择运算(或称为复制运算)把当前群体中适应度较高的个体按某种规则或模型遗传到下一代群体中。一般要求适应度较高的个体将有更多的机会遗传到下一代
      群体中。                   
本例中,我们采用与适应度成正比的概率来确定各个个体复制到下一代群体中
     的数量。其具体操作过程是:
         •  先计算出群体中所有个体的适应度的总和  fi  ( i=1.2,…,M );
         •  其次计算出每个个体的相对适应度的大小 fi / fi ,它即为每个个体被遗传
             到下一代群体中的概率,
         •  每个概率值组成一个区域,全部概率值之和为1;
         •  最后再产生一个0到1之间的随机数,依据该随机数出现在上述哪一个概率区
             域内来确定各个个体被选中的次数。

(5)  交叉运算
        交叉运算是遗传算法中产生新个体的主要操作过程,它以某一概率相互交换某
    两个个体之间的部分染色体。
       本例采用单点交叉的方法,其具体操作过程是:
       • 先对群体进行随机配对;
       • 其次随机设置交叉点位置;
       • 最后再相互交换配对染色体之间的部分基因。

(6)  变异运算
         变异运算是对个体的某一个或某一些基因座上的基因值按某一较小的概率进
     行改变,它也是产生新个体的一种操作方法。
        本例中,我们采用基本位变异的方法来进行变异运算,其具体操作过程是:
        • 首先确定出各个个体的基因变异位置,下表所示为随机产生的变异点位置,
          其中的数字表示变异点设置在该基因座处;
        • 然后依照某一概率将变异点的原有基因值取反。

对群体P(t)进行一轮选择、交叉、变异运算之后可得到新一代的群体p(t+1)。

从上表中可以看出,群体经过一代进化之后,其适应度的最大值、平均值都得
    到了明显的改进。事实上,这里已经找到了最佳个体“111111”。       
[注意]      
        需要说明的是,表中有些栏的数据是随机产生的。这里为了更好地说明问题,
   我们特意选择了一些较好的数值以便能够得到较好的结果,而在实际运算过程中
   有可能需要一定的循环次数才能达到这个最优结果。

 四.C语言实现

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <time.h>  
  4.   
  5.   
  6.   
  7. /////////////The definiton of user data////  
  8. #define Cmax 100   //certain maximal value  
  9. #define Cmin 0   //certain minimum value  
  10.   
  11. #define LENGHT1  3  //the chromosome length of 1st variable  
  12. #define LENGHT2  3  //the chromosome length of 2nd variable  
  13.   
  14. //总染体长度  
  15. #define CHROMLENGTH LENGHT1+LENGHT2   
  16. const int MaxGeneration = 100;  //最大代数  
  17. const int PopSize = 10;  //样本大小  
  18. const double Pc = 0.6; //交叉概率  
  19. const double Pm = 0.001; //变异概率 
  1. ////////////// 数据结构定义///////////////////  
  2. struct Individual{  
  3.     char chrom[CHROMLENGTH + 1];  //一个个体的染色体  
  4.     double value;  //目标值  
  5.     double fitness;  //适应度  
  6. };  
  7.   
  8. int generation ;  //进化次数  
  9. int bestIndex;  //最好个体的下标  
  10. int worstIndex;  //最坏个体的游标  
  11.   
  12. Individual bestIndividual ;  //当前一代中的最好个体  
  13. Individual worstIndividual ;  ///当前一代中的坏个体  
  14. // best individual by now   
  15. Individual currentBest ;// 到目前为止的最好个体  
  16. Individual  population [PopSize]  ;//样本  
  17.   
  18.   
  19. ///////////////////////  
  20. void generateInitialPopulation();  
  21. void generateNextPopulation();  
  22. void evalutePopulation();  
  23. long decomdeChromosome(char*, intint);  
  24. void calculateObjectValue();  
  25. void calculateFitnessValue();  
  26. void findBestAndWorstIndividual();  
  27. void performEvolution();  
  28. void selectionOperator();  
  29. void crossoverOperator();  
  30. void mutationOperator();  
  31. void outputTextReport();  
  32. //////////////////////////////////////////////  
  33.   
  34.   
  35. int main(){  
  36.     generation = 0;  
  37.     generateInitialPopulation();  
  38.     evalutePopulation();  
  39.     while (generation < MaxGeneration) {  
  40.         generation++;  
  41.         generateNextPopulation();  
  42.         evalutePopulation();  
  43.         performEvolution();  
  44.         outputTextReport();  
  45.     }  
  46.     return 0;  
  47. }  
  48.   
  49.   
  50. //////////////////////////////////////////////////////////////  
  51.   
  52. //////产生第一代样本/////  
  53. void generateInitialPopulation() {  
  54.     int i, j;  
  55.     srand((unsigned)time(NULL));  
  56.     for (i = 0; i < PopSize; i++) {  
  57.         for (j = 0; j < CHROMLENGTH; j++) {  
  58.             population[i].chrom[j] = ((rand() % 10) < 5) ? '0' : '1';  
  59.         }  
  60.         population[i].chrom[CHROMLENGTH] = '/0';  
  61.     }  
  62.   
  63. }  
  64.   
  65.   
  66. ////////产生下一代样本 //////  
  67. void generateNextPopulation() {  
  68.     selectionOperator();  
  69.     crossoverOperator();  
  70.     mutationOperator();  
  71. }  
  72.   
  73. //变异算子//  
  74. void mutationOperator() {  
  75.     int i, j;  
  76.     double p;  
  77.     // bit mutation  
  78.     for (i = 0; i < PopSize; i++) {  
  79.         for (j = 0; j < CHROMLENGTH; j++) {  
  80.             p = rand() % 1000 / 1000.0;  
  81.             if (p < Pm) {  
  82.                 population[i].chrom[j] = (population[i].chrom[j] == '0') ? '1''0';  
  83.             }  
  84.         }  
  85.     }  
  86.   
  87. }  
  88.   
  89. //交叉算子///  
  90. void crossoverOperator() {  
  91.     int i, j;  
  92.     int index[PopSize];  
  93.     int point, temp;  
  94.     double p;  
  95.     char ch;  
  96.     for (i = 0; i < PopSize; i++) {  
  97.         index[i] = i;  
  98.     }  
  99.     for (i = 0; i < PopSize; i++) {  
  100.   
  101.         point = rand() %(PopSize - i);  
  102.         temp = index[i];  
  103.         index[i] = index[point + i];  
  104.         index[point + i] = temp;  
  105.     }  
  106.     for (i = 0; i < PopSize - 1; i+=2) {  
  107.         p = rand() % 1000 / 1000.0;  
  108.         if (p < Pc) {  
  109.             point = rand()% (CHROMLENGTH - 1) + 1;  
  110.             for (j = point; j < CHROMLENGTH; j++) {  
  111.                 ch = population[index[i]].chrom[j];  
  112.                 population[index[i]].chrom[j] = population[index[i + 1]].chrom[j];  
  113.                 population[index[i + 1]].chrom[j] = ch;  
  114.             }  
  115.         }  
  116.     }  
  117. }  
  118.   
  119. ///选择算子/////////////  
  120. void selectionOperator() {  
  121.     int i, index;  
  122.     double p, sum = 0.0;  
  123.     double cfitness[PopSize];  
  124.     Individual newpopulation[PopSize];  
  125.     for (i = 0; i < PopSize; i++) {  
  126.         sum += population[i].fitness;  
  127.     }  
  128.     for (i = 0; i < PopSize; i++) {  
  129.         cfitness[i] = population[i].fitness / sum;  
  130.     }  
  131.     // calculate cumulative fitness   
  132.     for (i = 1; i < PopSize; i++) {  
  133.         cfitness[i] = cfitness[i] + cfitness[i - 1];  
  134.     }  
  135.     for (i = 0; i < PopSize; i++) {  
  136.         p = rand() % 1000 / 1000.0;  
  137.         index = 0;  
  138.         while (p > cfitness[index]) {  
  139.             index++;  
  140.         }  
  141.         newpopulation[i] = population[index];  
  142.     }  
  143.     for (i = 0; i < PopSize; i++) {  
  144.         population[i] = newpopulation[i];  
  145.     }  
  146. }  
  147.   
  148.   
  149. /////依据某些公式对样本进行评价////  
  150. void evalutePopulation() {  
  151.     calculateObjectValue();  
  152.     calculateFitnessValue();  
  153.     findBestAndWorstIndividual();  
  154. }  
  155.   
  156.   
  157.   
  158. //找出到目前为止最好的个体//////  
  159. void findBestAndWorstIndividual() {  
  160.   
  161.     int i;  
  162.     double sum = 0.0;  
  163.     bestIndividual = population[0];  
  164.     worstIndividual = population[0];  
  165.     for (i = 0; i < PopSize; i++) {  
  166.         if (population[i].fitness > bestIndividual.fitness) {  
  167.             bestIndividual = population[i];  
  168.             bestIndex = i;  
  169.         } else if (population[i].fitness < worstIndividual.fitness) {  
  170.             worstIndividual = population[i];  
  171.             worstIndex = i;  
  172.         }  
  173.         sum += population[i].fitness;  
  174.     }  
  175.     if (generation == 0) {  
  176.         currentBest = bestIndividual;  
  177.     } else {  
  178.         if (bestIndividual.fitness > currentBest.fitness) {  
  179.             currentBest = bestIndividual;  
  180.         }  
  181.     }  
  182.   
  183. }  
  184.   
  185. //计算适应度///  
  186. void calculateFitnessValue() {  
  187.     int i;  
  188.     long temp1, temp2;  
  189.     double x1, x2;  
  190.     for (i = 0; i < PopSize; i++) {  
  191.         temp1 = decomdeChromosome(population[i].chrom, 0, LENGHT1);  
  192.         temp2 = decomdeChromosome(population[i].chrom, LENGHT1, LENGHT2);  
  193.         x1 = temp1 * temp1;  
  194.         x2 = temp2 * temp2;  
  195.         population[i].fitness = x1+x2;  
  196.     }  
  197.   
  198. }  
  199.   
  200. //计算目标值  
  201. //目标函数为f(x) = x1* x1 +  x2*x2  
  202. void calculateObjectValue() {  
  203.     int i;  
  204.     long temp1, temp2;  
  205.     double x1, x2;  
  206.     for (i = 0; i < PopSize; i++) {  
  207.         temp1 = decomdeChromosome(population[i].chrom, 0, LENGHT1);  
  208.         temp2 = decomdeChromosome(population[i].chrom, LENGHT1, LENGHT2);  
  209.         x1 = temp1 * temp1;  
  210.         x2 = temp2 * temp2;  
  211.         population[i].value = x1 + x2;  
  212.     }  
  213.   
  214. }  
  215.   
  216. //把二进制转化为十进制  
  217. long decomdeChromosome(char* string, int point, int length) {  
  218.     int i;  
  219.     long decimal = 0L;  
  220.     char * pointer;  
  221.     for(i = 0, pointer=string+point; i < length;i++,pointer++){  
  222.         decimal += (*pointer - '0') << (length - 1 - i);  
  223.     }  
  224.     return decimal;  
  225. }  
  226.   
  227.   
  228. //进经同时把最坏个体用目前最好个人替代///  
  229. void performEvolution() {  
  230.     if (bestIndividual.fitness > currentBest.fitness) {  
  231.         currentBest = population[bestIndex];  
  232.     } else {  
  233.         population[worstIndex] = currentBest;  
  234.     }  
  235. }  
  236.   
  237.   
  238. //打印当前样本信息///  
  239. void outputTextReport() {  
  240.     int i;  
  241.     double sum;  
  242.     double average;  
  243.   
  244.     sum = 0.0;  
  245.     for (i = 0; i < PopSize; i++) {  
  246.         sum += population[i].value;  
  247.     }  
  248.     average = sum / PopSize;  
  249.     printf("gen=%d, avg=%f, best=%f",generation, average,currentBest.value);  
  250.     printf(" chromosome=");  
  251.     for(  i = 0; i < CHROMLENGTH; i++){  
  252.         printf("%c", currentBest.chrom[i]);  
  253.     }  
  254.     printf("/n");  
  255.   
  256. }  

 

输出结果:

[c-sharp] view plaincopy
  1. gen=1, avg=51.400000, best=74.000000 chromosome=101111  
  2. gen=2, avg=58.300000, best=74.000000 chromosome=101111  
  3. gen=3, avg=66.300000, best=74.000000 chromosome=101111  
  4. gen=4, avg=71.400000, best=74.000000 chromosome=101111  
  5. gen=5, avg=75.100000, best=98.000000 chromosome=111111  
  6. gen=6, avg=83.600000, best=98.000000 chromosome=111111  
  7. gen=7, avg=88.400000, best=98.000000 chromosome=111111  
  8. gen=8, avg=90.800000, best=98.000000 chromosome=111111  
  9. gen=9, avg=93.200000, best=98.000000 chromosome=111111  
  10. gen=10, avg=98.000000, best=98.000000 chromosome=111111  
  11. gen=11, avg=98.000000, best=98.000000 chromosome=111111  
  12. gen=12, avg=98.000000, best=98.000000 chromosome=111111  
  13. gen=13, avg=98.000000, best=98.000000 chromosome=111111  
  14. gen=14, avg=98.000000, best=98.000000 chromosome=111111  
  15. gen=15, avg=98.000000, best=98.000000 chromosome=111111  
  16. gen=16, avg=98.000000, best=98.000000 chromosome=111111  
  17. gen=17, avg=98.000000, best=98.000000 chromosome=111111  
  18. gen=18, avg=98.000000, best=98.000000 chromosome=111111  
  19. gen=19, avg=98.000000, best=98.000000 chromosome=111111  
  20. gen=20, avg=98.000000, best=98.000000 chromosome=111111  
  21. gen=21, avg=98.000000, best=98.000000 chromosome=111111  
  22. gen=22, avg=98.000000, best=98.000000 chromosome=111111  
  23. gen=23, avg=98.000000, best=98.000000 chromosome=111111  
  24. gen=24, avg=98.000000, best=98.000000 chromosome=111111  
  25. gen=25, avg=98.000000, best=98.000000 chromosome=111111  
  26. gen=26, avg=98.000000, best=98.000000 chromosome=111111  
  27. gen=27, avg=98.000000, best=98.000000 chromosome=111111  
  28. gen=28, avg=98.000000, best=98.000000 chromosome=111111  
  29. gen=29, avg=98.000000, best=98.000000 chromosome=111111  
  30. gen=30, avg=98.000000, best=98.000000 chromosome=111111  
  31. gen=31, avg=98.000000, best=98.000000 chromosome=111111  
  32. gen=32, avg=98.000000, best=98.000000 chromosome=111111  
  33. gen=33, avg=98.000000, best=98.000000 chromosome=111111  
  34. gen=34, avg=98.000000, best=98.000000 chromosome=111111  
  35. gen=35, avg=98.000000, best=98.000000 chromosome=111111  
  36. gen=36, avg=98.000000, best=98.000000 chromosome=111111  
  37. gen=37, avg=98.000000, best=98.000000 chromosome=111111  
  38. gen=38, avg=98.000000, best=98.000000 chromosome=111111  
  39. gen=39, avg=98.000000, best=98.000000 chromosome=111111  
  40. gen=40, avg=98.000000, best=98.000000 chromosome=111111  
  41. gen=41, avg=98.000000, best=98.000000 chromosome=111111  
  42. gen=42, avg=98.000000, best=98.000000 chromosome=111111  
  43. gen=43, avg=98.000000, best=98.000000 chromosome=111111  
  44. gen=44, avg=98.000000, best=98.000000 chromosome=111111  
  45. gen=45, avg=98.000000, best=98.000000 chromosome=111111  
  46. gen=46, avg=98.000000, best=98.000000 chromosome=111111  
  47. gen=47, avg=98.000000, best=98.000000 chromosome=111111  
  48. gen=48, avg=98.000000, best=98.000000 chromosome=111111  
  49. gen=49, avg=98.000000, best=98.000000 chromosome=111111  
  50. gen=50, avg=98.000000, best=98.000000 chromosome=111111  
  51. gen=51, avg=98.000000, best=98.000000 chromosome=111111  
  52. gen=52, avg=98.000000, best=98.000000 chromosome=111111  
  53. gen=53, avg=98.000000, best=98.000000 chromosome=111111  
  54. gen=54, avg=98.000000, best=98.000000 chromosome=111111  
  55. gen=55, avg=98.000000, best=98.000000 chromosome=111111  
  56. gen=56, avg=98.000000, best=98.000000 chromosome=111111  
  57. gen=57, avg=98.000000, best=98.000000 chromosome=111111  
  58. gen=58, avg=98.000000, best=98.000000 chromosome=111111  
  59. gen=59, avg=98.000000, best=98.000000 chromosome=111111  
  60. gen=60, avg=98.000000, best=98.000000 chromosome=111111  
  61. gen=61, avg=98.000000, best=98.000000 chromosome=111111  
  62. gen=62, avg=98.000000, best=98.000000 chromosome=111111  
  63. gen=63, avg=98.000000, best=98.000000 chromosome=111111  
  64. gen=64, avg=98.000000, best=98.000000 chromosome=111111  
  65. gen=65, avg=98.000000, best=98.000000 chromosome=111111  
  66. gen=66, avg=98.000000, best=98.000000 chromosome=111111  
  67. gen=67, avg=98.000000, best=98.000000 chromosome=111111  
  68. gen=68, avg=98.000000, best=98.000000 chromosome=111111  
  69. gen=69, avg=98.000000, best=98.000000 chromosome=111111  
  70. gen=70, avg=98.000000, best=98.000000 chromosome=111111  
  71. gen=71, avg=98.000000, best=98.000000 chromosome=111111  
  72. gen=72, avg=98.000000, best=98.000000 chromosome=111111  
  73. gen=73, avg=98.000000, best=98.000000 chromosome=111111  
  74. gen=74, avg=98.000000, best=98.000000 chromosome=111111  
  75. gen=75, avg=98.000000, best=98.000000 chromosome=111111  
  76. gen=76, avg=98.000000, best=98.000000 chromosome=111111  
  77. gen=77, avg=98.000000, best=98.000000 chromosome=111111  
  78. gen=78, avg=98.000000, best=98.000000 chromosome=111111  
  79. gen=79, avg=98.000000, best=98.000000 chromosome=111111  
  80. gen=80, avg=98.000000, best=98.000000 chromosome=111111  
  81. gen=81, avg=98.000000, best=98.000000 chromosome=111111  
  82. gen=82, avg=98.000000, best=98.000000 chromosome=111111  
  83. gen=83, avg=98.000000, best=98.000000 chromosome=111111  
  84. gen=84, avg=98.000000, best=98.000000 chromosome=111111  
  85. gen=85, avg=98.000000, best=98.000000 chromosome=111111  
  86. gen=86, avg=98.000000, best=98.000000 chromosome=111111  
  87. gen=87, avg=98.000000, best=98.000000 chromosome=111111  
  88. gen=88, avg=98.000000, best=98.000000 chromosome=111111  
  89. gen=89, avg=98.000000, best=98.000000 chromosome=111111  
  90. gen=90, avg=98.000000, best=98.000000 chromosome=111111  
  91. gen=91, avg=98.000000, best=98.000000 chromosome=111111  
  92. gen=92, avg=98.000000, best=98.000000 chromosome=111111  
  93. gen=93, avg=98.000000, best=98.000000 chromosome=111111  
  94. gen=94, avg=98.000000, best=98.000000 chromosome=111111  
  95. gen=95, avg=98.000000, best=98.000000 chromosome=111111  
  96. gen=96, avg=98.000000, best=98.000000 chromosome=111111  
  97. gen=97, avg=98.000000, best=98.000000 chromosome=111111  
  98. gen=98, avg=98.000000, best=98.000000 chromosome=111111  
  99. gen=99, avg=98.000000, best=98.000000 chromosome=111111  
  100. gen=100, avg=98.000000, best=98.000000 chromosome=111111  
  101. 请按任意键继续. . .  

从结果可以看出,遗传算法收敛得非常快,在第5代的时候,已经达到了全局最优解.如果把初始种群扩大,收敛得会更快.

遗传算法主要应用领域:

(1)组合优化     (2)函数优化
(3)自动控制     (4)生产调度
(5)图像处理      (6)机器学习
(7)人工生命      (8)数据挖掘

0 0
原创粉丝点击