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

📄 nndlg.cpp

📁 利用matlab编写的神经网络算法
💻 CPP
字号:
// NNDlg.cpp : implementation file
//

#include "stdafx.h"
#include "NN.h"
#include "NNDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNNDlg dialog

CNNDlg::CNNDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CNNDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CNNDlg)
	m_1 = 0.0;
	m_ss = _T("");
	m_testfile = _T("");
	m_num_sam = _T("");
	m_num_cha = _T("");
	m_num_hid = 20;
	m_num_out = _T("");
	m_right_ratio = 0.0;
	m_norm_filename = _T("");
	m_norm_loadfile = _T("");
	m_weight_file = _T("");
	m_weight2_file = _T("");
	m_alpha = 0.4;
	m_epoch = 1000;
	m_err = _T("");
	m_ratio = _T("");
	m_info = _T("");
	m_infofile = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CNNDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CNNDlg)
	DDX_Text(pDX, IDC_EDIT1, m_1);
	DDX_Text(pDX, IDC_EDIT2, m_ss);
	DDX_Text(pDX, IDC_EDIT_TESTFILE, m_testfile);
	DDX_Text(pDX, IDC_STATIC_NUM_SAM, m_num_sam);
	DDX_Text(pDX, IDC_STATIC_NUM_CHA, m_num_cha);
	DDX_Text(pDX, IDC_EDIT_NUM_HID, m_num_hid);
	DDX_Text(pDX, IDC_STATIC_NUM_OUT, m_num_out);
	DDX_Text(pDX, IDC_EDIT_RIGHT_RATIO, m_right_ratio);
	DDX_Text(pDX, IDC_EDIT_NORM_FILENAME, m_norm_filename);
	DDX_Text(pDX, IDC_EDIT_NORM_FILENAME2, m_norm_loadfile);
	DDX_Text(pDX, IDC_EDIT_WEIGHT_FILENAME, m_weight_file);
	DDX_Text(pDX, IDC_EDIT_WEIGHT_FILENAME2, m_weight2_file);
	DDX_Text(pDX, IDC_EDIT_ALPHA, m_alpha);
	DDX_Text(pDX, IDC_EDIT_EPOCH, m_epoch);
	DDX_Text(pDX, IDC_STATIC_ERR, m_err);
	DDX_Text(pDX, IDC_STATIC_RATIO, m_ratio);
	DDX_Text(pDX, IDC_STATIC_INFO, m_info);
	DDX_Text(pDX, IDC_EDIT_INFO, m_infofile);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CNNDlg, CDialog)
	//{{AFX_MSG_MAP(CNNDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON_TESTFILE, OnButtonTestfile)
	ON_BN_CLICKED(IDC_BUTTON_TEST, OnButtonTest)
	ON_BN_CLICKED(IDC_BUTTON_GETDATA, OnButtonGetdata)
	ON_BN_CLICKED(IDC_BUTTON_NORM, OnButtonNorm)
	ON_BN_CLICKED(IDC_BUTTON_LOAD_NORM, OnButtonLoadNorm)
	ON_BN_CLICKED(IDC_BUTTON_LOAD_NORM1, OnButtonLoadNorm1)
	ON_BN_CLICKED(IDC_BUTTON_WEIGHTFILE, OnButtonWeightfile)
	ON_BN_CLICKED(IDC_BUTTON_LOAD_WEIGHT, OnButtonLoadWeight)
	ON_BN_CLICKED(IDC_BUTTON_WEIGHTFILE2, OnButtonWeightfile2)
	ON_BN_CLICKED(IDC_BUTTON_INFO, OnButtonInfo)
	ON_BN_CLICKED(IDC_BUTTON_MIDDIS, OnButtonMiddis)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNNDlg message handlers

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

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 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
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CNNDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 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 CNNDlg::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 CNNDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CNNDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	if (m_ss=="")
	{
		AfxMessageBox("选择训练样本文件!");
		return;
	}
	mNN.Getdata(m_ss);
	mNN.number_hidden=m_num_hid;
	if (m_epoch<=0)
	{
		AfxMessageBox("训练步数不能小于0!");
		return;
	}
	mNN.epoch=m_epoch;
	mNN.alpha=m_alpha;
	mNN.normnofile();
	mNN.train();
	m_right_ratio=mNN.right_ratio;
	m_err="训练误差:";
	m_ratio="训练正确率:";
	m_1=mNN.err_sum;
  	UpdateData(FALSE);

}

void CNNDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	CFileDialog	FileDlg(TRUE,NULL,NULL,OFN_HIDEREADONLY, "All Files (*.*)|*.*||");
	if (FileDlg.DoModal()==IDCANCEL)	return;
	m_ss=FileDlg.GetPathName();
	UpdateData(FALSE);
	
}

void CNNDlg::OnButtonTestfile() 
{
	// TODO: Add your control notification handler code here
	CString ss;
	UpdateData(TRUE);
	CFileDialog	FileDlg(TRUE,NULL,NULL,OFN_HIDEREADONLY, "All Files (*.*)|*.*||");
	if (FileDlg.DoModal()==IDCANCEL)	return;
	m_testfile=FileDlg.GetPathName();
	test_filename=FileDlg.GetFileName();
 	UpdateData(FALSE);
}

void CNNDlg::OnButtonTest() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	if (m_testfile=="")
	{
		AfxMessageBox("选择测试样本文件!");
		return;
	}
	if (m_infofile=="")
	{
		AfxMessageBox("选择故障信息库!");
		return;
	}
	mNN.Getdata(m_ss);
	int sample_num_test,character_num_test,num_NN;
	character_num_test=mNN.character_num;
	num_NN=mNN.sample_num;//
	mNN.sample_num_temp=1;
	mNN.Getdata(m_testfile);

	if (character_num_test!=mNN.character_num)
	{
		
		AfxMessageBox("故障信息库与测试数据文件不匹配!");
		return;
	}

	mNN.normnofile();
	mNN.number_hidden=m_num_hid;
	CString weight1,weight2,filew1,filew2;
	m_info.Empty();
	int p=1;
	mNN.number_hidden=m_num_hid;
	BOOL term=TRUE;
	filew1=m_testfile;
	filew2=m_testfile;
    for(int i=0;i<mNN.sample_num;i++)
	{
		while (term)
		{

		    for (int j=0;j<mNN.character_num;j++)
			{
			    mNN.data_norm_temp[0][j]=mNN.data_norm[i][j];
			}
			weight1.Format("file_w1_%d.txt",p);
		    weight2.Format("file_w2_%d.txt",p);
//			filew1.Replace(test_filename,weight1);
//			filew2.Replace(test_filename,weight2);
//			AfxMessageBox(filew1);
		    mNN.LoadWeight(weight1,weight2);
	        mNN.feed();
	        mNN.output_combine();
		    if (mNN.output_com[0]==3)
			{
		    	m_info=m_info+mNN.LoadInfo(m_infofile,mNN.output_com[0])+"\n";
		        AfxMessageBox(m_info);
				term=FALSE;
			}
		    else if (mNN.output_com[0]==1)
			{
			    if (p>=num_NN)
				{
				    AfxMessageBox("没有匹配到故障模式!");
					term=FALSE;
				}
			    term=TRUE;
			    p++;
			}
		}
	}


	m_err="测试误差:";
	m_ratio="测试正确率";
	m_right_ratio=mNN.right_ratio;
	m_1=mNN.err_sum;
	UpdateData(FALSE);
//	
}

void CNNDlg::OnButtonGetdata() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	if (m_ss=="")
	{
		AfxMessageBox("choose your file to be treated!");
		return;
	}
	mNN.Getdata(m_ss);
	m_num_sam.Format("%d",mNN.sample_num);
	m_num_cha.Format("%d",mNN.character_num);
	m_num_out.Format("%d",mNN.output_num);
	UpdateData(FALSE);



}

void CNNDlg::OnButtonNorm() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);

		if (m_norm_filename=="")
		{
			AfxMessageBox("数据归一化并不存入文件");
			mNN.normnofile();

		}
		if (m_norm_filename!="")
		{
			mNN.norm(m_norm_filename);
			AfxMessageBox("数据归一化并存入文件:"+m_norm_filename);
		}

}

void CNNDlg::OnButtonLoadNorm() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	CFileDialog	FileDlg(TRUE,NULL,NULL,OFN_HIDEREADONLY, "All Files (*.*)|*.*||");
	if (FileDlg.DoModal()==IDCANCEL)	return;
	m_norm_loadfile=FileDlg.GetPathName();
	UpdateData(FALSE);
}

void CNNDlg::OnButtonLoadNorm1() 
{
	// TODO: Add your control notification handler code here

	mNN.LoadDatanorm(m_norm_loadfile);

}

void CNNDlg::OnButtonWeightfile() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	CFileDialog	FileDlg(TRUE,NULL,NULL,OFN_HIDEREADONLY, "All Files (*.*)|*.*||");
	if (FileDlg.DoModal()==IDCANCEL)	return;
	m_weight_file=FileDlg.GetPathName();
	UpdateData(FALSE);
}

void CNNDlg::OnButtonLoadWeight() 
{
	// TODO: Add your control notification handler code here
	mNN.LoadWeight(m_weight_file,m_weight2_file);
}

void CNNDlg::OnButtonWeightfile2() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	CFileDialog	FileDlg(TRUE,NULL,NULL,OFN_HIDEREADONLY, "All Files (*.*)|*.*||");
	if (FileDlg.DoModal()==IDCANCEL)	return;
	m_weight2_file=FileDlg.GetPathName();
	UpdateData(FALSE);
}



void CNNDlg::OnButtonInfo() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	CFileDialog	FileDlg(TRUE,NULL,NULL,OFN_HIDEREADONLY, "All Files (*.*)|*.*||");
	if (FileDlg.DoModal()==IDCANCEL)	return;
	m_infofile=FileDlg.GetPathName();
	UpdateData(FALSE);
}

void CNNDlg::OnButtonMiddis() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	if (m_testfile=="")
	{
		AfxMessageBox("选择测试样本!");
		return;

	}
	if (m_ss=="")
	{
		AfxMessageBox("选择故障样本库!");
		return;

	}
	mNN.Getdata(m_testfile);
	mNN.test_num=mNN.sample_num;
	if (mNN.test_num>10)
	{
		AfxMessageBox("测试样本最多为10组");
		return;
	}
	mNN.test_character_num=mNN.character_num;
    mNN.normnofile();
	int dd;
	for (int i=0;i<mNN.test_num;i++)
	{
		for (int j=0;j<mNN.test_character_num;j++)
		{
			mNN.data_norm_test[i][j]=mNN.data_norm[i][j];
		}
		mNN.data_out_test[i]=mNN.data_out[i];
		dd=mNN.data_out[i];;
	}
	mNN.Getdata(m_ss);

	if (mNN.test_character_num!=mNN.character_num)
	{
		AfxMessageBox("测试样本与故障库样本属性不匹配!");
		return;

	}
	if (m_infofile=="")
	{
		AfxMessageBox("选择故障信息库!");
		return;

	}

	mNN.normnofile();
	mNN.MinDis();
	CString ss;
	m_info.Empty();
	for (i=0;i<mNN.test_num;i++)
	{

	    m_info=m_info+mNN.LoadInfo(m_infofile,(int)mNN.min[i][1])+"\n";
	}
	m_right_ratio=mNN.right_ratio;
	UpdateData(FALSE);

	
	
}

⌨️ 快捷键说明

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