📄 cpso.txt
字号:
Math_Function.h
#ifndef Math_Function_H
#define Math_Function_H
#include iostream
#include vector
using namespace std;
#include cmath
class Multidimension
{
public
Multidimension();
Multidimension(int n);
Multidimension(const Multidimension & y);
~Multidimension();
bool reset_dimension(int n);
Multidimension operator+ (const Multidimension & x) const;
Multidimension operator- (const Multidimension & x) const;
Multidimension operator (float number) const;
friend Multidimension operator (float number, const Multidimension & x)
{return x number;}
Multidimension & operator= (const Multidimension & y);
friend ostream & operator (ostream & os, const Multidimension & x);
int N;维数
float x;
};
class Math_Function
{
public
Math_Function();
~Math_Function();
float f(const Multidimension & x);
Multidimension get_random_x(int n = DIMENSION);
void adjust_x(Multidimension & x);
Multidimension get_random_v(int n = DIMENSION);
void adjust_v(Multidimension & v);
int rdint(int n);
float rdft();
int rnd(int a, int b);
const static int DIMENSION = 2;二元函数
Multidimension solution;
private
float VMAX;
float XMIN;
float XMAX;
};
#endif
Math_Function.cpp
#include Math_Function.h
Math_FunctionMultidimension
Math_FunctionMath_Function()
{
VMAX = 2.0;
XMIN = -2.0;函数自变量定义域最小值
XMAX = 2.0;函数自变量定义域最大值
solution = get_random_x();随机初始化一个可行解,在PSO初始化种群中视作Gbest
}
Math_Function~Math_Function()
{
}
float Math_Functionf(const Multidimension & x)
{
float x1 = x.x[0];
float x2 = x.x[1];
return float(pow(x1+0.0, 2.0) + pow(x2+0.0, 2.0));
Goldstein-Price函数
float s = 1.0 + pow(x1+x2+1.0,2.0)
(19.0 - 14.0x1 + 3.0pow(x1,2) - 14.0x2 + 6.0x1x2 + 3.0pow(x2,2));
float h = 30.0 + pow(2.0x1-3.0x2,2)
(18.0 - 32.0x1 + 12.0pow(x1,2) + 48.0x2 - 36.0x1x2 + 27.0pow(x2,2));
return s h;
}
Multidimension Math_Functionget_random_x(int n)
{
Multidimension x(n);
for (int i = 0; i x.N; i++)
{
x.x[i] = rdft()(XMAX-XMIN) + XMIN;
}
return x;
}
void Math_Functionadjust_x(Multidimension & x)
{
for (int i = 0; i x.N; i++)
{
if (x.x[i] XMIN)
{
x.x[i] = 2XMIN - x.x[i];以XMIN对称回来
}
if (x.x[i] XMAX)
{
x.x[i] = 2XMAX - x.x[i];以XMAX对称回来
}
}
}
Multidimension Math_Functionget_random_v(int n)
{
Multidimension x(n);
for (int i = 0; i x.N; i++)
{
x.x[i] = rdft() VMAX;
}
return x;
}
void Math_Functionadjust_v(Multidimension & v)
{
for (int i = 0; i v.N; i++)
{
if (v.x[i] VMAX)
{
v.x[i] = VMAX;
}
}
}
int Math_Functionrdint(int n)
{
return rand() % n;
}
float Math_Functionrdft()
{
return float(double(rdint(16384)16383.0));
}
int Math_Functionrnd(int a, int b)
{
return rdint(b - a + 1) + int(a);
}
Math_FunctionMultidimension
MultidimensionMultidimension()Math_Function
{
N = 2;缺省为2维
x = new float[N];
}
MultidimensionMultidimension(int n)
{
if (n 1)
{
N = n;
x = new float[N];
}
}
MultidimensionMultidimension(const Multidimension & y)
{
N = y.N;
x = new float[N];
for (int i = 0; i N; i++)
{
x[i] = y.x[i];
}
}
Multidimension~Multidimension()
{
delete [] x;
}
bool Multidimensionreset_dimension(int n)
{
bool success = false;
if (n 1)
{
delete [] x;
N = n;
x = new float[N];
success = true;
}
return success;
}
Multidimension Multidimensionoperator+ (const Multidimension & y) const
{
Multidimension sum;
for (int i = 0; i N; i++)
{
sum.x[i] = x[i] + y.x[i];
}
return sum;
}
Multidimension Multidimensionoperator- (const Multidimension & y) const
{
Multidimension diff;
for (int i = 0; i N; i++)
{
diff.x[i] = x[i] - y.x[i];
}
return diff;
}
Multidimension Multidimensionoperator (float number) const
{
Multidimension result;
for (int i = 0; i N; i++)
{
result.x[i] = x[i] number;
}
return result;
}
Multidimension & Multidimensionoperator=(const Multidimension & y)
{
if (this == &y)
{
return this;
}
delete [] x;
N = y.N;
x = new float[N];
for (int i = 0; i N; i++)
{
x[i] = y.x[i];
}
}
ostream & operator(ostream & os, const Multidimension & x)
{
os [ ;
for (int i = 0; i x.N; i++)
{
os x.x[i] ;
}
os ];
return os;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -