C#遗传算法学习笔记

开发 后端 算法
本文介绍C#遗传算法学习笔记,通过运行程序,你会发现通过不断的进化,种群的总的适应环境的能力在逐步提高。

以下代码实现了C#遗传算法一个简单的花朵进化的模拟过程。

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

C#遗传算法实现代码:

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

以上是C#遗传算法学习笔记

【编辑推荐】

  1. C#生产者和消费者
  2. 详细介绍C#基础知识
  3. C#正则表达式学习笔记
  4. 简单描述C#存储过程
  5. 浅析C#基于TCP协议
责任编辑:佚名 来源: 博客园
相关推荐

2009-08-14 17:38:08

C#改写方法

2009-08-21 18:01:32

C#匿名方法

2009-08-12 17:32:44

C#反射方法

2009-08-31 16:51:11

C# Main()方法

2009-08-27 09:27:49

C#扩展方法

2021-03-10 15:49:20

人工智能遗传算法

2017-10-17 14:25:56

机器学习算法优化

2017-11-16 15:25:54

Go语言算法代码

2017-05-10 15:41:29

机器学习算法数据

2009-10-14 09:27:30

VB.NET编码算法

2009-08-13 18:21:52

C#学习笔记

2009-08-12 09:28:36

C# WiteOne

2009-08-20 15:02:57

C# If语句

2009-08-12 15:50:40

C# ListBox

2020-10-26 13:42:28

Python算法垃圾

2020-06-11 08:32:50

Python遗传算法代码

2021-03-16 11:30:33

Python遗传算法神经网络

2009-08-26 10:48:44

C# SQL命令

2009-08-31 15:27:33

C# TreeView

2009-08-24 15:46:46

C# SmartPho
点赞
收藏

51CTO技术栈公众号