C#遗传算法学习笔记

  • 本文介绍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. ///  
private int Fitness(int flower)  {  int theFitness = 0;  theFitness = Math.Abs(temperature[flower] - currentTemperature);  theFitnesstheFitness = theFitness + Math.Abs(water[flower] - currentWater);  theFitnesstheFitness = theFitness + Math.Abs(sunlight[flower] -  currentSunlight);  theFitnesstheFitness = theFitness + Math.Abs(nutrient[flower] -  currentNutrient);  theFitnesstheFitness = theFitness + Math.Abs(beneficialInsect[flower] -  currentBeneficialInsect);  theFitnesstheFitness = theFitness + Math.Abs(harmfulInsect[flower] -  currentHarmfulInsect);  return (theFitness);  }  /**  /// 排除适应能力差的花朵,让适应能力强的花朵杂交繁殖,产生下一代。同时有一定的概率变异。  ///  public void Evolve()  {  int[] fitTemperature = new int[kMaxFlowers];  int[] fitWater = new int[kMaxFlowers];  int[] fitSunlight = new int[kMaxFlowers];  int[] fitNutrient = new int[kMaxFlowers];  int[] fitBeneficialInsect = new int[kMaxFlowers];  int[] fitHarmfulInsect = new int[kMaxFlowers];  int[] fitness = new int[kMaxFlowers];  int i;  int leastFit = 0;  int leastFitIndex = 1;  for (i = 1; i < kMaxFlowers; i++)  if (Fitness(i) > leastFit)  {  leastFit = Fitness(i);  leastFitIndex = i;  }  temperature[leastFitIndex] = temperature[Rnd.Next(1, 10)];  water[leastFitIndex] = water[Rnd.Next(1, 10)];  sunlight[leastFitIndex] = sunlight[Rnd.Next(1, 10)];  nutrient[leastFitIndex] = nutrient[Rnd.Next(1, 10)];  beneficialInsect[leastFitIndex] = beneficialInsect[Rnd.Next(1, 10)];  harmfulInsect[leastFitIndex] = harmfulInsect[Rnd.Next(1, 10)];  for (i = 1; i < kMaxFlowers; i++)  {  fitTemperature[i] = temperature[Rnd.Next(1, 10)];  fitWater[i] = water[Rnd.Next(1, 10)];  fitSunlight[i] = sunlight[Rnd.Next(1, 10)];  fitNutrient[i] = nutrient[Rnd.Next(1, 10)];  fitBeneficialInsect[i] = beneficialInsect[Rnd.Next(1, 10)];  fitHarmfulInsect[i] = harmfulInsect[Rnd.Next(1, 10)];  }  for (i = 1; i < kMaxFlowers; i++)  {  temperature[i] = fitTemperature[i];  water[i] = fitWater[i];  sunlight[i] = fitSunlight[i];  nutrient[i] = fitNutrient[i];  beneficialInsect[i] = fitBeneficialInsect[i];  harmfulInsect[i] = fitHarmfulInsect[i];  }  for (i = 1; i < kMaxFlowers; i++)  {  if (Rnd.Next(1, 100) == 1)  temperature[i] = Rnd.Next(1, 75);  if (Rnd.Next(1, 100) == 1)  water[i] = Rnd.Next(1, 75);  if (Rnd.Next(1, 100) == 1)  sunlight[i] = Rnd.Next(1, 75);  if (Rnd.Next(1, 100) == 1)  nutrient[i] = Rnd.Next(1, 75);  if (Rnd.Next(1, 100) == 1)  beneficialInsect[i] = Rnd.Next(1, 75);  if (Rnd.Next(1, 100) == 1)  harmfulInsect[i] = Rnd.Next(1, 75);  }  }  /**  /// 显示种群中个体对环境的适应能力,还有所有个体对环境的适应能力之和。  ///  public void Show()  {  int sum = 0;  for (int i = 1; i < kMaxFlowers; i++)  {  int fitness = Fitness(i);  sum += fitness;  Console.WriteLine("No." + i + "'s fitness is " + fitness);  }  Console.WriteLine("fitness sum is " + sum);  }  }  }

转载 吾搜 空间

你可能感兴趣的:(算法,c#,random,string,class)