C#遗传算法学习笔记

  • 本文介绍C#遗传算法学习笔记,通过运行程序,你会发现通过不断的进化,种群的总的适应环境的能力在逐步提高。
  • 以下代码实现了C#遗传算法一个简单的花朵进化的模拟过程。

    花朵的种群数量是10,共进化了50代。通过运行程序,你会发现通过不断的进化,种群的总的适应环境的能力在逐步提高(fitness的值下降)。

    C#遗传算法实现代码:

      
      
    1. using System; 
    2. using System.Collections.Generic; 
    3. using System.Text; 
    4. namespace GA 
    5. class Program 
    6. static void Main(string[] args) 
    7. World world = new World(); 
    8. world.Init(); 
    9. for (int i = 0; i < 50; i++) 
    10. world.Evolve(); 
    11. Console.WriteLine(i); 
    12. world.Show(); 
    13.  
    14. class World 
    15. int kMaxFlowers = 11; 
    16. Random Rnd = new Random(); 
    17. public int[] temperature; 
    18. public int[] water; 
    19. public int[] sunlight; 
    20. public int[] nutrient; 
    21. public int[] beneficialInsect; 
    22. public int[] harmfulInsect; 
    23. public int currentTemperature; 
    24. public int currentWater; 
    25. public int currentSunlight; 
    26. public int currentNutrient; 
    27. public int currentBeneficialInsect; 
    28. public int currentHarmfulInsect; 
    29. public World() 
    30. temperature = new int[kMaxFlowers]; 
    31. water = new int[kMaxFlowers]; 
    32. sunlight = new int[kMaxFlowers]; 
    33. nutrient = new int[kMaxFlowers]; 
    34. beneficialInsect = new int[kMaxFlowers]; 
    35. harmfulInsect = new int[kMaxFlowers]; 
    36. /**  
      • /// 初始化第一代花朵的基因结构 
      • ///
       
    37. public void Init() 
    38. for (int i = 1; i < kMaxFlowers; i++) 
    39. temperature[i] = Rnd.Next(1, 75); 
    40. water[i] = Rnd.Next(1, 75); 
    41. sunlight[i] = Rnd.Next(1, 75); 
    42. nutrient[i] = Rnd.Next(1, 75); 
    43. beneficialInsect[i] = Rnd.Next(1, 75); 
    44. harmfulInsect[i] = Rnd.Next(1, 75); 
    45. currentTemperature = Rnd.Next(1, 75); 
    46. currentWater = Rnd.Next(1, 75); 
    47. currentSunlight = Rnd.Next(1, 75); 
    48. currentNutrient = Rnd.Next(1, 75); 
    49. currentBeneficialInsect = Rnd.Next(1, 75); 
    50. currentHarmfulInsect = Rnd.Next(1, 75); 
    51. /**  
      • /// 越大说明花朵的适应环境的能力差,小说明适应环境的能力强 
      • ///
       
    52. /// name="flower"> 
    53. ///  
    54. private int Fitness(int flower) 
    55. int theFitness = 0; 
    56. theFitness = Math.Abs(temperature[flower] - currentTemperature); 
    57. theFitnesstheFitness = theFitness + Math.Abs(water[flower] - currentWater); 
    58. theFitnesstheFitness = theFitness + Math.Abs(sunlight[flower] - 
    59. currentSunlight); 
    60. theFitnesstheFitness = theFitness + Math.Abs(nutrient[flower] - 
    61. currentNutrient); 
    62. theFitnesstheFitness = theFitness + Math.Abs(beneficialInsect[flower] - 
    63. currentBeneficialInsect); 
    64. theFitnesstheFitness = theFitness + Math.Abs(harmfulInsect[flower] - 
    65. currentHarmfulInsect); 
    66. return (theFitness); 
    67. /**  
      • /// 排除适应能力差的花朵,让适应能力强的花朵杂交繁殖,产生下一代。同时有一定的概率变异。 
      • ///
       
    68. public void Evolve() 
    69. int[] fitTemperature = new int[kMaxFlowers]; 
    70. int[] fitWater = new int[kMaxFlowers]; 
    71. int[] fitSunlight = new int[kMaxFlowers]; 
    72. int[] fitNutrient = new int[kMaxFlowers]; 
    73. int[] fitBeneficialInsect = new int[kMaxFlowers]; 
    74. int[] fitHarmfulInsect = new int[kMaxFlowers]; 
    75. int[] fitness = new int[kMaxFlowers]; 
    76. int i; 
    77. int leastFit = 0; 
    78. int leastFitIndex = 1; 
    79. for (i = 1; i < kMaxFlowers; i++) 
    80. if (Fitness(i) > leastFit) 
    81. leastFit = Fitness(i); 
    82. leastFitIndex = i; 
    83. temperature[leastFitIndex] = temperature[Rnd.Next(1, 10)]; 
    84. water[leastFitIndex] = water[Rnd.Next(1, 10)]; 
    85. sunlight[leastFitIndex] = sunlight[Rnd.Next(1, 10)]; 
    86. nutrient[leastFitIndex] = nutrient[Rnd.Next(1, 10)]; 
    87. beneficialInsect[leastFitIndex] = beneficialInsect[Rnd.Next(1, 10)]; 
    88. harmfulInsect[leastFitIndex] = harmfulInsect[Rnd.Next(1, 10)]; 
    89. for (i = 1; i < kMaxFlowers; i++) 
    90. fitTemperature[i] = temperature[Rnd.Next(1, 10)]; 
    91. fitWater[i] = water[Rnd.Next(1, 10)]; 
    92. fitSunlight[i] = sunlight[Rnd.Next(1, 10)]; 
    93. fitNutrient[i] = nutrient[Rnd.Next(1, 10)]; 
    94. fitBeneficialInsect[i] = beneficialInsect[Rnd.Next(1, 10)]; 
    95. fitHarmfulInsect[i] = harmfulInsect[Rnd.Next(1, 10)]; 
    96. for (i = 1; i < kMaxFlowers; i++) 
    97. temperature[i] = fitTemperature[i]; 
    98. water[i] = fitWater[i]; 
    99. sunlight[i] = fitSunlight[i]; 
    100. nutrient[i] = fitNutrient[i]; 
    101. beneficialInsect[i] = fitBeneficialInsect[i]; 
    102. harmfulInsect[i] = fitHarmfulInsect[i]; 
    103. for (i = 1; i < kMaxFlowers; i++) 
    104. if (Rnd.Next(1, 100) == 1) 
    105. temperature[i] = Rnd.Next(1, 75); 
    106. if (Rnd.Next(1, 100) == 1) 
    107. water[i] = Rnd.Next(1, 75); 
    108. if (Rnd.Next(1, 100) == 1) 
    109. sunlight[i] = Rnd.Next(1, 75); 
    110. if (Rnd.Next(1, 100) == 1) 
    111. nutrient[i] = Rnd.Next(1, 75); 
    112. if (Rnd.Next(1, 100) == 1) 
    113. beneficialInsect[i] = Rnd.Next(1, 75); 
    114. if (Rnd.Next(1, 100) == 1) 
    115. harmfulInsect[i] = Rnd.Next(1, 75); 
    116. /**  
      • /// 显示种群中个体对环境的适应能力,还有所有个体对环境的适应能力之和。 
      • ///
       
    117. public void Show() 
    118. int sum = 0; 
    119. for (int i = 1; i < kMaxFlowers; i++) 
    120. int fitness = Fitness(i); 
    121. sum += fitness; 
    122. Console.WriteLine("No." + i + "'s fitness is " + fitness); 
    123. Console.WriteLine("fitness sum is " + sum); 
    124. }

转载 吾搜 空间

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值