這篇文章給大家介紹C#中怎么實(shí)現(xiàn)一個(gè)遺傳算法,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
創(chuàng)新互聯(lián)建站作為成都網(wǎng)站建設(shè)公司,專注成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì),有關(guān)成都定制網(wǎng)頁(yè)設(shè)計(jì)方案、改版、費(fèi)用等問(wèn)題,行業(yè)涉及銅雕雕塑等多個(gè)領(lǐng)域,已為上千家企業(yè)服務(wù),得到了客戶的尊重與認(rèn)可。
C#遺傳算法實(shí)現(xiàn)代碼:
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> /// 初始化***代花朵的基因結(jié)構(gòu) /// </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> /// 越大說(shuō)明花朵的適應(yīng)環(huán)境的能力差,小說(shuō)明適應(yīng)環(huán)境的能力強(qiáng) /// </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> /// 排除適應(yīng)能力差的花朵,讓適應(yīng)能力強(qiáng)的花朵雜交繁殖,產(chǎn)生下一代。同時(shí)有一定的概率變異。 /// </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> /// 顯示種群中個(gè)體對(duì)環(huán)境的適應(yīng)能力,還有所有個(gè)體對(duì)環(huán)境的適應(yīng)能力之和。 /// </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); } } }
關(guān)于C#中怎么實(shí)現(xiàn)一個(gè)遺傳算法就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
分享文章:C#中怎么實(shí)現(xiàn)一個(gè)遺傳算法
分享地址:http://jinyejixie.com/article26/psphjg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、企業(yè)網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、服務(wù)器托管、虛擬主機(jī)、自適應(yīng)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)