成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

C#遺傳算法學(xué)習(xí)筆記

開(kāi)發(fā) 后端 算法
本文介紹C#遺傳算法學(xué)習(xí)筆記,通過(guò)運(yùn)行程序,你會(huì)發(fā)現(xiàn)通過(guò)不斷的進(jìn)化,種群的總的適應(yīng)環(huán)境的能力在逐步提高。

以下代碼實(shí)現(xiàn)了C#遺傳算法一個(gè)簡(jiǎn)單的花朵進(jìn)化的模擬過(guò)程。

花朵的種群數(shù)量是10,共進(jìn)化了50代。通過(guò)運(yùn)行程序,你會(huì)發(fā)現(xiàn)通過(guò)不斷的進(jìn)化,種群的總的適應(yīng)環(huán)境的能力在逐步提高(fitness的值下降)。

C#遺傳算法實(shí)現(xiàn)代碼:

  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. /// 初始化***代花朵的基因結(jié)構(gòu)  
  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. /// 越大說(shuō)明花朵的適應(yīng)環(huán)境的能力差,小說(shuō)明適應(yīng)環(huán)境的能力強(qiáng)  
  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. /// 排除適應(yīng)能力差的花朵,讓適應(yīng)能力強(qiáng)的花朵雜交繁殖,產(chǎn)生下一代。同時(shí)有一定的概率變異。  
  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. /// 顯示種群中個(gè)體對(duì)環(huán)境的適應(yīng)能力,還有所有個(gè)體對(duì)環(huán)境的適應(yīng)能力之和。  
  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#遺傳算法學(xué)習(xí)筆記

【編輯推薦】

  1. C#生產(chǎn)者和消費(fèi)者
  2. 詳細(xì)介紹C#基礎(chǔ)知識(shí)
  3. C#正則表達(dá)式學(xué)習(xí)筆記
  4. 簡(jiǎn)單描述C#存儲(chǔ)過(guò)程
  5. 淺析C#基于TCP協(xié)議
責(zé)任編輯:佚名 來(lái)源: 博客園
相關(guān)推薦

2009-08-14 17:38:08

C#改寫(xiě)方法

2009-08-21 18:01:32

C#匿名方法

2009-08-12 17:32:44

C#反射方法

2009-08-27 09:27:49

C#擴(kuò)展方法

2009-08-31 16:51:11

C# Main()方法

2021-03-10 15:49:20

人工智能遺傳算法

2017-10-17 14:25:56

機(jī)器學(xué)習(xí)算法優(yōu)化

2017-05-10 15:41:29

機(jī)器學(xué)習(xí)算法數(shù)據(jù)

2017-11-16 15:25:54

Go語(yǔ)言算法代碼

2025-01-16 07:10:00

2009-10-14 09:27:30

VB.NET編碼算法

2009-08-12 09:28:36

C# WiteOne

2009-08-13 18:21:52

C#學(xué)習(xí)筆記

2009-08-20 15:02:57

C# If語(yǔ)句

2009-08-12 15:50:40

C# ListBox

2024-07-03 08:00:00

2009-08-24 15:46:46

C# SmartPho

2009-08-26 10:48:44

C# SQL命令

2009-08-31 15:27:33

C# TreeView

2009-08-21 17:53:28

C#查詢(xún)結(jié)果
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 色偷偷人人澡人人爽人人模 | 欧美日韩不卡 | 国产精品毛片一区二区在线看 | 在线观看国产www | 免费一区二区在线观看 | 久久免费精品视频 | 成人深夜小视频 | 亚洲一二三区不卡 | 成人h免费观看视频 | 最新av在线播放 | 狠狠干在线 | 久久三区 | 国产视频1区 | 欧美日韩综合 | 日韩精品免费一区二区在线观看 | 国产一级免费视频 | 国产精品日韩欧美 | 成人精品一区二区三区中文字幕 | 日韩av在线免费 | 国产免费一二三区 | 国产精品成人久久久久a级 久久蜜桃av一区二区天堂 | 亚洲小视频 | 日韩精品色网 | 中文字幕av网站 | 蜜臀久久99精品久久久久久宅男 | 久久精品99国产精品日本 | 九色在线 | 波多野结衣在线观看一区二区三区 | 欧美一级三级在线观看 | 精品一区二区在线观看 | 亚洲第一福利视频 | 中文字幕一区在线 | 一区二区免费 | 国产亚洲精品久久情网 | 国产午夜av片 | 国产精品一区二区在线 | 97久久超碰 | 99在线免费视频 | 91免费看片 | 亚洲国产精品人人爽夜夜爽 | 亚洲成人自拍 |