问题描述:
/* 选择算子 */
void Select(int start,int stop,struct individual *newpopulation)
{
int i,index;
double p,sum=0.0;
double cfitness[POPSIZE];
for(i=start;i<stop;i++)
{
sum+=population[i].fitness;
}
for(i=start;i<stop;i++)
{
cfitness[i]=population[i].fitness/sum;
}
for(i=start+1;i<stop;i++)
{
cfitness[i]=cfitness[i-1]+cfitness[i];
}
for(i=start;i<stop;i++)
{
p=rand()%1000/1000.0;
index=0;
while(p>cfitness[index])
{
index++;
}
newpopulation[i]=population[index];
}
for(i=start;i<stop;i++)
{
population[i]=newpopulation[i];
}
}
void SelectionOperator(void)
{
struct individual newpopulation[POPSIZE];
int start,stop;
start=0;
stop=(int)(PopSize/3);
Select(start,stop,newpopulation);
start=stop+1;
stop=(int)(2*PopSize/3);
Select(start,stop,newpopulation);
start=stop+1;
stop=PopSize;
Select(start,stop,newpopulation);
}
int random(int randmax)
{
return rand()%randmax;
}
/* 交叉算子 */
void CrossoverOperator(void)
{
int i,j;
int index[POPSIZE];
int point,temp;
double p;
int CityNo;
for(i=0;i<PopSize;i++)
{
index[i]=i;
}
for(i=0;i<PopSize;i++)
{
point=random(PopSize-i);
temp=index[i];
index[i]=index[point+i];
index[point+i]=temp;
}
for(i=0;i<PopSize-1;i+=2)
{
p=rand()%1000/1000.0;
if(p<Pc)
{
point=random(CHROMLENGTH-1)+1;
for(j=point;j<CHROMLENGTH;j++)
{
CityNo=population[index[i]].chrom[j];
population[index[i]].chrom[j]=population[index[i+1]].chrom[j];
population[index[i+1]].chrom[j]=CityNo;
}
}
}
}
/* 变异算子 */
void MutationOperator(void)
{
int i,j;
double p;
for(i=0;i<PopSize;i++)
{
for(j=1;j<CHROMLENGTH;j++) /* 染色体第一个元素为1,不能变异*/
{
p=rand()%1000/1000.0;
if(p<Pm)
{
population[i].chrom[j]=random(CITYCOUNT-j)+1;
}
}
}
}
帮忙说明一下这段程序用的什么交叉方法,变异方法,急用,谢谢
是c的,通过VC++运行的
问题解答:
不会是C语言吧