#include #include #define TFACTR 0.9 #define ALEN(a,b,c,d) sqrt(((b)-(a))*((b)-(a))+((d)-(c))*((d)-(c))) void anneal(float x[], float y[], int iorder[], int ncity) { int irbit1(unsigned long *iseed); int metrop(float de, float t); float ran3(long *idum); float revcst(float x[], float y[], int iorder[], int ncity, int n[]); void reverse(int iorder[], int ncity, int n[]); float trncst(float x[], float y[], int iorder[], int ncity, int n[]); void trnspt(int iorder[], int ncity, int n[]); int ans,nover,nlimit,i1,i2; int i,j,k,nsucc,nn,idec; static int n[7]; long idum; unsigned long iseed; float path,de,t; nover=100*ncity; nlimit=10*ncity; path=0.0; t=0.5; for (i=1;i= n[1]) ++n[2]; nn=1+((n[1]-n[2]+ncity-1) % ncity); } while (nn<3); idec=irbit1(&iseed); if (idec == 0) { n[3]=n[2]+(int) (abs(nn-2)*ran3(&idum))+1; n[3]=1+((n[3]-1) % ncity); de=trncst(x,y,iorder,ncity,n); ans=metrop(de,t); if (ans) { ++nsucc; path += de; trnspt(iorder,ncity,n); } } else { de=revcst(x,y,iorder,ncity,n); ans=metrop(de,t); if (ans) { ++nsucc; path += de; reverse(iorder,ncity,n); } } if (nsucc >= nlimit) break; } printf("\n %s %10.6f %s %12.6f \n","T =",t, " Path Length =",path); printf("Successful Moves: %6d\n",nsucc); t *= TFACTR; if (nsucc == 0) return; } } #undef TFACTR #undef ALEN