📄 dlgart.cpp
字号:
// DlgART.cpp : implementation file
//
#include "stdafx.h"
#include "ANN.h"
#include "DlgART.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
double Wij[10][25]; //前向连接权
int Tij[10][25]; //反向连接权
bool Used[10]; //神经元是否被用
double alert=0.95; //警戒参数
double Sj[10]; //输出层神经元的激活值
int Result[4]; //输入模式对应的获胜神经元
int Uik[4][25]= //输入模式
{{1,0,0,0,0, 0,1,0,0,0, 0,0,1,0,0, 0,0,0,1,0, 0,0,0,0,1},
{ 1,0,0,0,1, 0,1,0,1,0, 0,0,1,0,0, 0,1,0,1,0, 1,0,0,0,1},
{ 1,0,0,0,1, 0,1,0,1,0, 1,1,1,1,1, 0,1,0,1,0, 1,0,0,0,1},
{ 1,0,0,0,1, 0,1,0,1,0, 1,1,1,1,1, 0,1,0,1,0, 1,0,0,0,1}};
int n=25; //输入层神经元的数目
int m=10; //输出层神经元的数目
int nSam=4; //输入模式的数目
/////////////////////////////////////////////////////////////////////////////
// CDlgART dialog
CDlgART::CDlgART(CWnd* pParent /*=NULL*/)
: CDialog(CDlgART::IDD, pParent)
{
//{{AFX_DATA_INIT(CDlgART)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void CDlgART::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDlgART)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDlgART, CDialog)
//{{AFX_MSG_MAP(CDlgART)
ON_BN_CLICKED(IDC_BUTTON_about, OnBUTTONabout)
ON_BN_CLICKED(IDC_BUTTON_Topo, OnBUTTONTopo)
ON_BN_CLICKED(IDC_BUTTON_Study, OnBUTTONStudy)
ON_BN_CLICKED(IDC_BUTTON_quan, OnBUTTONquan)
ON_BN_CLICKED(IDC_BUTTON_remember, OnBUTTONremember)
ON_BN_CLICKED(IDC_BUTTON_remember2, OnBUTTONremember2)
ON_CBN_SELCHANGE(IDC_COMBO_Num, OnSelchangeCOMBONum)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDlgART message handlers
BOOL CDlgART::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
this->SetWndRgnPart();
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CDlgART::SetWndRgnPart(){
CRgn rgn;
rgn.CreateRectRgn(0,0,1000,186);
SetWindowRgn((HRGN)rgn,TRUE);
}
void CDlgART::SetWndRgnAll(){
CRgn rgn;
rgn.CreateRectRgn(0,0,1000,1000);
SetWindowRgn((HRGN)rgn,TRUE);
}
void CDlgART::OnBUTTONabout()
{
CString s;
s+="【回想阶段特征】\n";
s+="1.新输入模式与某已记忆模式相似,该记忆模式的识别神经元识别它;\n";
s+="2.新输入模式与已记忆模式不相似,网络分配未用神经元识别它;\n";
s+="3.已记忆模式可能被回想,已分配给它的神经元识别它;\n";
s+="4.已记忆模式可能被忘记,网络分配未用神经元识别它;\n";
s+="5.已记忆模式可能被混淆,其它神经元识别它。\n\n";
s+="【参考书目】\n";
s+="[1]王伟,《人工神经网络原理--入门与应用》,北京航空航天大学出版社,1995年10月,第一版 \n";
s+="[2]王旭,《人工神经元网络原理与应用》,东北大学出版社,2000年,第一版\n";
MessageBox(s,"ART1",MB_ICONINFORMATION);
}
void CDlgART::OnBUTTONTopo()
{
// TODO: Add your control notification handler code here
CString sGoal="",s;
sGoal+="1、本ART1网的拓扑结构\n";
sGoal+="\n";
sGoal+="网络层 神经元数目\n";
sGoal+="------------------------------\n";
s.Format("输入层(比较层) %d\n",n);sGoal+=s;
s.Format("输出层(识别层) %d\n",m);sGoal+=s;
sGoal+="\n\n";
s.Format("2、本ART1网的%d个学习模式\n",nSam);sGoal+=s;
sGoal+="\n";
s.Format("%d个%d维的二值向量\n",nSam,n);sGoal+=s;
s.Format("警戒参数=%g\n",alert);sGoal+=s;
MessageBox(sGoal,"ART1",MB_ICONINFORMATION);
}
void CDlgART::OnBUTTONStudy()
{
// TODO: Add your control notification handler code here
this->SetWndRgnPart();
CButton *pBtn;
pBtn=(CButton*)GetDlgItem(IDC_BUTTON_Study);
pBtn->EnableWindow(FALSE);
pBtn=(CButton*)GetDlgItem(IDC_BUTTON_quan);
pBtn->EnableWindow(FALSE);
pBtn=(CButton*)GetDlgItem(IDC_BUTTON_remember);
pBtn->EnableWindow(FALSE);
pBtn->SetWindowText("回想>>");
pBtn=(CButton*)GetDlgItem(IDC_BUTTON_Topo);
pBtn->EnableWindow(FALSE);
/**************************************************************
初始化前向连接权、反向连接权和警戒参数;
选取一个输入模式提供给输入层;
计算输出层神经元的激活值;
选取输出层具有最大激活值的神经元g,作为获胜神经元;
求反馈连接权矢量与输入模式矢量的相似程度;
如果相似程度低于警戒参数{
标识g为失败神经元;
选取下一个未败的具有最大激活值的神经元,作为获胜神经元;
如果没有神经元可选,提示并退出;
}
调整连接获胜神经元的前向连接权与反向连接权;
如果还有输入模式尚未识别,则选取下一个输入模式进行识别;
**************************************************************/
/*(下列注释的变量已经全局化)
double Wij[4][25]; //前向连接权
int Tij[4][25]; //反向连接权
double alert; //警戒参数
double Sj[4]; //输出层神经元的激活值
int Result[4]; //输入模式对应的获胜神经元
int Uik[4][25]= //输入模式
{{1,0,0,0,0, 0,1,0,0,0, 0,0,1,0,0, 0,0,0,1,0, 0,0,0,0,1},
{ 1,0,0,0,1, 0,1,0,1,0, 0,0,1,0,0, 0,1,0,1,0, 1,0,0,0,1},
{ 1,0,0,0,1, 0,1,0,1,0, 1,1,1,1,1, 0,1,0,1,0, 1,0,0,0,1},
{ 1,0,0,0,1, 0,1,0,1,0, 1,1,1,1,1, 0,1,0,1,0, 1,0,0,0,1}};
int n; //输入层神经元的数目
int m; //输出层神经元的数目
int nSam; //输入模式的数目
*/
double max; //输出层神经元的最大激活值
int sg; //获胜神经元
double a,b,c;
int i,j,k;
CString sResult="",s,sWeight;
for(i=0; i<m; i++){
Result[i]=-1;
Used[i]=false;
}
//初始化前向连接权、反向连接权和警戒参数;
;
for(i=0; i<m; i++){
for(j=0; j<n; j++){
Wij[i][j]=1/(1+(double)n);
Tij[i][j]=1;
}
}
//选取一个输入模式提供给输入层;
for(k=0; k<nSam; k++){
//计算输出层神经元的激活值;
for(i=0; i<m; i++){
Sj[i]=0;
for(j=0; j<n; j++){
Sj[i]=Sj[i]+Wij[i][j]*Uik[k][j];
}
}
//选取输出层具有最大激活值的神经元g,作为获胜神经元;
CLOSET: max=0.0; sg=0;
for(i=0; i<m; i++){
if(Sj[i]>max){
max=Sj[i];
sg=i;
}
}
if(Sj[sg]<max){
::AfxMessageBox("所有输出层神经元已经用完!");
pBtn=(CButton*)GetDlgItem(IDC_BUTTON_Study);
pBtn->EnableWindow(TRUE);
pBtn=(CButton*)GetDlgItem(IDC_BUTTON_Topo);
pBtn->EnableWindow(TRUE);
this->SetDlgItemText(IDC_EDIT_StudyResult,"学习失败!");
return;
}
//求反馈连接权矢量与输入模式矢量的相似程度;
a=0;
b=0;
for(j=0; j<n; j++){
a+=Uik[k][j];
b+=(Tij[sg][j]*Uik[k][j]);
}
c=b/a;
//如果相似程度低于警戒参数{
if(c<alert){
//标识g为失败神经元;
Sj[sg]=-1;
//选取下一个未败的具有最大激活值的神经元,作为获胜神经元;
//如果没有神经元可选,提示并退出;
goto CLOSET;
}
//}
//调整连接获胜神经元的前向连接权与反向连接权;
Result[k]=sg;
Used[sg]=true;
a=0;
for(j=0; j<n; j++){
a+=(Tij[sg][j]*Uik[k][j]);
}
for(j=0; j<n; j++){
Wij[sg][j]=(Tij[sg][j]*Uik[k][j])/(0.5+a);
Tij[sg][j]=Tij[sg][j]*Uik[k][j];
}
//如果还有输入模式尚未识别,则选取下一个输入模式进行识别;
}
//输出学习结果(各个输入模式对应的输出神经元);
sResult="";
for(k=0; k<nSam; k++){
//获取格式化的输入模式;
s.Format("输入模式%d=(%d",k,Uik[k][0]);
sResult+=s;
for(j=1; j<n; j++){
s.Format(",%d",Uik[k][j]);
sResult+=s;
}
sResult+=")\r\n";;
//获取模式的识别神经元;
s.Format("识别神经元=%d\r\n",Result[k]);
sResult+=s;
}
this->SetDlgItemText(IDC_EDIT_StudyResult,sResult);
//将连接权写入文件;
CTime time=CTime::GetCurrentTime();
s=time.Format("记录时间 %Y-%m-%d %H:%M:%S\n\n" );
sWeight=s;
s.Format("前向连接权W[%d][%d]:\n",m,n);
sWeight+=s;
for(i=0; i<m; i++){
for(j=0; j<n; j++){
s.Format("%f\t",Wij[i][j]);
sWeight+=s;
}
sWeight+="\n";
}
sWeight+="\n";
s.Format("反向连接权T[%d][%d]:\n",m,n);
sWeight+=s;
for(i=0; i<m; i++){
for(j=0; j<n; j++){
s.Format("%-8d\t",Tij[i][j]);
sWeight+=s;
}
sWeight+="\n";
}
sWeight+="\n";
CStdioFile out;
out.Open("ART1Weight.txt", CFile::modeCreate | CFile::modeWrite);
out.WriteString(sWeight);
out.Close();
pBtn=(CButton*)GetDlgItem(IDC_BUTTON_Study);
pBtn->EnableWindow(TRUE);
pBtn=(CButton*)GetDlgItem(IDC_BUTTON_quan);
pBtn->EnableWindow(TRUE);
pBtn=(CButton*)GetDlgItem(IDC_BUTTON_remember);
pBtn->EnableWindow(TRUE);
pBtn=(CButton*)GetDlgItem(IDC_BUTTON_Topo);
pBtn->EnableWindow(TRUE);
}
void CDlgART::OnBUTTONquan()
{
// TODO: Add your control notification handler code here
HWND h=::FindWindowEx(NULL,NULL,NULL,"Microsoft Internet Explorer");
::ShellExecute(h,"open","ART1Weight.txt",NULL,NULL,SW_SHOWNORMAL);
}
void CDlgART::OnBUTTONremember()
{
// TODO: Add your control notification handler code here
this->SetWndRgnAll();
CButton *pBtn;
pBtn=(CButton*)GetDlgItem(IDC_BUTTON_remember);
pBtn->SetWindowText("回 想");
}
void CDlgART::OnBUTTONremember2()
{
// TODO: Add your control notification handler code here
CButton *pBtn;
pBtn=(CButton*)GetDlgItem(IDC_BUTTON_Study);
pBtn->EnableWindow(FALSE);
pBtn=(CButton*)GetDlgItem(IDC_BUTTON_quan);
pBtn->EnableWindow(FALSE);
pBtn=(CButton*)GetDlgItem(IDC_BUTTON_remember);
pBtn->EnableWindow(FALSE);
pBtn=(CButton*)GetDlgItem(IDC_BUTTON_remember2);
pBtn->EnableWindow(FALSE);
pBtn=(CButton*)GetDlgItem(IDC_BUTTON_Topo);
pBtn->EnableWindow(FALSE);
this->SetDlgItemText(IDC_EDIT_Result,"");
int iSam[25];
CString sSam,s,sResult,sWeight;
int i,j,sg;
double Sj[10],max,a,b,c;
//获取输入模式;
this->GetDlgItemText(IDC_EDIT_Sample,sSam);
for(i=0; i<n; i++){
s=sSam[i*2];
sscanf(s,"%d",&iSam[i]);
//s.Format("%d",iSam[i]);
//::AfxMessageBox(s);
}
//计算输出层神经元的激活值;
for(i=0; i<m; i++){
Sj[i]=0;
for(j=0; j<n; j++){
Sj[i]=Sj[i]+Wij[i][j]*iSam[j];
}
}
//选取输出层具有最大激活值的神经元g,作为获胜神经元;
CLOSET: max=0.0; sg=0;
for(i=0; i<m; i++){
if(Sj[i]>max){
max=Sj[i];
sg=i;
}
}
if(Sj[sg]<max){
::AfxMessageBox("所有输出层神经元已经用完!");
pBtn=(CButton*)GetDlgItem(IDC_BUTTON_Study);
pBtn->EnableWindow(TRUE);
pBtn=(CButton*)GetDlgItem(IDC_BUTTON_quan);
pBtn->EnableWindow(TRUE);
pBtn=(CButton*)GetDlgItem(IDC_BUTTON_remember);
pBtn->EnableWindow(TRUE);
pBtn=(CButton*)GetDlgItem(IDC_BUTTON_remember2);
pBtn->EnableWindow(TRUE);
pBtn=(CButton*)GetDlgItem(IDC_BUTTON_Topo);
pBtn->EnableWindow(TRUE);
return;
}
//求反馈连接权矢量与输入模式矢量的相似程度;
a=0;
b=0;
for(j=0; j<n; j++){
a+=iSam[j];
b+=(Tij[sg][j]*iSam[j]);
}
c=b/a;
//如果相似程度低于警戒参数{
if(c<alert){
//标识g为失败神经元;
Sj[sg]=-1;
//选取下一个未败的具有最大激活值的神经元,作为获胜神经元;
//如果没有神经元可选,提示并退出;
goto CLOSET;
}
//}
//调整连接获胜神经元的前向连接权与反向连接权;
/*Result[k]=sg;*/
a=0;
for(j=0; j<n; j++){
a+=(Tij[sg][j]*iSam[j]);
}
for(j=0; j<n; j++){
Wij[sg][j]=(Tij[sg][j]*iSam[j])/(0.5+a);
Tij[sg][j]=Tij[sg][j]*iSam[j];
}
//如果使用新的神经元,则提示
if(!Used[sg]){
Used[sg]=true;
sResult="";
s.Format("\r\n新的输入模式=(%d",iSam[0]);
sResult+=s;
for(j=1; j<n; j++){
s.Format(",%d",iSam[j]);
sResult+=s;
}
sResult+=")\r\n";;
//获取模式的识别神经元;
s.Format("新识别神经元=%d\r\n",sg);
sResult+=s;
GetDlgItemText(IDC_EDIT_StudyResult,s);
SetDlgItemText(IDC_EDIT_StudyResult,s+sResult);
}
//输出识别结果(各个输入模式对应的输出神经元);
sResult.Format("识别神经元=%d",sg);
this->SetDlgItemText(IDC_EDIT_Result,sResult);
//将连接权写入文件;
CTime time=CTime::GetCurrentTime();
s=time.Format("记录时间(回想) %Y-%m-%d %H:%M:%S\n\n" );
sWeight=s;
s.Format("前向连接权W[%d][%d]:\n",m,n);
sWeight+=s;
for(i=0; i<m; i++){
for(j=0; j<n; j++){
s.Format("%f\t",Wij[i][j]);
sWeight+=s;
}
sWeight+="\n";
}
sWeight+="\n";
s.Format("反向连接权T[%d][%d]:\n",m,n);
sWeight+=s;
for(i=0; i<m; i++){
for(j=0; j<n; j++){
s.Format("%-8d\t",Tij[i][j]);
sWeight+=s;
}
sWeight+="\n";
}
sWeight+="\n";
CStdioFile out;
out.Open("ART1Weight.txt", CFile::modeCreate | CFile::modeWrite);
out.WriteString(sWeight);
out.Close();
pBtn=(CButton*)GetDlgItem(IDC_BUTTON_Study);
pBtn->EnableWindow(TRUE);
pBtn=(CButton*)GetDlgItem(IDC_BUTTON_quan);
pBtn->EnableWindow(TRUE);
pBtn=(CButton*)GetDlgItem(IDC_BUTTON_remember);
pBtn->EnableWindow(TRUE);
pBtn=(CButton*)GetDlgItem(IDC_BUTTON_remember2);
pBtn->EnableWindow(TRUE);
pBtn=(CButton*)GetDlgItem(IDC_BUTTON_Topo);
pBtn->EnableWindow(TRUE);
}
void CDlgART::OnSelchangeCOMBONum()
{
// TODO: Add your control notification handler code here
CString sSample,s;
int k,j;
k=this->GetDlgItemInt(IDC_COMBO_Num);
sSample="";
s.Format("%d",Uik[k][0]);
sSample+=s;
for(j=1; j<n; j++){
s.Format(",%d",Uik[k][j]);
sSample+=s;
}
this->SetDlgItemText(IDC_EDIT_Sample,sSample);
this->SetDlgItemText(IDC_EDIT_Result,"");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -