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

📄 modelmanagedlg.cpp

📁 OpenSVM was developped under Visual C++ 6.0 SP6, You can open the workspace file(*.dsw) in the ope
💻 CPP
字号:
// ModelManageDlg.cpp : implementation file
//

#include "stdafx.h"
#include "OpenSVM.h"
#include "ModelManageDlg.h"


#include "MainFrm.h"
#include "OpenSVMDoc.h"
#include "ResultDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CModelManageDlg dialog


CModelManageDlg::CModelManageDlg(CWnd* pParent)
	: CDialog(CModelManageDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CModelManageDlg)
	m_paramInfo = _T("");
	m_modelInfo = _T("");
	//}}AFX_DATA_INIT


//////////////////////////////////////////////////////////////////////////
// other init
	modelBasepath=AppPath;
	modelBasepath+="\\Models\\modelsbase";

}


void CModelManageDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CModelManageDlg)
	DDX_Text(pDX, IDC_PARAMINFO, m_paramInfo);
	DDX_Text(pDX, IDC_MODELINFO, m_modelInfo);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CModelManageDlg, CDialog)
	//{{AFX_MSG_MAP(CModelManageDlg)
	ON_CBN_SELCHANGE(IDC_MODELS, OnSelchangeModels)
	ON_BN_CLICKED(IDC_PROBABILITY, OnProbability)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CModelManageDlg message handlers





void CModelManageDlg::OnOK() 
{
	// TODO: Add extra validation here
	UpdateData(FALSE);
	FILE *fp = fopen("C:\\tempView.tmp","w");
	fflush(fp);
	fclose(fp);
	
	freopen("C:\\tempView.tmp","a",stdout);

	CMainFrame  *pFrame=(CMainFrame *)AfxGetMainWnd();   
	COpenSVMDoc *pDoc  =(COpenSVMDoc*)pFrame->GetActiveDocument();
	CString inputname=pDoc->GetPathName();

	FILE *input, *output;
	if ((inputname.GetLength()==0)||(index < 0))
	{
		MessageBox("You must open a test file or select a model.","Error", 
			MB_ICONERROR | MB_OK);
	}
	else
	{
		input = fopen(inputname,"r");
		if(input == NULL)
		{
			MessageBox("can't open input file.");
		}
		
		output = fopen("C:\\Predict.out","w");
		if(output == NULL)
		{
			MessageBox("can't open output file.");
		}
		
		CString modelPath=AppPath;
		modelPath+="\\Models\\";
		modelPath+=modelName;
		modelPath+=".model";
		
		if((model=svm_load_model(modelPath))==0)
		{
			MessageBox("can't open model file.");
		}
		
		
		line = (char *) malloc(max_line_len*sizeof(char));
		x = (struct svm_node *) malloc(max_nr_attr*sizeof(struct svm_node));
		
		if(predict_probability)
			if(svm_check_probability_model(model)==0)
			{
				MessageBox("Model does not support probabiliy estimates\n");
			}
		predict(input,output);
		svm_destroy_model(model);
		free(line);
		free(x);
			
		fclose(input);
		fclose(output);
			
		printf("\n\n*** The predict result was saved in C:\\Predict.out ***");
		fclose(stdout);
		CResultDlg result;
		result.DoModal();
		
	}
	CDialog::OnOK();
}


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

	// TODO: Add extra initialization here
	FILE *fp=fopen(modelBasepath,"r");
	if (fp == NULL)
	{
		CString modelDir=AppPath;
		modelDir+="\\Models";
		CreateDirectory(modelDir,NULL);
		fp= fopen(modelBasepath,"w");
	}
	fclose(fp);


	UpdateData(FALSE);
 
	CFile datafile(modelBasepath,CFile::modeRead);
	CArchive loadArchive(&datafile,CArchive::load);
	CString TextLine;
	
	UINT nr_line = 1;
	char c;
	for (UINT i = 0;i<(datafile.GetLength());i++)
	{
		loadArchive >> c;
		if (c == '\n') nr_line++;
	}
	datafile.SeekToBegin();
	
	for(i=0;i<nr_line-1;i++)
	{
		loadArchive.ReadString(TextLine);
		((CComboBox*)GetDlgItem(IDC_MODELS))->AddString(TextLine);
	}
 	loadArchive.Close();





	//((CComboBox*)GetDlgItem(IDC_MODELS))->SetCurSel(0);
 	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
 }



void CModelManageDlg::OnSelchangeModels() 
{
	// TODO: Add your control notification handler code here

	((CButton*)GetDlgItem(IDC_PROBABILITY))->EnableWindow(false);
	CString TextLine;
	int nr_line = 1;
	char c;
	index = -1;
	index = ((CComboBox*)GetDlgItem(IDC_MODELS))->GetCurSel();

	//get model name
	CFile datafile(modelBasepath,CFile::modeRead);
	CArchive loadArchive(&datafile,CArchive::load);

	for(int i =0;i<=index;i++)
	{
		loadArchive.ReadString(modelName);
	}
 	loadArchive.Close();



	//show info file 
	CString infoPath=AppPath;
	infoPath+="\\Models\\";
	infoPath+=modelName;
	infoPath+=".info";
	CFile infofile(infoPath,CFile::modeRead);
	CArchive infoArchive(&infofile,CArchive::load);
	m_paramInfo="";
	for (i = 0;i<(int)(infofile.GetLength());i++)
	{
		infoArchive >> c;
		if (c == '\n') nr_line++;
	}
	infofile.SeekToBegin();
	
	for(i =0;i<nr_line;i++)
	{
		infoArchive.ReadString(TextLine);
		if(TextLine =="DidProbability")
		{
			((CButton*)GetDlgItem(IDC_PROBABILITY))->EnableWindow(true);
		}
		TextLine+="\r\n";
		m_paramInfo+=TextLine;
	}
	infoArchive.Close();

	//show model info
	CString modelPath=AppPath;
	modelPath+="\\Models\\";
	modelPath+=modelName;
	modelPath+=".model";
	CFile modelfile(modelPath,CFile::modeRead);
	CArchive modelArchive(&modelfile,CArchive::load);
	m_modelInfo="";
	nr_line = 1;
	for (i = 0;i<(int)(modelfile.GetLength());i++)
	{
		modelArchive >> c;
		if (c == '\n') nr_line++;
	}
	modelfile.SeekToBegin();
	
	for(i =0;i<nr_line;i++)
	{
		modelArchive.ReadString(TextLine);
		TextLine+="\r\n";
		m_modelInfo+=TextLine;
	}
	modelArchive.Close();

	UpdateData(FALSE);
}


//////////////////////////////////////////////////////////////////////////
// function predict from libsvm is followed
/*
Copyright (c) 2000-2007 Chih-Chung Chang and Chih-Jen Lin
All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  
	1. Redistributions of source code must retain the above copyright
	notice, this list of conditions and the following disclaimer.
	
	2. Redistributions in binary form must reproduce the above copyright
	notice, this list of conditions and the following disclaimer in the
	documentation and/or other materials provided with the distribution.
	  
	3. Neither name of copyright holders nor the names of its contributors
	may be used to endorse or promote products derived from this software
	without specific prior written permission.
		
		  
	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
	``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
	LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
	A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR
	CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
	PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
	PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
	LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
	NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
	SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


*/
void CModelManageDlg::predict(FILE *input, FILE *output)
{
	int correct = 0;
	int total = 0;
	double error = 0;
	double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0;
	
	int svm_type=svm_get_svm_type(model);
	int nr_class=svm_get_nr_class(model);
	int *labels=(int *) malloc(nr_class*sizeof(int));
	double *prob_estimates=NULL;
	int j;
	
	if(predict_probability)
	{
		if (svm_type==NU_SVR || svm_type==EPSILON_SVR)
			printf("Prob. model for test data: target value = predicted value + z,\nz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma=%g\n",svm_get_svr_probability(model));
		else
		{
			svm_get_labels(model,labels);
			prob_estimates = (double *) malloc(nr_class*sizeof(double));
			fprintf(output,"labels");		
			for(j=0;j<nr_class;j++)
				fprintf(output," %d",labels[j]);
			fprintf(output,"\n");
		}
	}
	while(1)
	{
		int i = 0;
		int c;
		double target,v;
		
		if (fscanf(input,"%lf",&target)==EOF)
			break;
		
		while(1)
		{
			if(i>=max_nr_attr-1)	// need one more for index = -1
			{
				max_nr_attr *= 2;
				x = (struct svm_node *) realloc(x,max_nr_attr*sizeof(struct svm_node));
			}
			
			do {
				c = getc(input);
				if(c=='\n' || c==EOF) goto out2;
			} while(isspace(c));
			ungetc(c,input);
			if (fscanf(input,"%d:%lf",&x[i].index,&x[i].value) < 2)
			{
				fprintf(stderr,"Wrong input format at line %d\n", total+1);
				exit(1);
			}
			++i;
		}	
		
out2:
		x[i++].index = -1;
		
		if (predict_probability && (svm_type==C_SVC || svm_type==NU_SVC))
		{
			v = svm_predict_probability(model,x,prob_estimates);
			fprintf(output,"%g ",v);
			for(j=0;j<nr_class;j++)
				fprintf(output,"%g ",prob_estimates[j]);
			fprintf(output,"\n");
		}
		else
		{
			v = svm_predict(model,x);
			fprintf(output,"%g\n",v);
		}
		
		if(v == target)
			++correct;
		error += (v-target)*(v-target);
		sumv += v;
		sumy += target;
		sumvv += v*v;
		sumyy += target*target;
		sumvy += v*target;
		++total;
	}
	if (svm_type==NU_SVR || svm_type==EPSILON_SVR)
	{
		printf("Mean squared error = %g (regression)\n",error/total);
		printf("Squared correlation coefficient = %g (regression)\n",
			((total*sumvy-sumv*sumy)*(total*sumvy-sumv*sumy))/
			((total*sumvv-sumv*sumv)*(total*sumyy-sumy*sumy))
			);
	}
	else
		printf("Accuracy = %g%% (%d/%d) (classification)\n",
		(double)correct/total*100,correct,total);
	if(predict_probability)
	{
		free(prob_estimates);
		free(labels);
	}
}
// function predict ended here
//////////////////////////////////////////////////////////////////////////

void CModelManageDlg::OnProbability() 
{
	// TODO: Add your control notification handler code here
	predict_probability = !predict_probability;
}

⌨️ 快捷键说明

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