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

📄 smodlg.cpp

📁 对支持向量机svm算法的改进算法
💻 CPP
字号:
// smoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "smo.h"
#include "smoDlg.h"
#include "mysmo.h"
#include <fstream.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()

/////////////////////////////////////////////////////////////////////////////
// CSmoDlg dialog

CSmoDlg::CSmoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSmoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSmoDlg)
	m_dimension = 19;
	m_testFileName = _T("");
	m_testNum = 198;
	m_trainFileName = _T("");
	m_trainNum = 463;
	
	m_C = 1000.0f;
	m_eps = 0.001f;
	m_sigma = 2.0f;
	m_tolerance = 0.001f;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSmoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSmoDlg)
	DDX_Text(pDX, IDC_EDITDIMENSION, m_dimension);
	DDV_MinMaxUInt(pDX, m_dimension, 1, 65536);
	DDX_Text(pDX, IDC_EDITTESTFILENAME, m_testFileName);
	DDX_Text(pDX, IDC_EDITTESTNUM, m_testNum);
	DDV_MinMaxUInt(pDX, m_testNum, 1, 4294967295);
	DDX_Text(pDX, IDC_EDITTRAINFILENAME, m_trainFileName);
	DDX_Text(pDX, IDC_EDITTRAINNUM, m_trainNum);
	DDV_MinMaxUInt(pDX, m_trainNum, 1, 4294967295);
	DDX_Text(pDX, IDC_EDITC, m_C);
	DDV_MinMaxFloat(pDX, m_C, 1.f, 4.29497e+009f);
	DDX_Text(pDX, IDC_EDITEPS, m_eps);
	DDV_MinMaxFloat(pDX, m_eps, 0.f, 1.f);
	DDX_Text(pDX, IDC_EDITSIGMA, m_sigma);
	DDV_MinMaxFloat(pDX, m_sigma, 0.f, 10.f);
	DDX_Text(pDX, IDC_EDITTOLERANCE, m_tolerance);
	DDV_MinMaxFloat(pDX, m_tolerance, 0.f, 1.f);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSmoDlg, CDialog)
	//{{AFX_MSG_MAP(CSmoDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTONPATH, OnButtonpath)
	ON_BN_CLICKED(IDC_BUTTONPATH2, OnButtonpath2)
	ON_BN_CLICKED(IDC_SCALE, OnScale)
	ON_BN_CLICKED(IDC_SKIP, OnSkip)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSmoDlg message handlers

BOOL CSmoDlg::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
	((CComboBox*)GetDlgItem(IDC_COMBO1))->SetCurSel(0);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CSmoDlg::OnButtonpath() 
{
	// TODO: Add your control notification handler code here
	CFileDialog filedlg(TRUE,_T("data"),NULL,NULL,_T("Ini file(*.data)|*.data|All file(*.*)|*.*||"));
	if(filedlg.DoModal()==IDOK)
		SetDlgItemText(IDC_EDITTRAINFILENAME,filedlg.GetPathName());
	GetDlgItem(IDC_EDITTRAINFILENAME)->SetFocus();
	((CEdit *)GetDlgItem(IDC_EDITTRAINFILENAME))->SetSel(0,-1,true);
}

void CSmoDlg::OnButtonpath2() 
{
	// TODO: Add your control notification handler code here
	CFileDialog filedlg(TRUE,_T("data"),NULL,NULL,_T("Ini file(*.data)|*.data|All file(*.*)|*.*||"));
	if(filedlg.DoModal()==IDOK)
		SetDlgItemText(IDC_EDITTESTFILENAME,filedlg.GetPathName());
	GetDlgItem(IDC_EDITTESTFILENAME)->SetFocus();
	((CEdit *)GetDlgItem(IDC_EDITTESTFILENAME))->SetSel(0,-1,true);
}

void CSmoDlg::OnOK() 
{
	// TODO: Add extra validation here
	UpdateData(TRUE);
	srand((unsigned)time(NULL));
	if(m_trainFileName == "")
	{
		AfxMessageBox("No Train File Name!");
		return;
	}
	char fileName[200];
	mysmo smo(m_C,m_tolerance,m_eps);
	clock_t pastTime,start,finish;
	start = clock();
	if(smo.readPoint((char *)(const char *)m_trainFileName,m_trainNum,m_dimension))
	{
		AfxMessageBox("Train File read error!");
		return;
	}	
	smo.createSmo();
	//Write results to out-svm.data
	smo.getFileName(fileName,(char *)(const char *)m_trainFileName);
	strcat(fileName,".a");
	smo.LMToFile(fileName);	
	ofstream fout;
	if(m_testFileName != "")
	{
		smo.testing((char *)(const char *)m_testFileName,m_testNum);
		smo.getFileName(fileName,(char *)(const char *)m_testFileName);
		strcat(fileName,".result");		
		fout.open(fileName,ios::app);
	}
	else
	{
		AfxMessageBox("Train success, no testing example!");
		return;
	}
	finish = clock();
	pastTime = finish - start;
	SetDlgItemInt(IDC_EDITTIMEUSING,pastTime);
	if(fout.is_open())
	{
		fout<<"time: "<<pastTime<<"ms"<<endl;
		fout.close();
	}	
	CString str;
	str.Format("%.2f",smo.precision);
	AfxMessageBox("success,precision "+str);
}


void CSmoDlg::OnScale() 
{
	// TODO: Add extra validation here
	UpdateData(TRUE);
	mysmo smo(m_C,m_tolerance,m_eps);
	if(m_trainFileName != "")
	{		
		if(smo.scale((char *)(const char *)m_trainFileName,m_trainNum,m_dimension))
		{
			AfxMessageBox("Train File read error!");
			return;
		}
	}
	if(m_testFileName != "")
	{
		smo.freeCsmo();
		if(smo.scale((char *)(const char *)m_testFileName,m_testNum,m_dimension))
		{
			AfxMessageBox("Test File read error!");
			return;
		}
	}
	AfxMessageBox("success");
}

void CSmoDlg::OnSkip() 
{
	UpdateData(TRUE);
	mysmo smo(m_C,m_tolerance,m_eps);
	if(m_trainFileName != "")
	{		
		if(smo.skip((char *)(const char *)m_trainFileName,m_trainNum,m_dimension))
		{
			AfxMessageBox("Train File read error!");
			return;
		}
	}
	if(m_testFileName != "")
	{
		smo.freeCsmo();
		if(smo.skip((char *)(const char *)m_testFileName,m_testNum,m_dimension))
		{
			AfxMessageBox("Test File read error!");
			return;
		}
	}
	AfxMessageBox("success");	
}

⌨️ 快捷键说明

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