📄 epdlg.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 = 50;
maxgen = 204;
lemata = 0.95;
L = 0.025;
T0 = 0.25;
bTime = true;
int i;
double *tempP = new double[2*N];
CreatRand(2*N,tempP);
for(i = 0; i < N; i++)//个体数
{
//生成初始个体
oldpop[i].course = -10.0 + fmod(*(tempP + 2*i),200.0)/10.0;//[-10,10]
oldpop[i].velocity = -10.0 + fmod(*(tempP + 2*i + 1),200.0)/10.0;//[-10,10]
//计算各个体的适应值
oldpop[i].fitness = CalculateFitness(oldpop[i].course,oldpop[i].velocity);
newpop[i] = oldpop[i];
}
delete tempP;
}
double CEPDlg::CalculateFitness(double x, double y)
{
double result,sum1,sum2;
int i;
sum1 = sum2 = result = 0;
for(i = 1; i <= 5; i++)
{
sum1 += i*cos((i + 1)*x + i);
}
for(i = 1; i <= 5; i++)
{
sum2 += i*cos((i + 1)*y + i);
}
result = sum1*sum2;//+0.0*((pow(x + 1.42513,2.0) + y + 0.80032));
// result = (-123.577 - sum1*sum2)/((pow(x + 1.42513,2.0) + y + 0.80032));
return(result);
}
//变异产生新的N个个体
void CEPDlg::Mutation( )
{
int i;
double pk;
double *tempP = new double[2];
double x,y;
for(i = 0; i < N; i++)//个体数
{
CreatRand(2,tempP);
pk = -pi/2.0 + fmod(*tempP,pi*50.0)/50.0;//[-pi/2.0, pi/2.0]
newpop[N + i].course = oldpop[i].course + 20.0*T0*pow(lemata,gen - 1)*tan(pk);
x = newpop[N + i].course;
//首先确定x,y是否在定义域,如不在,则需用镜像原则将其修正
while(x < -10 || x > 10){
if(x < -10) x = -20 - x;
else x = 20 - x;
}
newpop[N + i].course = x;
pk = -pi/2.0 + fmod(*(tempP + 1),pi*50.0)/50.0;//[-pi/2.0, pi/2.0]
newpop[N + i].velocity = oldpop[i].velocity + 20.0*T0*pow(lemata,gen - 1)*tan(pk);
y = newpop[N + i].velocity;
while(y < -10 || y > 10){
if(y < -10) y = -20 - y;
else y = 20 - y;
}
newpop[N + i].velocity = y;
newpop[N + i].fitness = CalculateFitness(x,y);
}
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 = sqrt(pow((newpop[i].course - newpop[j].course)/20.0,2.0) + pow((newpop[i].velocity - newpop[j].velocity)/20.0,2.0));
if(dis < L)//相似
{
if(newpop[i].fitness > newpop[j].fitness) newpop[i].fitness = 1000.0;
else newpop[j].fitness = 1000.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;
}
}
//筛掉适应值最差的后10个个体后放入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 = "EP2Result.txt";
FILE *EPFile = fopen(tempFileName, "w" );
for(i = 0; i < N; i++)
{
fprintf(EPFile, "%3.6f\t%3.6f\t%3.6f\n",oldpop[i].course,oldpop[i].velocity,oldpop[i].fitness);
}
fclose(EPFile);
// double tempx[20] = {-7.08355,-7.08348,5.48288,-7.70838,-0.80023,-1.42512,5.48282,-7.70825,4.85815,4.858085,-0.80022,-0.80035,1.42502,-7.70833,-7.08332,-1.42525,4.85813,5.48298,5.48283,-7.08359};
// double tempy[20] = {-7.70824,-1.42514,4.85804,-0.80020,4.85817,-7.08342,-1.42521,5.48302,-7.08345,-0.80021,-1.42508,-7.70818,5.48276,-7.08340,4.85808,-0.80023,5.48269,-7.70815,-6.47861,-6.47852};
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -