该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
//更新生成新种群,采用杰出者记录策略------------------------------
void nextgeneration(population *p)
{
int cur_best=find_the_best(p);
int cur_worst=find_the_worst(p);
if(p[cur_best].fit>pbest.fit)
{
for (int i = 0; i <20; i++)
pbest.x[i] = p[cur_best].x[i];
pbest.fit=p[cur_best].fit;
pbest.x1=p[cur_best].x1;
pbest.x2=p[cur_best].x2;
pbest.cfit=p[cur_best].cfit;
}
else
{
for (int i = 0; i <20; i++)
2/3页
p[cur_worst].x[i]=pbest.x[i];
p[cur_worst].fit=pbest.fit;
p[cur_worst].x1=pbest.x1;
p[cur_worst].x2=pbest.x2;
p[cur_worst].cfit=pbest.cfit;
}
}
//----------------------------------------------------------------
//显示函数--------------------------------------------------------
void print(population *p,int t)
{
printf("-------------------------------------------------------\n");
printf("第%d代\n",t);
int i,j;
for(i=0;i
{
for(j=0;j<20;j++)
cout<
cout<
cout<
cout<
}
printf("第%d代的最优个体为:\n",t);
cout<
cout<
printf("-------------------------------------------------------\n");
}
//-----------------------------------------------------------------
//-----------------------------------------------------------------
//主函数-----------------------------------------------------------
int main(void)
{
int t=0;
initial(p);
printf("initialed\n\n");
decode(p);
evaluatefit(p);
keep_the_best(p);
print(p,t);
while(t
{
keep_the_best(p);
select(p);
cross(p);
mutate(p);
decode(p);