📄 chenpsodlg.cpp
字号:
// ChenPSODlg.cpp : implementation file
//
#include "stdafx.h"
#include "ChenPSO.h"
#include "ChenPSODlg.h"
#include "PSO.h"
#include <math.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//申明相关数据
const int PNum = 20; //微粒个数
const int PDim = 2; //微粒维数
const int n=2000;
double Xup[] = {100, 100}; //自变量上界
double Xdown[] = {-100, -100}; //自变量下界
double op2[2];
static long sn2;
double fit2;
////////////////////////////////////////////////////////////////////////////
//派生自己的PSO类
class MyPSO : public PSO
{
public:
MyPSO(int d, int n,double W,double C1,double C2):PSO(d, n,W,C1,C2){}; //构造函数,给出微粒维数和微粒个数
double GetFit(PARTICLE &p) //适合度计算方法,必须定义
{
//函数:Schaffer's F6
double f6;
f6 = 1+0.001*(p.X[0]*p.X[0]+p.X[1]*p.X[1]);
f6 *= f6;
f6 = 0.5-(sin(sqrt(p.X[0]*p.X[0]+p.X[1]*p.X[1]))*
sin(sqrt(p.X[0]*p.X[0]+p.X[1]*p.X[1]))-0.5)/f6;
return f6;
}
};
//定义通讯函数
bool MyCom(double fit, double *op, double**,int)
{
static long sn=1;
sn2=sn;
fit2=fit;//输出最优值
op2[0]=op[0];//输出最优位置
op2[1]=op[1];
sn++;
return true;
}
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CChenPSODlg dialog
CChenPSODlg::CChenPSODlg(CWnd* pParent /*=NULL*/)
: CDialog(CChenPSODlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CChenPSODlg)
m_w = 1.0;
m_c1 = 2.0;
m_c2 = 2.0;
//}}AFX_DATA_INIT
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CChenPSODlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CChenPSODlg)
DDX_Control(pDX, IDC_EDIT1, m_ew);
DDX_Control(pDX, IDOK, m_ok);
DDX_Control(pDX, IDC_RES, m_res);
DDX_Control(pDX, IDC_OUT, m_out);
DDX_Text(pDX, IDC_EDIT1, m_w);
DDX_Text(pDX, IDC_EDIT2, m_c1);
DDX_Text(pDX, IDC_EDIT3, m_c2);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CChenPSODlg, CDialog)
//{{AFX_MSG_MAP(CChenPSODlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CChenPSODlg message handlers
BOOL CChenPSODlg::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);
}
}
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
m_ew.SetFocus();
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CChenPSODlg::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 CChenPSODlg::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();
}
}
HCURSOR CChenPSODlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CChenPSODlg::OnCancel()
{
// TODO: Add extra cleanup here
CDialog::OnCancel();
}
void CChenPSODlg::OnOK()
{
m_res.SetWindowText(" ");
m_out.SetWindowText("begining to compute......");
m_ok.EnableWindow(false);
UpdateData(true);
double W=m_w,C1=m_c1,C2=m_c2;
MyPSO pso(PDim, PNum,W,C1,C2); //生成微粒群实例
pso.SetXup(Xup); //设置自变量上界
pso.SetXdown(Xdown); //设置自变量下界
pso.SetVmax(0.2); //设置最大速度
pso.SetCom(MyCom); //设置通讯函数
pso.Run(n); //运行微粒群
char output[100]; //输出结果
sprintf(output,"No=%d Fun=%f X(0)=%f X(1)=%f",sn2,fit2,op2[0],op2[1]);
m_out.SetWindowText(output);
double f=pso.GetBest(Xup);
sprintf(output,"The Result is: %f",f);
m_res.SetWindowText(output);
m_ok.EnableWindow(true);
m_ok.SetWindowText("继续计算");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -