c语言 pso粒子群优化算法源代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
/*
* =====================================================================================
*
* Filename: particle.c
*
* Description:
*
* Version: 1.0
* Created: 2012年03月17日 15时27分13秒
* Revision: none
* Compiler: gcc
*
* Author: MaZheng (/mazheng1989), mazheng19891019@
* Company: Dalian University Of Technology
*
* =====================================================================================
*/
//粒子群PSO算法
#include
#include
#include
#include
#define PI 3.141592653589 /* */
#define P_num 200 //粒子数目
#define dim 50
#define low -100 //搜索域范围
#define high 100
#define iter_num 1000
#define V_max 20 //速度范围
#define c1 2
#define c2 2
#define w 0.5
#define alp 1
double particle[P_num][dim]; //个体集合
double particle_loc_best[P_num][dim]; //每个个体局部最优向量
double particle_loc_fit[P_num]; //个体的局部最优适应度,有局部最优向量计算而来
double particle_glo_best[dim]; //全局最优向量
double gfit; //全局最优适应度,有全局最优向量计算而来
double particle_v[P_num][dim]; //记录每个个体的当前代速度向量
double particle_fit[P_num]; //记录每个粒子的当前代适应度
double Sphere(double a[])
{
int i;
double sum=0.0;
for(i=0; i
sum+=a[i]*a[i];
}
return sum;
}
double Rosenbrock(double a[])
{
int i;
double sum=0.0;
for(i=0;i
sum+= 100*(a[i+1]-a[i]*a[i])*(a[i+1]-a[i]*a[i])+(a[i]-1)*(a[i]-1);
}
return sum;
}
double Rastrigin(double a[])
{
int i;
double sum=0.0;
for(i=0;i
sum+=a[i]*a[i]-10.0*cos(2*PI*a[i])+10.0;
}
return sum;
}
double fitness(double a[]) //适应度函数
{
return Rastrigin(a);
}
void initial()
{
int i,j;
for(i=0; i
for(j=0; j
particle[i][j] = low+(high-low)*1.0*rand()/RAND_MAX; //初始化群体
particle_loc_best[i][j] = particle[i][j]; //将当前最优结果写入局部最优集合
particle_v[i][j] = -V_max+2*V_max*1.0*rand()/RAND_MAX; //速度
}
}
for(i=0; i
particle_fit[i] = fitness(particle[i]);
particle_loc_fit[i] = particle_fit[i];
}
gfit = particle_loc_fit[0]; //找出全局最优
j=0;
for(i=1; i
if(particle_loc_fit[i]
gfit = particle_loc_fit[i];
j = i;
}
}
for(i=0; i
particle_glo_best[i] = particle_loc_best[j][i];
}
}
void renew_particle()
{
int i,j;
for(i=0; i
for(j=0; j
particle[i][j] += alp*particle_v[i][j];
if(particle[i][j] > high)
{
parti
cle[i][j] = high;
}
if(particle[i][j] < low)
{
particle[i][j] = low;
}
}
}
}
void renew_var()
{
int i, j;
for(i=0; i
particle_fit[i] = fitness(particle[i]);
if(particle_fit[i] < particle_loc_fit[i]) //更新个体局部最优值
{
particle_loc_fit[i] = particle_fit[i];
for(j=0; j
particle_loc_best[i][j] = particle[i][j];
}
}
}
for(i=0,j=-1; i
if(particle_loc_fit[i]
gfit = particle_loc_fit[i];
j = i;
}
}
if(j != -1)
{
for(i=0; i
particle_glo_best[i] = particle_loc_best[j][i];
}
}
for(i=0; i
for(j=0; j
particle_v[i][j]=w*particle_v[i][j]+
c1*1.0*rand()/RAND_MAX*(particle_loc_best[i][j]-particle[i][j])+
c2*1.0*rand()/RAND_MAX*(particle_glo_best[j]-particle[i][j]);
if(particle_v[i][j] > V_max)
{
particle_v[i][j] = V_max;
}
if(particle_v[i][j] < -V_max)
{
particle_v[i][j] = -V_max;
}
}
}
}
int main()
{
freopen("result.txt","a+",stdout);
int i=0;
srand((unsigned)time(NULL));
initial();
while(i < iter_num)
{
renew_particle();
renew_var();
i++;
}
printf("粒子个数:%d\n",P_num);
printf("维度为:%d\n",dim);
printf("最优值为%.10lf\n", gfit);
return 0;
}