TSP问题的程序Word格式.docx

上传人:b****6 文档编号:20674227 上传时间:2023-01-25 格式:DOCX 页数:69 大小:29.39KB
下载 相关 举报
TSP问题的程序Word格式.docx_第1页
第1页 / 共69页
TSP问题的程序Word格式.docx_第2页
第2页 / 共69页
TSP问题的程序Word格式.docx_第3页
第3页 / 共69页
TSP问题的程序Word格式.docx_第4页
第4页 / 共69页
TSP问题的程序Word格式.docx_第5页
第5页 / 共69页
点击查看更多>>
下载资源
资源描述

TSP问题的程序Word格式.docx

《TSP问题的程序Word格式.docx》由会员分享,可在线阅读,更多相关《TSP问题的程序Word格式.docx(69页珍藏版)》请在冰豆网上搜索。

TSP问题的程序Word格式.docx

nn,:

[aa,bb]=size(FARM);

%交叉和变异

whileaa<

ifnn<

=2

nnper=randperm

(2);

else

nnper=randperm(nn);

A=FARM(nnper

(1),:

B=FARM(nnper

(2),:

[A,B]=intercross(A,B);

FARM=[FARM;

A;

B];

ifaa>

n,:

%保持种群规模为n

farm=FARM;

clearFARM

counter=counter+1

Rlength=myLength(D,R);

function[a,b]=intercross(a,b)

L=length(a);

ifL<

=10%确定交叉宽度

W=1;

elseif((L/10)-floor(L/10))>

=rand&

&

L>

10

W=ceil(L/10);

else

W=floor(L/10);

p=unidrnd(L-W+1);

%随机选择交叉范围,从p到p+W

W%交叉

x=find(a==b(1,p+i-1));

y=find(b==a(1,p+i-1));

[a(1,p+i-1),b(1,p+i-1)]=exchange(a(1,p+i-1),b(1,p+i-1));

[a(1,x),b(1,y)]=exchange(a(1,x),b(1,y));

function[x,y]=exchange(x,y)

temp=x;

x=y;

y=temp;

%计算路径的子程序

functionlen=myLength(D,p)

len=D(p(1,N),p(1,1));

(N-1)

len=len+D(p(1,i),p(1,i+1));

%计算归一化适应值子程序

functionfitness=fit(len,m,maxlen,minlen)

fitness=len;

length(len)

fitness(i,1)=(1-((len(i,1)-minlen)/(maxlen-minlen+0.000001))).^m;

end

一个C++的程序:

//c++的程序

#include<

iostream.h>

stdlib.h>

template<

classT>

classGraph

{

public:

Graph(intvertices=10)

n=vertices;

e=0;

}

~Graph(){}

virtualboolAdd(intu,intv,constT&

w)=0;

virtualboolDelete(intu,intv)=0;

virtualboolExist(intu,intv)const=0;

intVertices()const{returnn;

intEdges()const{returne;

protected:

intn;

inte;

};

classMGraph:

publicGraph<

T>

MGraph(intVertices=10,TnoEdge=0);

~MGraph();

boolAdd(intu,intv,constT&

w);

boolDelete(intu,intv);

boolExist(intu,intv)const;

voidFloyd(T**&

d,int**&

path);

voidprint(intVertices);

private:

TNoEdge;

T**a;

MGraph<

:

MGraph(intVertices,TnoEdge)

n=Vertices;

NoEdge=noEdge;

a=newT*[n];

for(inti=0;

i<

n;

i++){

a[i]=newT[n];

a[i][i]=0;

for(intj=0;

j<

j++)if(i!

=j)a[i][j]=NoEdge;

~MGraph()

i++)delete[]a[i];

delete[]a;

boolMGraph<

Exist(intu,intv)const

if(u<

0||v<

0||u>

n-1||v>

n-1||u==v||a[u][v]==NoEdge)returnfalse;

returntrue;

Add(intu,intv,constT&

w)

n-1||u==v||a[u][v]!

=NoEdge){

cerr<

<

"

BadInput!

endl;

returnfalse;

a[u][v]=w;

e++;

delete(intu,intv)

n-1||u==v||a[u][v]==NoEdge){

a[u][v]=NoEdge;

e--;

voidMGraph<

Floyd(T**&

path)

d=newT*[n];

path=newint*[n];

d[i]=newT[n];

path[i]=newint[n];

j++){

d[i][j]=a[i][j];

if(i!

=j&

a[i][j]<

NoEdge)path[i][j]=i;

elsepath[i][j]=-1;

for(intk=0;

k<

k++){

for(i=0;

i++)

j++)

if(d[i][k]+d[k][j]<

d[i][j]){

d[i][j]=d[i][k]+d[k][j];

path[i][j]=path[k][j];

print(intVertices)

Vertices;

cout<

'

'

;

if(j==Vertices-1)cout<

#definenoEdge10000

voidmain()

请输入该图的节点数:

intvertices;

cin>

>

vertices;

float>

b(vertices,noEdge);

请输入u,v,w:

intu,v;

floatw;

u>

v>

w;

while(w!

=noEdge){

//u=u-1;

b.Add(u-1,v-1,w);

b.Add(v-1,u-1,w);

b.print(vertices);

int**Path;

int**&

path=Path;

float**D;

float**&

d=D;

b.Floyd(d,path);

Path[i][j]<

if(j==vertices-1)cout<

int*V;

V=newint[vertices+1];

请输入任意一个初始H-圈:

for(intn=0;

n<

=vertices;

n++){

V[n];

for(n=0;

55;

n-1;

if(i+1>

0&

j>

i+1&

n-1){

if(D[V[i]][V[j]]+D[V[i+1]][V[j+1]]<

D[V[i]][V[i+1]]+D[V[j]][V[j+1]]){

intl;

l=V[i+1];

V[i+1]=V[j];

V[j]=l;

floattotal=0;

最小回路:

V[i]+1<

total+=D[V[i]][V[i+1]];

最短路径长度:

total;

}

C语言程序:

stdio.h>

math.h>

alloc.h>

conio.h>

float.h>

time.h>

graphics.h>

bios.h>

#definemaxpop100

#definemaxstring100

structpp{unsignedcharchrom[maxstring];

floatx,fitness;

unsignedintparent1,parent2,xsite;

structpp*oldpop,*newpop,*p1;

unsignedintpopsize,lchrom,gem,maxgen,co_min,jrand;

unsignedintnmutation,ncross,jcross,maxpp,minpp,maxxy;

floatpcross,pmutation,sumfitness,avg,max,min,seed,maxold,oldrand[maxstring];

unsignedcharx[maxstring],y[maxstring];

float*dd,ff,maxdd,refpd,fm[201];

FILE*fp,*fp1;

floatobjfunc(float);

voidstatistics();

intselect();

intflip(float);

intcrossover();

voidgeneration();

voidinitialize();

voidreport();

floatdecode();

voidcrtinit();

voidinversion();

floatrandom1();

voidrandomize1();

main()

{unsignedintgen,k,j,tt;

charfname[10];

floatttt;

clrscr();

co_min=0;

if((oldpop=(structpp*)farmalloc(maxpop*sizeof(structpp)))==NULL)

{printf("

memoryrequstfail!

\n"

exit(0);

if((dd=(float*)farmalloc(maxstring*maxstring*sizeof(float)))==NULL)

if((newpop=(structpp*)farmalloc(maxpop*sizeof(structpp)))==NULL)

if((p1=(structpp*)farmalloc(sizeof(structpp)))==NULL)

for(k=0;

maxpop;

k++)oldpop[k].chrom[0]='

\0'

k++)newpop[k].chrom[0]='

printf("

EnterResultDataFilename:

gets(fname);

if((fp=fopen(fname,"

w+"

))==NULL)

cannotopenfile\n"

gen=0;

randomize();

initialize();

fputs("

thisisresultoftheTSPproblem:

fp);

fprintf(fp,"

city:

%2dpsize:

%3dRef.TSP_path:

%f\n"

lchrom,popsize,refpd);

Pc:

%fPm:

%fSeed:

pcross,pmutation,seed);

Xsite:

lchrom;

k++)

{if((k%16)==0)fprintf(fp,"

%5d"

x[k]);

\nYsite:

y[k]);

crtinit();

statistics(oldpop);

report(gen,oldpop);

getch();

maxold=min;

fm[0]=100.0*oldpop[maxpp].x/ff;

do{

gen=gen+1;

generation();

if(max>

maxold)

{maxold=max;

fm[gen%200]=100.0*oldpop[maxpp].x/ff;

gotoxy(30,25);

ttt=clock()/18.2;

tt=ttt/60;

RunClock:

%2d:

%4.2f"

tt/60,tt%60,ttt-tt*60.0);

Min=%6.4fNm:

%d\n"

min,co_min);

}while((gen<

100)&

!

bioskey

(1));

\ngen=%d"

gen);

do{

if((gen%100)==0)report(gen,oldpop);

maxgen)&

{if((k%16)==0)fprintf(fp,"

oldpop[maxpp].chrom[k]);

fclose(fp);

farfree(dd);

farfree(p1);

farfree(oldpop);

farfree(newpop);

restorecrtmode();

/*%%%%%%%%%%%%%%%%*/

floatobjfunc(floatx1)

{floaty;

y=100.0*ff/x1;

returny;

/*&

*/

voidstatistics(pop)

structpp*pop;

{intj;

sumfitness=pop[0].fitness;

min=pop[0].fitness;

max=pop[0].fitness;

maxpp=0;

minpp=0;

for(j=1;

popsize;

{sumfitness=sumfitness+pop[j].fitness;

if(pop[j].fitness>

max)

{max=pop[j].fitness;

maxpp=j;

if(pop[j].fitness<

min)

{min=pop[j].fitness;

minpp=j;

avg=sumfitness/(float)popsize;

/*%%%%%%%%%%%%%%%%%%%%*/

voidgeneration()

{unsignedintk,j,j1,j2,i1,i2,mate1,mate2;

floatf1,f2;

j=0;

mate1=select();

pp:

mate2=select();

if(mate1==mate2)gotopp;

crossover(oldpop[mate1].chrom,oldpop[mate2].chrom,j);

newpop[j].x=(float)decode(newpop[j].chrom);

newpop[j].fitness=objfunc(newpop[j].x);

newpop[j].parent1=mate1;

newpop[j].parent2=mate2;

newpop[j].xsite=jcross;

newpop[j+1].x=(float)decode(newpop[j+1].chrom);

newpop[j+1].fitness=objfunc(newpop[j+1].x);

newpop[j+1].parent1=mate1;

newpop[j+1].parent2=mate2;

newpop[j+1].xsite=jcross;

if(newpop[j].fitness>

{for(k=0;

oldpop[minpp].chrom[k]=newpop[j].chrom[k];

oldpop[minpp].x=newpop[j].x;

oldpop[minpp].fitness=newpop[j].fitness;

co_min++;

return;

if(newpop[j+1].fitness>

oldpop[minpp].chrom[k]=newpop[j+1].chrom[k];

oldpop[minpp].x=newpop[j+1].x;

oldpop[minpp].fitness=newpop[j+1].fitness;

j=j+2;

}while(j<

popsize);

/*%%%%%%%%%%%%%%%%%*/

voidinitdata()

{unsignedintch,j;

-----------------------\n"

ASGA\n"

------------------------\n"

/*pause();

*/clrscr();

*******SGADATAENTRYANDINITILIZATION*******\n"

inputpopsize"

scanf("

%d"

&

inputch

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 研究生入学考试

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1