以下代码实现了一个简单的花朵进化的模拟过程
花朵的种群数量是共进化了代
通过运行程序你会发现通过不断的进化种群的总的适应环境的能力在逐步提高(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);}}}