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

📄 winsvmdlg.cpp

📁 采用c++编写的有关svm交叉验证的程序。
💻 CPP
📖 第 1 页 / 共 4 页
字号:
						}				}
				break;
			case 3:
		        switch(param.kernel_type)		        {			        case 1:						{						param.degree=m_pMyPropSheet->m_kdegree;						param.coef0=m_pMyPropSheet->m_kr;						param.C=m_pMyPropSheet->m_cset;						param.p=m_pMyPropSheet->m_plost;						degreey=m_pMyPropSheet->m_kdegreeend;						kry=m_pMyPropSheet->m_krend;						krystep=m_pMyPropSheet->m_krstep;						cy=m_pMyPropSheet->m_csetend;						cystep=m_pMyPropSheet->m_csetstep;						py=m_pMyPropSheet->m_plostend;						pystep=m_pMyPropSheet->m_ploststep;						delete m_pMyPropSheet;						m_pMyPropSheet=NULL;						m_mythread=AfxBeginThread( (AFX_THREADPROC)lpRecvProcy6, this );//线程运行				        break;						}			        case 2:						{						param.C=m_pMyPropSheet->m_cset;						param.p=m_pMyPropSheet->m_plost;						cy=m_pMyPropSheet->m_csetend;						cystep=m_pMyPropSheet->m_csetstep;						py=m_pMyPropSheet->m_plostend;						pystep=m_pMyPropSheet->m_ploststep;						delete m_pMyPropSheet;						m_pMyPropSheet=NULL;						m_mythread=AfxBeginThread( (AFX_THREADPROC)lpRecvProcy7, this );//线程运行				        break;						}			        case 3:						{						param.coef0=m_pMyPropSheet->m_kr;						param.C=m_pMyPropSheet->m_cset;						param.p=m_pMyPropSheet->m_plost;						kry=m_pMyPropSheet->m_krend;						krystep=m_pMyPropSheet->m_krstep;						cy=m_pMyPropSheet->m_csetend;						cystep=m_pMyPropSheet->m_csetstep;						py=m_pMyPropSheet->m_plostend;						pystep=m_pMyPropSheet->m_ploststep;						delete m_pMyPropSheet;						m_pMyPropSheet=NULL;						m_mythread=AfxBeginThread( (AFX_THREADPROC)lpRecvProcy8, this );//线程运行				        break;						}				}
				break;
			case 4:
		        switch(param.kernel_type)		        {			        case 1:						{						param.degree=m_pMyPropSheet->m_kdegree;						param.coef0=m_pMyPropSheet->m_kr;						param.C=m_pMyPropSheet->m_cset;						param.nu=m_pMyPropSheet->m_nu;						degreey=m_pMyPropSheet->m_kdegreeend;						kry=m_pMyPropSheet->m_krend;						krystep=m_pMyPropSheet->m_krstep;						cy=m_pMyPropSheet->m_csetend;						cystep=m_pMyPropSheet->m_csetstep;						nuy=m_pMyPropSheet->m_nuend;						nuystep=m_pMyPropSheet->m_nustep;						delete m_pMyPropSheet;						m_pMyPropSheet=NULL;						m_mythread=AfxBeginThread( (AFX_THREADPROC)lpRecvProcy9, this );//线程运行				        break;						}			        case 2:						{						param.C=m_pMyPropSheet->m_cset;						param.nu=m_pMyPropSheet->m_nu;						cy=m_pMyPropSheet->m_csetend;						cystep=m_pMyPropSheet->m_csetstep;						nuy=m_pMyPropSheet->m_nuend;						nuystep=m_pMyPropSheet->m_nustep;						delete m_pMyPropSheet;						m_pMyPropSheet=NULL;						m_mythread=AfxBeginThread( (AFX_THREADPROC)lpRecvProcy10, this );//线程运行				        break;						}			        case 3:						param.coef0=m_pMyPropSheet->m_kr;						param.C=m_pMyPropSheet->m_cset;						param.nu=m_pMyPropSheet->m_nu;						kry=m_pMyPropSheet->m_krend;						krystep=m_pMyPropSheet->m_krstep;						cy=m_pMyPropSheet->m_csetend;						cystep=m_pMyPropSheet->m_csetstep;						nuy=m_pMyPropSheet->m_nuend;						nuystep=m_pMyPropSheet->m_nustep;						delete m_pMyPropSheet;						m_pMyPropSheet=NULL;						m_mythread=AfxBeginThread( (AFX_THREADPROC)lpRecvProcy11, this );//线程运行				        break;				}
				break;
			}
	        GetDlgItem(IDC_BUTTON4)->EnableWindow();
            GetDlgItem(IDC_BUTTON1)->EnableWindow(false);
            GetDlgItem(IDC_BUTTON6)->EnableWindow();
			return;
		}
		else
		{
	    delete m_pMyPropSheet;
		m_pMyPropSheet=NULL;
		//线程运行
	    m_mythread=AfxBeginThread( (AFX_THREADPROC)lpRecvProc, this );
	    GetDlgItem(IDC_BUTTON4)->EnableWindow();
        GetDlgItem(IDC_BUTTON1)->EnableWindow(false);
        GetDlgItem(IDC_BUTTON6)->EnableWindow();
		return;
		}
	};
	delete m_pMyPropSheet;
	m_pMyPropSheet=NULL;
}

// 训练函数
void CwinsvmDlg::svmtrain()
{
    //Sleep(60000);//调试用
	if (read_problem(input_file_name)==0)
	{
	    svm_destroy_param(&param);
		return;//应该打不开返回推出。
	}

	if (param.svm_type == 1)
	{
	double opq = svm_check_parameter(&prob,&param);
	if(opq)
	{
		info.Format("规定的ν-SVC的参数nu不能大于 %f",opq);
		MessageBox(info);
	    svm_destroy_param(&param);
	    free(prob.y);
	    free(prob.x);
	    free(x_space);
	    info.Empty();
		return ;
	}
	}
  //  CTime  ts1 = CTime::GetCurrentTime();
	if(cross_validation)
	{
        CFile fg;
        CFileException e;
        if( !fg.Open( model_file_name, CFile::modeCreate | CFile::modeWrite, &e ) )
        {
		    MessageBox("交叉核对输出不能备件!");
	        svm_destroy_param(&param);
	        free(prob.y);
	        free(prob.x);
	        free(x_space);
	        info.Empty();
		    return;
        }

		do_cross_validation();
		fg.Write(info ,info.GetLength());//写文本	    fg.Close();	    MessageBox("交叉核对完成");
		ShellExecute(NULL , NULL ,model_file_name,NULL ,NULL , SW_NORMAL);//打开文本
	}
	else
	{
		trainmodel = svm_train(&prob,&param);
		svm_save_model(model_file_name,trainmodel);
		svm_destroy_model(trainmodel); 
		MessageBox(info);
	}
 //   CTimeSpan  ts = CTime::GetCurrentTime()-ts1;
	//CString show;
	//show.Format("训练时间为%d日%d小时%d分钟%d秒",ts.GetDays(),ts.GetHours() ,ts.GetMinutes(),ts.GetSeconds());
	//info+=show;
	svm_destroy_param(&param);
	free(prob.y);
	free(prob.x);
	free(x_space);
	info.Empty();
}

int CwinsvmDlg::read_problem(const char *filename)
{
	int elements, max_index, i, j;
	FILE *fp = fopen(filename,"r");
	
	if(fp == NULL)
	{
		//fprintf(stderr,"can't open input file %s\n",filename);
		CString strs;
		strs.Format("不能打开文件 %s\n",filename);
		MessageBox(strs);
		return 0;
	}

	prob.l = 0;
	elements = 0;
	while(1)
	{
		int c = fgetc(fp);
		switch(c)
		{
			case '\n':
				++prob.l;
				// fall through,
				// count the '-1' element
			case ':':
				++elements;
				break;
			case EOF:
				goto out;
			default:
				;
		}
	}
out:
	rewind(fp);

	prob.y = Malloc(double,prob.l);
	prob.x = Malloc(struct svm_node *,prob.l);
	x_space = Malloc(struct svm_node,elements);

	max_index = 0;
	j=0;
	for(i=0;i<prob.l;i++)
	{
		double label;
		prob.x[i] = &x_space[j];
		fscanf(fp,"%lf",&label);
		prob.y[i] = label;

		while(1)
		{
			int c;
			do {
				c = getc(fp);
				if(c=='\n') goto out2;
			} while(isspace(c));
			ungetc(c,fp);
			if (fscanf(fp,"%d:%lf",&(x_space[j].index),&(x_space[j].value)) < 2)
			{
				//fprintf(stderr,"Wrong input format at line %d\n", i+1);
				//exit(1);
		        CString strs;
		        strs.Format("错误的输入格式在 %d行\n",i+1);
		        MessageBox(strs);
				fclose(fp);
				info.Empty();
	            free(prob.y);
	            free(prob.x);
	            free(x_space);
		        return 0;
			}
			++j;
		}	
out2:
		if(j>=1 && x_space[j-1].index > max_index)
			max_index = x_space[j-1].index;
		x_space[j++].index = -1;
	}

	if(param.gamma == 0)
		param.gamma = 1.0/max_index;

	if(param.kernel_type == PRECOMPUTED)
		for(i=0;i<prob.l;i++)
		{
			if (prob.x[i][0].index != 0)
			{
				//fprintf(stderr,"Wrong input format: first column must be 0:sample_serial_number\n");
				//exit(1);
				MessageBox("错误的输入格式: 样本序列号第一列必须为零\n");
				return 0 ;
			}
			if ((int)prob.x[i][0].value <= 0 || (int)prob.x[i][0].value > max_index)
			{
				//fprintf(stderr,"Wrong input format: sample_serial_number out of range\n");
				//exit(1);
				MessageBox("错误的输入格式: 样本序列号超出范围\n");
				return 0 ;

			}
		}

	fclose(fp);
	return 1;

}

void CwinsvmDlg::do_cross_validation(void)
{
	int i;
	int total_correct = 0;
	double total_error = 0;
	double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0;
	double *target = Malloc(double,prob.l);

	svm_cross_validation(&prob,&param,nr_fold,target);
	if(param.svm_type == EPSILON_SVR ||
	   param.svm_type == NU_SVR)
	{
		for(i=0;i<prob.l;i++)
		{
			double y = prob.y[i];
			double v = target[i];
			total_error += (v-y)*(v-y);
			sumv += v;
			sumy += y;
			sumvv += v*v;
			sumyy += y*y;
			sumvy += v*y;
		}
		//printf("Cross Validation Mean squared error = %g\n",total_error/prob.l);
		//printf("Cross Validation Squared correlation coefficient = %g\n",
		//	((prob.l*sumvy-sumv*sumy)*(prob.l*sumvy-sumv*sumy))/
		//	((prob.l*sumvv-sumv*sumv)*(prob.l*sumyy-sumy*sumy))
		//	);

		CString str;
		str.Format("交叉验证的方差是 %g\n",total_error/prob.l);
		info+=str;//MessageBox(str);
		str.Format("交叉验证的平方相关系数(相似性)是 %g\n",
			((prob.l*sumvy-sumv*sumy)*(prob.l*sumvy-sumv*sumy))/
			((prob.l*sumvv-sumv*sumv)*(prob.l*sumyy-sumy*sumy))
			);
		info+=str;//MessageBox(str);
		//return 1-total_error/prob.l;
	}
	else
	{
		for(i=0;i<prob.l;i++)
		{
			if(target[i] == prob.y[i])
			{
				++total_correct;
			}
		}
		CString str;
		str.Format("交叉验证正确率 = %g%%\n",100.0*total_correct/prob.l);
		info+=str;//MessageBox(str);
		//return total_correct/prob.l;
	}
	free(target);

}

void CwinsvmDlg::OnClose()
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if(IDOK == AfxMessageBox(_T("要退出程序吗?"),MB_OKCANCEL))
	{
	if( m_mythread && GetExitCodeThread( m_mythread->m_hThread, &dwCode ) )
	{
		if( dwCode == STILL_ACTIVE )
		{
			TerminateThread( m_mythread->m_hThread, 0 );
			CloseHandle( m_mythread->m_hThread );
			m_mythread = NULL;
			yichuanend();
		}
	}
		CDHtmlDialog::OnClose();
	}
}


void CwinsvmDlg::OnBnClickedButton4()//暂停
{
	if(m_mythread)
		SuspendThread(m_mythread->m_hThread);
	// TODO: 在此添加控件通知处理程序代码
    GetDlgItem(IDC_BUTTON4)->EnableWindow(false);
	GetDlgItem(IDC_BUTTON5)->EnableWindow();
}

void CwinsvmDlg::OnBnClickedButton5()//继续
{
	if(m_mythread)
		ResumeThread(m_mythread->m_hThread);
	// TODO: 在此添加控件通知处理程序代码
    GetDlgItem(IDC_BUTTON4)->EnableWindow();
	GetDlgItem(IDC_BUTTON5)->EnableWindow(false);
}

void CwinsvmDlg::OnBnClickedButton6()//停止
{
	if( m_mythread && GetExitCodeThread( m_mythread->m_hThread, &dwCode ) )
	{
		if( dwCode == STILL_ACTIVE )
		{
			TerminateThread( m_mythread->m_hThread, 0 );
			CloseHandle( m_mythread->m_hThread );
			m_mythread = NULL;
		}
	}	
	AfxMessageBox( "怎么这么不耐烦!" );/*AfxThrowUserException( );*/
    GetDlgItem(IDC_BUTTON4)->EnableWindow(false);
	GetDlgItem(IDC_BUTTON5)->EnableWindow(false);
    GetDlgItem(IDC_BUTTON6)->EnableWindow(false);
	GetDlgItem(IDC_BUTTON1)->EnableWindow();
	yichuanend();
}

void CwinsvmDlg::OnSize(UINT nType, int cx, int cy)
{
	CDHtmlDialog::OnSize(nType, cx, cy);

	// TODO: 在此处添加消息处理程序代码
	if(nType == SIZE_MINIMIZED)
	{
	   sprintf(m_NotifyIconData.szTip,"winsvm2007");
	   m_NotifyIconData.cbSize = sizeof(NOTIFYICONDATA);
	   m_NotifyIconData.hIcon = m_hIcon;
	   m_NotifyIconData.hWnd =  GetSafeHwnd();
	   m_NotifyIconData.uCallbackMessage = UM_NOTIFY;
	   m_NotifyIconData.uFlags = NIF_MESSAGE|NIF_TIP|NIF_ICON;
	   m_NotifyIconData.uID = IDIC_NOTIFY;
	   Shell_NotifyIcon(NIM_ADD, &m_NotifyIconData);//在状态栏加个图标,接受鼠标消息
	   ShowWindow(SW_HIDE); // 隐藏窗口 
 	}
	else
	{
	//	Shell_NotifyIcon(NIM_DELETE, &m_NotifyIconData);
	}

}

LRESULT CwinsvmDlg::OnMyNotify(WPARAM wParam, LPARAM lParam)//改过
{
	WINDOWPLACEMENT wndPlace;
	GetWindowPlacement(&wndPlace);
	if(lParam == WM_LBUTTONUP)
	{
		WINDOWPLACEMENT wndPlace;
		GetWindowPlacement(&wndPlace);
		if(wndPlace.showCmd == SW_SHOWMINIMIZED)
		{
	      ShowWindow(SW_SHOW);//SW_RESTORE);
	      ShowWindow(SW_RESTORE);
	      SetActiveWindow(); 
		}
		else
		{
			ShowWindow(SW_SHOWMINIMIZED);
		}
	}
	else if(lParam == WM_RBUTTONUP && wndPlace.showCmd == SW_SHOWMINIMIZED)
	{

		CMenu PopMenu;
		PopMenu.CreatePopupMenu();
	    PopMenu.AppendMenu(MF_STRING, IDM_RESTORE,  _T("恢复(&R)"));
	    PopMenu.AppendMenu(MF_STRING, IDOK,      _T("关闭(&C)")); 
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			PopMenu.AppendMenu(MF_SEPARATOR);
			PopMenu.AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
		POINT pt; 
        GetCursorPos(&pt);   
		PopMenu.TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON, pt.x, pt.y, this); 
		//TRACE("%d,%d, %d\n",lParam, HIWORD(wParam), LOWORD(wParam));
	}
	return LRESULT();
}

void CwinsvmDlg::OnMyRestore()
{
	ShowWindow(SW_SHOW); 
	ShowWindow(SW_RESTORE);
	SetActiveWindow();
}

void CwinsvmDlg::OnAbout()
{
	CAboutDlg dlgAbout;
	dlgAbout.DoModal();
}

void CwinsvmDlg::OnOK()
{
	// TODO: 在此添加专用代码和/或调用基类
	if(IDOK == AfxMessageBox(_T("要退出程序吗?"),MB_OKCANCEL))
	{
		CDHtmlDialog::OnOK();
	}
}

//预测
void CwinsvmDlg::OnBnClickedButton2()
{
		//线程运行
	if( m_mythread && GetExitCodeThread( m_mythread->m_hThread, &dwCode ) )
	{
		if( dwCode == STILL_ACTIVE )

⌨️ 快捷键说明

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