?? tsp5view.cpp
字號:
// tsp5View.cpp : implementation of the CTsp5View class
//
#include "stdafx.h"
#include "tsp5.h"
#include "tsp5Doc.h"
#include "tsp5View.h"
#include <math.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CTsp5View
IMPLEMENT_DYNCREATE(CTsp5View, CView)
BEGIN_MESSAGE_MAP(CTsp5View, CView)
//{{AFX_MSG_MAP(CTsp5View)
ON_COMMAND(IDM_TSP, OnTsp)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTsp5View construction/destruction
CTsp5View::CTsp5View()
{
// TODO: add construction code here
}
CTsp5View::~CTsp5View()
{
}
BOOL CTsp5View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CTsp5View drawing
void CTsp5View::OnDraw(CDC* pDC)
{
CTsp5Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CTsp5View printing
BOOL CTsp5View::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CTsp5View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CTsp5View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CTsp5View diagnostics
#ifdef _DEBUG
void CTsp5View::AssertValid() const
{
CView::AssertValid();
}
void CTsp5View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CTsp5Doc* CTsp5View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTsp5Doc)));
return (CTsp5Doc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CTsp5View message handlers
void CTsp5View::OnTsp()
{
// TODO: Add your command handler code here
CClientDC dc(this);
dc.TextOut(10,10,"HOPFIELD應用----ranee");
dc.TextOut (10,30,"20074228068040");
int i;
double f1;
double Emin=1000;
int Imin=0;
//int Jmin=0;
double Dmin=1000;
fp=fopen("ranee.txt","w");
i=0;
f1=-0.07;
//預置一批S型激勵函數值,以留作后面使用
do
{
i++;
f1+=sub; //sub=0.00001;
v1[i]=G(f1);
// fprintf(fp,"v1[i]=",v1[i]);
} while((v1[i]<=0.999)&&(i<=13999));
scities(); //坐標 距離 初始閾值
for(i=1;i<=1500;i++)
{
fprintf(fp,"組號%d\n",i);
tm=0;
aa=i*10;
//產生神經元的初始狀態(tài)
sinit();
f=0;
do
{ //進行神經元狀態(tài)的迭代運算
energy();
//判斷前后兩次的能量函數值是否很接近,若很接近,則結束運算
if(fabs(e-f)<1e-20)
break;
//檢查旅行路線的合法性
if( check())
break;
f=e;
}while(tm<1000); //迭代超過1000次未達到穩(wěn)定狀態(tài),則求解失敗
//計算在最終穩(wěn)定狀態(tài)下合法旅行路徑的長度,并顯示所有神經元的狀態(tài)
display();
if( check())
{
if(e<Emin)
{
Emin=e;
Imin=i;
for(int u=0;u<N;u++)
{
for(int w=0;w<N;w++)
{
int y0=10*w;
if(v[y0+u]>0.99)
{
c[u][0]=xx[w];
c[u][1]=yy[w];
}
}
}
}
}
}
fprintf(fp,"最佳路徑組號為=%d 最小能量值=%f\n",Imin,Emin);
fprintf(fp,"到達城市順序:\n");
for(i=0;i<N;i++)
{
fprintf(fp,"x=%f y=%f",c[i][0], c[i][1]);
fprintf(fp,"\n");
}
draw(); //畫較優(yōu)路徑
//Draw();
fclose(fp);
}
void CTsp5View::energy()
{
int i1,i,j,x,y,x0,y0;
double z,k,z1;
e=0.0;k=0;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
k+=v[i*N+j]; //E3
//求能量函數值
e=0.0;
for(x=0;x<N;x++) //10行
{
x0=x*N;
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
if(i==j) continue;
e+=v[x0+i]*v[x0+j]; // E1每行兩兩相乘 之和
}
}
}
for(i=0;i<N;i++) //10列
for(x=0;x<N;x++)
{
x0=x* N;
for(y=0;y<N;y++)
{
if (x==y) continue;
e+=v[x0+i]*v[y*N+i]; //E2每列兩兩相乘 之和
}
}
for(x=0;x<N;x++)
{
x0=x*N;
for(y=0;y<N;y++)
{
if (y==x) continue;
y0=y*N;
for(i=0;i<N;i++)
{
if (i==0)
e+=v[x0]*dd[x0+y]*(v[y0+1]+v[y0+N-1]);
else
if(i==N-1)
e+=v[x0+i]*dd[x0+y]*(v[y0+N-2]+v[y0]);
else
e+=v[x0+i] * dd[x0+y]*(v[y0+i+1]+v[y0+i-1]); //E4
}
}
}
e+=(A*e+C*(k-N)*(k-N))/2.0; //能量函數E
//計算 duxi/dt
for(x=0;x<N;x++)
{
x0=x*N;
for(i=0;i<N;i++)
{
z=-C*(k-m);
for(j=0;j<N;j++)
{
if(i==j) continue;
z-=v[x0+j];
}
for(y=0;y<N;y++)
{
if(x==y) continue;
z-=v[y*N+i];
}
u[x0+i]+=h*z;
//再求神經元的狀態(tài)
z1=u[x0+i]* 100000.0+0.5;
i1=(int) z1 +7000;
if(i1>13907) v[x0+i]=v1[13907];
if(i1<=1) v[x0+i]=v1[1];
if(i1>1 && i1<=13907) v[x0+i]=v1[i1];
}
}
tm+=1;
}
int CTsp5View::check()
{
int i,j,x0;
double k;
/*neuron's State must access 0 or 1 */
for(i=0;i<NN;i++)
if((v[i]>0.01) && (v[i]<0.99))
return 0;
/*every row have and only have one 1 */
for(i=0;i<N;i++)
{
k=0.0;
x0 =i*N;
for(j=0;j<N;j++)
k+=v[x0+j]; //每行v的和
if((k-1.0)>0.1)
return 0;
}
/* every column have and only have one 1*/
for(i=0;i<N;i++) //10列
{
k=0.0;
for(j=0;j<N;j++)
k+=v[j*N+i]; //每列v的和
if((k -1.0)>0.1)
return 0;
}
return 1;
}
void CTsp5View::scities()
{
double r[N];
double s;
double w;
double oo;
//給出每個城市的坐標
xx[0]=0.4; yy[0]=0.4493;
xx[1]=0.2493;yy[1]=0.1463;
xx[2]=0.1707;yy[2]=0.2293;
xx[3]=0.2293;yy[3]=0.7610;
xx[4]=0.5171;yy[4]=0.9414;
xx[5]=0.8732;yy[5]=0.6536;
xx[6]=0.6878;yy[6]=0.5219;
xx[7]=0.8488;yy[7]=0.3609;
xx[8]=0.6683;yy[8]=0.2536;
xx[9]=0.6195;yy[9]=0.2643;
//計算城市之間的距離
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
{
if(i==j) continue;
dd[i*N+j]=hypot(xx[i]-xx[j],yy[i]-yy[j]);
}
//根據坐標先初始化下神經元的閾值
for(int q=0;q<N;q++)
{ s=(yy[q]-0.5)/(xx[q]-0.5);
r[q]=atan(s); //角度
oo=hypot(xx[q]-0.5,yy[q]-0.5); //到點(0.5,0.5)的距離
for(int p=0;p<N;p++)
{
w=r[q]+(p-1)*2*pi/(float)N;
l[q*N+p]=cos(w)*oo;
}
}
}
void CTsp5View::sinit()
{
int i,i1;
double u00=-u0*log(N-1)/2.0; //初值
srand((unsigned)time(NULL));
for(i=0;i<aa;i++)
{
t[0]=rand()/(float)32767;
}
//小于1的隨機數
for(i=aa;i<NN+aa;i++)
{
t[i-aa]=rand()/(float)32767;
}
//求神經元的初始狀態(tài)
for(i=0;i<NN;i++)
{
u[i]=u00+0.001*(t[i]*2-1)+0.002*l[i]; //u
i1=(int)(u[i]*100000.0+0.5)+7000;
if(i1 > 13907) v[i]=v1[13907];
if(i1<=1) v[i]=v1[1];
if(i1>1 && i1<=13907)
v[i]=v1[i1];
}
}
void CTsp5View::display()
{
int i, j, x0;
fprintf(fp,"迭代次數=%d 能量e=%f",tm, e);
if(check())
{
fprintf(fp, " 合法路徑\n");
//求最終的路徑長度
dis=0;
for (i=0;i<N;i++)
for (j=0;j<N;j++)
{
if (v[i*N+j]>0.98)
dis=dis+dd[i*N+j];
}
// fprintf(fp,"dis= %f \n",dis);
/* ovput the result of neuron satrix */
for(i=0;i<N;i++)
{
x0=i*N;
for(j=0;j<N;j++)
fprintf(fp,"%3.0f",v[x0+j],", ");
fprintf(fp,"\n");
}
fprintf(fp,"\n\n");
}
else
{
fprintf(fp, " 非法路徑\n\n");
}
}
void CTsp5View::draw()
{
CClientDC dc(this);
for(int i=0;i<N;i++)
{
double x[N],y[N];
c[i][0]=400+200*c[i][0];
c[i][1]=250-200*c[i][1];
x[i]=400+200*xx[i];
y[i]=250-200*yy[i];
dc.Ellipse(x[i]-3,y[i]-3, x[i]+3,y[i]+3);
}
dc.TextOut(480,250,"較優(yōu)路徑");
dc.MoveTo(c[0][0],c[0][1]);
for(int j=1;j<N;j++)
{
dc.LineTo(c[j][0],c[j][1]);
}
dc.LineTo(c[0][0] ,c[0][1]);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -