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

📄 gadlg.cpp

📁 毕业设计论文~~要的来看啊~~~ 很完整的
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// GADlg.cpp : implementation file
//

#include "stdafx.h"
#include "GA.h"
#include "GADlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

#include "Tasks.h"
#include "Agents.h"
#include "stdio.h"


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()

/////////////////////////////////////////////////////////////////////////////
// CGADlg dialog

CGADlg::CGADlg(CWnd* pParent /*=NULL*/)
	: CDialog(CGADlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CGADlg)
	m_abilityNum = 0;
	m_agentNum = 0;
	m_GenTimes = 0;
	m_popSize = 0;
	m_retryTimes = 0;
	m_taskNum = 0;
	m_radio1 = -1;
	m_crossP = 0.0f;
	m_mutationP = 0.0f;
	m_randTimes = 1000;
	m_timeDFS = 3.0f;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CGADlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGADlg)
	DDX_Control(pDX, IDC_COMBO_ParamSet, m_comboParam);
	DDX_Control(pDX, IDC_COMBO_TASK, m_comboTask);
	DDX_Control(pDX, IDC_COMBO_AGENT, m_comboAgent);
	DDX_Text(pDX, IDC_EDIT_ABILITY_NUM, m_abilityNum);
	DDX_Text(pDX, IDC_EDIT_AGNET, m_agentNum);
	DDX_Text(pDX, IDC_EDIT_Gen, m_GenTimes);
	DDX_Text(pDX, IDC_EDIT_POPSIZE, m_popSize);
	DDX_Text(pDX, IDC_EDIT_RETRY, m_retryTimes);
	DDX_Text(pDX, IDC_EDIT_TASK, m_taskNum);
	DDX_Radio(pDX, IDC_RADIO1, m_radio1);
	DDX_Text(pDX, IDC_EDIT_CROSS, m_crossP);
	DDX_Text(pDX, IDC_EDIT_MUTATION, m_mutationP);
	DDX_Text(pDX, IDC_EDIT_RANDTIMES, m_randTimes);
	DDX_Text(pDX, IDC_EDIT_TIMELIM, m_timeDFS);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CGADlg, CDialog)
	//{{AFX_MSG_MAP(CGADlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_BN_CLICKED(IDC_BUTTON_EXCUTE, OnButtonExcute)
	ON_CBN_SELENDOK(IDC_COMBO_TASK, OnSelendokComboTask)
	ON_BN_CLICKED(IDC_RADIO1, OnRadio1)
	ON_BN_CLICKED(IDC_RADIO2, OnRadio2)
	ON_BN_CLICKED(IDC_RADIO3, OnRadio3)
	ON_BN_CLICKED(IDC_BUTTON_TASKVIEW, OnButtonTaskView)
	ON_BN_CLICKED(IDC_BUTTON_AGENTVIEW, OnButtonAgentView)
	ON_CBN_SELENDOK(IDC_COMBO_AGENT, OnSelendokComboAgent)
	ON_CBN_SELENDOK(IDC_COMBO_ParamSet, OnSelendokCOMBOParamSet)
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGADlg message handlers

BOOL CGADlg::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
	m_genetic = NULL;
	m_testResSet = NULL;
	m_selectAS = 0;
	m_selectTS = 0;
	m_selectPS = 0;

	this->init_tasks_set();
	this->init_agents_set();
	this->init_param_set();

	m_taskListCtrl.Create(
		WS_CHILD|WS_VISIBLE|WS_BORDER|LVS_REPORT,
		CRect(0,0,400,200), (CWnd *)&m_taskListView, 1);
	this->update_task_list_ctrl();


	m_agentListCtrl.Create(
		WS_CHILD|WS_VISIBLE|WS_BORDER|LVS_REPORT,
		CRect(10,10,600,500), (CWnd *)&m_agentListView, 1);
	this->update_agent_list_ctrl();

	RECT rect;
	m_ResultView.GetWindowRect(&rect);
	rect.left -= 100;
	rect.top -=  200;
	rect.right -= 100;
	rect.bottom -= 200;
	m_ResultView.MoveWindow(&rect);

	this->update_param_view();

	m_radio1 = 0;

	int cnt = 5;
	while(cnt-- && !(m_matlabEG=engOpen(NULL))) ;// 测试是否启动 Matlab 引擎成功。
	if (cnt < 0)
	{
		AfxMessageBox("Matlab 引擎失败");
	}

	m_rand_arr = NULL;
	m_rand_cs_flag = NULL;
	m_dfs_arr = NULL;
	m_dfs_cs_flag = NULL;

	m_rand_best_fitness = 0.0f;
	m_dfs_best_fitness = 0.0f;

	this->GetWindowText(m_strTitle);

	UpdateData(FALSE);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CGADlg::OnButtonExcute() 
{
	// TODO: Add your control notification handler code here

	switch (m_radio1)
	{
		case 0:
				this->excute_GA();
				AfxMessageBox("GA 算法执行完毕!");
				break;
		case 1: 
				this->excute_RA();
				AfxMessageBox("RA 算法执行完毕!");
				break;
		case 2: 
				this->excute_DFS();
				AfxMessageBox("DFS 算法执行完毕!");
			
				break;
		default:
				break;
	}

	this->viewResult();
	this->m_ResultView.ShowWindow(TRUE);
	this->m_ResultView.UpdateData(FALSE);

}

void CGADlg::init_tasks_set()
{
	m_tasksSet.clear();
	m_tasksSet.push_back(new CTasks(RAND_TASK_NUM, RAND_ABLIT_NUM, RAND_MIN_ABLT, RAND_MAX_ABLT)); //默认随机任务列表
	m_mapTaskSet["默认随机"] = 1;

	char filename[] = "task_list.txt";
	FILE *fp = fopen(filename, "r");
	if(fp==NULL) {
		AfxMessageBox(CString("open ") + filename + "failed");
		return;
	}
	char str[256];
	int cnt = 1;
	while (true)
	{
		if(fscanf(fp, "%s", str)==EOF) break;
		if(!strcmp(str, "end")) break;

		if(!strncmp(str, "test", 4))
		{
			m_tasksSet.push_back(new CTasks);
			m_iterTS = m_tasksSet.end() -1;
			char testID[256];
			fgets(testID, 255, fp);
			m_comboTask.AddString(testID);
			m_mapTaskSet[testID] = ++cnt;
		}
		if(!strncmp(str,"number", 6))
		{
			fscanf(fp, "%d", &(*m_iterTS)->m_number); 
		}
		if (!strncmp(str, "ability_num", 11))
		{
			fscanf(fp, "%d", &(*m_iterTS)->m_abilityNum); 
		}
		if (!strncmp(str, "table", 5))
		{
			(*m_iterTS)->m_taskList = new task[(*m_iterTS)->m_number+1];
			fgets(str, 255, fp);
			for(int i=1; i<(*m_iterTS)->m_number+1; i++)
			{
				for (int j=0; j<(*m_iterTS)->m_abilityNum; j++)
				{
					fscanf(fp, "%d", &(*m_iterTS)->m_taskList[i].need_ability[j]);
				}
				fscanf(fp, "%d", &(*m_iterTS)->m_taskList[i].profit);
				fscanf(fp, "%d", &(*m_iterTS)->m_taskList[i].time);
				(*m_iterTS)->m_taskList[i].flag = 0;
			}
		}

	}

	fclose(fp);

}


void CGADlg::init_agents_set()
{
	m_agentsSet.clear();
	m_agentsSet.push_back(new CAgents(RAND_AGENT_NUM, RAND_ABLIT_NUM, RAND_MIN_ABLT, RAND_MAX_ABLT)); //默认随机任务列表
	m_mapAgentSet["默认随机"] = 1;
	
	char filename[] = "agent_list.txt";
	FILE *fp = fopen(filename, "r");
	if(fp==NULL) {
		AfxMessageBox(CString("open ") + filename + "failed");
		return;
	}
	char str[256];
	int cnt = 1;
	while (true)
	{
		if(fscanf(fp, "%s", str)==EOF) break;
		if(!strcmp(str, "end")) break;
		
		if(!strncmp(str, "test", 4))
		{
			m_agentsSet.push_back(new CAgents);
			m_iterAS = m_agentsSet.end() -1;
			char testID[256];
			fgets(testID, 255, fp);
			m_comboAgent.AddString(testID);
			m_mapAgentSet[testID] = ++cnt;
		}
		if(!strncmp(str,"number", 6))
		{
			fscanf(fp, "%d", &(*m_iterAS)->m_number); 
		}
		if (!strncmp(str, "ability_num", 11))
		{
			fscanf(fp, "%d", &(*m_iterAS)->m_abilityNum); 
		}
		if (!strncmp(str, "ability_min", 11))
		{
			fscanf(fp, "%d", &(*m_iterAS)->m_abilityMin); 
		}
		if (!strncmp(str, "ability_max", 11))
		{
			fscanf(fp, "%d", &(*m_iterAS)->m_abilityMax); 
		}
		if (!strncmp(str, "table", 5))
		{
			(*m_iterAS)->m_agentList = new agent[(*m_iterAS)->m_number];
			fgets(str, 255, fp);
			for(int i=0; i<(*m_iterAS)->m_number; i++)
			{
				for (int j=0; j<(*m_iterAS)->m_abilityNum; j++)
				{
					fscanf(fp, "%d", &(*m_iterAS)->m_agentList[i].ability[j]);
				}
				fscanf(fp, "%d", &(*m_iterAS)->m_agentList[i].cost);
				(*m_iterAS)->m_agentList[i].flag = -1;
			}
		}
		
	}
	
	fclose(fp);

}

void CGADlg::OnSelendokComboTask() 
{
	// TODO: Add your control notification handler code here
	
	CString str;
	m_comboTask.GetLBText(m_comboTask.GetCurSel(), str);
	m_selectTS = m_mapTaskSet.find(str)->second - 1;

	this->update_task_list_ctrl();
	UpdateData(FALSE);
}


void CGADlg::OnRadio1() 
{
	// TODO: Add your control notification handler code here
	m_radio1 = 0;
}

void CGADlg::OnRadio2() 
{
	// TODO: Add your control notification handler code here
	m_radio1 = 1;
}

void CGADlg::OnRadio3() 
{
	// TODO: Add your control notification handler code here
	m_radio1 = 2;
	
}

void CGADlg::OnButtonTaskView() 
{
	m_taskListView.ShowWindow(true);

}

void CGADlg::update_task_list_ctrl()
{
	int i;
	m_taskListCtrl.DeleteAllItems();
	while(m_taskListCtrl.DeleteColumn(0)); 

	m_taskListCtrl.InsertColumn(0, "Task", LVCFMT_CENTER, 50);
	int nAB = m_tasksSet[m_selectTS]->m_abilityNum;
	CString str;
	for (i=0; i<nAB; i++)
	{
	
		str.Format("NdAb%d", i+1);
		m_taskListCtrl.InsertColumn(i+1, str, LVCFMT_CENTER, 50);
	}
	m_taskListCtrl.InsertColumn(++i, "profit", LVCFMT_CENTER, 50);

	m_taskListCtrl.InsertColumn(++i, "time", LVCFMT_CENTER, 50);

	m_taskListCtrl.InsertColumn(++i, "flag", LVCFMT_CENTER, 50);

	int nTask = m_tasksSet[m_selectTS]->m_number;
	for(i=1; i< nTask+1; i++)
	{
		str.Format("%d", i);
		m_taskListCtrl.InsertItem(i-1, str);
		for (int j=0; j<nAB; j++)
		{
			str.Format("%d", m_tasksSet[m_selectTS]->m_taskList[i].need_ability[j]);
			m_taskListCtrl.SetItemText(i-1, j+1, str);
		}
		str.Format("%d", m_tasksSet[m_selectTS]->m_taskList[i].profit);
		m_taskListCtrl.SetItemText(i-1, ++j, str);

		str.Format("%d", m_tasksSet[m_selectTS]->m_taskList[i].time);
		m_taskListCtrl.SetItemText(i-1, ++j, str);
		
		str.Format("%d", m_tasksSet[m_selectTS]->m_taskList[i].flag);
		m_taskListCtrl.SetItemText(i-1, ++j, str);
	}

	RECT rect;
	rect.left = 0;
	rect.top = 0;
	rect.right = rect.left + 50*(1+nAB+3) + 50;
	if(nTask <= 0) rect.bottom = rect.top + 200 + 50;
	else rect.bottom = rect.top + 15*(nTask+1) + 50;
	m_taskListView.MoveWindow(&rect);
	
	rect.left =10;
	rect.top = 10;
	rect.right = rect.left + 50*(1+nAB+3) ;
	if(nTask <= 0) rect.bottom = rect.top + 200 ;
	else rect.bottom = rect.top + 15*(nTask+1);

⌨️ 快捷键说明

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