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

📄 毕业设计dlg.cpp

📁 神经元网络。可以进行矩阵神经网络计算。MFC写的
💻 CPP
字号:
// 毕业设计Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "毕业设计.h"
#include "毕业设计Dlg.h"

#include "DlgProxy.h"
#include <math.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
static double lastTime=COleDateTime::GetCurrentTime()*100000;
#define N 20//结点数目。
#define INP 2
#define OUP 2
#define T 100//时间帧长度。

double WWWW[N][N];//权矩阵
double WWWCL[N][N];//公共状态样本容器
double WWWCR[N][N];//公共状态样本容器

double bWWWW[N][N];//b权矩阵
double bWWWCL[N][N];//b公共状态样本容器
double bWWWCR[N][N];//b公共状态样本容器

double WWWP[N][1];//混合后的当前状态矩阵
double WWWJ[N][1];//自我激发的状态矩阵
double WWWB[N][1];//过去状态矩阵
double WWWF[N][1];//未来状态矩阵
CTime df;



double vinp[INP];//对内影响的值
int inps[INP];//对内影响的节点有哪些


double voup[OUP];//对外影响的值
int oups[OUP];//对外影响的节点有哪些







inline void swap(double &a,double &b){double c=a;a=b;b=c;};//交换大小

bool myTime(double t,double * lastTime1)//t为毫秒//全局计时器
{
    if(COleDateTime::GetCurrentTime()*100000-*lastTime1>=t)
	{
		*lastTime1=COleDateTime::GetCurrentTime()*100000;
         return true;
          //CMyDlg::MessageBox("fdf",NULL,MB_OK) ;
	}
     
     
    return false;
}

//方形矩阵相乘。
void mulMatrix(double a[N][N],double b[N][N],double c[N][N])
{
	int i,j,k=0,m=0,l=0;

	for(i=0;i<N;i++)
	{//	vinp[0]=0.99f; 2 3 4 6
	
	for(j=0;j<N;j++)
	{			
           
				
				for(k=0;k<N;k++)
			{
			
           c[j][i]= c[j][i]+a[k][i]*b[j][k];
				
				}	   		
	}

	}
}
//线矩阵乘方式可逆矩阵。
void mulMatrix2(double a[N][1],double b[N][N],double c[N][1])
{
	int j,k=0,m=0,l=0;

	
	
	for(j=0;j<N;j++)
	{			
           
				
				for(k=0;k<N;k++)
			{
			
           c[j][0]= c[j][0]+a[k][0]*b[j][k];
				
				}	   		
	}

	
}
//矩阵求逆
bool DinV(double A[N][N])
{
 int i,j,k;
 double d;
int n=N;
 int JS[N],IS[N];
 for (k=0;k<n;k++)
 {
  d=0;
  for (i=k;i<n;i++)
   for (j=k;j<n;j++)
  {
   if (fabs(A[i][j])>d)
   {
    d=fabs(A[i][j]);
    IS[k]=i;
    JS[k]=j;
   }
  }
   //-------------------------
  if (d+1.0==1.0) return false;
  //-----------------


  if (IS[k]!=k)
   for (j=0;j<n;j++)
    swap(A[k][j],A[IS[k]][j]);
   //-------------------------
  if (JS[k]!=k)
   for (i=0;i<n;i++)
    swap(A[i][k],A[i][JS[k]]);
   //---------------------------
  A[k][k]=1/A[k][k];
  for (j=0;j<n;j++)
   if (j!=k) A[k][j]=A[k][j]*A[k][k];
   //--------------------------
  for (i=0;i<n;i++)
   if (i!=k)
    for (j=0;j<n;j++)
     if (j!=k) A[i][j]=A[i][j]-A[i][k]*A[k][j];
	 //-------------
  for (i=0;i<n;i++)
   if (i!=k) A[i][k]=-A[i][k]*A[k][k];
 }
 //-------------
 for (k=n-1;k>=0;k--)
 {
  for (j=0;j<n;j++)
   if (JS[k]!=k) swap(A[k][j],A[JS[k]][j]);

  for (i=0;i<n;i++)
   if (IS[k]!=k) swap(A[i][k],A[i][IS[k]]);
 }

 return true;
}
/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog

IMPLEMENT_DYNAMIC(CMyDlg, CDialog);

CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMyDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_pAutoProxy = NULL;
}

CMyDlg::~CMyDlg()
{
	// If there is an automation proxy for this dialog, set
	//  its back pointer to this dialog to NULL, so it knows
	//  the dialog has been deleted.
	if (m_pAutoProxy != NULL)
		m_pAutoProxy->m_pDialog = NULL;
}

void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);


	//{{AFX_DATA_MAP(CMyDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
	//{{AFX_MSG_MAP(CMyDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	//}}AFX_MSG_MAP

	
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers

BOOL CMyDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	

//初始化随机种子。
	lastTime=lastTime-COleDateTime::GetCurrentTime();
	srand((int)lastTime);

  double pp; 
      	//     srand((UINT)GetCurrentTime()); 
           pp=rand()/(RAND_MAX+0.00); 

	//------初始化各存储节点。
CMyDlg::clearAll();

	//随机初始化状态容器左,保证容器左可力,并利用左容器矩阵与权矩阵计算出容器右。//随机初始化自激发值
CMyDlg::initaiWWWC();


   //进入帧。
SetTimer(0,T,NULL);
	return TRUE;  // return TRUE  unless you set the focus to a control
}
///-----------------multMatrix

///-----------------------------------
void CMyDlg::clearAll()
{
	int i,j;

  for(i=0;i<N;i++)
  {
   WWWJ[i][0]=0.0f;
   WWWP[i][0]=0.0f;
   WWWB[i][0]=0.0f;
   WWWF[i][0]=0.0f;
	for(j=0;j<N;j++)
	{
     WWWW[i][j]=0.0f;
     WWWCL[i][j]=0.0f;
     WWWCR[i][j]=0.0f;
	}

  }
for(i=0;i<INP;i++)
{
//inps[i]=0;
vinp[i]=0.0f;
}

for(i=0;i<OUP;i++)
{
//oups[i]=0;
voup[i]=0.0f;
}
//inps[INP]={0,1};//输入绑定的结点端口
inps[0]=0;
inps[1]=1;
//oups[OUP]={8,9};//输出绑定的结点端口
//oups[0]=8;
//oups[1]=9;
}
void CMyDlg::clearframe()
{
	int i,j=0;

  for(i=0;i<N;i++)
  {
 //  WWWJ[i][0]=0.0f;
   WWWP[i][0]=0.0f;
   WWWB[i][0]=0.0f;



  }
for(i=0;i<INP;i++)
{
//inps[i]=0;
vinp[i]=0.0f;
}

for(i=0;i<OUP;i++)
{
//oups[i]=0;
voup[i]=0.0f;
}

}
void CMyDlg::clearmid()
{
	int i,j;

  for(i=0;i<N;i++)
  {
   WWWJ[i][0]=0.0f;
   WWWP[i][0]=0.0f;
   WWWB[i][0]=0.0f;
 //  WWWF[i][0]=0.0f;
	for(j=0;j<N;j++)
	{
    // WWWW[i][j]=0.0f;
   //  WWWCL[i][j]=0.0f;
    // WWWCR[i][j]=0.0f;
	}

  }
for(i=0;i<INP;i++)
{
//inps[i]=0;
vinp[i]=0.0f;
}

for(i=0;i<OUP;i++)
{
//oups[i]=0;
voup[i]=0.0f;
}
//inps[INP]={0,1};//输入绑定的结点端口
//inps[0]=0;
//inps[1]=1;
//oups[OUP]={8,9};//输出绑定的结点端口
//oups[0]=8;
//oups[1]=9;
}
//----------------初始化权矩阵,左右容器样本,还有激发值。

void CMyDlg::initaiWWWC()
{
//	srand(time(NULL));


int i,j;
line3:
for(i=0;i<N;i++)
{
	for(j=0;j<N;j++)
	{

WWWCL[j][i]=rand()/(RAND_MAX+0.00);
WWWCR[j][i]=rand()/(RAND_MAX+0.00);
	}
}
//
if(DinV(WWWCL)!=true)//左是否是可逆矩阵,是直接返回到WWWCL
{goto line3;}
else
{
	//--求出WWWCL逆矩阵后
//	CMyDlg::MessageBox("FD",NULL,MB_OK);
 mulMatrix(WWWCR,WWWCL,WWWW);

}



	if(DinV(WWWW)!=true){goto line3;}
/*
//随机选出一个右样本作为激发值。
int pint;

line10:
CString sf;

pint=(int)(rand()*0.0003f*(double)(N/10.0f));
sf.Format("%d",pint); 
CMyDlg::MessageBox(sf,NULL,MB_OK);
if(pint>=0&&pint<N){;}else{goto line10;}
*/
for(i=0;i<N;i++)
{
WWWJ[i][0]=rand()/(RAND_MAX+0.00); 
}
DinV(WWWCL);//back
DinV(WWWW);//back


}
//---------------------进入帧运算

