kutta
10-07-2008, 06:31 AM
I shall be thankful to the Author, if the following annealing algorithm is properly dosed with appropriate medicines viz., make it compilable with out errors.
Thanking You
:)
#include <iostream>
#include "nr3.h"
#include "ran.h"
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <fstream.h>
#include<ctype.h>
#define Ranq1
#define myran
int main()
{
struct Anneal{
Anneal();
// Function declaration
void order(VecDoub_I x,VecDoub_I y,VecInt_IO iorder);
Doub revcst(VecDoub_I &x, VecDoub_I &y, VecInt_I &iorder, VecInt_IO &n);
void reverse(VecInt_IO &iorder, VecInt_I &n);
Doub trncst(VecDoub_I &x, VecDoub_I &y, VecInt_I &iorder, VecInt_IO &n);
void trnspt(VecInt_IO &iorder, VecInt_I &n);
bool metrop( double de , double t);
}
int k;
int iorder,n;
double x,y;
cout << "Enter the x " << x;
cin >> x;
cout << "Enter the y "<< y;
cin >> y;
cout << "Enter the n: "<< n;
cin >> n;
cout << "Enter the iorder: "<< iorder;
cin >> iorder;
void order(VecDoub_I x,VecDoub_I y,VecInt_IO iorder);
return 0;
}
void order(VecDoub_I &x,VecDoub_I &y,VecInt_IO &iorder)
{
const Doub TFACTR=0.9;
//step.
Bool ans;
Int i,i1,i2,nn;
VecInt n(6);
//0.5 is the initial temperature.
Doub de,path=0.0,t=0.5;
Int ncity=x.size();
//Maximum number of paths tried at any temperature.
Int nover=100*ncity;
Int nlimit=10*ncit;
// Maximum number of successful path changes bcontinuing.
for (i=0;i<ncity-1;i++) { //Calculate initial path length.
i1=iorder[i];
i2=iorder[i+1];
path += alen(x[i1],x[i2],y[i1],y[i2]);
}
i1=iorder[ncity-1];
i2=iorder[0];
path += alen(x[i1],x[i2],y[i1],y[i2]);
cout << fixed << setprecision(6);
for (Int j=0;j<100;j++) { //Try up to 100 temperature steps.
Int nsucc=0;
for (Int k=0;k<nover;k++) {
do {
// Choose beginning of segment
n[0]=Int(ncity*myran.doub());
//..and end of segment.
//..
n[1]=Int((ncity-1)*myran.doub());
if (n[1] >= n[0]) ++n[1];
//nn is the number of cities
nn=(n[0]-n[1]+ncity-1) % ncity;
// not on the segment.
} while (nn<2);
// Decide whether to do a segment reversal or transport:
// Do a transport.
if (myran.doub() < 0.5) {
n[2]=n[1]+Int(abs(nn-1)*myran.doub())+1;
// Transport tlocation not on the path.
n[2] %= ncity;
// Calculate cost.
de=trncst(x,y,iorder,n);
//Consult the oracle.
ans=metrop(de,t);
if (ans) {
++nsucc;
path += de;
// Carry out transport.
trnspt(iorder,n);
}
// Do a path reversal.
} else {
// Calculate cost.
de=revcst(x,y,iorder,n);
//Consult the oracle.
ans=metrop(de,t);
if (ans) {
++nsucc;
path += de;
//Carry out the reversal.
reverse(iorder,n);
}
}
//Finish early if we have enough suc-
if (nsucc >= nlimit) break;
//cessful changes.
}
cout << endl << "T = " << setw(12) << t;
cout << " Path Length = " << setw(12) << path << endl;
cout << "Successful Moves: " << nsucc << endl;
t *= TFACTR;
// If no success, we are done.
if (nsucc == 0)
return;
}
};
Doub revcst(VecDoub_I &x, VecDoub_I &y, VecInt_I &iorder, VecInt_IO &n)
{
VecDoub xx(4),yy(4);
Int ncity=x.size();
// Find the city before n[0] ..
n[2]=(n[0]+ncity-1) % ncity;
// .. and the city after n[1].
n[3]=(n[1]+1) % ncity;
for (Int j=0;j<4;j++) {
// Find coordinates for the four cities in-
Int ii=iorder[n[j]];
// volved.
xx[j]=x[ii];
yy[j]=y[ii];
}
// Calculate cost of disconnecting
Doub de = -alen(xx[0],xx[2],yy[0],yy[2]);
// the segment at both ends
de -= alen(xx[1],xx[3],yy[1],yy[3]);
// and reconnecting in the op-
de += alen(xx[0],xx[3],yy[0],yy[3]);
// posite order.
de += alen(xx[1],xx[2],yy[1],yy[2]);
return de;
}
void reverse(VecInt_IO &iorder, VecInt_I &n)
{
Int ncity=iorder.size();
Int nn=(1+((n[1]-n[0]+ncity) % ncity))/2; //This many cities must be swapped
//to e?ect the reversal.
for (Int j=0;j<nn;j++) {
//Start at the ends of the segment and
Int k=(n[0]+j) % ncity;
// swap pairs of cities, moving to-
Int l=(n[1]-j+ncity) % ncity;
// ward the center.
Int itmp=iorder[k];
iorder[k]=iorder[l];
iorder[l]=itmp;
}
}
Doub trncst(VecDoub_I &x, VecDoub_I &y, VecInt_I &iorder, VecInt_IO &n)
{
VecDoub xx(6),yy(6);
Int ncity=x.size();
// .. Find the city following n[2]..
n[3]=(n[2]+1) % ncity;
// ..and the one preceding n[0]..
n[4]=(n[0]+ncity-1) % ncity;
// ..and the one following n[1].
n[5]=(n[1]+1) % ncity;
for (Int j=0;j<6;j++) {
// Determine coordinates for the six cities
Int ii=iorder[n[j]];
// involved.
xx[j]=x[ii];
yy[j]=y[ii];
}
Doub de = -alen(xx[1],xx[5],yy[1],yy[5]);
// Calculate the cost of disconnecting
// the path segment from n[0] to
de -= alen(xx[0],xx[4],yy[0],yy[4]);
// n[1], opening a space between
de -= alen(xx[2],xx[3],yy[2],yy[3]);
// n[2] and n[3], connecting the
de += alen(xx[0],xx[2],yy[0],yy[2]);
//segment in the space, and con-
de += alen(xx[1],xx[3],yy[1],yy[3]);
// necting n[4] to n[5].
de += alen(xx[4],xx[5],yy[4],yy[5]);
return de;
}
void trnspt(VecInt_IO &iorder, VecInt_I &n)
{
Int ncity=iorder.size();
VecInt jorder(ncity);
// Find number of cities from n[0] to n[1]
Int m1=(n[1]-n[0]+ncity) % ncity;
//...and the number from n[3] to n[4]
Int m2=(n[4]-n[3]+ncity) % ncity;
//...and the number from n[5] to n[2].
Int m3=(n[2]-n[5]+ncity) % ncity;
Int nn=0;
for (Int j=0;j<=m1;j++) {
//Copy the chosen segment.
Int jj=(j+n[0]) % ncity;
jorder[nn++]=iorder[jj];
}
// Then copy the segment from n[3] to
for (Int j=0;j<=m2;j++) {
Int jj=(j+n[3]) % ncity; // n[4].
jorder[nn++]=iorder[jj];
}
// Finally, the segment from n[5] to n[2].
for (Int j=0;j<=m3;j++) {
Int jj=(j+n[5]) % ncity;
jorder[nn++]=iorder[jj];
}
// Copy jorder back into iorder.
for (Int j=0;j<ncity;j++)
iorder[j]=jorder[j];
}
bool metrop( Doub de , Doub t)
{
return de < 0.0 || ( myran.doub() < exp(-de/t));
}
[/CODE]
Thanking You
:)
#include <iostream>
#include "nr3.h"
#include "ran.h"
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <fstream.h>
#include<ctype.h>
#define Ranq1
#define myran
int main()
{
struct Anneal{
Anneal();
// Function declaration
void order(VecDoub_I x,VecDoub_I y,VecInt_IO iorder);
Doub revcst(VecDoub_I &x, VecDoub_I &y, VecInt_I &iorder, VecInt_IO &n);
void reverse(VecInt_IO &iorder, VecInt_I &n);
Doub trncst(VecDoub_I &x, VecDoub_I &y, VecInt_I &iorder, VecInt_IO &n);
void trnspt(VecInt_IO &iorder, VecInt_I &n);
bool metrop( double de , double t);
}
int k;
int iorder,n;
double x,y;
cout << "Enter the x " << x;
cin >> x;
cout << "Enter the y "<< y;
cin >> y;
cout << "Enter the n: "<< n;
cin >> n;
cout << "Enter the iorder: "<< iorder;
cin >> iorder;
void order(VecDoub_I x,VecDoub_I y,VecInt_IO iorder);
return 0;
}
void order(VecDoub_I &x,VecDoub_I &y,VecInt_IO &iorder)
{
const Doub TFACTR=0.9;
//step.
Bool ans;
Int i,i1,i2,nn;
VecInt n(6);
//0.5 is the initial temperature.
Doub de,path=0.0,t=0.5;
Int ncity=x.size();
//Maximum number of paths tried at any temperature.
Int nover=100*ncity;
Int nlimit=10*ncit;
// Maximum number of successful path changes bcontinuing.
for (i=0;i<ncity-1;i++) { //Calculate initial path length.
i1=iorder[i];
i2=iorder[i+1];
path += alen(x[i1],x[i2],y[i1],y[i2]);
}
i1=iorder[ncity-1];
i2=iorder[0];
path += alen(x[i1],x[i2],y[i1],y[i2]);
cout << fixed << setprecision(6);
for (Int j=0;j<100;j++) { //Try up to 100 temperature steps.
Int nsucc=0;
for (Int k=0;k<nover;k++) {
do {
// Choose beginning of segment
n[0]=Int(ncity*myran.doub());
//..and end of segment.
//..
n[1]=Int((ncity-1)*myran.doub());
if (n[1] >= n[0]) ++n[1];
//nn is the number of cities
nn=(n[0]-n[1]+ncity-1) % ncity;
// not on the segment.
} while (nn<2);
// Decide whether to do a segment reversal or transport:
// Do a transport.
if (myran.doub() < 0.5) {
n[2]=n[1]+Int(abs(nn-1)*myran.doub())+1;
// Transport tlocation not on the path.
n[2] %= ncity;
// Calculate cost.
de=trncst(x,y,iorder,n);
//Consult the oracle.
ans=metrop(de,t);
if (ans) {
++nsucc;
path += de;
// Carry out transport.
trnspt(iorder,n);
}
// Do a path reversal.
} else {
// Calculate cost.
de=revcst(x,y,iorder,n);
//Consult the oracle.
ans=metrop(de,t);
if (ans) {
++nsucc;
path += de;
//Carry out the reversal.
reverse(iorder,n);
}
}
//Finish early if we have enough suc-
if (nsucc >= nlimit) break;
//cessful changes.
}
cout << endl << "T = " << setw(12) << t;
cout << " Path Length = " << setw(12) << path << endl;
cout << "Successful Moves: " << nsucc << endl;
t *= TFACTR;
// If no success, we are done.
if (nsucc == 0)
return;
}
};
Doub revcst(VecDoub_I &x, VecDoub_I &y, VecInt_I &iorder, VecInt_IO &n)
{
VecDoub xx(4),yy(4);
Int ncity=x.size();
// Find the city before n[0] ..
n[2]=(n[0]+ncity-1) % ncity;
// .. and the city after n[1].
n[3]=(n[1]+1) % ncity;
for (Int j=0;j<4;j++) {
// Find coordinates for the four cities in-
Int ii=iorder[n[j]];
// volved.
xx[j]=x[ii];
yy[j]=y[ii];
}
// Calculate cost of disconnecting
Doub de = -alen(xx[0],xx[2],yy[0],yy[2]);
// the segment at both ends
de -= alen(xx[1],xx[3],yy[1],yy[3]);
// and reconnecting in the op-
de += alen(xx[0],xx[3],yy[0],yy[3]);
// posite order.
de += alen(xx[1],xx[2],yy[1],yy[2]);
return de;
}
void reverse(VecInt_IO &iorder, VecInt_I &n)
{
Int ncity=iorder.size();
Int nn=(1+((n[1]-n[0]+ncity) % ncity))/2; //This many cities must be swapped
//to e?ect the reversal.
for (Int j=0;j<nn;j++) {
//Start at the ends of the segment and
Int k=(n[0]+j) % ncity;
// swap pairs of cities, moving to-
Int l=(n[1]-j+ncity) % ncity;
// ward the center.
Int itmp=iorder[k];
iorder[k]=iorder[l];
iorder[l]=itmp;
}
}
Doub trncst(VecDoub_I &x, VecDoub_I &y, VecInt_I &iorder, VecInt_IO &n)
{
VecDoub xx(6),yy(6);
Int ncity=x.size();
// .. Find the city following n[2]..
n[3]=(n[2]+1) % ncity;
// ..and the one preceding n[0]..
n[4]=(n[0]+ncity-1) % ncity;
// ..and the one following n[1].
n[5]=(n[1]+1) % ncity;
for (Int j=0;j<6;j++) {
// Determine coordinates for the six cities
Int ii=iorder[n[j]];
// involved.
xx[j]=x[ii];
yy[j]=y[ii];
}
Doub de = -alen(xx[1],xx[5],yy[1],yy[5]);
// Calculate the cost of disconnecting
// the path segment from n[0] to
de -= alen(xx[0],xx[4],yy[0],yy[4]);
// n[1], opening a space between
de -= alen(xx[2],xx[3],yy[2],yy[3]);
// n[2] and n[3], connecting the
de += alen(xx[0],xx[2],yy[0],yy[2]);
//segment in the space, and con-
de += alen(xx[1],xx[3],yy[1],yy[3]);
// necting n[4] to n[5].
de += alen(xx[4],xx[5],yy[4],yy[5]);
return de;
}
void trnspt(VecInt_IO &iorder, VecInt_I &n)
{
Int ncity=iorder.size();
VecInt jorder(ncity);
// Find number of cities from n[0] to n[1]
Int m1=(n[1]-n[0]+ncity) % ncity;
//...and the number from n[3] to n[4]
Int m2=(n[4]-n[3]+ncity) % ncity;
//...and the number from n[5] to n[2].
Int m3=(n[2]-n[5]+ncity) % ncity;
Int nn=0;
for (Int j=0;j<=m1;j++) {
//Copy the chosen segment.
Int jj=(j+n[0]) % ncity;
jorder[nn++]=iorder[jj];
}
// Then copy the segment from n[3] to
for (Int j=0;j<=m2;j++) {
Int jj=(j+n[3]) % ncity; // n[4].
jorder[nn++]=iorder[jj];
}
// Finally, the segment from n[5] to n[2].
for (Int j=0;j<=m3;j++) {
Int jj=(j+n[5]) % ncity;
jorder[nn++]=iorder[jj];
}
// Copy jorder back into iorder.
for (Int j=0;j<ncity;j++)
iorder[j]=jorder[j];
}
bool metrop( Doub de , Doub t)
{
return de < 0.0 || ( myran.doub() < exp(-de/t));
}
[/CODE]