?? pso.cpp
字號(hào):
// pso.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "PSO.h"
#include <iostream.h>
#include <math.h>
//派生自己的PSO類
class MyPSO : public PSO
{
public:
MyPSO(int d, int n):PSO(d, n){}; //構(gòu)造函數(shù),給出微粒維數(shù)和微粒個(gè)數(shù)
double GetFit(PARTICLE &p) //適合度計(jì)算方法,必須定義
{
//函數(shù):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;
//函數(shù):Rastrigrin
// double f=0;
// for (int i=0;i<10;i++)
// {
// f+=p.X[i]*p.X[i]-10*cos(2*3.1415926*p.X[i])+10;
// }
// return -1*f;
double f=0;
for (int i=0;i<10;i++)
{
f+=p.X[i]*p.X[i]-10*cos(2*3.1415926*p.X[i])+10;
}
return -1*f;
}
};
//定義通訊函數(shù)
bool MyCom(double fit, double *op, double**,int)
{
static long sn=1;
cout<<"\rNo="<<sn++<<"\tFun="<<fit;
for(int i=0; i<2; i++)
cout<<"\tX("<<i<<")="<<op[i];
return true;
}
//申明相關(guān)數(shù)據(jù)
//const int PNum = 20; //微粒個(gè)數(shù)
//const int PDim = 2; //微粒維數(shù)
// double Xup[] = {100, 100}; //自變量上界
// double Xdown[] = {-100, -100}; //自變量下界
const int PNum = 20; //微粒個(gè)數(shù)
const int PDim = 10; //微粒維數(shù)
double Xup[] = {5.12, 5.12, 5.12, 5.12, 5.12, 5.12, 5.12, 5.12, 5.12, 5.12}; //自變量上界
double Xdown[] = {-5.12, -5.12, -5.12, -5.12, -5.12, -5.12, -5.12, -5.12, -5.12, -5.12}; //自變量下界
//主程序
void main()
{
MyPSO pso(PDim, PNum); //生成微粒群實(shí)例
pso.SetXup(Xup); //設(shè)置自變量上界
pso.SetXdown(Xdown); //設(shè)置自變量下界
pso.SetVmax(0.2); //設(shè)置最大速度
pso.SetCom(MyCom); //設(shè)置通訊函數(shù)
cout<<"\nRun Now:\n";
pso.Run(2000); //運(yùn)行微粒群
cout<<"\nThe Result is:\t"<<pso.GetBest(Xup)<<"\n"; //輸出結(jié)果
pso.Run(0.999); //運(yùn)行微粒群
cout<<"\nThe Result is:\t"<<pso.GetBest(Xup)<<"\n"; //輸出結(jié)果
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -