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

📄 resultview.cpp

📁 毕业设计论文~~要的来看啊~~~ 很完整的
💻 CPP
字号:
// ResultView.cpp : implementation file
//

#include "stdafx.h"
#include "GA.h"
#include "ResultView.h"
#include "GADlg.h"
//#include "engine.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//extern 	CGADlg dlg;


/////////////////////////////////////////////////////////////////////////////
// CResultView dialog


CResultView::CResultView(CWnd* pParent /*=NULL*/)
	: CDialog(CResultView::IDD, pParent)
{
	//{{AFX_DATA_INIT(CResultView)
		m_testID = 0;
		m_editResRand = 0.0f;
		m_editReDFS = 0.0f;
		m_editResGA = 0.0f;
	// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT

	this->Create(IDD);
	UpdateData(FALSE);

}


void CResultView::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CResultView)
	DDX_Control(pDX, IDC_LIST_VIEW_GA, m_listGA);
	DDX_Control(pDX, IDC_LIST_VIEW_RAND, m_listRand);
	DDX_Control(pDX, IDC_LIST_VIEW_DFS, m_listDFS);
	DDX_Control(pDX, IDC_LIST_VIEW, m_list);
	DDX_Text(pDX, IDC_EDIT_TESTID, m_testID);
	DDX_Text(pDX, IDC_EDIT_RAND, m_editResRand);
	DDX_Text(pDX, IDC_EDIT_DFS, m_editReDFS);
	DDX_Text(pDX, IDC_EDIT_GA, m_editResGA);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CResultView, CDialog)
	//{{AFX_MSG_MAP(CResultView)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton_GO)
	ON_BN_CLICKED(IDC_BUTTON2, OnButtonNext)
	ON_BN_CLICKED(IDC_BUTTON3, OnButtonLast)
	ON_BN_CLICKED(IDC_BUTTON4, OnButtonFirst)
	ON_EN_CHANGE(IDC_EDIT_TESTID, OnChangeEditTestid)
	ON_BN_CLICKED(IDC_BUTTON_SaveToFile, OnBUTTONSaveToFile)
	ON_BN_CLICKED(IDC_BUTTON_COMPARE, OnButtonCompare)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CResultView message handlers

void CResultView::OnButton_GO() 
{
	// TODO: Add your control notification handler code here
	UpdateData(FALSE);
	matlab_paint_sumary();
}

void CResultView::OnButtonNext() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	CGADlg *pGADlg = (CGADlg*)AfxGetApp()->m_pMainWnd;

	if (m_testID + 1 > pGADlg->m_retryTimes)
	{
		AfxMessageBox("have reach last test!!!", MB_OK);
		return;
	}
	++ m_testID;
	UpdateData(FALSE);

	this->OnChangeEditTestid();
}

void CResultView::OnButtonLast() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	CGADlg *pGADlg = (CGADlg*)AfxGetApp()->m_pMainWnd;
	m_testID = pGADlg->m_retryTimes;
	UpdateData(FALSE);

	this->OnChangeEditTestid();
}

void CResultView::OnButtonFirst() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	CGADlg *pGADlg = (CGADlg*)AfxGetApp()->m_pMainWnd;
	m_testID = pGADlg->m_retryTimes ? 1 : 0;
	UpdateData(FALSE);

	this->OnChangeEditTestid();
}

void CResultView::OnChangeEditTestid() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	this->showResultList();
	UpdateData(FALSE);
	
}

void CResultView::showResultList()
{
	
	UpdateData(TRUE);

	CGADlg *pGADlg = (CGADlg*)AfxGetApp()->m_pMainWnd;
	CString str;

	if (m_testID > pGADlg->m_testResSet->testNumber || m_testID <= 0)
	{
	
		str.Format("please input testID : 1 ~ %d", pGADlg->m_testResSet);
		AfxMessageBox(str);
		return ;
	}

	while(m_list.GetCount() > 0)
		m_list.DeleteString(0); // 清空

	str.Format("Gen, bestFitness");
	m_list.AddString((LPCTSTR)str);

	for(int i=0; i<pGADlg->m_testResSet->res_set[m_testID-1].genTimes; i++)
	{
		str.Format("%03d, %f", i+1, pGADlg->m_testResSet->res_set[m_testID-1].res[i].elitist.fitness);
		m_list.AddString((LPCTSTR)str);
	}

	this->update_task_flag();
	this->update_agent_flag();
	this->matlab_paint_one();

}

void CResultView::OnBUTTONSaveToFile() 
{
	// TODO: Add your control notification handler code here
	FILE *fp = fopen("result.txt", "w");
	CString str;
	for (int i=0; i<m_list.GetCount(); i++)
	{
			m_list.GetText(i, str);
			fprintf(fp, "%s\n", (LPCTSTR)str);
	}
	fclose(fp);
}

void CResultView::update_task_flag()
{
	CGADlg *pGADlg = (CGADlg*)AfxGetApp()->m_pMainWnd;
	int nItem = pGADlg->m_taskListCtrl.GetItemCount();

	CString str;
	int i = 0;
	int col;
	
	col = 3 + pGADlg->m_tasksSet[pGADlg->m_selectTS]->m_abilityNum;
	
	for (i=1; i<nItem+1; i++)
	{
		int flag = pGADlg->m_testResSet->res_set[m_testID-1].cs_flag[i];
		str.Format("%d", flag);
		pGADlg->m_taskListCtrl.SetItemText(i-1, col, str);
	}
	pGADlg->m_taskListCtrl.UpdateData(FALSE);
}

void CResultView::update_agent_flag()
{
	CGADlg *pGADlg = (CGADlg*)AfxGetApp()->m_pMainWnd;
	int nItem = pGADlg->m_agentListCtrl.GetItemCount();
	
	CString str;
	int i = 0;
	int col;
	
	col = 2 + pGADlg->m_agentsSet[pGADlg->m_selectAS]->m_abilityNum;

	int bestindex = pGADlg->m_testResSet->res_set[m_testID-1].bestIndex;

	for (i=0; i<nItem; i++)
	{
		int flag = pGADlg->m_testResSet->res_set[m_testID-1].res[bestindex].elitist.chrom[i];
		str.Format("%d", flag);
		pGADlg->m_agentListCtrl.SetItemText(i, col, str);
	}
	pGADlg->m_taskListCtrl.UpdateData(FALSE);
}

void CResultView::matlab_paint_one()
{
		CGADlg *pGADlg = (CGADlg*)AfxGetApp()->m_pMainWnd;
		engine * ep = pGADlg->m_matlabEG;
		int M = pGADlg->m_GenTimes;	
		int i;
		
		double * a = new double[M];
		double * x = new double[M];
	
		
		
		for (i=0; i<M; i++)
		{
			x[i] = i+1;
			a[i] = pGADlg->m_testResSet->res_set[m_testID-1].res[i].elitist.fitness;
		}



		mxArray *xx = mxCreateDoubleMatrix(1,M, mxREAL);
		mxArray *aa = mxCreateDoubleMatrix(1,M, mxREAL);

		
		memcpy(mxGetPr(xx),x,M*sizeof(double)); 
		memcpy(mxGetPr(aa),a,M*sizeof(double)); 

		engPutVariable(ep, "xx",xx);
		engPutVariable(ep, "aa",aa);

		
		engEvalString(ep, "title('遗传进化过程')");
		engEvalString(ep, "xlabel('generation process'),ylabel('bestfitness');hold on");
		engEvalString(ep,"plot(xx, aa ,'b');"); 
		
		mxDestroyArray(xx); // 销毁 mxArray 数组 xx 和 yy 。 
		mxDestroyArray(aa); 
	 
}

void CResultView::matlab_paint_sumary()
{	
		CGADlg *pGADlg = (CGADlg*)AfxGetApp()->m_pMainWnd;
		engine * ep = pGADlg->m_matlabEG;
		int N = pGADlg->m_retryTimes;
		int i;

		double * y = new double[N];
		double * b = new double[N];

		for (i=0; i<N; i++)
		{
			y[i] = i+1;
			int bestindex = pGADlg->m_testResSet->res_set[i].bestIndex;
			b[i] = pGADlg->m_testResSet->res_set[i].res[bestindex].elitist.fitness;
		}

		mxArray *yy = mxCreateDoubleMatrix(1,N, mxREAL);
		mxArray *bb = mxCreateDoubleMatrix(1,N, mxREAL);

		memcpy(mxGetPr(yy),y,N*sizeof(double)); 
		memcpy(mxGetPr(bb),b,N*sizeof(double)); 
	
		engPutVariable(ep, "yy",yy);
		engPutVariable(ep, "bb",bb);

		engEvalString(ep, "figure(2)");
		engEvalString(ep, "title('多次运行的结果')");
		engEvalString(ep, "xlabel('test ID'),ylabel('bestfitness');hold on");
		engEvalString(ep, "plot(yy, bb,'r');");
		mxDestroyArray(bb);
		mxDestroyArray(yy); 
}

void CResultView::OnButtonCompare() 
{
	// TODO: Add your control notification handler code here
	
	CGADlg *pGADlg = (CGADlg*)AfxGetApp()->m_pMainWnd;
	engine * ep = pGADlg->m_matlabEG;
	int N = pGADlg->m_retryTimes;
	int i;
	
	double * y = new double[N];
	double * b = new double[N];
	double * c = new double[N];
	double * d = new double[N];
	
	for (i=0; i<N; i++)
	{
		y[i] = i+1;
		int bestindex = pGADlg->m_testResSet->res_set[i].bestIndex;
		b[i] = pGADlg->m_testResSet->res_set[i].res[bestindex].elitist.fitness;
		c[i] = pGADlg->m_rand_best_fitness;
		d[i] = pGADlg->m_dfs_best_fitness;
	}
	
	mxArray *yy = mxCreateDoubleMatrix(1,N, mxREAL);
	mxArray *bb = mxCreateDoubleMatrix(1,N, mxREAL);
	mxArray *cc = mxCreateDoubleMatrix(1,N, mxREAL);
	mxArray *dd = mxCreateDoubleMatrix(1,N, mxREAL);
	
	memcpy(mxGetPr(yy),y,N*sizeof(double)); 
	memcpy(mxGetPr(bb),b,N*sizeof(double)); 
	memcpy(mxGetPr(cc),c,N*sizeof(double)); 
	memcpy(mxGetPr(dd),d,N*sizeof(double)); 
	
	engPutVariable(ep, "yy",yy);
	engPutVariable(ep, "bb",bb);
	engPutVariable(ep, "cc",cc);
	engPutVariable(ep, "dd",dd);

	
	engEvalString(ep, "figure(3)");
	engEvalString(ep, "title('三种方法结果对比')");
	engEvalString(ep, "xlabel('Test ID'),ylabel('bestFitness');hold on");
	engEvalString(ep, "plot(yy, bb,'b',yy, cc,'r',yy,dd,'y');");
    engEvalString(ep, "legend('GA','RA','DFS',3);");

	mxDestroyArray(bb);
	mxDestroyArray(yy); 
	mxDestroyArray(cc);
	mxDestroyArray(dd); 

}

⌨️ 快捷键说明

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