C # genetic algorithm learning notes

Source: Internet
Author: User
    This article introduces C # genetic algorithm learning notes. By running a program, you will find that through continuous evolution, the overall adaptability of the population to the environment is gradually improving.

    The following code implements a simple simulation process of flower evolution using the C # genetic algorithm.

    The number of flowers is 10, and 50 generations have been made. By running the program, you will find that through continuous evolution, the overall adaptability of the population to the environment is gradually increasing (fitness value is reduced ).

    C # Genetic Algorithm Implementation Code:

 
 
  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 = 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. /// Initialize the genetic structure of the first generation of flowers
  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. /// The larger the value indicates that the adaptability of flowers to the environment is poor, and the better the adaptability of novels to the environment
  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. /// Exclude the flowers with poor adaptability, so that the flowers with strong adaptability can breed and generate the next generation. At the same time, there is a certain probability variation.
  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. /// Display the adaptability of individual to the environment in the population, and the sum of adaptability of all individuals to the environment.
  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. }

Reprinted my search space

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.