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

📄 epdlg.cpp

📁 一个小生境进化算法的程序
💻 CPP
字号:
// EPDlg.cpp : implementation file
//

#include "stdafx.h"
#include "EP.h"
#include "EPDlg.h"
#include "math.h"
#include "time.h"

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

double pi = 3.1415926535897;
/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CEPDlg dialog

CEPDlg::CEPDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CEPDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CEPDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CEPDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CEPDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CEPDlg, CDialog)
	//{{AFX_MSG_MAP(CEPDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_EPSelect, OnEPSelect)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEPDlg message handlers

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

void CEPDlg::CreatRand(LONG num, double *tempRand)
{
	int i = 0;
	if(bTime)
	{
		srand( (unsigned)time( NULL ) );//设置伪随机数发生器的种子
		bTime = false;
	}
	for( i = 0; i < num; i++)
		*(tempRand + i) = (double)rand();
}

void CEPDlg::InitializeGen()
{
	N = 30;
	maxgen =  30;
	lemata = 0.95;
	L = 0.00001;
	T0 = 0.2;
	bTime = true;

	int i;
	double *tempP = new double[N];
	CreatRand(N,tempP);

	for(i = 0; i < N; i++)//个体数
	{
		//生成初始个体
		oldpop[i].x1 = 0.0 + fmod(*(tempP + i),200.0)/200.0;//[0,1]

		//计算各个体的适应值
		oldpop[i].fitness = CalculateFitness(oldpop[i].x1);
		newpop[i] = oldpop[i];
	}

	delete tempP;
}

double CEPDlg::CalculateFitness(double x)
{
	double result = 0;

	result = fabs(sin(30.0*x))*(1 - 0.5*x);

	return(result);
}

//变异产生新的N个个体
void CEPDlg::Mutation( )
{
	int i;
	double pk;
	double *tempP = new double[1];
	double x;

	for(i = 0; i < N; i++)//个体数
	{
		CreatRand(1,tempP);
		pk = -pi/2.0 + fmod(*tempP,pi*50.0)/50.0;//[-pi/2.0, pi/2.0]
		newpop[N + i].x1 = oldpop[i].x1 + 1.0*T0*pow(lemata,gen - 1)*tan(pk);
		x = newpop[N + i].x1;
		//首先确定x,y是否在定义域,如不在,则需用镜像原则将其修正
		while(x < 0 || x > 1){
			if(x < 0) x = fmod(fabs(x),1);
			else x = 1 - fmod(x,1);
		}
		newpop[N + i].x1 = x;

		newpop[N + i].fitness = CalculateFitness(x);
	}

	delete tempP;
}

//用距离函数使个体分配区间
void CEPDlg::Assign()
{
	int i,j;
	double dis;
	for(i = 0; i < 2*N -1; i++)//个体数
	for(j = i + 1; j < 2*N; j++)//个体数
	{
		dis = fabs(newpop[i].x1 - newpop[j].x1);
		if(dis < L)//相似
		{
			if(newpop[i].fitness > newpop[j].fitness)	newpop[i].fitness = 0.0;
			else	newpop[j].fitness = 0.0;
		}
	}

}

//选择
void CEPDlg::Select()
{
	individual tempInd;
	int i,j;
	//按适应值优劣降序排列
	for(j = 0; j < 2*N; j++)
		for(i = j + 1; i < 2*N; i++)
		{
			if(newpop[j].fitness < newpop[i].fitness)//本例是求最大值
			{
				tempInd = newpop[j];
				newpop[j] = newpop[i];
				newpop[i] = tempInd;
			}
		}
	//筛掉适应值最差的后N个个体后放入oldpop中
	for(i = 0; i < N; i++)
	{
		oldpop[i] = newpop[i] ;
	}
}

void CEPDlg::OnEPSelect() 
{
	int i;
	InitializeGen();
	gen = 1;
	do{
		Mutation();
		Assign();
		Select();
		gen += 1;
	}while(gen < maxgen);

	//记录最后结果
	CString tempFileName = "Result_onevar.txt";
	FILE *EPFile = fopen(tempFileName, "w" );
	for(i = 0; i < N; i++)
	{
		fprintf(EPFile, "%3.7f\t%3.7f\n",oldpop[i].x1,oldpop[i].fitness);
	}
	fclose(EPFile);

}

⌨️ 快捷键说明

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