C#遺傳演算法學習筆記

來源:互聯網
上載者:User
    本文介紹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. /// 初始化第一代花朵的基因結構 
  38. ///  
  39. public void Init() 
  40. for (int i = 1; i  kMaxFlowers; i++) 
  41. temperature[i] = Rnd.Next(1, 75); 
  42. water[i] = Rnd.Next(1, 75); 
  43. sunlight[i] = Rnd.Next(1, 75); 
  44. nutrient[i] = Rnd.Next(1, 75); 
  45. beneficialInsect[i] = Rnd.Next(1, 75); 
  46. harmfulInsect[i] = Rnd.Next(1, 75); 
  47. currentTemperature = Rnd.Next(1, 75); 
  48. currentWater = Rnd.Next(1, 75); 
  49. currentSunlight = Rnd.Next(1, 75); 
  50. currentNutrient = Rnd.Next(1, 75); 
  51. currentBeneficialInsect = Rnd.Next(1, 75); 
  52. currentHarmfulInsect = Rnd.Next(1, 75); 
  53. /**////  
  54. /// 越大說明花朵的適應環境的能力差,小說明適應環境的能力強 
  55. ///  
  56. /// name="flower"> 
  57. ///  
  58. private int Fitness(int flower) 
  59. int theFitness = 0; 
  60. theFitness = Math.Abs(temperature[flower] - currentTemperature); 
  61. theFitnesstheFitness = theFitness + Math.Abs(water[flower] - currentWater); 
  62. theFitnesstheFitness = theFitness + Math.Abs(sunlight[flower] - 
  63. currentSunlight); 
  64. theFitnesstheFitness = theFitness + Math.Abs(nutrient[flower] - 
  65. currentNutrient); 
  66. theFitnesstheFitness = theFitness + Math.Abs(beneficialInsect[flower] - 
  67. currentBeneficialInsect); 
  68. theFitnesstheFitness = theFitness + Math.Abs(harmfulInsect[flower] - 
  69. currentHarmfulInsect); 
  70. return (theFitness); 
  71. /**////  
  72. /// 排除適應能力差的花朵,讓適應能力強的花朵雜交繁殖,產生下一代。同時有一定的機率變異。 
  73. ///  
  74. public void Evolve() 
  75. int[] fitTemperature = new int[kMaxFlowers]; 
  76. int[] fitWater = new int[kMaxFlowers]; 
  77. int[] fitSunlight = new int[kMaxFlowers]; 
  78. int[] fitNutrient = new int[kMaxFlowers]; 
  79. int[] fitBeneficialInsect = new int[kMaxFlowers]; 
  80. int[] fitHarmfulInsect = new int[kMaxFlowers]; 
  81. int[] fitness = new int[kMaxFlowers]; 
  82. int i; 
  83. int leastFit = 0; 
  84. int leastFitIndex = 1; 
  85. for (i = 1; i  kMaxFlowers; i++) 
  86. if (Fitness(i) > leastFit) 
  87. leastFit = Fitness(i); 
  88. leastFitIndex = i; 
  89. temperature[leastFitIndex] = temperature[Rnd.Next(1, 10)]; 
  90. water[leastFitIndex] = water[Rnd.Next(1, 10)]; 
  91. sunlight[leastFitIndex] = sunlight[Rnd.Next(1, 10)]; 
  92. nutrient[leastFitIndex] = nutrient[Rnd.Next(1, 10)]; 
  93. beneficialInsect[leastFitIndex] = beneficialInsect[Rnd.Next(1, 10)]; 
  94. harmfulInsect[leastFitIndex] = harmfulInsect[Rnd.Next(1, 10)]; 
  95. for (i = 1; i  kMaxFlowers; i++) 
  96. fitTemperature[i] = temperature[Rnd.Next(1, 10)]; 
  97. fitWater[i] = water[Rnd.Next(1, 10)]; 
  98. fitSunlight[i] = sunlight[Rnd.Next(1, 10)]; 
  99. fitNutrient[i] = nutrient[Rnd.Next(1, 10)]; 
  100. fitBeneficialInsect[i] = beneficialInsect[Rnd.Next(1, 10)]; 
  101. fitHarmfulInsect[i] = harmfulInsect[Rnd.Next(1, 10)]; 
  102. for (i = 1; i  kMaxFlowers; i++) 
  103. temperature[i] = fitTemperature[i]; 
  104. water[i] = fitWater[i]; 
  105. sunlight[i] = fitSunlight[i]; 
  106. nutrient[i] = fitNutrient[i]; 
  107. beneficialInsect[i] = fitBeneficialInsect[i]; 
  108. harmfulInsect[i] = fitHarmfulInsect[i]; 
  109. for (i = 1; i  kMaxFlowers; i++) 
  110. if (Rnd.Next(1, 100) == 1) 
  111. temperature[i] = Rnd.Next(1, 75); 
  112. if (Rnd.Next(1, 100) == 1) 
  113. water[i] = Rnd.Next(1, 75); 
  114. if (Rnd.Next(1, 100) == 1) 
  115. sunlight[i] = Rnd.Next(1, 75); 
  116. if (Rnd.Next(1, 100) == 1) 
  117. nutrient[i] = Rnd.Next(1, 75); 
  118. if (Rnd.Next(1, 100) == 1) 
  119. beneficialInsect[i] = Rnd.Next(1, 75); 
  120. if (Rnd.Next(1, 100) == 1) 
  121. harmfulInsect[i] = Rnd.Next(1, 75); 
  122. /**////  
  123. /// 顯示種群中個體對環境的適應能力,還有所有個體對環境的適應能力之和。 
  124. ///  
  125. public void Show() 
  126. int sum = 0; 
  127. for (int i = 1; i  kMaxFlowers; i++) 
  128. int fitness = Fitness(i); 
  129. sum += fitness; 
  130. Console.WriteLine("No." + i + "'s fitness is " + fitness); 
  131. Console.WriteLine("fitness sum is " + sum); 
  132. }

轉載 吾搜 空間

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.