C#实现遗传算法,模拟花朵的进化。

 以下代码实现了一个简单的花朵进化的模拟过程。
花朵的种群数量是10,共进化了50代。

通过运行程序,你会发现通过不断的进化,种群的总的适应环境的能力在逐步提高(fitness的值下降)。

实现代码:

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(175);

                water[i] 
= Rnd.Next(175);

                sunlight[i] 
= Rnd.Next(175);

                nutrient[i] 
= Rnd.Next(175);

                beneficialInsect[i] 
= Rnd.Next(175);

                harmfulInsect[i] 
= Rnd.Next(175);
            }


            currentTemperature 
= Rnd.Next(175);

            currentWater 
= Rnd.Next(175);

            currentSunlight 
= Rnd.Next(175);

            currentNutrient 
= Rnd.Next(175);

            currentBeneficialInsect 
= Rnd.Next(175);

            currentHarmfulInsect 
= Rnd.Next(175);

        }


        
/// <summary>
        
/// 越大说明花朵的适应环境的能力差,小说明适应环境的能力强
        
/// </summary>
        
/// <param name="flower"></param>
        
/// <returns></returns>

        private int Fitness(int flower)
        
{
            
int theFitness = 0;

            theFitness 
= Math.Abs(temperature[flower] - currentTemperature);

            theFitness 
= theFitness + Math.Abs(water[flower] - currentWater);

            theFitness 
= theFitness + Math.Abs(sunlight[flower] -

                                                  currentSunlight);

            theFitness 
= theFitness + Math.Abs(nutrient[flower] -

                                                  currentNutrient);

            theFitness 
= theFitness + Math.Abs(beneficialInsect[flower] -

                                                  currentBeneficialInsect);

            theFitness 
= 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(110)];

            water[leastFitIndex] 
= water[Rnd.Next(110)];

            sunlight[leastFitIndex] 
= sunlight[Rnd.Next(110)];

            nutrient[leastFitIndex] 
= nutrient[Rnd.Next(110)];

            beneficialInsect[leastFitIndex] 
= beneficialInsect[Rnd.Next(110)];

            harmfulInsect[leastFitIndex] 
= harmfulInsect[Rnd.Next(110)];

            
for (i = 1; i < kMaxFlowers; i++)
            
{

                fitTemperature[i] 
= temperature[Rnd.Next(110)];

                fitWater[i] 
= water[Rnd.Next(110)];

                fitSunlight[i] 
= sunlight[Rnd.Next(110)];

                fitNutrient[i] 
= nutrient[Rnd.Next(110)];

                fitBeneficialInsect[i] 
= beneficialInsect[Rnd.Next(110)];

                fitHarmfulInsect[i] 
= harmfulInsect[Rnd.Next(110)];

            }


            
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(1100== 1)

                    temperature[i] 
= Rnd.Next(175);

                
if (Rnd.Next(1100== 1)

                    water[i] 
= Rnd.Next(175);

                
if (Rnd.Next(1100== 1)

                    sunlight[i] 
= Rnd.Next(175);

                
if (Rnd.Next(1100== 1)

                    nutrient[i] 
= Rnd.Next(175);

                
if (Rnd.Next(1100== 1)

                    beneficialInsect[i] 
= Rnd.Next(175);

                
if (Rnd.Next(1100== 1)

                    harmfulInsect[i] 
= Rnd.Next(175);

            }


        }


        
/// <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);
        }

    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值