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

📄 testgadlg.cpp

📁 本程序为遗传算法示范程序(visaul c++ 源程序)
💻 CPP
字号:
// TestGADlg.cpp : implementation file
//

#include "stdafx.h"
#include "TestGA.h"
#include "TestGADlg.h"

#include "GeneGroup.h"
#include <time.h>
#include <conio.h>

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

extern pp * oldpop;
extern pp * newpop;
extern pp * p1;

extern unsigned int gen,
			 nmutation,
			 ncross,
			 jcross,
			 maxpp,
			 minpp,
			 jrand;
extern float sumfitness,
			 avg,
			 max,
			 min,
			 seed,
			 rj[MAX_POP],
			 oldrand[MAX_POP];
extern double coef;

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



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

/////////////////////////////////////////////////////////////////////////////
// CTestGADlg dialog

CTestGADlg::CTestGADlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTestGADlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTestGADlg)
	m_nPopSize = 30;
	m_nChromLen = 10;
	m_nMaxGen = 30;
	m_fPCross = 0.1f;
	m_fPMutation = 0.02f;
	m_strOutput = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTestGADlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTestGADlg)
	DDX_Text(pDX, IDC_POP_SIZE, m_nPopSize);
	DDX_Text(pDX, IDC_CHROM_LENGTH, m_nChromLen);
	DDX_Text(pDX, IDC_MAX_GENERATION, m_nMaxGen);
	DDX_Text(pDX, IDC_CROSSOVER_PROB, m_fPCross);
	DDX_Text(pDX, IDC_MUTATION_PROB, m_fPMutation);
	DDX_Text(pDX, IDC_OUTPUT, m_strOutput);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTestGADlg, CDialog)
	//{{AFX_MSG_MAP(CTestGADlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_START, OnStart)
	ON_BN_CLICKED(IDC_OK, OnOk)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestGADlg message handlers

BOOL CTestGADlg::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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

//1999-05-21,鲍捷,开始计算
void CTestGADlg::OnStart() 
{
	CGeneGroup group;

	long gen = 0;//世代
	long k,j;
	float oldmax;
	int oldmaxpp;

	oldpop = new pp[MAX_POP];
	if(!oldpop) {AfxMessageBox("内存分配错误");return;}
	
	newpop = new pp[MAX_POP];
	if(!newpop) {AfxMessageBox("内存分配错误");return;}
	
	p1 = new pp[MAX_POP];
	if(!p1) {AfxMessageBox("内存分配错误");return;}

	for( k = 0 ; k < MAX_POP ; k++)
		oldpop[k].chrom[0] = newpop[k].chrom[0] = '\0';

	UpdateData(TRUE);//获取输入参数
	group.m_fPCross = m_fPCross ;
	group.m_fPMutation = m_fPMutation;
	group.m_nChromLen = m_nChromLen;
	group.m_nMaxGen = m_nMaxGen;
	group.m_nPopSize = m_nPopSize;

	srand( (unsigned)time( NULL ) );//设置随机数种子

	p1 = newpop;
	newpop = oldpop;
	group.Statistics(newpop);
	Report(gen);
	newpop=p1;
	getch();
	do{
		gen++;
		oldmax = max;
		oldmaxpp = maxpp;
		group.Generation();
		group.Statistics(newpop);
		if( max < oldmax)
		{
			for( j =0 ; j < m_nChromLen ; j++)
				newpop[minpp].chrom[j] = oldpop[oldmaxpp].chrom[j];
			newpop[minpp].x = oldpop[oldmaxpp].x;
			newpop[minpp].fitness = oldpop[oldmaxpp].fitness;
			group.Statistics(newpop);
		}
		Report(gen);
		p1 = oldpop;
		oldpop = newpop;
		newpop = p1;
		getch();
	}while(gen<m_nMaxGen);

//	delete p1;
//	delete oldpop;
//	delete newpop;

}

//1999-05-21,鲍捷,报告
void CTestGADlg::Report(int gen)
{
	CString strTemp;
	m_strOutput.Empty();
	
	
	strTemp.Format("世代:%3d\n",gen);
	m_strOutput+=strTemp;

	for(int j = 0 ; j < m_nPopSize ; j++ )
	{
		strTemp.Format("%2d ) ( %2d,",j,newpop[j].parent1);
		m_strOutput+=strTemp;
		
		strTemp.Format("%2d ) ( %2d",newpop[j].parent2,newpop[j].xsite);
		m_strOutput+=strTemp;
	
		for(int k = 0 ; k < m_nChromLen ; k++)
		{
			strTemp.Format("%d ",newpop[j].chrom[k]);
			m_strOutput+=strTemp;
		}
		
		strTemp.Format("%12.1f",newpop[j].x);
		m_strOutput+=strTemp;

		strTemp.Format("%6.4f -New. \n",newpop[j].fitness);
		m_strOutput+=strTemp;
	}
	
	strTemp.Format("\nAVG= %8.4f MIN = %8.4f MAX=%8.4f\n",avg,min,max);
	m_strOutput+=strTemp;
	strTemp.Format("ncross = %4d nmutation = %4d\n",ncross,nmutation);
	m_strOutput+=strTemp;
	
	UpdateData(FALSE);
}

void CTestGADlg::OnOk() 
{
	OnOK();	
}

⌨️ 快捷键说明

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