示例#1
0
int main(void)
{
	int i,iflg,ipts=0,j,n;
	unsigned long iseed=12345;
	float twoinv,delay[NBIN+1];

	/* Calculate distribution of runs of zeros */
	for (i=1;i<=NBIN;i++) delay[i]=0.0;
	printf("distribution of runs of n zeros\n");
	printf("%6s %22s %18s\n","n","probability","expected");
	for (i=1;i<=NTRIES;i++) {
		if (irbit1(&iseed) == 1) {
			++ipts;
			iflg=0;
			for (j=1;j<=NBIN;j++) {
				if ((irbit1(&iseed) == 1) && (iflg == 0)) {
					iflg=1;
					++delay[j];
				}
			}
		}
	}
	twoinv=0.5;
	for (n=1;n<=NBIN;n++) {
		printf("%6d %19.4f %20.4f\n",
			(n-1),delay[n]/ipts,twoinv);
		twoinv /= 2.0;
	}
	return 0;
}
示例#2
0
文件: anneal.c 项目: gnovak/bin
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<ncity;i++) {
		i1=iorder[i];
		i2=iorder[i+1];
		path += ALEN(x[i1],x[i2],y[i1],y[i2]);
	}
	i1=iorder[ncity];
	i2=iorder[1];
	path += ALEN(x[i1],x[i2],y[i1],y[i2]);
	idum = -1;
	iseed=111;
	for (j=1;j<=100;j++) {
		nsucc=0;
		for (k=1;k<=nover;k++) {
			do {
				n[1]=1+(int) (ncity*ran3(&idum));
				n[2]=1+(int) ((ncity-1)*ran3(&idum));
				if (n[2] >= 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;
	}
}