?? 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 = maxpop;
maxgen = 100000;
lemata = 0.8;
L = 0.001;
T0 = 0.3;
bTime = true;
int i,j;
double *tempP = new double[N*30];
CreatRand(N*30,tempP);
for(i = 0; i < N; i++)//個體數
{
//生成初始個體
for(j = 0; j < 30; j++)//
oldpop[i].x[j] = int(fmod(*(tempP + i*30 + j),200.0)/200.0 + 0.5);//[0,1]
//計算各個體的適應值
oldpop[i].fitness = CalculateFitness(oldpop[i].x);
newpop[i] = oldpop[i];
}
delete tempP;
}
double CEPDlg::CalculateFitness(int* x)
{
double result = 0;
int i,j,s = 0;
for(i = 0; i < 5; i++)
{
s = 0;
for(j = 0; j < 6; j++)
{
s += *(x + 6*i + j);
}
result += CalculateU(s);
}
return(result);
}
//變異產生新的N個個體
void CEPDlg::Mutation( )
{
int i,j;
double pk;
double *tempP = new double[N];
double x;
for(i = 0; i < N; i++)//個體數
{
CreatRand(N,tempP);
for(j = 0; j < 30; j++)//
{
pk = -pi/2.0 + fmod(*(tempP + j),pi*50.0)/50.0;//[-pi/2.0, pi/2.0]
x = oldpop[i].x[j] + 1.0*T0*lemata*tan(pk);//pow(lemata,gen - 1)*tan(pk);
// x = oldpop[i].x[j] + 1.0*T0*pow(lemata,gen - 1)*tan(pk);
//首先確定x,y是否在定義域,如不在,則需用鏡像原則將其修正
while(x < 0 || x > 1){
if(x < 0) x = fmod(fabs(x),1);
else x = 1 - fmod(x,1);
}
newpop[N + i].x[j] = int(x + 0.5);
}
newpop[N + i].fitness = CalculateFitness(newpop[N + i].x);
}
delete tempP;
}
//用距離函數使個體分配區間
void CEPDlg::Assign()
{
int i,j,k;
double dis;
for(i = 0; i < 2*N -1; i++)//個體數
for(j = i + 1; j < 2*N; j++)//個體數
{
dis = 0;
for(k = 0; k < 30; k++)//
{
dis += pow((int(0.5 + newpop[i].x[k]) - int(0.5 + newpop[j].x[k])),2.0);
}
dis = sqrt(dis);
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,kk= 1001;
int success=0;
// for(kk = 1001; kk <= 1001; kk++)
// {
char buffer[8];
FILE *EPFile;
sprintf(buffer, "%d.txt", kk);
CString tempFileName = "Result_30var";
tempFileName = tempFileName + buffer;
EPFile = fopen(tempFileName, "w" );
fclose(EPFile);
InitializeGen();
gen = 1;
do{
Mutation();
Assign();
Select();
gen += 1;
if(success >= 8) T0 = 0.25;
if(success >= 18) T0 = 0.2;
if(success > 23) T0 = 0.1;
if(oldpop[success].fitness > 4.7){
//記錄最后結果
EPFile = fopen(tempFileName, "a+" );
fprintf(EPFile, "N=%d\tmaxgen=%d\tlemata=%3.6f\tL=%3.6f\tT0=%3.6f\n",N,gen,lemata,L,T0);
fprintf(EPFile, "%d\t%d\n",success,gen);
success = success + 1;
fclose(EPFile);
}
}while(newpop[31].fitness < 4.7);//gen < maxgen ||
EPFile = fopen(tempFileName, "a+" );
for(i = 0; i < N; i++)
{
if(oldpop[i].fitness > 4.7)
{
for(int j = 0; j < 30; j++)
fprintf(EPFile, "%d ",int(0.5 + oldpop[i].x[j]));
fprintf(EPFile, "%3.6f\n",oldpop[i].fitness);
}
}
fclose(EPFile);
// }
}
double CEPDlg::CalculateU(int s)
{
double us;
switch (s)
{
case 3:
us = 0.640576;
break;
case 2:
case 4:
us = 0.360384;
break;
case 1:
case 5:
us = 0;
break;
case 0:
case 6:
us = 1;
break;
}
return(us);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -