📄 chooseview.cpp
字号:
m_result="苹果";
//MessageBox("苹果");
break;
case 50:
m_result="桔子";
//MessageBox("桔子");
break;
case 25:
m_result="海棠";
//MessageBox("海棠");
break;
default:
m_result="无法识别";
}
this->GetDocument()->m_jieguo = m_result;
this->GetDocument()->gotorec = true;
this->GetDocument()->jieguo = result*100;
//Cpdoc->m_jieguo = m_result;
//this->GetDocument()->UpdateAllViews(this);
//this->GetDocument()->m_jieguo = m_result;
//theApp.myresult=m_result;
/*CAppleDoc* pDoc =(CAppleDoc* )GetDocument();
pDoc->UpdateAllViews (this);*/
CFrameWnd *pFrame;
// CWnd *pChild;
// CView *pView;
// CAppleView *pView;
////获取主窗口指针
pFrame =(CFrameWnd*)AfxGetApp()->m_pMainWnd;
//pFrame->GetActiveView();
pFrame->PostMessage(WM_MY_MESSAGE);
((CAppleApp*)AfxGetApp())->m_WNsign =true;
//pView->PostMessage(WM_MY_MESSAGE);
//// 获取子窗口指针
//pChild = (CMDIChildWnd *) pFrame->GetActiveFrame();
////获取视类指针
//pView = pFrame->GetActiveView();
//CFormView *temp;
//temp->GetParent();
// temp->PostMessage(WM_MY_MESSAGE);
//this->PostMessage(WM_MY_MESSAGE);
//if(pView != NULL)
//pView->PostMessage(WM_MY_MESSAGE);//发送消息
//SendMessage(WM_MY_MESSAGE);
//theApp.m_pMainWnd->PostMessage(WM_MY_MESSAGE);
// PostMessage(WM_MY_MESSAGE);
//SendMessage(wm_com,
}
double CChooseView::recognize(double *to_recog)
{
double result = -1;
forwardcp(INPUT_NUMBER,HIDE_NUMBER,(double*)dblWeight_IN_HD,to_recog,dblOut_HD,CW_HD);
forwardcp(HIDE_NUMBER,OUT_NUMBER,(double*)dblWeight_HD_OT,dblOut_HD,dblOut_OT,CW_OT);
//这里只有一个最后的输出,为什么要判断三次????????????????????
//for(int i=0;i<3;i++)
//{
// switch (i)
// {
// case 0:
// if (dblOut_OT[i]>=0.8)
// return 1;
// break;
// case 1:
// if ((dblOut_OT[i]>0.5)/*&&(dblOut_OT[i]<0.8)*/)
// return 0.5;
// break;
// case 2:
// if (dblOut_OT[i]<0.5)
// return 0.25;
// break;
// }
//}
if (dblOut_OT[0]>T[0][0]-0.1)
return result=1;
if ((dblOut_OT[0]>=T[1][0]-0.1)&&(dblOut_OT[0]<=T[1][0]+0.1))
return result = 0.5;
if (dblOut_OT[0]<T[2][0]+0.1)
return result = 0.25;
return result;
}
void CChooseView::OnInitialUpdate()
{
//CFormView::OnInitialUpdate();
// TODO: 在此添加专用代码和/或调用基类
GetDlgItem(IDC_BTNREC)->EnableWindow(false);
}
void CChooseView::OnBnClickedBtnstudy2()
{
// TODO: 在此添加控件通知处理程序代码
GAStudy();
}
void CChooseView::GAStudy(void)
{
initpop(); //初始化种群
bool yflag=false;
while(1)
{
train_count++; //下一代
ComputeFit(); //计算适应度
SelectAndDeletePop(); //选择和淘汰种群
CrossPop(); //交叉操作
Mutation(); //变异操作
for(int i=0;i<POPNUM;i++)
{
if(GAerrorp[i]<0.5)
{
yflag=true;
break;
}
}
if(yflag)
break;
}
}
void CChooseView::initpop()
{
//for(int i=0;i<popnum;i++)
CAppleDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
wgtinit((double*)GAdblWeight_IN_HD,POPNUM*INPUT_NUMBER*HIDE_NUMBER); //输入层的9个初始 weight
wgtinit((double*)GAdblWeight_HD_OT,POPNUM*OUT_NUMBER*HIDE_NUMBER); //输出层的3个初始 weight
CStdioFile myfile;
CString ss;
myfile.Open("weight.txt",CFile::modeWrite|CFile::modeCreate);
ss="初始权重-------";
myfile.Write(ss,strlen(ss));
myfile.WriteString("\n");
for(int i=0;i<POPNUM;i++)
{
CString str,str1,str2,str3,str4,str5,str6,str7,str8,str9,str10,str11,str12;
str="";
str1.Format ("%f ",GAdblWeight_IN_HD[i][0][0]);
str2.Format ("%f ",GAdblWeight_IN_HD[i][0][1]);
str3.Format ("%f ",GAdblWeight_IN_HD[i][0][2]);
str4.Format ("%f ",GAdblWeight_IN_HD[i][1][0]);
str5.Format ("%f ",GAdblWeight_IN_HD[i][1][1]);
str6.Format ("%f ",GAdblWeight_IN_HD[i][1][2]);
str7.Format ("%f ",GAdblWeight_IN_HD[i][2][0]);
str8.Format ("%f ",GAdblWeight_IN_HD[i][2][1]);
str9.Format ("%f ",GAdblWeight_IN_HD[i][2][2]);
str10.Format ("%f ",GAdblWeight_HD_OT[i][0][0]);
str11.Format ("%f ",GAdblWeight_HD_OT[i][0][1]);
str12.Format ("%f ",GAdblWeight_HD_OT[i][0][2]);
CString w;
w.Format ("%d ",i);
str=w+str1+str2+str3+str4+str5+str6+str7+str8+str9+str10+str11+str12;
//dc.TextOut (0,350+i*20,str);
myfile.WriteString(str);
myfile.WriteString("\n");
}
myfile.Close();
}
void CChooseView::ComputeFit()
{
//
CStdioFile myfile;
CString ss,str;
myfile.Open("weight.txt",CFile::modeRead|CFile::modeWrite);
for(int p=0;p<POPNUM;p++)
{
Errp=0;
for(int i=0;i<INPUT_NUMBER;i++)
for(int j=0;j<HIDE_NUMBER;j++)
dblWeight_IN_HD[i][j]=GAdblWeight_IN_HD[p][i][j];
for(i=0;i<OUT_NUMBER;i++)
for(int j=0;j<HIDE_NUMBER;j++)
dblWeight_HD_OT[i][j]=GAdblWeight_HD_OT[p][i][j];
for(int s=0;s<SpNUM;s++)
{
for(i=0;i<INPUT_NUMBER;i++)
dblOut_IN[i]=X[s][i]; //dblOut_IN[i]=x=[0][i]{0,0.1} 第i个样本的输入
forwardcp(INPUT_NUMBER,HIDE_NUMBER,(double*)dblWeight_IN_HD,dblOut_IN,dblOut_HD,CW_HD);
forwardcp(HIDE_NUMBER,OUT_NUMBER,(double*)dblWeight_HD_OT,dblOut_HD,dblOut_OT,CW_OT);
Errp+=fabs(dblOut_OT[0]-T[s][0]);
}
GAerrorp[p]=Errp;
ss.Format ("%f\t\t",GAerrorp[p]);
str+=ss;
}
myfile.SeekToEnd();
myfile.WriteString("误差\n");
ss="";
for(int i=0;i<POPNUM;i++)
{
CString y;
y.Format ("%d",i);
ss+="第"+y+"个\t\t";
}
myfile.WriteString(ss);
myfile.WriteString("\n");
myfile.WriteString(str);
myfile.Close();
}
void CChooseView::SelectAndDeletePop()
{
//int *t = new int[POPNUM+1];
for(int i=0;i<POPNUM;i++)
t[i]=i;
IndirectSort(GAerrorp,t,POPNUM);
//进行选择操作 输入层到隐含层的权重
for(int p=0;p<2;p++)
for(int i=0;i<INPUT_NUMBER;i++)
for(int j=0;j<HIDE_NUMBER;j++)
GAdblWeight_IN_HD[t[POPNUM-p-1]][i][j]=GAdblWeight_IN_HD[t[p]][i][j];
//进行选择操作 输入层到隐含层的权重
for(p=0;p<2;p++)
for(int i=0;i<OUT_NUMBER;i++)
for(int j=0;j<HIDE_NUMBER;j++)
GAdblWeight_HD_OT[t[POPNUM-p-1]][i][j]=GAdblWeight_HD_OT[t[p]][i][j];
CStdioFile myfile;
CString ss,stre;
myfile.Open("weight.txt",CFile::modeRead|CFile::modeWrite);
myfile.SeekToEnd();
myfile.WriteString("\n误差排序--->>>升序\r\n");
ss="";
for(i=0;i<POPNUM;i++)
{
CString y;
//ss.Format ("原始的%d\t ",t[i]);
y.Format ("%d",t[i]);
ss+="原第"+y+"个\t\t";
}
myfile.WriteString(ss);
myfile.WriteString("\n");
for(i=0;i<POPNUM;i++)
{
//ss.Format ("原始的%d\t ",t[i]);
ss.Format ("%f\t\t",GAerrorp[t[i]]);
stre+=ss;
}
myfile.WriteString(stre);
myfile.WriteString("\n选择后的权重\n");
for(i=0;i<POPNUM;i++)
{
//选择后的权重
CString str,str1,str2,str3,str4,str5,str6,str7,str8,str9,str10,str11,str12;
str1.Format ("%f ",GAdblWeight_IN_HD[i][0][0]);
str2.Format ("%f ",GAdblWeight_IN_HD[i][0][1]);
str3.Format ("%f ",GAdblWeight_IN_HD[i][0][2]);
str4.Format ("%f ",GAdblWeight_IN_HD[i][1][0]);
str5.Format ("%f ",GAdblWeight_IN_HD[i][1][1]);
str6.Format ("%f ",GAdblWeight_IN_HD[i][1][2]);
str7.Format ("%f ",GAdblWeight_IN_HD[i][2][0]);
str8.Format ("%f ",GAdblWeight_IN_HD[i][2][1]);
str9.Format ("%f ",GAdblWeight_IN_HD[i][2][2]);
str10.Format ("%f ",GAdblWeight_HD_OT[i][0][0]);
str11.Format ("%f ",GAdblWeight_HD_OT[i][0][1]);
str12.Format ("%f ",GAdblWeight_HD_OT[i][0][2]);
CString w;
w.Format ("%d ",i);
str=w+str1+str2+str3+str4+str5+str6+str7+str8+str9+str10+str11+str12;
myfile.WriteString(str);
myfile.WriteString("\n");
}
myfile.Close();
}
void CChooseView::IndirectSort(double e[],int t[],int n)
{
int temp;
for(int i=1;i<POPNUM;i++)
{
for(int j=0;j<POPNUM-i;j++)
if(e[t[j]]>e[t[j+1]])
{
temp=t[j];
t[j]=t[j+1];
t[j+1]=temp;
}
}
}
void CChooseView::CrossPop()
{
srand((unsigned)time(NULL));
double temp;
CString s;
for(int p=0;p<1;p++)
{
//采用单点杂交的方式
//t[POPNUM-p-1]这两个是不能用的;
//对哪两个种群进行交叉进行随机的选择
int d,e;
s="";
d=RandBetween(0,POPNUM);
e=RandBetween(0,POPNUM);
//for(int i=0;i<2;i++) //交叉前6个权重的效果不太好
// for(int j=0;j<HIDE_NUMBER;j++)
int dot1,dot2;
dot1=RandBetween(0,3);
dot2=RandBetween(0,3);
CString sd,se,s1,s2;
for(int i=0;i<dot1;i++) //采用多点杂交
for(int j=0;j<dot2;j++)
{
temp=GAdblWeight_IN_HD[t[p]][i][j];
GAdblWeight_IN_HD[t[p]][i][j]=GAdblWeight_IN_HD[d][i][j];
GAdblWeight_IN_HD[d][i][j]=temp;
//temp=GAdblWeight_IN_HD[POPNUM-p-1][i][j];
//GAdblWeight_IN_HD[POPNUM-p-1][i][j]=GAdblWeight_IN_HD[e][i][j];
//GAdblWeight_IN_HD[e][i][j]=temp;
}
sd.Format ("d=%d ",d);
se.Format ("e=%d ",e);
s1.Format ("dot1=%d ",dot1);
s2.Format ("dot2=%d ",dot2);
s=sd+se+s1+s2;
}
CStdioFile myfile;
myfile.Open("weight.txt",CFile::modeRead|CFile::modeWrite);
myfile.SeekToEnd();
myfile.WriteString("\n各个随机值\n");
//myfile.WriteString("\nd e dot1 dot2\n");
myfile.WriteString(s);
myfile.Close ();
}
void CChooseView::Mutation(void)
{
double rate=RandDotNum();
if(MutationRate>rate)
{
int i=RandBetween(0,3);
int j=RandBetween(0,3);
double RandWeight=RandDotNum()*0.2;
//int MutationWho=RandBetween(0,5);
//指定变异种群,最好的种群
GAdblWeight_IN_HD[t[0]][i][j]=RandWeight;
//MessageBox("变异了!");
}
CStdioFile myfile;
CString ss,str,count;
myfile.Open("weight.txt",CFile::modeRead|CFile::modeWrite);
myfile.SeekToEnd();
count.Format ("%d",train_count);
myfile.WriteString("\n交叉,变异后结果, 第 "+count+" 代 \n");
for(int i=0;i<POPNUM;i++)
{
//交叉变异后的权重
CString str,str1,str2,str3,str4,str5,str6,str7,str8,str9,str10,str11,str12;
str1.Format ("%f ",GAdblWeight_IN_HD[i][0][0]);
str2.Format ("%f ",GAdblWeight_IN_HD[i][0][1]);
str3.Format ("%f ",GAdblWeight_IN_HD[i][0][2]);
str4.Format ("%f ",GAdblWeight_IN_HD[i][1][0]);
str5.Format ("%f ",GAdblWeight_IN_HD[i][1][1]);
str6.Format ("%f ",GAdblWeight_IN_HD[i][1][2]);
str7.Format ("%f ",GAdblWeight_IN_HD[i][2][0]);
str8.Format ("%f ",GAdblWeight_IN_HD[i][2][1]);
str9.Format ("%f ",GAdblWeight_IN_HD[i][2][2]);
str10.Format ("%f ",GAdblWeight_HD_OT[i][0][0]);
str11.Format ("%f ",GAdblWeight_HD_OT[i][0][1]);
str12.Format ("%f ",GAdblWeight_HD_OT[i][0][2]);
CString w;
w.Format ("%d ",i);
str=w+str1+str2+str3+str4+str5+str6+str7+str8+str9+str10+str11+str12;
myfile.WriteString(str);
myfile.WriteString("\n");
}
myfile.Close();
}
int CChooseView::RandBetween(int a,int b)
{
int result;
//srand((unsigned)time(NULL));
result= a+rand()%(b-a);
return result;
}
double CChooseView::RandDotNum()
{
double result;
//srand((unsigned)time(NULL));
result=rand()/32767.0;
return result;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -