C#遺傳演算法學習筆記

來源:互聯網
上載者:User
    本文介紹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. /**////  
  47. /// 初始化第一代花朵的基因結構 
  48. ///  
  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. /**////  
  68. /// 越大說明花朵的適應環境的能力差,小說明適應環境的能力強 
  69. ///  
  70. /// name="flower"> 
  71. ///  
  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. /**////  
  88. /// 排除適應能力差的花朵,讓適應能力強的花朵雜交繁殖,產生下一代。同時有一定的機率變異。 
  89. ///  
  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. /**////  
  149. /// 顯示種群中個體對環境的適應能力,還有所有個體對環境的適應能力之和。 
  150. ///  
  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. } 
  163. }

轉載 吾搜 空間

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.