📄 cgroup.cs
字号:
using System;
/// <summary>
/// Group 的摘要说明。
/// </summary>
public class CGroup
{
// Construction
// Attributes
//public:
/* The Dfinition of Constant */
/* The Dfinition of Constant */
static int POPSIZE=501; //POPULATION SIZE
static int MAXIMIZATION=1; //maximization flag
static int MINIMIZATION=2; //minimization flag
static int SN=1024;
static int FunctionMode=MAXIMIZATION;
/* The Definition of User Data
(For different problem, there are some difference.) */
static int Cmax=100 ; //certain maximal value
static int Cmin=0; //certain minimum valuePOPSIZE 501 //POPULATION SIZE
//static double PI=3.1415926 ;
static double PI=Math.PI ;
/* The Definition of User Data
(For different problem, there are some difference.) */
public double[] gef=new double[SN],gex=new double[SN];
private System.Random random;
public int PopSize, Pointnum,i,j; //population size
public int MaxGeneration; //max. numberof generation
public double Pc; //probability of crossover
public double Pm; //probability of mutation
double averagevalue,fangcha;
public double outa,outb,outsita,outfai;
int generation; //number of generation
int best_index; //index of best individual
int worst_index; //index of worst individual
class individual //data structure of individual
{
public char[] chrom=new char[61];//A STRING OF CODE REPRESENTING INDIVIDUAL
public double value; //object value of this individual
public double fitness; //fitness value of this individual
public bool dirty;
};
individual bestindividual=new individual();//best indidual of current generation
individual worstindividual=new individual();//worst individual of current generation
individual currentbest=new individual(); //best individual by nowstruct individual population[POPSIZE];//population
individual[] population=new individual[POPSIZE];
int LENGTH1, LENGTH2, LENGTH3, LENGTH4, CHROMLENGTH;
public CGroup()
{
generation=0;
averagevalue=0.0;
Pointnum=1024;
PopSize=80;
MaxGeneration=200;
Pc=0.6;
Pm=0.01;
random=new System .Random ();
LENGTH1=(int)Math.Ceiling(Math.Log(Pointnum)/Math.Log(2)); //*the chromosome length of 1st variable
LENGTH2=(int)Math.Ceiling(Math.Log(Pointnum)/Math.Log(2)); //*the chromosome length of 2nd variable
LENGTH3=(int)Math.Ceiling(Math.Log(Pointnum)/Math.Log(2)); //*the chromosome length of 3nd variable
LENGTH4=(int)Math.Ceiling(Math.Log(Pointnum)/Math.Log(2)); //*the chromosome length of 4nd variable
CHROMLENGTH=LENGTH1+LENGTH2+LENGTH3+LENGTH4; //total length of chromosome
for(int i=0;i<CHROMLENGTH;i++)
bestindividual.chrom[i]='0';
bestindividual.chrom[CHROMLENGTH]='\0';
bestindividual.fitness=0.0;
bestindividual.value=0.0;
for(i=0;i<CHROMLENGTH;i++)
currentbest.chrom[i]='0';
currentbest.chrom[CHROMLENGTH]='\0';
currentbest.fitness=0.0;
currentbest.value=0.0;
for(i=0;i<Pointnum;i++)
gex[i]=i*1.0/Pointnum;
}
public void GenerateInitialPopulation()
{
int i,j;float iii;
generation=0;
averagevalue=0;
for(i=0;i<POPSIZE;i++)
population[i]=new individual();
for(i=0;i<CHROMLENGTH;i++)
bestindividual.chrom[i]='0';
bestindividual.chrom[CHROMLENGTH]='\0';
bestindividual.fitness=0.0;
bestindividual.value=0.0;
for(i=0;i<CHROMLENGTH;i++)
currentbest.chrom[i]='0';
currentbest.chrom[CHROMLENGTH]='\0';
currentbest.fitness=0.0;
currentbest.value=0.0;
for(i=0;i<PopSize;i++)
{
for(j=0;j<CHROMLENGTH;j++)
{
iii=(float)Rand(10.0);
population[i].chrom[j]=(iii<5.0)?'0':'1';
}
population[i].chrom[CHROMLENGTH]='\0';
population[i].dirty=true;
}
EvaluatePopulation();
}
/* Function:Initialize the first generation.
Variable:None. */
public void GenerateNextPopulation()
{
generation++;
SelectionOperator();
CrossoverOperator();
MutationOperator();
EvaluatePopulation();
PerformEvolution();
}
/* Function:Evaluate population accordingto certain formula.
Variable:None. */
void EvaluatePopulation()
{
CalculateObjectValue(); //calculate object value
CalculateFitnessValue(); //calculate finess value
Annealing();
FindBestAndWorstIndividual(); //find the best and worst individual
}
/* Function:To decode a binary chromosome into a decimal integer.
Variable:None.
Note:The returned value may be plus,or minus.
For fifferent coding method,this value may be changed into
"unsigned int". */
int DecodeChromosome(char[] String,int point,int length)
{
int i;
int Decimal=0;
char[] pointer=new char[20];
System.Array.Copy(String,point,pointer,0,length); //pointer=string+point;
for(i=1;i<length;i++)
pointer[i]=(pointer[i-1]==pointer[i])?'0':'1';
for(i=0;i<length;i++)
{
Decimal+=(pointer[i]-'0')*(int)Math.Pow(2,length-1-i);
}
return(Decimal);
}
public void DecodeBest()
{
int temp1,temp2,temp3,temp4;
temp1=DecodeChromosome(currentbest.chrom,0,LENGTH1);
temp2=DecodeChromosome(currentbest.chrom,LENGTH1,LENGTH2);
temp3=DecodeChromosome(currentbest.chrom,LENGTH1+LENGTH2,LENGTH3);
temp4=DecodeChromosome(currentbest.chrom,LENGTH1+LENGTH2+LENGTH3,LENGTH4);
if(temp1==0)temp2=0;
outa=temp1/20.0;
outb=temp2/(double)Pointnum;
outsita=temp3/20.0;
outfai=2*PI*temp4/Pointnum;
}
/* Function:To calculate object value.
Variable:None.
Note:For different problem, user must change these code.This
example is dealing with Rosenbrock function. Rosenbrock
function is defined as:f(x1,x2)=100*(x1**2-x2)**2+(1-x1)**2
Its maxmal value is:f(-2.048,-2.048)=3905.926227 */
void CalculateObjectValue()
{
int i;
int temp3,temp4,temp1,temp2;
double x1,x2,x3,x4;
//Rosenbrock function
for(i=0;i<PopSize;i++)
{
if(population[i].dirty)
{
temp1=DecodeChromosome(population[i].chrom,0,LENGTH1);
temp2=DecodeChromosome(population[i].chrom,LENGTH1,LENGTH2);
temp3=DecodeChromosome(population[i].chrom,LENGTH1+LENGTH2,LENGTH3);
temp4=DecodeChromosome(population[i].chrom,LENGTH1+LENGTH2+LENGTH3,LENGTH4);
if(temp1==0)
temp2=0;
x1=temp1/20.0;
x2=temp2/(double)Pointnum;
x3=temp3/20.0;
x4=2*PI*temp4/Pointnum;
CalPopulation(i,x1,x2,x3,x4);
}
}
}
void CalPopulation(int i,double x1,double x2,double x3,double x4)
{
double sum;
int jj;
double k;
if(true)
{
sum=0.0;
for(jj=0;jj<Pointnum;jj++)
{
sum=sum+Math.Pow(impulse(gex[jj],x1,x2,x3,x4),2.0);
}
k=1.0/Math.Sqrt(sum);
population[i].value=0.0;
for(jj=0;jj<Pointnum;jj++)
{
population[i].value+=gef[jj]*k*impulse(gex[jj],x1,x2,x3,x4);
}
}
else
{
sum=0.0;
for(j=0;j<Pointnum;++j)
{
sum += Math.Pow(gef[j] - impulse(gex[j],x1,x2,x3,x4),2);
}
k = 1.0 / ( Math.Sqrt(sum * Pointnum));
population[i].value=0.0;
for(jj=0;jj<128;jj++)
{
if(Math.Abs(gef[jj]) > Double.Epsilon)
population[i].value+=k*impulse(gex[jj],x1,x2,x3,x4)/gef[jj];
}
}
}
/* fUNCTION:TO CALCULATE FITNESS VALUE.
Variable:None. */
void CalculateFitnessValue()
{
int i;
double temp=0.0;
for(i=0;i<PopSize;i++)
{
if(population[i].dirty==true)
{
if(FunctionMode==MAXIMIZATION)
{ //maximization
temp=(Cmin+population[i].value)*(Cmin+population[i].value);
// temp=18000+population[i].value;
// ASSERT(temp>0);
}
else if(FunctionMode==MINIMIZATION)
{ //minimization
if(population[i].value<Cmax)
{
//AfxMessageBox("Error!!");
temp=Cmax-population[i].value;}
else
{
temp=0.0;
}
}
population[i].fitness=temp;
}
}
}
/* Function:To Find out the best individual so far current generation.
Variable:None. */
void FindBestAndWorstIndividual()
{
int i;
//find out the best and worst individual of this generation
getIndividual(bestindividual,population[0]);
getIndividual(worstindividual,population[0]);
best_index=worst_index=0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -