下面代码遗传算法中,是交叉操作的哪种方法呢(部分匹配交叉法,单点交叉,顺序交叉,循环交叉)
以下代码是交叉操作的哪种方法呢 ?是部分匹配交叉法,单点交叉,顺序交叉,循环交叉?还是其他的方法呢?void Cross(PTSP city,double pc)
{//交叉概率是pc
int crossNum,i;
int cityCross[2][CITY_NUM+1];
int tempXuh[2];
crossNum=int(POPSIZE*pc);
for(i=0;i<crossNum/2;i++)
{
tempXuh[0]=rand()%(POPSIZE-1);
do
{
tempXuh[1]=rand()%(POPSIZE-1);
}while(tempXuh[0]==tempXuh[1]);
copyCityXuhTo(city,tempXuh,cityCross);
crossTwo(city,tempXuh,cityCross);
}
void copyCityXuhTo(PTSP city,int *tempXuh,int cityCross[][CITY_NUM+1])
{
int i,h;
for(h=0;h<2;h++)
{
for(i=0;i<=CITY_NUM;i++)
{
cityCross[h][i]=city->colony[tempXuh[h]][i];
}
}
return;
void crossTwo(PTSP city,int *tempXuh,int cityCross[][CITY_NUM+1]) //交叉操作
{
int genePoint[2],temp1,countCrossGene,flaglike;
int samePoint=0;
int i,j,k,h;
int tempcityxuh[2][CITY_NUM+1];
do
{
genePoint[0]=rand()%(CITY_NUM-1);
genePoint[1]=rand()%(CITY_NUM-1)+1;
}while(genePoint[0]==genePoint[1]||genePoint[0]==0||genePoint[1]==0);
if(genePoint[0]>genePoint[1]) {temp1=genePoint[0];genePoint[0]=genePoint[1];genePoint[1]=temp1;}
countCrossGene=genePoint[1]-genePoint[0];
for(k=0;k<2;k++)//相同的部分屏蔽
{
for(i=genePoint[0]+1;i<=genePoint[1];i++)
{
for(j=1;j<CITY_NUM;j++)
{
if(!(j>genePoint[0]&&j<=genePoint[1]))
{
if(cityCross[k][j]==cityCross[(k+1)%2][i])
{
++samePoint;
cityCross[k][j]=CITY_NUM;//用CITYNUM代表H
}
}
}
}
}
samePoint=samePoint/2;
//for(h=0;h<2;h++)
//{
for(k=0;k<2;k++)//one
{
j=1;
for(i=genePoint[0]+1;i<=genePoint[1];i++)
{
tempcityxuh[k][j++]=cityCross[(k+1)%2][i];
cityCross[(k+1)%2][i]=0;
}
for(i=1;i<=CITY_NUM-1;i++)
{
if(cityCross[k][i]==CITY_NUM)
{
tempcityxuh[k][j++]=cityCross[k][i];
cityCross[k][i]=0;
}
}
for(i=1;i<=CITY_NUM-1;i++)
{
if(cityCross[k][i]!=0&&!(i>genePoint[0]&&i<=genePoint[1]))
{
tempcityxuh[k][j++]=cityCross[k][i];
}
}
}//for k
// }
if(samePoint!=0)//cross
{
for(h=0;h<2;h++)
{
k=countCrossGene;
for(i=1;i<=countCrossGene;i++)
{
flaglike=0;
for(j=1;j<=countCrossGene&&flaglike==0;j++)
{
if(tempcityxuh[h][i]==tempcityxuh[(h+1)%2][j])
{
flaglike=1;
}
}
if(flaglike==0) {tempcityxuh[(h+1)%2][++k]=tempcityxuh[h][i];}
}
}//forh
}
}