C#实现遗传算法,模拟花朵的进化。
 以下代码实现了一个简单的花朵进化的模拟过程。
花朵的种群数量是10,共进化了50代。
通过运行程序,你会发现通过不断的进化,种群的总的适应环境的能力在逐步提高(fitness的值下降)。
实现代码:
 using System;
using System; using System.Collections.Generic;
using System.Collections.Generic; using System.Text;
using System.Text;
 namespace GA
namespace GA {
{ class Program
    class Program {
    { static void Main(string[] args)
        static void Main(string[] args) {
        { World world = new World();
            World world = new World();
 world.Init();
            world.Init();
 for (int i = 0; i < 50; i++)
            for (int i = 0; i < 50; i++) {
            { world.Evolve();
                world.Evolve();
 Console.WriteLine(i);
                Console.WriteLine(i); world.Show();
                world.Show(); }
            } }
        } }
    }
 class World
    class World {
    { int kMaxFlowers = 11;
        int kMaxFlowers = 11;
 Random Rnd = new Random();
        Random Rnd = new Random();
 public int[] temperature;
        public int[] temperature;
 public int[] water;
        public int[] water;
 public int[] sunlight;
        public int[] sunlight;
 public int[] nutrient;
        public int[] nutrient;
 public int[] beneficialInsect;
        public int[] beneficialInsect;
 public int[] harmfulInsect;
        public int[] harmfulInsect;
 public int currentTemperature;
        public int currentTemperature;
 public int currentWater;
        public int currentWater;
 public int currentSunlight;
        public int currentSunlight;
 public int currentNutrient;
        public int currentNutrient;
 public int currentBeneficialInsect;
        public int currentBeneficialInsect;
 public int currentHarmfulInsect;
        public int currentHarmfulInsect;
 public World()
        public World() {
        { temperature = new int[kMaxFlowers];
            temperature = new int[kMaxFlowers]; water = new int[kMaxFlowers];
            water = new int[kMaxFlowers]; sunlight = new int[kMaxFlowers];
            sunlight = new int[kMaxFlowers]; nutrient = new int[kMaxFlowers];
            nutrient = new int[kMaxFlowers]; beneficialInsect = new int[kMaxFlowers];
            beneficialInsect = new int[kMaxFlowers]; harmfulInsect = new int[kMaxFlowers];
            harmfulInsect = new int[kMaxFlowers]; }
        }
 /// <summary>
        /// <summary> /// 初始化第一代花朵的基因结构
        /// 初始化第一代花朵的基因结构 /// </summary>
        /// </summary> public void Init()
        public void Init() {
        {
 for (int i = 1; i < kMaxFlowers; i++)
            for (int i = 1; i < kMaxFlowers; i++) {
            { temperature[i] = Rnd.Next(1, 75);
                temperature[i] = Rnd.Next(1, 75);
 water[i] = Rnd.Next(1, 75);
                water[i] = Rnd.Next(1, 75);
 sunlight[i] = Rnd.Next(1, 75);
                sunlight[i] = Rnd.Next(1, 75);
 nutrient[i] = Rnd.Next(1, 75);
                nutrient[i] = Rnd.Next(1, 75);
 beneficialInsect[i] = Rnd.Next(1, 75);
                beneficialInsect[i] = Rnd.Next(1, 75);
 harmfulInsect[i] = Rnd.Next(1, 75);
                harmfulInsect[i] = Rnd.Next(1, 75); }
            }
 currentTemperature = Rnd.Next(1, 75);
            currentTemperature = Rnd.Next(1, 75);
 currentWater = Rnd.Next(1, 75);
            currentWater = Rnd.Next(1, 75);
 currentSunlight = Rnd.Next(1, 75);
            currentSunlight = Rnd.Next(1, 75);
 currentNutrient = Rnd.Next(1, 75);
            currentNutrient = Rnd.Next(1, 75);
 currentBeneficialInsect = Rnd.Next(1, 75);
            currentBeneficialInsect = Rnd.Next(1, 75);
 currentHarmfulInsect = Rnd.Next(1, 75);
            currentHarmfulInsect = Rnd.Next(1, 75);
 }
        }
 /// <summary>
        /// <summary> /// 越大说明花朵的适应环境的能力差,小说明适应环境的能力强
        /// 越大说明花朵的适应环境的能力差,小说明适应环境的能力强 /// </summary>
        /// </summary> /// <param name="flower"></param>
        /// <param name="flower"></param> /// <returns></returns>
        /// <returns></returns> private int Fitness(int flower)
        private int Fitness(int flower) {
        { int theFitness = 0;
            int theFitness = 0;
 theFitness = Math.Abs(temperature[flower] - currentTemperature);
            theFitness = Math.Abs(temperature[flower] - currentTemperature);
 theFitness = theFitness + Math.Abs(water[flower] - currentWater);
            theFitness = theFitness + Math.Abs(water[flower] - currentWater);
 theFitness = theFitness + Math.Abs(sunlight[flower] -
            theFitness = theFitness + Math.Abs(sunlight[flower] -
 currentSunlight);
                                                  currentSunlight);
 theFitness = theFitness + Math.Abs(nutrient[flower] -
            theFitness = theFitness + Math.Abs(nutrient[flower] -
 currentNutrient);
                                                  currentNutrient);
 theFitness = theFitness + Math.Abs(beneficialInsect[flower] -
            theFitness = theFitness + Math.Abs(beneficialInsect[flower] -
 currentBeneficialInsect);
                                                  currentBeneficialInsect);
 theFitness = theFitness + Math.Abs(harmfulInsect[flower] -
            theFitness = theFitness + Math.Abs(harmfulInsect[flower] -
 currentHarmfulInsect);
                                                  currentHarmfulInsect);
 return (theFitness);
            return (theFitness); }
        }
 /// <summary>
        /// <summary> /// 排除适应能力差的花朵,让适应能力强的花朵杂交繁殖,产生下一代。同时有一定的概率变异。
        /// 排除适应能力差的花朵,让适应能力强的花朵杂交繁殖,产生下一代。同时有一定的概率变异。 /// </summary>
        /// </summary> public void Evolve()
        public void Evolve() {
        { int[] fitTemperature = new int[kMaxFlowers];
            int[] fitTemperature = new int[kMaxFlowers];
 int[] fitWater = new int[kMaxFlowers];
            int[] fitWater = new int[kMaxFlowers];
 int[] fitSunlight = new int[kMaxFlowers];
            int[] fitSunlight = new int[kMaxFlowers];
 int[] fitNutrient = new int[kMaxFlowers];
            int[] fitNutrient = new int[kMaxFlowers];
 int[] fitBeneficialInsect = new int[kMaxFlowers];
            int[] fitBeneficialInsect = new int[kMaxFlowers];
 int[] fitHarmfulInsect = new int[kMaxFlowers];
            int[] fitHarmfulInsect = new int[kMaxFlowers];
 int[] fitness = new int[kMaxFlowers];
            int[] fitness = new int[kMaxFlowers];
 int i;
            int i;
 int leastFit = 0;
            int leastFit = 0;
 int leastFitIndex = 1;
            int leastFitIndex = 1;
 for (i = 1; i < kMaxFlowers; i++)
            for (i = 1; i < kMaxFlowers; i++)
 if (Fitness(i) > leastFit)
                if (Fitness(i) > leastFit) {
                {
 leastFit = Fitness(i);
                    leastFit = Fitness(i);
 leastFitIndex = i;
                    leastFitIndex = i;
 }
                }
 temperature[leastFitIndex] = temperature[Rnd.Next(1, 10)];
            temperature[leastFitIndex] = temperature[Rnd.Next(1, 10)];
 water[leastFitIndex] = water[Rnd.Next(1, 10)];
            water[leastFitIndex] = water[Rnd.Next(1, 10)];
 sunlight[leastFitIndex] = sunlight[Rnd.Next(1, 10)];
            sunlight[leastFitIndex] = sunlight[Rnd.Next(1, 10)];
 nutrient[leastFitIndex] = nutrient[Rnd.Next(1, 10)];
            nutrient[leastFitIndex] = nutrient[Rnd.Next(1, 10)];
 beneficialInsect[leastFitIndex] = beneficialInsect[Rnd.Next(1, 10)];
            beneficialInsect[leastFitIndex] = beneficialInsect[Rnd.Next(1, 10)];
 harmfulInsect[leastFitIndex] = harmfulInsect[Rnd.Next(1, 10)];
            harmfulInsect[leastFitIndex] = harmfulInsect[Rnd.Next(1, 10)];
 for (i = 1; i < kMaxFlowers; i++)
            for (i = 1; i < kMaxFlowers; i++) {
            {
 fitTemperature[i] = temperature[Rnd.Next(1, 10)];
                fitTemperature[i] = temperature[Rnd.Next(1, 10)];
 fitWater[i] = water[Rnd.Next(1, 10)];
                fitWater[i] = water[Rnd.Next(1, 10)];
 fitSunlight[i] = sunlight[Rnd.Next(1, 10)];
                fitSunlight[i] = sunlight[Rnd.Next(1, 10)];
 fitNutrient[i] = nutrient[Rnd.Next(1, 10)];
                fitNutrient[i] = nutrient[Rnd.Next(1, 10)];
 fitBeneficialInsect[i] = beneficialInsect[Rnd.Next(1, 10)];
                fitBeneficialInsect[i] = beneficialInsect[Rnd.Next(1, 10)];
 fitHarmfulInsect[i] = harmfulInsect[Rnd.Next(1, 10)];
                fitHarmfulInsect[i] = harmfulInsect[Rnd.Next(1, 10)];
 }
            }
 for (i = 1; i < kMaxFlowers; i++)
            for (i = 1; i < kMaxFlowers; i++) {
            {
 temperature[i] = fitTemperature[i];
                temperature[i] = fitTemperature[i];
 water[i] = fitWater[i];
                water[i] = fitWater[i];
 sunlight[i] = fitSunlight[i];
                sunlight[i] = fitSunlight[i];
 nutrient[i] = fitNutrient[i];
                nutrient[i] = fitNutrient[i];
 beneficialInsect[i] = fitBeneficialInsect[i];
                beneficialInsect[i] = fitBeneficialInsect[i];
 harmfulInsect[i] = fitHarmfulInsect[i];
                harmfulInsect[i] = fitHarmfulInsect[i];
 }
            }
 for (i = 1; i < kMaxFlowers; i++)
            for (i = 1; i < kMaxFlowers; i++) {
            {
 if (Rnd.Next(1, 100) == 1)
                if (Rnd.Next(1, 100) == 1)
 temperature[i] = Rnd.Next(1, 75);
                    temperature[i] = Rnd.Next(1, 75);
 if (Rnd.Next(1, 100) == 1)
                if (Rnd.Next(1, 100) == 1)
 water[i] = Rnd.Next(1, 75);
                    water[i] = Rnd.Next(1, 75);
 if (Rnd.Next(1, 100) == 1)
                if (Rnd.Next(1, 100) == 1)
 sunlight[i] = Rnd.Next(1, 75);
                    sunlight[i] = Rnd.Next(1, 75);
 if (Rnd.Next(1, 100) == 1)
                if (Rnd.Next(1, 100) == 1)
 nutrient[i] = Rnd.Next(1, 75);
                    nutrient[i] = Rnd.Next(1, 75);
 if (Rnd.Next(1, 100) == 1)
                if (Rnd.Next(1, 100) == 1)
 beneficialInsect[i] = Rnd.Next(1, 75);
                    beneficialInsect[i] = Rnd.Next(1, 75);
 if (Rnd.Next(1, 100) == 1)
                if (Rnd.Next(1, 100) == 1)
 harmfulInsect[i] = Rnd.Next(1, 75);
                    harmfulInsect[i] = Rnd.Next(1, 75);
 }
            }
 }
        }
 /// <summary>
        /// <summary> /// 显示种群中个体对环境的适应能力,还有所有个体对环境的适应能力之和。
        /// 显示种群中个体对环境的适应能力,还有所有个体对环境的适应能力之和。 /// </summary>
        /// </summary> public void Show()
        public void Show() {
        { int sum = 0;
            int sum = 0; for (int i = 1; i < kMaxFlowers; i++)
            for (int i = 1; i < kMaxFlowers; i++) {
            { int fitness = Fitness(i);
                int fitness = Fitness(i); sum += fitness;
                sum += fitness; Console.WriteLine("No." + i + "'s fitness is " + fitness);
                Console.WriteLine("No." + i + "'s fitness is " + fitness); }
            }
 Console.WriteLine("fitness sum is " + sum);
            Console.WriteLine("fitness sum is " + sum); }
        } }
    } }
}
 
                    
                     
                    
                 
                    
                 
 
        

 
     
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号