#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#define N 7 /* number of cities */
seven()
{
float u0,t,tao;
float v0,v1,v2,v3,v4,v5,v6,dv,delta,s,dist;
float u[8][8],vb[9][9],v[9][9],du[8][8];
float A,B,C,D,J,K;
int x,i,y,j,e;
int n,column[9]; /* cycling times */
float d[8][8]={0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0, /* distance */
0.0,0.0,1.3,1.25,14.0,14.0,14.0,14.0, /* between */
0.0,1.3,0.0,14.0,1.35,1.95,2.345,14.0,/* 7 cities */
0.0,1.25,14.0,0.0,1.3,2.725,1.95,14.0,
0.0,14.0,1.35,1.3,0.0,1.425,1.325,14.0,
0.0,14.0,1.95,2.725,1.425,0.0,14.0,1.45,
0.0,14.0,2.345,1.95,1.325,14.0,0.0,1.375,
0.0,14.0,14.0,14.0,14.0,1.45,1.375,0.0};
u0=0.02; /* step 1:initialization */
A=3.3,B=3.3,C=1.5,D=1.8; /* step 1: */
J=30.0,K=30.0; /* step 1: */
t=0.005;
tao=100.0;
u[0][0]=0.5*u0*log(N-1); /* step 1: */
n=1; /* step 1: */
printf("u[0][0]=%8.6f\n",u[0][0]); /* */
srand(time(NULL)); /* second as seed */
for(x=1;x<8;x++) /* initial input */
{
for(i=1;i<8;i++) /* values */
{ /* of 49 cells */
delta=(rand()%2000)/1000.0-1.0;
u[x][i]=u[0][0]+delta;
printf("u[%d][%d]=%6.4f ",x,i,u[x][i]);
}
printf("\n");
}
for(x=0;x<8;x++) /* step 02: output values */
for(i=0;i<8;i++) /* step 02: of cell [x][i]*/
vb[x][i]=0.0; /* step 02: at time t0 */
do
{
e=0;
for(x=1;x<8;x++) /* step 2: */
for(i=1;i<8;i++) /* step 2: */
{
v[x][i]=0.5*(1.0+tanh(u[x][i]/u0)); /* step 2:new output value */
dv=v[x][i]-vb[x][i]; /* step 2: variated value */
vb[x][i]=v[x][i];
if(fabs(dv)>0.001) /* step 3: one of criteria */
e=1; /* step 3: for end */
}
for(x=1;x<8;x++)
{
v[x][0]=v[x][7];
v[x][8]=v[x][1];
}
if(e==0) /* if all dv<0.001 */
break; /* stop */
else /* if any dv>0.001 */
n+=1; /* continue */
for(x=1;x<8;x++) /* step 3: */
for(i=1;i<8;i++) /* */
{
v1=0.0; /* start point */
v2=0.0;
v3=0.0;
v4=0.0;
v5=0.0;
v6=0.0;
v0=-u[x][i]/tao; /* step 3: ---- term 0 */
for(j=1;j<8;j++) /* step 3: total values of */
if(j!=i) /* step 3: cells in line x */
v1=v1+v[x][j];
v1=-A*v1; /* step 3: ---- term 1 */
for(y=1;y<8;y++) /* step 3: total values of */
if(y!=x) /* step 3: cells in column i */
v2=v2+v[y][i]; /* */
v2=-B*v2; /* step 3: ---- term 2 */
for(y=1;y<8;y++) /* step 3: total values */
for(j=1;j<8;j++) /* step 3: of all cells */
v3=v3+v[y][j]; /* */
v3=-C*(v3-N); /* step 3: ---- term 3 */
for(j=1;j<8;j++) /* step 3: possible distance */
v4=v4+d[x][j]*(v[j][i+1]+v[j][i-1]);/* step 3: between any 2 */
v4=-D*v4; /* step 3: ---- term 4 */
for(j=1;j<8;j++) /* two more restriction */
v5=v5+v[x][j]; /* conditions : v5 and v6 */
v5=-J*(v5-1.0);
for(y=1;y<8;y++)
v6=v6+v[y][i];
v6=-K*(v6-1.0);
du[x][i]=v0+v1+v2+v3+v4+v5+v6; /* step 3: value of du/dt */
u[x][i]=u[x][i]+du[x][i]*t; /* step 4: new input */
}
printf("\n");
}while(n<5000); /* the other criteria */
for(i=1;i<8;i++)
{
for(j=1;j<8;j++)
printf("u%d,%d=%7.5f ",i,j,u[i][j]); /* result: input values */
printf("\n");
}
printf("\n");
printf("times=%d\n",n); /* result: times */
for(x=1;x<8;x++) /* display the v matrix */
{ /* in float number */
for(y=1;y<8;y++)
printf("%7.4f ",v[x][y]);
printf("\n");
}
for(x=1;x<8;x++) /* dispaly the v matrix */
{ /* in integer number */
for(y=1;y<8;y++)
{
v[x][y]=floor(v[x][y]+0.38);
printf("%.0f ",v[x][y]);
}
printf("\n");
}
getch();
}
main()
{
seven();
}
- 1
- 2
- 3
前往页