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

📄 gadlg.cpp

📁 这是一个利用遗传算法求解函数极值
💻 CPP
字号:
// GADlg.cpp : implementation file
//

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

#include "GA1.h"
#include "ParaSetDlg.h"

#include <time.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()

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

CGADlg::CGADlg(CWnd* pParent /*=NULL*/)
	: CDialog(CGADlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CGADlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_pcross=90;
	m_pmutation=5;
	m_maxgen=150;
	m_precision=4;
	m_popsize=100;
	m_nFuncType=1;
}

void CGADlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGADlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CGADlg, CDialog)
	//{{AFX_MSG_MAP(CGADlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_PARAMSET, OnParamSet)
	ON_BN_CLICKED(IDC_CALCULATE, OnCalculate)
	ON_CBN_SELCHANGE(IDC_COMBO1, OnSelchangeCombo1)
	//}}AFX_MSG_MAP
	ON_MESSAGE(UM_DRAWFUNCST,OnDrawFuncSt)
	ON_MESSAGE(UM_DRAWPARAMST,OnDrawParamSt)
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
    ((CComboBox*)GetDlgItem(IDC_COMBO1))->AddString("第1个函数");
	((CComboBox*)GetDlgItem(IDC_COMBO1))->AddString("第2个函数");
	((CComboBox*)GetDlgItem(IDC_COMBO1))->AddString("第3个函数");
	((CComboBox*)GetDlgItem(IDC_COMBO1))->AddString("第11个函数");
	((CComboBox*)GetDlgItem(IDC_COMBO1))->AddString("第12个函数");

	((CComboBox*)GetDlgItem(IDC_COMBO1))->SetCurSel(0);

	CString str;
	str.Format("交叉概率:%d%%  变异概率:%d%%\n\n种群规模:%d  自变量精确到小数点后 %d 位\n\n最大计算代数:%d",
		    m_pcross,m_pmutation,m_popsize,m_precision,m_maxgen);
    ((CStatic*)GetDlgItem(IDC_PARAMSTATIC))->SetWindowText(str);

	m_nFuncType=((CComboBox*)GetDlgItem(IDC_COMBO1))->GetCurSel()+1;
	str.Format("f(x1,x2)=x1*x1+x2*x2+x3*x3   -5.12<=xi<=5.12");
    ((CStatic*)GetDlgItem(IDC_FUNCSTATIC))->SetWindowText(str);

	SetWindowText( "GA-佳点集交叉" );
	
	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::OnParamSet() 
{
	// TODO: Add your control notification handler code here
	CParaSetDlg dlg;

	dlg.m_maxgen=m_maxgen;
	dlg.m_pcross=m_pcross;
	dlg.m_pmutation=m_pmutation;
	dlg.m_popsize=m_popsize;
	dlg.m_precision=m_precision;
	if(IDOK==dlg.DoModal())
	{
		m_maxgen=dlg.m_maxgen;
		m_pcross=dlg.m_pcross;
		m_pmutation=dlg.m_pmutation;
		m_popsize=dlg.m_popsize;
		m_precision=dlg.m_precision;
	}
	PostMessage(UM_DRAWPARAMST);
}

void CGADlg::OnCalculate() 
{
	// TODO: Add your control notification handler code here
//	int index=((CComboBox*)GetDlgItem(IDC_COMBO1))->GetCurSel();

	time_t tBefore,tAfter;

	((CStatic*)GetDlgItem(IDC_CALCULATESTATIC))->SetWindowText(""); 

	CGA gA((float(m_pcross)/100),(float(m_pmutation)/100),
		        m_popsize,m_precision,m_nFuncType,m_maxgen);

	tBefore = time(NULL);

	gA.GA();

	tAfter = time(NULL);
	CTimeSpan tUse(tAfter-tBefore);
	CString str=tUse.Format("\n\n计算用时:%H:%M:%S" );
	
	gA.m_strResult+=str;
	((CStatic*)GetDlgItem(IDC_CALCULATESTATIC))->SetWindowText(gA.m_strResult);  
}

void CGADlg::OnSelchangeCombo1() 
{
	// TODO: Add your control notification handler code here
	PostMessage(UM_DRAWFUNCST);
}

afx_msg LRESULT CGADlg::OnDrawFuncSt(WPARAM   wParam,   LPARAM   lParam)
{
	CString str;
	m_nFuncType=((CComboBox*)GetDlgItem(IDC_COMBO1))->GetCurSel()+1;
	switch(m_nFuncType)
	{
	case 1:
		str.Format("f(x1,x2)=x1*x1+x2*x2+x3*x3   -5.12<=xi<=5.12");
		break;
	case 2:
		str.Format("f(x1,x2,x3)=100(x1*x1-x2)(x1*x1-x2)+(1-x1)(1-x1)  -2.048<=xi<=2.048");
		break;
	case 3:
		str.Format("f(x1,x2,x3,x4,x5)=integer(x1)+integer(x2)+integer(x3)+integer(x4)+integer(x5);  -5.12<=xi<=5.12");
		break;
	case 4:
		break;
	case 5:
		str.Format("k3{sin(2k4x1)+Σ(xi-k5)2[1+k6sin2(2k4xi+1)]+(xn-k5)2[1+k6sin2(2k7xn)])");
		break;
	}
    ((CStatic*)GetDlgItem(IDC_FUNCSTATIC))->SetWindowText(str);
	return TRUE;
}

afx_msg LRESULT CGADlg::OnDrawParamSt(WPARAM   wParam,   LPARAM   lParam)
{
	CString str;
	str.Format("交叉概率:%d%%  变异概率:%d%%\n\n种群规模:%d  自变量精确到小数点后 %d 位\n\n最大计算代数:%d",
		    m_pcross,m_pmutation,m_popsize,m_precision,m_maxgen);
    ((CStatic*)GetDlgItem(IDC_PARAMSTATIC))->SetWindowText(str);
	return TRUE;
}

⌨️ 快捷键说明

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