⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pso.h

📁 看见这里面关于微粒群的东西太少了 所以上传点相关的东西来
💻 H
字号:
//PSO头文件PSO.hpp如下:

//群微粒算法:本算法用群微粒算法求目标函数的最大值
//本算法使用步骤
// (1)派生自己的群微粒类,类中必须定义double GetFit(PARTICLE&)方法,用来计算每个微粒的适合度
// (2)生成派生类实例,并在构造函数中指明微粒坐标维数和群体个数
// (2)设置微粒坐标上界数组和下界数组,并用SetXup与SetXdown设置微粒坐标上下界
// (3)用SetVmax方法设置微粒最大速度
// (4)设置可选参数:C1,C2,W和通讯函数
// (5)采用Run方法进行优化运算,优化后用GetBest方法获得最优个体适合度和坐标

#ifndef __PSO_H
#define __PSO_H

//微粒类
class PARTICLE
{
public:
	double *X; //微粒的坐标数组
	double *V; //微粒的速度数组
	double *XBest; //微粒的最好位置数组
	int Dim; //微粒的维数
	double Fit; //微粒适合度
	double FitBest; //微粒最好位置适合度
	
	//构造函数
	PARTICLE(); //空构造函数
	PARTICLE(int n); //维数为参数的构造函数
	//析构函数
	~PARTICLE();
	
	void SetDim(int d); //设置微粒的维数
};

//定义群粒子类
class PSO
{
protected:
	PARTICLE *Particle; //微粒群数组
	int PNum; //微粒个数
	int GBestIndex; //最好微粒索引
	double W; //惯性权重
	double C1; //加速度系数1---局部系数
	double C2; //加速度系数2---全局系数
	double *Xup; //微粒坐标上界数组
	double *Xdown; //微粒坐标下界数组
	double *Vmax; //微粒最大速度数组
	
	void Initialize(int Seed); //初始化群体
	void CalFit(); //计算全体适合度
	virtual void ParticleFly(); //微粒飞翔,产生新一代微粒

	//通讯函数,返回值为false时,系统停止优化
	bool (*Com)( //最优微粒适合度
		//最优微粒坐标数组
		PARTICLE*, //所有微粒坐标指针数组
		int); //当前最优微粒索引
public:
	//构造函数
	PSO(); //空构造函数
	PSO(int dim, //微粒维数
		int num); //微粒个数
	//析构函数
	~PSO();
	void SetXup(double*); //设置微粒坐标上界
	void SetXdown(double*); //设置微粒坐标下界
	void SetVmax(double*); //设置微粒最大速度,以数组为参数
	void SetVmax(double,double *); //设置微粒最大速度,以上下界百分比为参数
	void SetW(double w){W=w;}; //设置权重
	void SetC1(double c){C1=c;};//设置C1
	void SetC2(double c){C2=c;};//设置C2
	void SetCom(void *p){Com=(bool(*)(PARTICLE*,int))p;};//设置通讯函数
	
	//计算特定微粒坐标所对应适合度,必须由派生的实际PSO类定义,以便计算适合度
	virtual double GetFit(PARTICLE&)=0;
	
	//运行类进行优化
	PARTICLE& Run(int max,int seed); //按最多次数限制运行PSO
	PARTICLE& Run(double fit,int seed,int n); //按最佳适合度目标运行PSO
	double GetBest(double*); //获得最优微粒适合度和坐标
};

#endif
//PSO源文件PSO.CPP如下:

//群微粒算法:本算法涌群微粒算法求目标函数的最大值
//本算法使用步骤
// (1)派生自己的群微粒类,类中必须定义double GetFit(PARTICLE&)方法,用来计算每个微粒的适合度
// (2)生成派生类实例,并在构造函数中指明微粒坐标维数和群体个数
// (2)设置微粒坐标上界数组和下界数组,并用SetXup与SetXdown设置微粒坐标上下界
// (3)用SetVmax方法设置微粒最大速度
// (4)设置可选参数:C1,C2,W和通讯函数
// (5)采用Run方法进行优化运算,优化后用GetBest方法获得最优个体适合度和坐标

#include <stdlib.h>
#include <time.h>
#include <math.h>
//#include "PSO.H"

//微粒构造函数
PARTICLE::PARTICLE() //空构造函数
{
	X = 0; V = 0; XBest = 0; Dim = 0;
}
PARTICLE::PARTICLE(int n) //维数为参数的构造函数
{
	Dim = n;
	X = new double[Dim];
	V = new double[Dim];
	XBest = new double[Dim];
}

//微粒析构函数
PARTICLE::~PARTICLE()
{
	if(Dim)
	{
		delete []X;
		delete []V;
		delete []XBest;
	}
}

//设置微粒的维数
void PARTICLE::SetDim(int d)
{
	if(X) delete []X;
	if(V) delete []V;
	if(XBest) delete []XBest;
	Dim = d;
	X = new double[Dim];
	V = new double[Dim];
	XBest = new double[Dim];
}

//PSO构造函数
PSO::PSO()
{
	Particle = 0;
	PNum = 0;
	GBestIndex = 0;
	Xup = 0;
	Xdown = 0;
	W = 1;
	C1 = 2;
	C2 = 2;
	Com = 0;
}
PSO::PSO(int dim, int num)
{
	Particle = new PARTICLE[num];
	for(int i=0; i<num; i++) Particle[i].SetDim(dim);
	PNum = num;
	GBestIndex = 0;
//	Xup = new double[dim];
//	Xdown = new double[dim];
//	Vmax = new double[dim];
	W = 1;
	C1 = 2;
	C2 = 2;
	Com = 0;
}

//析构函数
PSO::~PSO()
{
	if(Particle) delete []Particle;
//	if(Xup) delete []Xup;
//	if(Xdown) delete []Xdown;
//	if(Vmax) delete []Vmax;
}
//设置坐标上界
void PSO::SetXup(double *up)
{
	if(!Particle) return;
	//for(int i=0; i<Particle[0].Dim; i++)
		Xup = up;
}
//设置坐标下界
void PSO::SetXdown(double *d)
{
	if(!Particle) return;
//	for(int i=0; i<Particle[0].Dim;i++)
		Xdown = d;
}
//设置最大速度
void PSO::SetVmax(double *max)
{
	if(!Particle) return;
//	for(int i=0; i<Particle[0].Dim;i++)
		Vmax = max;
}
void PSO::SetVmax(double p,double *max)
{
	if(!Particle) return;
	Vmax = max;
	for(int i=0; i<Particle[0].Dim; i++)
		Vmax[i] = (Xup[i]-Xdown[i])*p;
}

//初始化群体
void PSO::Initialize(int Seed)
{
	if(!Particle) return;
//	static int kk=(unsigned)time(NULL);
//	srand((unsigned)time(NULL)+kk++);
    if(Seed)
		srand(Seed);
	GBestIndex = 0;
	for(int i=0; i<PNum; i++)
	{
		for(int j=0; j<Particle[i].Dim; j++)
		{
			Particle[i].X[j] = rand()/(double)RAND_MAX*(Xup[j]-Xdown[j])+Xdown[j];//初始化坐标
			Particle[i].XBest[j] = Particle[i].X[j];
			Particle[i].V[j] = rand()/(double)RAND_MAX*Vmax[j]-Vmax[j]/2;//初始化速度
		}
		Particle[i].Fit = GetFit(Particle[i]); //计算该微粒适合度
		Particle[i].FitBest = Particle[i].Fit; //设最优适合度初值
		if(Particle[i].Fit>Particle[GBestIndex].Fit) GBestIndex = i;//查找群体最优微粒
	}
}

//计算群体各个微粒适合度
void PSO::CalFit()
{
	if(!Particle) return;
	for(int i=0; i<PNum; i++)
		Particle[i].Fit = GetFit(Particle[i]);
}

//微粒飞翔,产生新一代微粒
void PSO::ParticleFly()
{
//	static double FitBak[100];//?????100????PNum
	if(!Particle) return;
	//整个群体飞向新的位置
	int i,j;
	for( i=0; i<PNum; i++)
	{
		for(j=0; j<Particle[i].Dim; j++){
			Particle[i].V[j] = W*Particle[i].V[j]+// (FitBak[i]-Particle[i].Fit)+//修改速度
			rand()/(double)RAND_MAX*C1*(Particle[i].XBest[j]-Particle[i].X[j])+
	//wo:RAND_MAX为最大随机数
			rand()/(double)RAND_MAX*C2*(Particle[GBestIndex].XBest[j]-Particle[i].X[j]);
		//for(j=0; j<Particle[i].Dim; j++) //检查速度最大值
		//{
			if(Particle[i].V[j]>Vmax[j]) Particle[i].V[j] = Vmax[j];
			if(Particle[i].V[j]<-Vmax[j]) Particle[i].V[j] = -Vmax[j];
		/*}
		for(j=0; j<Particle[i].Dim; j++)
		{*/
			Particle[i].X[j] += Particle[i].V[j]; //修改坐标
			if(Particle[i].X[j]>Xup[j]) Particle[i].X[j]=Xup[j];//保护
			if(Particle[i].X[j]<Xdown[j]) Particle[i].X[j]=Xdown[j];
		}
//	}
		Particle[i].Fit = GetFit(Particle[i]);//计算各微粒适合度
		//	CalFit();
		//	for(i=0; i<PNum; i++) FitBak[i] = Particle[i].Fit;
		//设置新的个体最好位置
		//for(i=0; i<PNum; i++)
		if(Particle[i].Fit>=Particle[i].FitBest)
		{
			Particle[i].FitBest = Particle[i].Fit;
			for(int j=0; j<Particle[i].Dim; j++)
				Particle[i].XBest[j] = Particle[i].X[j];
		}
		//设置新的最优个体
		//for(i=0; i<PNum; i++)
		if(Particle[i].FitBest>=Particle[GBestIndex].FitBest && i!=GBestIndex) GBestIndex = i;
	}
}

//运行群粒算法
PARTICLE& PSO::Run(int n,int seed)
{
	Initialize(seed);
	for(int i=0; i<n; i++)
	{	
		W = (W-0.4) * (n - i)/n +0.4; 
		ParticleFly();
		if(Com) //通讯函数存在,完成通讯
		{
			if(!Com(Particle,GBestIndex)) break;
		} 
	}
	return Particle[GBestIndex];
}
PARTICLE& PSO::Run(double fit,int seed,int n)
{
	double iter = 0;
	int M = 100*n;
	Initialize(seed);
	do
	{
		W = (W-0.4) * (1 - iter/(iter+M)) +0.4; 
		iter++;
		for(int i=0; i<n; i++)
			ParticleFly();
		if(Com) //通讯函数存在,完成通讯
		{
			if(!Com(Particle,GBestIndex)) break;
		}
	}while(Particle[GBestIndex].FitBest<fit);
	return Particle[GBestIndex];
}

//返回最佳个体
double PSO::GetBest(double *r)
{
	for(int i=0; i<Particle[GBestIndex].Dim; i++)
		r[i] = Particle[GBestIndex].XBest[i];
	return Particle[GBestIndex].FitBest;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -