【文件属性】:
文件名称:C语言数值算法程序大全
文件大小:401KB
文件格式:RAR
更新时间:2013-04-24 14:08:31
c 程序
史无前例的全,很实用。看了就知道。
#include
#include
#include
#include
#include "nr.h"
using namespace std;
namespace {
inline DP alen(const DP a, const DP b, const DP c, const DP d)
{
return sqrt((b-a)*(b-a)+(d-c)*(d-c));
}
}
void NR::anneal(Vec_I_DP &x, Vec_I_DP &y, Vec_IO_INT &iorder)
{
const DP TFACTR=0.9;
bool ans;
int i,i1,i2,idec,idum,j,k,nn,nover,nlimit,nsucc;
static Vec_INT n(6);
unsigned long iseed;
DP path,de,t;
int ncity=x.size();
nover=100*ncity;
nlimit=10*ncity;
path=0.0;
t=0.5;
for (i=0;i= n[0]) ++n[1];
nn=(n[0]-n[1]+ncity-1) % ncity;
} while (nn<2);
idec=irbit1(iseed);
if (idec == 0) {
n[2]=n[1]+int(abs(nn-1)*ran3(idum))+1;
n[2] %= ncity;
de=trncst(x,y,iorder,n);
ans=metrop(de,t);
if (ans) {
++nsucc;
path += de;
trnspt(iorder,n);
}
} else {
de=revcst(x,y,iorder,n);
ans=metrop(de,t);
if (ans) {
++nsucc;
path += de;
reverse(iorder,n);
}
}
if (nsucc >= nlimit) break;
}
cout << endl << "T = " << setw(12) << t;
cout << " Path Length = " << setw(12) << path << endl;
cout << "Successful Moves: " << nsucc << endl;
t *= TFACTR;
if (nsucc == 0) return;
}
}