c语言编程交错排序,下面代码遗传算法中,是交叉操作的哪种方法呢(部分匹配交叉法,单点交叉,顺序交叉,循环交叉)...

已结贴√

问题点数:10 回复次数:8

ca56232b3bbedf9a539d07f37fffb99a.gif

3144d8b7615c79d9f638db40d5689d26.gif

a218af6549b45ee526caf607ebff1358.gif

0f8df0e29816ae721419de940fb833d1.gif

下面代码遗传算法中,是交叉操作的哪种方法呢(部分匹配交叉法,单点交叉,顺序交叉,循环交叉)

以下代码是交叉操作的哪种方法呢 ?是部分匹配交叉法,单点交叉,顺序交叉,循环交叉?还是其他的方法呢?

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

{

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

{

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

}

}

搜索更多相关主题的帖子:

Cross

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值