void CMyDlg::OnTimer(UINT nIDEvent)
{
int	open=0;
CString stro;
stro.Format ("%s","On Thinking");
CMyDlg::SetDlgItemText(IDC_STATIC,stro);
//--------------------------------------------------------------------

//判断如果存在输入值,就把该影响值与自激发状态值混合(以输入值覆盖自激发值)
int i,j,k=0,m=0;//不存在直接当前矩阵

//先把激发值传入混合模版。

	for(j=0;j<N;j++)
	{
      WWWP[j][0]=WWWJ[j][0];
	}


//把影响值也加到混合模版。
for(i=0;i<INP;i++)
{
	if(vinp[i]!=0.0f)
	{
		WWWP[inps[i]][0]=(double)vinp[i];
		open=1;
       //WWWJ[inps[INP]]=vinp[INP];
	}

}
//goto line7;
if(open!=1)
{goto line7;}
else
{
//利用激发值矩阵和公共矩阵求出过去状态矩阵值。(函数)
DinV(WWWW);//对矩阵求逆。
mulMatrix2(WWWJ,WWWW,WWWB);
DinV(WWWW);//反回
}
//goto line7;
//将过去状态矩阵与当前状态矩阵加入到,状态容器中,加入的原则是,一样的不加,不一样的加,
//并且是随机覆盖原则。
int count;
for(j=0;j<N;j++)
{
count=0;
for(i=0;i<N;i++)
{
	//看是否有一样的记录,一旦一个样本记录的数量,就认定为有一样的,就退出,更新矩阵,真接用旧矩阵输出新的WWWF。
	if(WWWB[i][0]==WWWCL[i][j]&&WWWP[i][0]==WWWCR[i][j])
	{count++;if(count==N-1){ goto line7;}}
}
}
//背份WWWCL和WWWCR以及 WWWW
for(i=0;i<N;i++)
{
	for(j=0;j<N;j++)
	{
bWWWW[j][i]=WWWW[j][i];
bWWWCL[j][i]=WWWCL[j][i];
bWWWCR[j][i]=WWWCR[j][i];

	}

}

//随机产生一个切入点。

int pint;

line10:
pint=(int)(rand()*0.0003f*(double)(N/10.0f));
if(pint>=0&&pint<N){;}else{goto line10;}

            //利用随机切入点保证,覆盖左右容器。
for(i=0;i<N;i++)
{

	WWWCL[i][pint]=WWWB[i][0];
	WWWCR[i][pint]=WWWP[i][0];

}
//DinV(WWWCL,WWWCL);
//mulMatrix(WWWCR,WWWCL,WWWW);
//goto line7;
//然后得到的新的状态容器,利用该容器,求这个新的权矩阵.(函数)
//判断WWWCL是否是可逆矩阵,可逆的话,就利用容器求出该权矩阵。
//不可逆就把,重新生成一个切入点。

       if(DinV(WWWCL)==false)
	   {	//用背份还原。
     	  for(i=0;i<N;i++)
		  {
	       for(j=0;j<N;j++)
		   {

          //WWWW[j][i]=bWWWW[j][i];
          WWWCL[j][i]=bWWWCL[j][i];
          WWWCR[j][i]=bWWWCR[j][i];

		   }
		  }
            goto line7;	
	   }
      else
	  {
		  for(i=0;i<N;i++)
		  {
		  	for(j=0;j<N;j++)
			{

		   WWWW[j][i]=0;
			
			}
		  }
			
             mulMatrix(WWWCR,WWWCL,WWWW);
	  }
			 if(DinV(WWWW)==false)//判断WWWW是否是一个可逆矩阵
			 {	
				 for(i=0;i<N;i++)
				 {
	           for(j=0;j<N;j++)
			   {

              WWWW[j][i]=bWWWW[j][i];
                WWWCL[j][i]=bWWWCL[j][i];
                   WWWCR[j][i]=bWWWCR[j][i];

			   }
				 }
                goto line7;	 
			 }
              else
				{
		  DinV(WWWW);//是可逆就回求,利用该矩阵来求,新的激发值。
				}


//利用新的权矩阵,求当前矩阵的一下状态,并释放空间。



line7:
//CMyDlg::clearmid();
	for(j=0;j<N;j++)
	{

		   WWWJ[j][0]=0;
			
	}
	


mulMatrix2(WWWP,WWWW,WWWJ);
//归元化一输出。
	for(j=0;j<N;j++)
	{

		   WWWJ[j][0]=1.0f/(exp(WWWJ[j][0])+1.0f);
			
	}
	




//整个过程在时间帧中运行。


CString str;
CString strs;
CString str1;
CString strs1;

for( i=0;i<N;i++)
{

	for( j=0;j<N;j++)
	{
str.Format("%f",WWWCL[j][i]);

strs=strs + " ("+ str +") ";

	}
	
}
CMyDlg::SetDlgItemText(IDC_EDIT1,strs);
//----------------------------
	for( j=0;j<N;j++)
	{

str1.Format("%f",WWWJ[j][0]);

strs1=strs1 + "  "+ str1;


	}
CMyDlg::SetDlgItemText(IDC_EDIT2,strs1);
//CMyDlg::clearframe();
//if(myTime(10,&lastTime)==true)
//{
//	CMyDlg::MessageBox("fd",NULL,MB_OK); 


//}
//----------------------------------------------------------------
CString stro2;



stro2.Format ("%s","Waiting to think!\n");
CMyDlg::SetDlgItemText(IDC_STATIC,stro2);
//-----------------------
CMyDlg::clearframe();

}







// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMyDlg::OnPaint() 
{
	


	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
				
	// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMyDlg::OnQueryDragIcon()
{

	return (HCURSOR) m_hIcon;
}

// Automation servers should not exit when a user closes the UI
//  if a controller still holds on to one of its objects.  These
//  message handlers make sure that if the proxy is still in use,
//  then the UI is hidden but the dialog remains around if it
//  is dismissed.

void CMyDlg::OnClose() 
{
	if (CanExit())
		CDialog::OnClose();
}

void CMyDlg::OnOK() 
{
	if (CanExit())
		CDialog::OnOK();
}

void CMyDlg::OnCancel() 
{
	if (CanExit())
		CDialog::OnCancel();
}

BOOL CMyDlg::CanExit()
{
	// If the proxy object is still around, then the automation
	//  controller is still holding on to this application.  Leave
	//  the dialog around, but hide its UI.
	if (m_pAutoProxy != NULL)
	{
		ShowWindow(SW_HIDE);
		return FALSE;
	}

	return TRUE;
}

void CMyDlg::OnButton1() 
{
	vinp[0]=0.99f;
	vinp[1]=0.001f;
	/*
	WWWCL[0][0]=2;
	WWWCL[1][0]=3;
	WWWCL[0][1]=3;
	WWWCL[1][1]=5;

	WWWW[0][0]=2;
	WWWW[1][0]=-1;
	WWWW[0][1]=-1;
	WWWW[1][1]=1;

	DinV(WWWW);
	DinV(WWWW);

CString str;
CString strs;

CString str1;
CString strs1;
int i,j;
for(i=0;i<N;i++)
{
	for( j=0;j<N;j++)
	{

str.Format("%f",WWWW[j][i]);

strs=strs + "  "+ str;


	}
}

CMyDlg::SetDlgItemText(IDC_EDIT1,strs);
*/
	/*
for( j=0;j<N;j++)
	{

str1.Format("%f",WWWL[j][0]);

strs1=strs1 + "  "+ str1;


	}

CMyDlg::SetDlgItemText(IDC_EDIT2,strs1);

*/

	
//	CMyDlg::KillTimer(0); 
//	lastTime=COleDateTime::GetCurrentTime()*100000;
//	CString strp;
//char msd;
//char *ms;	
//CMyDlg::GetDlgItemText(IDC_EDIT2,ms,1);
//strcpy(&msd,ms);
//strp.Format("%s",msd);
//CMyDlg::SetDlgItemText(IDC_EDIT1, msd);
	/*
	CString str1;
	str1.Format("%f",(double)rand()*0.00003f);
CMyDlg::SetDlgItemText(IDC_EDIT1,str1);)*/
//myTime(10,&lastTime);

//	str.Format("%f",lastTime);
//	CMyDlg::SetDlgItemText(IDC_EDIT1,str);

}

void CMyDlg::OnButton2() 
{
	vinp[0]=0.001f;
	vinp[1]=0.999f;
	// TODO: Add your control notification handler code here
	
}

void CMyDlg::OnButton3() 
{
vinp[0]=0.55f;
	vinp[1]=0.55f;	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -