⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tsp5view.cpp

📁 Hopfiled 实现TSP旅行商问题 CH
💻 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;               
	
		 //产生神经元的初始状态
		 sinit();
 		 f=0;
         do
		 { //进行神经元状态的迭代运算
			 energy();
             //判断前后两次的能量函数值是否很接近,若很接近,则结束运算
			 if(fabs(e-f)<1e-20) 
				 break;
 			 //检查旅行路线的合法性
			 if( check())
				 break;
             f=e;
		 }while(tm<1000);  //迭代超过1000次未达到稳定状态,则求解失败
 		 //计算在最终稳定状态下合法旅行路径的长度,并显示所有神经元的状态
		 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();           //画较优路径
	  //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;
            //再求神经元的状态
			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;
	 }
	
	 //求神经元的初始状态
     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,"较优路径");
	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 + -