这篇文章给大家介绍C#中怎么实现一个遗传算法,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
C#遗传算法实现代码:
using System; using System.Collections.Generic; using System.Text; namespace GA { class Program { static void Main(string[] args) { World world = new World(); world.Init(); for (int i = 0; i < 50; i++) { world.Evolve(); Console.WriteLine(i); world.Show(); } } } class World { int kMaxFlowers = 11; Random Rnd = new Random(); public int[] temperature; public int[] water; public int[] sunlight; public int[] nutrient; public int[] beneficialInsect; public int[] harmfulInsect; public int currentTemperature; public int currentWater; public int currentSunlight; public int currentNutrient; public int currentBeneficialInsect; public int currentHarmfulInsect; public World() { temperature = new int[kMaxFlowers]; water = new int[kMaxFlowers]; sunlight = new int[kMaxFlowers]; nutrient = new int[kMaxFlowers]; beneficialInsect = new int[kMaxFlowers]; harmfulInsect = new int[kMaxFlowers]; } /**//// <summary> /// 初始化***代花朵的基因结构 /// </summary> public void Init() { for (int i = 1; i < kMaxFlowers; i++) { temperature[i] = Rnd.Next(1, 75); water[i] = Rnd.Next(1, 75); sunlight[i] = Rnd.Next(1, 75); nutrient[i] = Rnd.Next(1, 75); beneficialInsect[i] = Rnd.Next(1, 75); harmfulInsect[i] = Rnd.Next(1, 75); } currentTemperature = Rnd.Next(1, 75); currentWater = Rnd.Next(1, 75); currentSunlight = Rnd.Next(1, 75); currentNutrient = Rnd.Next(1, 75); currentBeneficialInsect = Rnd.Next(1, 75); currentHarmfulInsect = Rnd.Next(1, 75); } /**//// <summary> /// 越大说明花朵的适应环境的能力差,小说明适应环境的能力强 /// </summary> /// <param name="flower"></param> /// <returns></returns> 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); } /**//// <summary> /// 排除适应能力差的花朵,让适应能力强的花朵杂交繁殖,产生下一代。同时有一定的概率变异。 /// </summary> 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); } } /**//// <summary> /// 显示种群中个体对环境的适应能力,还有所有个体对环境的适应能力之和。 /// </summary> 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#中怎么实现一个遗传算法就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。