TSPCC++Word文件下载.docx
《TSPCC++Word文件下载.docx》由会员分享,可在线阅读,更多相关《TSPCC++Word文件下载.docx(21页珍藏版)》请在冰豆网上搜索。
pc,则选择vi作为一个父代。
将所选的父代两两组队,随机产生一个位置进行交叉,如:
81421386325734324221
6123568563185633211
交叉后为:
81421386325185633211
6123568563734324221
变异过程:
本文采用均匀多点变异。
类似交叉操作中选择父代的过程,在r<
pm的标准下选择多个染色体vi作为父代。
对每一个选择的父代,随机选择多个位置,使其在每位置按均匀变异(该变异点xk的取值范围为[ukmin,ukmax],产生一个[0,1]中随机数r,该点变异为x'
k=ukmin+r(ukmax-ukmin))操作。
如:
变异后:
814213106322734524121
反grefenstette编码:
交叉和变异都是在grefenstette编码之后进行的,为了循环操作和返回最终结果,必须逆grefenstette编码过程,将编码恢复到自然编码。
循环操作:
判断是否满足设定的带数xzome,否,则跳入适应度f的计算;
是,结束遗传操作,跳出。
一个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>
#define
maxpop
100
maxstring
100
struct
pp{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)
{printf("
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,"
fprintf(fp,"
oldpop[maxpp].chrom[k]);
fclose(fp);
farfree(dd);
farfree(p1);
farfree(oldpop);
farfree(newpop);
restorecrtmode();
/*%%%%%%%%%%%%%%%%*/
float
objfunc(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;