﻿ C#實現遺傳算法 模擬花朵的進化_電腦知識網

# C#實現遺傳算法 模擬花朵的進化

2013-11-13 09:56:14  來源: .NET編程
以下代碼實現了一個簡單的花朵進化的模擬過程
花朵的種群數量是共進化了

通過運行程序你會發現通過不斷的進化種群的總的適應環境的能力在逐步提高（fitness的值下降）

實現代碼

using System; using SystemCollectionsGeneric; using SystemText; namespace GA { class Program { static void Main(string[] args) { World world = new World(); worldInit(); for (int i = ; i < ; i++) { worldEvolve(); ConsoleWriteLine(i); worldShow(); } } } class World { int kMaxFlowers = ; 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]; } /**//// /// 初始化第一代花朵的基因結構 /// public void Init() { for (int i = ; i < kMaxFlowers; i++) { temperature[i] = RndNext( ); water[i] = RndNext( ); sunlight[i] = RndNext( ); nutrient[i] = RndNext( ); beneficialInsect[i] = RndNext( ); harmfulInsect[i] = RndNext( ); } currentTemperature = RndNext( ); currentWater = RndNext( ); currentSunlight = RndNext( ); currentNutrient = RndNext( ); currentBeneficialInsect = RndNext( ); currentHarmfulInsect = RndNext( ); } /**//// /// 越大說明花朵的適應環境的能力差小說明適應環境的能力強 /// /// /// private int Fitness(int flower) { int theFitness = ; theFitness = MathAbs(temperature[flower] currentTemperature); theFitness = theFitness + MathAbs(water[flower] currentWater); theFitness = theFitness + MathAbs(sunlight[flower] currentSunlight); theFitness = theFitness + MathAbs(nutrient[flower] currentNutrient); theFitness = theFitness + MathAbs(beneficialInsect[flower] currentBeneficialInsect); theFitness = theFitness + MathAbs(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 = ; int leastFitIndex = ; for (i = ; i < kMaxFlowers; i++) if (Fitness(i) > leastFit) { leastFit = Fitness(i); leastFitIndex = i; } temperature[leastFitIndex] = temperature[RndNext( )]; water[leastFitIndex] = water[RndNext( )]; sunlight[leastFitIndex] = sunlight[RndNext( )]; nutrient[leastFitIndex] = nutrient[RndNext( )]; beneficialInsect[leastFitIndex] = beneficialInsect[RndNext( )]; harmfulInsect[leastFitIndex] = harmfulInsect[RndNext( )]; for (i = ; i < kMaxFlowers; i++) { fitTemperature[i] = temperature[RndNext( )]; fitWater[i] = water[RndNext( )]; fitSunlight[i] = sunlight[RndNext( )]; fitNutrient[i] = nutrient[RndNext( )]; fitBeneficialInsect[i] = beneficialInsect[RndNext( )]; fitHarmfulInsect[i] = harmfulInsect[RndNext( )]; } for (i = ; 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 = ; i < kMaxFlowers; i++) { if (RndNext( ) == ) temperature[i] = RndNext( ); if (RndNext( ) == ) water[i] = RndNext( ); if (RndNext( ) == ) sunlight[i] = RndNext( ); if (RndNext( ) == ) nutrient[i] = RndNext( ); if (RndNext( ) == ) beneficialInsect[i] = RndNext( ); if (RndNext( ) == ) harmfulInsect[i] = RndNext( ); } } /**//// /// 顯示種群中個體對環境的適應能力還有所有個體對環境的適應能力之和 /// public void Show() { int sum = ; for (int i = ; i < kMaxFlowers; i++) { int fitness = Fitness(i); sum += fitness; ConsoleWriteLine(No + i + s fitness is + fitness); } ConsoleWriteLine(fitness sum is + sum); } } }

From:http://tw.wingwit.com/Article/program/net/201311/12037.html