C#遗传算法学习笔记

开发 后端 算法
本文介绍C#遗传算法学习笔记,通过运行程序,你会发现通过不断的进化,种群的总的适应环境的能力在逐步提高。

以下代码实现了C#遗传算法一个简单的花朵进化的模拟过程。

花朵的种群数量是10,共进化了50代。通过运行程序,你会发现通过不断的进化,种群的总的适应环境的能力在逐步提高(fitness的值下降)。

C#遗传算法实现代码:

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(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> 
/// 越大说明花朵的适应环境的能力差,小说明适应环境的能力强  
/// </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> 
/// 排除适应能力差的花朵,让适应能力强的花朵杂交繁殖,产生下一代。同时有一定的概率变异。  
/// </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> 
/// 显示种群中个体对环境的适应能力,还有所有个体对环境的适应能力之和。  
/// </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);  
}  
}  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.

以上是C#遗传算法学习笔记

【编辑推荐】

  1. C#生产者和消费者
  2. 详细介绍C#基础知识
  3. C#正则表达式学习笔记
  4. 简单描述C#存储过程
  5. 浅析C#基于TCP协议
责任编辑:佚名 来源: 博客园
相关推荐

2009-08-14 17:38:08

C#改写方法

2009-08-27 09:27:49

C#扩展方法

2009-08-31 16:51:11

C# Main()方法

2009-08-21 18:01:32

C#匿名方法

2009-08-12 17:32:44

C#反射方法

2021-03-10 15:49:20

人工智能遗传算法

2017-10-17 14:25:56

机器学习算法优化

2017-11-16 15:25:54

Go语言算法代码

2025-01-16 07:10:00

2017-05-10 15:41:29

机器学习算法数据

2009-10-14 09:27:30

VB.NET编码算法

2009-08-20 15:02:57

C# If语句

2009-08-12 15:50:40

C# ListBox

2009-08-12 09:28:36

C# WiteOne

2009-08-13 18:21:52

C#学习笔记

2024-07-03 08:00:00

2009-08-21 17:53:28

C#查询结果

2009-08-25 15:26:03

C#调用ImOK

2009-08-12 10:46:54

C#命名空间

2009-08-24 14:30:49

C# WMI封装
点赞
收藏

51CTO技术栈公众号