为了给别人说明白遗传算法的程序而编写的一个小例子 转载请注明出处
Code
using System;
using System.Collections.Generic;
using System.Text;
// write by feathersky
// to compute y= 3 * sin(x*x) + 5 * x; but we don't know the 3,5 , leb a chromosome has two genes that are a and b ,use GP program to get the right a,b
namespace Gene
{
class IntelligentTestSystem
{
public
static int population=100; // 种群个数
static int maxNumber = 2; //每个个体(染色体)所用的基因个数
double[,] chromosome = new double[population, maxNumber]; //每个个体(染色体)的种群
double[] fitness = new double[population]; //适应度
int questionTypeNumber = 0;
int[] questionType = new int[maxNumber];
double[] sampleX = new double[10];
double[] sampleY = new double[10];
public void initial()
{
Random rand = new Random(unchecked(5 * (int)DateTime.Now.Ticks));
for (int i = 0; i < population; i++) //随机生成染色体
{
for (int j = 0; j < 2; j++)
{
chromosome[i, j] = (rand.Next(200) - 100 ) / 10.0;
}
}
for (int i = 0; i < 10; i++) //函数的10个坐标点 y=3*sin(x*x)+5*x;
{
sampleX[i] = i;
sampleY[i] = 3 * Math.Sin(sampleX[i] * sampleX[i]) + 5 * sampleX[i];
}
}
public double calculateSingleFitness(int n)
{
double[] y = new double[10];
double a = chromosome[n, 0];
double b = chromosome[n, 1];
double c = 0.0; //保存误差平方和
for (int i = 0; i < 10; i++)
{
y[i] = a * Math.Sin(sampleX[i] * sampleX[i]) + b * sampleX[i];
c = c + (sampleY[i] - y[i]) * (sampleY[i] - y[i]);
}
fitness[n] = -c ;
return fitness[n];
}
public void calculateFitness( )
{
for (int i = 0; i < population; i++) calculateSingleFitness(i);
}
public void sort()
{
double instead =0;
double ch0 = 0;
double ch1 = 0;
//冒泡排序
for(int j=0;j<population ;j++)
{
for (int i = population-1; i >0; i--)
{
if (fitness[i] > fitness[i - 1])
{
//交换邻近的两个
//交换适应度
instead = fitness[i - 1];
fitness[i - 1] = fitness[i];
fitness[i] = instead;
//交换染色体
ch0 = chromosome[i-1,0];
ch1 = chromosome[i-1, 1];
chromosome[i - 1, 0] = chromosome[i, 0];
chromosome[i - 1, 1] = chromosome[i, 1];
chromosome[i , 0] = ch0;
chromosome[i, 1] = ch1;
}
}
}
}
public void intercross(int father, int mother, int son, int daughter)
{
chromosome[son,0] = chromosome[father,0];
chromosome[son,1] = chromosome[mother,1];
chromosome[daughter, 0] = 0.5 * chromosome[father, 0] + 0.5 * chromosome[mother, 0];
chromosome[daughter, 1] = 0.5 * chromosome[father, 1] + 0.5 * chromosome[mother, 1];
}
public void aberrance(int father, int son)
{
Random rand = new Random(unchecked(5*(int)DateTime.Now.Ticks));
chromosome[son, 0] = chromosome[father, 0] + (rand.Next(200) - 100) / 100.0;
chromosome[son, 1] = chromosome[father, 1] + (rand.Next(200) - 100) / 100.0;
}
public void generate()
{
Random rand = new Random(unchecked(7 * (int)DateTime.Now.Ticks));
for (int k = 31; k < 80; k+=2)
{
int father = rand.Next(30);
int mother = rand.Next(30);
int son = k;
int daughter = k+1;
intercross(father, mother, son, daughter);
}
for (int k = 81; k < 100; k ++)
{
int father = rand.Next(30);
int son = k;
aberrance(father, son);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
// write by feathersky
// to compute y= 3 * sin(x*x) + 5 * x; but we don't know the 3,5 , leb a chromosome has two genes that are a and b ,use GP program to get the right a,b
namespace Gene
{
class IntelligentTestSystem
{
public
static int population=100; // 种群个数
static int maxNumber = 2; //每个个体(染色体)所用的基因个数
double[,] chromosome = new double[population, maxNumber]; //每个个体(染色体)的种群
double[] fitness = new double[population]; //适应度
int questionTypeNumber = 0;
int[] questionType = new int[maxNumber];
double[] sampleX = new double[10];
double[] sampleY = new double[10];
public void initial()
{
Random rand = new Random(unchecked(5 * (int)DateTime.Now.Ticks));
for (int i = 0; i < population; i++) //随机生成染色体
{
for (int j = 0; j < 2; j++)
{
chromosome[i, j] = (rand.Next(200) - 100 ) / 10.0;
}
}
for (int i = 0; i < 10; i++) //函数的10个坐标点 y=3*sin(x*x)+5*x;
{
sampleX[i] = i;
sampleY[i] = 3 * Math.Sin(sampleX[i] * sampleX[i]) + 5 * sampleX[i];
}
}
public double calculateSingleFitness(int n)
{
double[] y = new double[10];
double a = chromosome[n, 0];
double b = chromosome[n, 1];
double c = 0.0; //保存误差平方和
for (int i = 0; i < 10; i++)
{
y[i] = a * Math.Sin(sampleX[i] * sampleX[i]) + b * sampleX[i];
c = c + (sampleY[i] - y[i]) * (sampleY[i] - y[i]);
}
fitness[n] = -c ;
return fitness[n];
}
public void calculateFitness( )
{
for (int i = 0; i < population; i++) calculateSingleFitness(i);
}
public void sort()
{
double instead =0;
double ch0 = 0;
double ch1 = 0;
//冒泡排序
for(int j=0;j<population ;j++)
{
for (int i = population-1; i >0; i--)
{
if (fitness[i] > fitness[i - 1])
{
//交换邻近的两个
//交换适应度
instead = fitness[i - 1];
fitness[i - 1] = fitness[i];
fitness[i] = instead;
//交换染色体
ch0 = chromosome[i-1,0];
ch1 = chromosome[i-1, 1];
chromosome[i - 1, 0] = chromosome[i, 0];
chromosome[i - 1, 1] = chromosome[i, 1];
chromosome[i , 0] = ch0;
chromosome[i, 1] = ch1;
}
}
}
}
public void intercross(int father, int mother, int son, int daughter)
{
chromosome[son,0] = chromosome[father,0];
chromosome[son,1] = chromosome[mother,1];
chromosome[daughter, 0] = 0.5 * chromosome[father, 0] + 0.5 * chromosome[mother, 0];
chromosome[daughter, 1] = 0.5 * chromosome[father, 1] + 0.5 * chromosome[mother, 1];
}
public void aberrance(int father, int son)
{
Random rand = new Random(unchecked(5*(int)DateTime.Now.Ticks));
chromosome[son, 0] = chromosome[father, 0] + (rand.Next(200) - 100) / 100.0;
chromosome[son, 1] = chromosome[father, 1] + (rand.Next(200) - 100) / 100.0;
}
public void generate()
{
Random rand = new Random(unchecked(7 * (int)DateTime.Now.Ticks));
for (int k = 31; k < 80; k+=2)
{
int father = rand.Next(30);
int mother = rand.Next(30);
int son = k;
int daughter = k+1;
intercross(father, mother, son, daughter);
}
for (int k = 81; k < 100; k ++)
{
int father = rand.Next(30);
int son = k;
aberrance(father, son);
}
}
}
}