有错误不知怎么该,请哪位高手帮帮忙,很急,非常感谢
#include <stdio.h>#include<time.h>
#include <stdlib.h>
#include <iostream.h>
//part1
#define PopSize 500
#define N 21
double dist[14][4]=
{
{430,800,1180,1500},
{225,300,650,1105},
{560,238,320,822},
{870,475,158,500},
{1120,856,432,210},
{530,897,1225,1600},
{320,385,750,1080},
{950,564,263,570},
{1175,915,520,308},
{637,966,1189,1580},
{412,487,835,1120},
{720,402,513,885},
{998,650,347,603},
{1208,1000,615,400},
};//箱区到泊位距离
int capatity[]={40,50,45,35,30,78,100,150,110,90,118,140,172,136,103};// 各箱区容量
int inctn_num[]={42,311,101,229,129,37,208,198,130}; //各船进口箱量
//int PopSize=500;
int MaxGeneration=200;
double Pc=0.7; //交叉概率
double Pm=0.01; //变异概率
int gen; //目前的代数
//part2
struct individual //基因结构体
{
int chrom[N]; // 编码
double value; // 目标值
double fitness; // 适应度值
double funvalue; //目标值
};
typedef struct individual individual;
individual X[PopSize],Y[PopSize],bestindividual;
//part3
void GenerationInitialPopulation(void);//初始群体
void EvaluatePopulation(void); //评价
void CalculateObjectValue(void); //目标值
void CalculateFitnessValue(void);//适应度
void selection(void); //选择
void Crossover(void); //交叉
void Mutation(void); //变异
void FindBestandWorstIndividual(void);
void srand(unsigned int seed);//随机生成
//part4
void main(void)
{
gen=0;
GenerationInitialPopulation();//初始群体
EvaluatePopulation(); //评价
while (gen<=MaxGeneration)
{
int i;
EvaluatePopulation(); //评价
for(i=0;i<N;i++)
{
cout<<X[0].chrom ;
}
cout<<endl;
selection();
Crossover();
Mutation();
gen++;
}
}
//初始种群
void GenerateInitialPopulation(void)
{
creat(double c[21])
{
int i,j,d[15],m;
double x,y;
double a[21];
do
{
m=0;
for(j=0;j<15;j++)
{
d[j]=0;
}
for(i=0;i<21;i++)
{
a[i]=rand()%1000;
a[i]/=1000;
c[i]=15*a[i]+1;
for(j=0;j<15;j++)
{
if(c[i]==j+1)
{d[j]+=1;}
}
}
for(j=0;j<15;j++)
{
if(d[j]>3)
m=1;
}
}
while(m==1);
}
main()
{
int i,j,X.chrom[500][21];
srand((int)time(0));
for(i=0;i<500;i++)
{
creat(X.chrom[i]);
}
for(i=0;i<20;i++)
{
for(j=0;j<21;j++)
{printf(" %d",X.chrom[i][j]);
if(j==20)
printf("\n");
}
}
}
//评价
void EvaluatePopulation(void)
{
CalculateObjectvalue();
CalculateFitnessValue();
FindBestandWorstIndividual();
X[PopSize-1]=bestindividual;//把最优的保存下来
}
//目标值
void CalculateObjectValue(void)
{
int i=0,j=0;
for (i=0;i<=PopSize;i++)
{ int f=0,f1=0,f2=0,f3=0,f4=0,f5=0,f6=0,f7=0,f8=0,f9=0;
for (j=0;j<N;j++)
{
if(j=0)
f1=f1+dist[X.chrom[j]][0];
else if (1<=j<=4)
f2=f2+dist[x.chrom[j]][0];
else if (4<j<=6)
f3=f3+dist[x.chrom[j]][1];
else if (6<j<=9)
f4=f4+dist[X.chrom[j]][1];
else if (9<j<=11)
f5=f5+dist[X.chrom[j]][2];
else if (11<j<=12)
f6=f6+dist[X.chrom[j]][2];
else if (12<j<=15)
f7=f7+dist[X.chrom[j]][2];
else if (15<j<=18)
f8=f8+dist[X.chrom[j]][3];
else
f9=f9+dist[X.chrom[j]][3];
f=f+f1+f2+f3+f4+f5+f6+f7+f8+f9;
X.value=f;
}
}
}
//适应度
void CalculateFitnessValue()
{
int i=0,j=0;
individual temp;
for (i=0;i<PopSize;i++)
{
X.fitness=X.value;
}
for (i=0;i<PopSize-1;i++)
for (j=0;j<PopSize;j++)
{
if (X.fitness<X[j].fitness)
{
temp=X;
X=X[j];
X[j]=temp;
}
}
}
void selection(void)
{
int i,index;
double p,sum=0.0;
double cfitness[PopSize];
individual newX[PopSize];
for (i=0;i<PopSize;i++)
{
sum+=X.fitness;
}
for (i=0;i<PopSize;i++)
{
cfitness=X.fitness/sum;
}
for (i=0;i<PopSize;i++)
{
cfitness=cfitness[i-1]+cfitness;
}
for (i=0;i<PopSize;i++)
{
p=(rand()%1001)/1000.0;
index=0;
while (p>cfitness[index])
{
index++;
}
newX=X[index];
for (i=0;i<PopSize;i++)
{
X=newX;
}
}
void Crossover(void)
{
int i=0,j=0,k=0;
for (i=0;i<PopSize;i++)
{
int p=0,q=0;
do
{
p=rand()%PopSize;
q=rand()%PopSize;
}while(p==q)
int w=1+rand()%N;
for (j=0;j<w;j++)
{
Y.chrom[j]=X[p].chrom[j];
}
double r=(rand()%1001)/1000.0;
if (r<pc)
{
Y.chrom[j]=X[q].chrom[j];
}
else
{
Y.chrom[j]=X[p].chrom[j];
}
}
for (i=0;i<PopSize;i++)
X=Y;
}
void Mutation(void)
{
int i=0,j=0,k=0,q=0;
double p=0;
for (i=0;i<PopSize;i++)
{
for (j=0;j<N;j++)
{
p=(rand()%1001)/1000.0;
if (p<Pm)
{
if(X.chrom[j]==1)
{
X.chrom[j]=0;
}
else
{
X.chrom[j]=1;
}
}
}
}
}
void FindBestandWorstIndividual(void)
{
int i;
bestindividual=X[0];
for(i=1;i<PopSize;i++)
{
if (X.fitness>bestindividual.fitness)
{
bestindividual.Value=X.value;
//bestindex=i;
}
}
}