c++小程序转化成java代码
如果您愿意帮我,我非常感谢。如果感到麻烦,我愿付钱,我已经问了不下10个人了,给钱不要,不给钱又不帮忙
如果您想帮忙,请到http://www.nlld.net/program/tiezijiyiqv.do?method=viewtiezi&id=1826回复帖子,此网站担保,只要您回答了,就能得到我的报酬。#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <iomanip.h>
#include <iostream.h>
#include "bpso.h"const unsigned int Size=100; //群体规模
double a=-600,b=600; //变量取值范围
const unsigned int Dim=50; //变量个数
const unsigned long int maxgen=3000; //可根据优化对象的复杂程度来设置
const unsigned int sampnum=50;
//运行一次的统计量
double gbest[maxgen]; //最佳微粒的最佳适应值
double velocity[maxgen]; //最佳微粒的平均速度
double averfit[maxgen]; //群体的平均适应值
double divers[maxgen]; //群体的多样性
double averV[maxgen]; //群体的平均速度
const unsigned int runnum=30;
//多次运行统计量
double GenBfit[runnum][maxgen];//每独立运行一次中的每代最优解
double StatisBfit[maxgen];//多次运行的每代最优解平均值
double Genfit[runnum][maxgen];//每独立运行一次中的群体平均适应度
double Statisfit[maxgen];//多次运行的每代群体平均适应度的均值
double Gendivers[runnum][maxgen];//每独立运行一次中每代群体的多样性
double StatisD[maxgen];//多次运行中每代多样性的平均值
double GenaverV[runnum][maxgen]; //每独立运行一次中每代群体的平均速度
double StatisV[maxgen]; //多次运行中每代群体平均速度的均值typedef struct
{
double X[Dim]; //微粒的坐标数组
double V[Dim]; //微粒的速度数组
double XBest[Dim]; //微粒的最好位置数组
double Fit; //微粒适应度
double FitBest; //微粒的最佳适应度
double AverVBest; //微粒的平均速度
}PARTICLE; //个体微粒的结构定义typedef struct
{
PARTICLE Particle[Size]; //微粒群数组
int GBestIndex; //最好微粒索引
double W; //惯性权重
double C1; //加速度系数1
double C2; //加速度系数2
double Xup[Dim]; //微粒坐标上界数组
double Xdown[Dim]; //微粒坐标下界数组
double Vmax[Dim]; //微粒最大速度数组
double Diversity; //群体多样性
double AverP[Dim];//群体的平均中心
double AverV;//群体的平均速度
double AverFit;//群体的平均适应值
}SWARM; //群体结构的定义
SWARM swarm;/**********根据目标函数计算微粒适应值************/
void GetFit(PARTICLE& p) //Griewank (-600,600)
{
double z1=0.0; double z2=1;
p.Fit=0.0;
for(int i=1;i<Dim;i++)
{
z1+=p.X[i]*p.X[i];
z2*=cos(p.X[i]/sqrt(i));
}
p.Fit-=(z1/4000-z2+1);
}void Initswarm()//初始化
{
//参数 初始化
swarm.GBestIndex = 0;
swarm.W=0.72;
swarm.C1=swarm.C2=1.49;
//群粒初始化
for(int k=0;k<Dim;k++)
{
swarm.Vmax[k]=(b-a)/2;
swarm.Xdown[k]=a;
swarm.Xup[k]=b;
}
for(int i=0;i<Size;i++)
{
for(int j=0;j<Dim;j++)
{
swarm.Particle[i].X[j]=RandUniform(a,b);
swarm.Particle[i].XBest[j] = swarm.Particle[i].X[j];
swarm.Particle[i].V[j] = Rand01()*swarm.Vmax[j]-swarm.Vmax[j]/2;//初始化速度
}
GetFit(swarm.Particle[i]);
swarm.Particle[i].FitBest = swarm.Particle[i].Fit; //设最优适合度初值
if(swarm.Particle[i].Fit>swarm.Particle[swarm.GBestIndex].Fit)
{ swarm.GBestIndex = i; }//查找群体最优微粒
}
}
void PrintP(PARTICLE& p) //输出微粒信息
{
for(int i=0;i<Dim;i++)
{
cout<<setw(8)<<setprecision(5)
<<p.XBest[i]<<" ";
}
for( i=0;i<Dim;i++)
{
cout<<setw(8)<<setprecision(5)
<<p.V[i]<<" ";
}
cout<<setw(10)<<setprecision(5)<<p.FitBest
<<endl;
}
void PrintS() //输出群体
{
for(int i=0;i<Size;i++)
{
cout<<"particle"<<i<<": ";
PrintP(swarm.Particle[i]);
}
}
void ParticleFly()//更新产生新一代微粒,Gbest 模型//{
static int tt=(unsigned)time(NULL);
srand((unsigned)time(NULL)+tt++);
//整个群体飞向新的位置
for(int i=0; i<Size; i++)
{
for(int j=0; j<Dim; j++)
{
swarm.Particle[i].V[j] = swarm.W*swarm.Particle[i].V[j]+
Rand01()*swarm.C1*(swarm.Particle[i].XBest[j]-swarm.Particle[i].X[j])+
Rand01()*swarm.C2*(swarm.Particle[swarm.GBestIndex].XBest[j]-swarm.Particle[i].X[j]);
}
for(j=0; j<Dim; j++) //检查速度最大值
{
if(swarm.Particle[i].V[j]>swarm.Vmax[j])
{
swarm.Particle[i].V[j] = 0.1*swarm.Vmax[j];
}
if(swarm.Particle[i].V[j]<-swarm.Vmax[j])
{
swarm.Particle[i].V[j] = -0.1*swarm.Vmax[j];
}
}
for(j=0; j<Dim; j++)
{
swarm.Particle[i].X[j] += swarm.Particle[i].V[j]; //修改坐标
if(swarm.Particle[i].X[j]>swarm.Xup[j])
{
swarm.Particle[i].X[j]=0.5*swarm.Xup[j];//保护
}
if(swarm.Particle[i].X[j]<swarm.Xdown[j])
{swarm.Particle[i].X[j]=0.5*swarm.Xdown[j];}
}
GetFit(swarm.Particle[i]);//计算各微粒适合度
}
//设置新的个体最好位置
for(i=0; i<Size; i++)
if(swarm.Particle[i].Fit>=swarm.Particle[i].FitBest)
{
swarm.Particle[i].FitBest = swarm.Particle[i].Fit;
for(int j=0; j<Dim; j++)
swarm.Particle[i].XBest[j] = swarm.Particle[i].X[j];
}
//设置新的最优个体
swarm.GBestIndex = 0;
for(i=0; i<Size; i++)
{
if(swarm.Particle[i].FitBest>=swarm.Particle[swarm.GBestIndex].FitBest && i!=swarm.GBestIndex)
{swarm.GBestIndex = i;}
}
}
void GetAverFit() // 计算群体平均适应值
{
swarm.AverFit=0.0;
for(int i=0;i<Size;i++)
{
swarm.AverFit+=fabs(swarm.Particle[i].FitBest);
}
swarm.AverFit=swarm.AverFit/Size;
}
void GetAverV() //获取群体微粒的平均速度
{
swarm.AverV=0.0;
for(int i=0;i<Size;i++)
{
for(int j=0;j<Dim;j++)
{
swarm.AverV+=fabs(swarm.Particle[i].V[j]);
}
}
swarm.AverV=swarm.AverV/(Size*Dim);
}
void GetAverVBest() //获取最佳微粒的平均速度
{
swarm.Particle[swarm.GBestIndex].AverVBest=0.0;
for(int j=0;j<Dim;j++)
{
swarm.Particle[swarm.GBestIndex].AverVBest+=fabs(swarm.Particle[swarm.GBestIndex].V[j]);
}
swarm.Particle[swarm.GBestIndex].AverVBest=swarm.Particle[swarm.GBestIndex].AverVBest/Dim;
}
void GetDiversity() //以群体平均点为中心计算多样性
{
swarm.Diversity=0.0;
double DiverTemp;
for(int j=0;j<Dim;j++)
{
swarm.AverP[j]=0.0;
for(int i=0;i<Size;i++)
{
swarm.AverP[j]+=swarm.Particle[i].X[j];
}
swarm.AverP[j]=swarm.AverP[j]/Size;
}
for(int i=0;i<Size;i++)
{
DiverTemp=0.0;
for(j=0;j<Dim;j++)
{
DiverTemp+=(swarm.Particle[i].X[j]-swarm.AverP[j])*(swarm.Particle[i].X[j]-swarm.AverP[j]);
}
swarm.Diversity+=sqrt(DiverTemp);
}
swarm.Diversity=swarm.Diversity/(Size*2*b*sqrt(Dim));
}
void main() //各算法统计数据
{
static int tt=(unsigned)time(NULL);
srand((unsigned)time(NULL)+tt++);
clock_t start,finish;
double totaltime[runnum]; double avetime=0.0;
int congen[runnum]; int avegen=0;
double confit[runnum]; double avefit=0.0;
double Gbestfit=50.0; double Gworstfit=0.0;
int successN=0; double SD=0.0;
double e=0.1; //认为算法成功收敛时所需满足的精度;注意各函数复杂程度不同,精度要求不同
for(int k=0;k<runnum;k++)
{
confit[k]=0.0;
congen[k]=0;
}
int i=0;
while(i<runnum)
{
start=clock();
Initswarm();
GetDiversity();
divers[0]=swarm.Diversity;
int j=1;
while(j<maxgen+1 )
{
swarm.C1=0.5+2.0*(maxgen-j)/maxgen;
swarm.C2=0.5+2.0*j/maxgen; */
ParticleFly();
GetDiversity();
divers[j]=swarm.Diversity;
j++;
}
finish=clock();
confit[i]=fabs(swarm.Particle[swarm.GBestIndex].FitBest);
if(confit[i]<e) successN++;
if(confit[i]>Gworstfit) Gworstfit=confit[i];
if(confit[i]<Gbestfit) Gbestfit=confit[i];
SD+=confit[i]*confit[i];
avefit+=confit[i];
avegen+=congen[i];
totaltime[i]=(double)(finish-start)/CLOCKS_PER_SEC;
avetime+=totaltime[i];
i++;
}
cout<<"最差适应值 "<<Gworstfit<<endl;
cout<<"最佳适应值"<<Gbestfit<<endl;
avefit=avefit/runnum;
cout<<runnum<<"次平均收敛适应值为:"<<avefit<<endl;
SD=sqrt(SD)/runnum;
cout<<runnum<<"次方差为:"<<SD<<endl;
avetime=avetime/runnum;
cout<<runnum<<"次平均运行时间为:"<<avetime<<endl;
cout<<"次中成功收敛次数为:"<<successN<<endl;
}bpso.h#include "iostream.h"
#include "math.h"
#define RandInt(i) (rand()%(int)(i))
#define Rand01() ((double)rand()/(double)(RAND_MAX+1))
#define RandUniform(a,b) (Rand01()*((b)-(a))+(a))
#define Rand(a,b) (RandInt((int)(b)-(int)(a)+1)+(int)(a))_inline
double RandNormal(void)
{
static double dRandTempResult=0.0;
static bool nNomalRandFlag=false;
if(nNomalRandFlag)
{
nNomalRandFlag=false;
return dRandTempResult;
} double u1,u2,w;
do
{
u1=Rand01();
u2=Rand01();
u1=2*u1-1;
u2=2*u2-1;
w=u1*u1+u2*u2;
}while(w>1); double y=sqrt((-2*log(w))/w);
dRandTempResult=y*u2;
nNomalRandFlag=1;
return y*u1;
};_inline
double RandNormal(double p,double o)
{
return RandNormal()*o+p;
};
如果您愿意帮我,我非常感谢。如果感到麻烦,我愿付钱,我已经问了不下10个人了,给钱不要,不给钱又不帮忙
如果您想帮忙,请到http://www.nlld.net/program/tiezijiyiqv.do?method=viewtiezi&id=1826回复帖子,此网站担保,只要您回答了,就能得到我的报酬。#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <iomanip.h>
#include <iostream.h>
#include "bpso.h"const unsigned int Size=100; //群体规模
double a=-600,b=600; //变量取值范围
const unsigned int Dim=50; //变量个数
const unsigned long int maxgen=3000; //可根据优化对象的复杂程度来设置
const unsigned int sampnum=50;
//运行一次的统计量
double gbest[maxgen]; //最佳微粒的最佳适应值
double velocity[maxgen]; //最佳微粒的平均速度
double averfit[maxgen]; //群体的平均适应值
double divers[maxgen]; //群体的多样性
double averV[maxgen]; //群体的平均速度
const unsigned int runnum=30;
//多次运行统计量
double GenBfit[runnum][maxgen];//每独立运行一次中的每代最优解
double StatisBfit[maxgen];//多次运行的每代最优解平均值
double Genfit[runnum][maxgen];//每独立运行一次中的群体平均适应度
double Statisfit[maxgen];//多次运行的每代群体平均适应度的均值
double Gendivers[runnum][maxgen];//每独立运行一次中每代群体的多样性
double StatisD[maxgen];//多次运行中每代多样性的平均值
double GenaverV[runnum][maxgen]; //每独立运行一次中每代群体的平均速度
double StatisV[maxgen]; //多次运行中每代群体平均速度的均值typedef struct
{
double X[Dim]; //微粒的坐标数组
double V[Dim]; //微粒的速度数组
double XBest[Dim]; //微粒的最好位置数组
double Fit; //微粒适应度
double FitBest; //微粒的最佳适应度
double AverVBest; //微粒的平均速度
}PARTICLE; //个体微粒的结构定义typedef struct
{
PARTICLE Particle[Size]; //微粒群数组
int GBestIndex; //最好微粒索引
double W; //惯性权重
double C1; //加速度系数1
double C2; //加速度系数2
double Xup[Dim]; //微粒坐标上界数组
double Xdown[Dim]; //微粒坐标下界数组
double Vmax[Dim]; //微粒最大速度数组
double Diversity; //群体多样性
double AverP[Dim];//群体的平均中心
double AverV;//群体的平均速度
double AverFit;//群体的平均适应值
}SWARM; //群体结构的定义
SWARM swarm;/**********根据目标函数计算微粒适应值************/
void GetFit(PARTICLE& p) //Griewank (-600,600)
{
double z1=0.0; double z2=1;
p.Fit=0.0;
for(int i=1;i<Dim;i++)
{
z1+=p.X[i]*p.X[i];
z2*=cos(p.X[i]/sqrt(i));
}
p.Fit-=(z1/4000-z2+1);
}void Initswarm()//初始化
{
//参数 初始化
swarm.GBestIndex = 0;
swarm.W=0.72;
swarm.C1=swarm.C2=1.49;
//群粒初始化
for(int k=0;k<Dim;k++)
{
swarm.Vmax[k]=(b-a)/2;
swarm.Xdown[k]=a;
swarm.Xup[k]=b;
}
for(int i=0;i<Size;i++)
{
for(int j=0;j<Dim;j++)
{
swarm.Particle[i].X[j]=RandUniform(a,b);
swarm.Particle[i].XBest[j] = swarm.Particle[i].X[j];
swarm.Particle[i].V[j] = Rand01()*swarm.Vmax[j]-swarm.Vmax[j]/2;//初始化速度
}
GetFit(swarm.Particle[i]);
swarm.Particle[i].FitBest = swarm.Particle[i].Fit; //设最优适合度初值
if(swarm.Particle[i].Fit>swarm.Particle[swarm.GBestIndex].Fit)
{ swarm.GBestIndex = i; }//查找群体最优微粒
}
}
void PrintP(PARTICLE& p) //输出微粒信息
{
for(int i=0;i<Dim;i++)
{
cout<<setw(8)<<setprecision(5)
<<p.XBest[i]<<" ";
}
for( i=0;i<Dim;i++)
{
cout<<setw(8)<<setprecision(5)
<<p.V[i]<<" ";
}
cout<<setw(10)<<setprecision(5)<<p.FitBest
<<endl;
}
void PrintS() //输出群体
{
for(int i=0;i<Size;i++)
{
cout<<"particle"<<i<<": ";
PrintP(swarm.Particle[i]);
}
}
void ParticleFly()//更新产生新一代微粒,Gbest 模型//{
static int tt=(unsigned)time(NULL);
srand((unsigned)time(NULL)+tt++);
//整个群体飞向新的位置
for(int i=0; i<Size; i++)
{
for(int j=0; j<Dim; j++)
{
swarm.Particle[i].V[j] = swarm.W*swarm.Particle[i].V[j]+
Rand01()*swarm.C1*(swarm.Particle[i].XBest[j]-swarm.Particle[i].X[j])+
Rand01()*swarm.C2*(swarm.Particle[swarm.GBestIndex].XBest[j]-swarm.Particle[i].X[j]);
}
for(j=0; j<Dim; j++) //检查速度最大值
{
if(swarm.Particle[i].V[j]>swarm.Vmax[j])
{
swarm.Particle[i].V[j] = 0.1*swarm.Vmax[j];
}
if(swarm.Particle[i].V[j]<-swarm.Vmax[j])
{
swarm.Particle[i].V[j] = -0.1*swarm.Vmax[j];
}
}
for(j=0; j<Dim; j++)
{
swarm.Particle[i].X[j] += swarm.Particle[i].V[j]; //修改坐标
if(swarm.Particle[i].X[j]>swarm.Xup[j])
{
swarm.Particle[i].X[j]=0.5*swarm.Xup[j];//保护
}
if(swarm.Particle[i].X[j]<swarm.Xdown[j])
{swarm.Particle[i].X[j]=0.5*swarm.Xdown[j];}
}
GetFit(swarm.Particle[i]);//计算各微粒适合度
}
//设置新的个体最好位置
for(i=0; i<Size; i++)
if(swarm.Particle[i].Fit>=swarm.Particle[i].FitBest)
{
swarm.Particle[i].FitBest = swarm.Particle[i].Fit;
for(int j=0; j<Dim; j++)
swarm.Particle[i].XBest[j] = swarm.Particle[i].X[j];
}
//设置新的最优个体
swarm.GBestIndex = 0;
for(i=0; i<Size; i++)
{
if(swarm.Particle[i].FitBest>=swarm.Particle[swarm.GBestIndex].FitBest && i!=swarm.GBestIndex)
{swarm.GBestIndex = i;}
}
}
void GetAverFit() // 计算群体平均适应值
{
swarm.AverFit=0.0;
for(int i=0;i<Size;i++)
{
swarm.AverFit+=fabs(swarm.Particle[i].FitBest);
}
swarm.AverFit=swarm.AverFit/Size;
}
void GetAverV() //获取群体微粒的平均速度
{
swarm.AverV=0.0;
for(int i=0;i<Size;i++)
{
for(int j=0;j<Dim;j++)
{
swarm.AverV+=fabs(swarm.Particle[i].V[j]);
}
}
swarm.AverV=swarm.AverV/(Size*Dim);
}
void GetAverVBest() //获取最佳微粒的平均速度
{
swarm.Particle[swarm.GBestIndex].AverVBest=0.0;
for(int j=0;j<Dim;j++)
{
swarm.Particle[swarm.GBestIndex].AverVBest+=fabs(swarm.Particle[swarm.GBestIndex].V[j]);
}
swarm.Particle[swarm.GBestIndex].AverVBest=swarm.Particle[swarm.GBestIndex].AverVBest/Dim;
}
void GetDiversity() //以群体平均点为中心计算多样性
{
swarm.Diversity=0.0;
double DiverTemp;
for(int j=0;j<Dim;j++)
{
swarm.AverP[j]=0.0;
for(int i=0;i<Size;i++)
{
swarm.AverP[j]+=swarm.Particle[i].X[j];
}
swarm.AverP[j]=swarm.AverP[j]/Size;
}
for(int i=0;i<Size;i++)
{
DiverTemp=0.0;
for(j=0;j<Dim;j++)
{
DiverTemp+=(swarm.Particle[i].X[j]-swarm.AverP[j])*(swarm.Particle[i].X[j]-swarm.AverP[j]);
}
swarm.Diversity+=sqrt(DiverTemp);
}
swarm.Diversity=swarm.Diversity/(Size*2*b*sqrt(Dim));
}
void main() //各算法统计数据
{
static int tt=(unsigned)time(NULL);
srand((unsigned)time(NULL)+tt++);
clock_t start,finish;
double totaltime[runnum]; double avetime=0.0;
int congen[runnum]; int avegen=0;
double confit[runnum]; double avefit=0.0;
double Gbestfit=50.0; double Gworstfit=0.0;
int successN=0; double SD=0.0;
double e=0.1; //认为算法成功收敛时所需满足的精度;注意各函数复杂程度不同,精度要求不同
for(int k=0;k<runnum;k++)
{
confit[k]=0.0;
congen[k]=0;
}
int i=0;
while(i<runnum)
{
start=clock();
Initswarm();
GetDiversity();
divers[0]=swarm.Diversity;
int j=1;
while(j<maxgen+1 )
{
swarm.C1=0.5+2.0*(maxgen-j)/maxgen;
swarm.C2=0.5+2.0*j/maxgen; */
ParticleFly();
GetDiversity();
divers[j]=swarm.Diversity;
j++;
}
finish=clock();
confit[i]=fabs(swarm.Particle[swarm.GBestIndex].FitBest);
if(confit[i]<e) successN++;
if(confit[i]>Gworstfit) Gworstfit=confit[i];
if(confit[i]<Gbestfit) Gbestfit=confit[i];
SD+=confit[i]*confit[i];
avefit+=confit[i];
avegen+=congen[i];
totaltime[i]=(double)(finish-start)/CLOCKS_PER_SEC;
avetime+=totaltime[i];
i++;
}
cout<<"最差适应值 "<<Gworstfit<<endl;
cout<<"最佳适应值"<<Gbestfit<<endl;
avefit=avefit/runnum;
cout<<runnum<<"次平均收敛适应值为:"<<avefit<<endl;
SD=sqrt(SD)/runnum;
cout<<runnum<<"次方差为:"<<SD<<endl;
avetime=avetime/runnum;
cout<<runnum<<"次平均运行时间为:"<<avetime<<endl;
cout<<"次中成功收敛次数为:"<<successN<<endl;
}bpso.h#include "iostream.h"
#include "math.h"
#define RandInt(i) (rand()%(int)(i))
#define Rand01() ((double)rand()/(double)(RAND_MAX+1))
#define RandUniform(a,b) (Rand01()*((b)-(a))+(a))
#define Rand(a,b) (RandInt((int)(b)-(int)(a)+1)+(int)(a))_inline
double RandNormal(void)
{
static double dRandTempResult=0.0;
static bool nNomalRandFlag=false;
if(nNomalRandFlag)
{
nNomalRandFlag=false;
return dRandTempResult;
} double u1,u2,w;
do
{
u1=Rand01();
u2=Rand01();
u1=2*u1-1;
u2=2*u2-1;
w=u1*u1+u2*u2;
}while(w>1); double y=sqrt((-2*log(w))/w);
dRandTempResult=y*u2;
nNomalRandFlag=1;
return y*u1;
};_inline
double RandNormal(double p,double o)
{
return RandNormal()*o+p;
};
解决方案 »
- 奇了,怪了,居然写入文件总是有一部乱码。关于filewriter.
- scjp题,麻烦大家帮忙
- 方法
- 应如何选择学习java和.net
- 各位热心大虾请进,小弟跪求帮忙调通此java程序的逻辑错误(在线等待 急~~~~~)
- helloworld的问题
- 我安装了j2se sdk,可是我在机器了找不到启动该开发平台的图标,我该则怎么办(在线等待,马上给分)
- ★★★★★特大好消息! 与大家共享,java爱好者必看!(高兴顺便送分)
- 建国53周年,举国同庆! 发“分”图强!
- 正在学习《java2图形设计卷1:awt〉的弟兄进来报个名呀。
- 请帮我看看。我有点迷糊。
- 初学Java,自编双色球选号小程序,出错了,请各位帮忙~~
public double RandInt(double i) {
return Math.random()%(int)(i);
}
public double Rand01() {
return (double)Math.random()/(double)(32767+1);
}
public double RandUniform(double a,double b) {
return Rand01()*((b)-(a))+(a);
}
public double Rand(double a,double b) {
return RandInt((int)(b)-(int)(a)+1)+(int)(a);
} public double RandNormal(){
double dRandTempResult=0;
boolean nNomalRandFlag=false;
if(nNomalRandFlag){
nNomalRandFlag=false;
return dRandTempResult;
}
double u1,u2,w;
do
{
u1=Rand01();
u2=Rand01();
u1=2*u1-1;
u2=2*u2-1;
w=u1*u1+u2*u2;
}while(w>1);
double y=Math.sqrt((-2*Math.log(w))/w);
dRandTempResult=y*u2;
nNomalRandFlag=true;
return y*u1;
}
public double RandNormal(double p,double o){
return RandNormal()*o+p;
}
}
PARTICLE.java//个体微粒的结构定义
public class PARTICLE{
int Dim=50;
double[] X=new double[Dim]; //微粒的坐标数组
double[] V=new double[Dim]; //微粒的速度数组
double[] XBest=new double[Dim]; //微粒的最好位置数组
double Fit; //微粒适应度
double FitBest; //微粒的最佳适应度
double AverVBest; //微粒的平均速度
public int getDim() {
return Dim;
}
public void setDim(int dim) {
Dim = dim;
}
public double[] getX() {
return X;
}
public void setX(double[] x) {
X = x;
}
public double[] getV() {
return V;
}
public void setV(double[] v) {
V = v;
}
public double[] getXBest() {
return XBest;
}
public void setXBest(double[] best) {
XBest = best;
}
public double getFit() {
return Fit;
}
public void setFit(double fit) {
Fit = fit;
}
public double getFitBest() {
return FitBest;
}
public void setFitBest(double fitBest) {
FitBest = fitBest;
}
public double getAverVBest() {
return AverVBest;
}
public void setAverVBest(double averVBest) {
AverVBest = averVBest;
}
public PARTICLE(int dim) {
super();
Dim = dim;
X = new double[Dim];
V = new double[Dim];
XBest = new double[Dim];
}}
public class SWARM {
int Size=100; //群体规模
double a=-600,b=600; //变量取值范围
int Dim=50; //变量个数
int maxgen=3000; //可根据优化对象的复杂程度来设置
int sampnum=50;
//运行一次的统计量
double[] gbest=new double[maxgen]; //最佳微粒的最佳适应值
double[] velocity=new double[maxgen]; //最佳微粒的平均速度
double[] averfit=new double[maxgen]; //群体的平均适应值
double[] divers=new double[maxgen]; //群体的多样性
double[] averV=new double[maxgen]; //群体的平均速度
int runnum=30;
//多次运行统计量
double[][] GenBfit=new double[runnum][maxgen];//每独立运行一次中的每代最优解
double[] StatisBfit=new double[maxgen];//多次运行的每代最优解平均值
double[][] Genfit=new double[runnum][maxgen];//每独立运行一次中的群体平均适应度
double[] Statisfit=new double[maxgen];//多次运行的每代群体平均适应度的均值
double[][] Gendivers=new double[runnum][maxgen];//每独立运行一次中每代群体的多样性
double[] StatisD=new double[maxgen];//多次运行中每代多样性的平均值
double[][] GenaverV=new double[runnum][maxgen]; //每独立运行一次中每代群体的平均速度
double[] StatisV=new double[maxgen]; //多次运行中每代群体平均速度的均值 PARTICLE[] Particle=new PARTICLE[Size]; //微粒群数组
int GBestIndex; //最好微粒索引
double W; //惯性权重
double C1; //加速度系数1
double C2; //加速度系数2
double[] Xup=new double[Dim]; //微粒坐标上界数组
double[] Xdown=new double[Dim]; //微粒坐标下界数组
double[] Vmax=new double[Dim]; //微粒最大速度数组
double Diversity; //群体多样性
double[] AverP=new double[Dim];//群体的平均中心
double AverV;//群体的平均速度
double AverFit;//群体的平均适应值
bpso bpso=new bpso();
public SWARM(){
}
public void Initswarm()//初始化
{
//参数 初始化
this.GBestIndex = 0;
this.W=0.72;
this.C1=1.49;
this.C2=1.49;
//群粒初始化
for(int k=0;k <Dim;k++)
{
this.Vmax[k]=(b-a)/2;
this.Xdown[k]=a;
this.Xup[k]=b;
}
for(int i=0;i <Size;i++)
{
this.Particle[i]=new PARTICLE(Dim);
for(int j=0;j <Dim;j++)
{
this.Particle[i].getX()[j]=bpso.RandUniform(a,b);
this.Particle[i].getXBest()[j] = this.Particle[i].getX()[j];
this.Particle[i].getV()[j] = bpso.Rand01()*this.Vmax[j]-this.Vmax[j]/2;//初始化速度
}
GetFit(this.Particle[i]);
this.Particle[i].setFitBest(this.Particle[i].getFit()); //设最优适合度初值
if(this.Particle[i].getFit()>this.Particle[this.GBestIndex].getFit())
{ this.GBestIndex = i; }//查找群体最优微粒
}
} /**********根据目标函数计算微粒适应值************/
void GetFit(PARTICLE p) //Griewank (-600,600)
{
double z1=0.0;
double z2=1;
double Fit=0.0;
for(int i=1;i <Dim;i++)
{
z1+=p.getX()[i]*p.getX()[i];
z2*=Math.cos(p.getX()[i]/Math.sqrt(i));
}
Fit-=(z1/4000-z2+1);
p.setFit(Fit);
}
void PrintP(PARTICLE p) //输出微粒信息
{
java.text.DecimalFormat myformat=new java.text.DecimalFormat("#0.00000"); for(int i=0;i <Dim;i++)
{
System.out.print(" "+myformat.format(p.getXBest()[i])+" ");
} for(int i=0;i <Dim;i++)
{
System.out.print(" "+myformat.format(p.getV()[i])+" ");
}
System.out.println(" "+myformat.format(p.getFitBest()));
}
void PrintS() //输出群体
{
for(int i=0;i <Size;i++)
{
System.out.print("particle"+i+": ");
PrintP(Particle[i]);
}
}
void ParticleFly()//更新产生新一代微粒,Gbest 模型// {
//整个群体飞向新的位置
for(int i=0; i <Size; i++)
{
for(int j=0; j <Dim; j++)
{
this.Particle[i].getV()[j] = this.W*this.Particle[i].getV()[j]+
bpso.Rand01()*this.C1*(this.Particle[i].getXBest()[j]-this.Particle[i].getX()[j])+
bpso.Rand01()*this.C2*(this.Particle[this.GBestIndex].getXBest()[j]-this.Particle[i].getX()[j]);
}
for(int j=0; j <Dim; j++) //检查速度最大值
{
if(this.Particle[i].getV()[j]>this.Vmax[j]){
this.Particle[i].getV()[j] = 0.1*this.Vmax[j];
}
if(this.Particle[i].getV()[j] <-this.Vmax[j]){
this.Particle[i].getV()[j] = -0.1*this.Vmax[j];
}
}
for(int j=0; j <Dim; j++)
{
this.Particle[i].getX()[j] += this.Particle[i].getV()[j]; //修改坐标
if(this.Particle[i].getX()[j]>this.Xup[j]){
this.Particle[i].getX()[j]=0.5*this.Xup[j];//保护
}
if(this.Particle[i].getX()[j] <this.Xdown[j]){
this.Particle[i].getX()[j]=0.5*this.Xdown[j];
}
}
GetFit(this.Particle[i]);//计算各微粒适合度
}
//设置新的个体最好位置
for(int i=0; i <Size; i++){
if(this.Particle[i].getFit()>=this.Particle[i].getFitBest()){
this.Particle[i].setFitBest(this.Particle[i].getFit());
for(int j=0; j <Dim; j++){
this.Particle[i].getXBest()[j] = this.Particle[i].getX()[j];
}
}
//设置新的最优个体
this.GBestIndex = 0;
for(i=0; i <Size; i++)
{
if(this.Particle[i].getFitBest()>=this.Particle[this.GBestIndex].getFitBest() && i!=this.GBestIndex){
this.GBestIndex = i;
}
}
}
}
void GetAverFit() // 计算群体平均适应值
{
this.AverFit=0.0;
for(int i=0;i <Size;i++)
{
this.AverFit+=Math.abs(this.Particle[i].getFitBest());
}
this.AverFit=this.AverFit/Size;
} void GetAverV() //获取群体微粒的平均速度
{
this.AverV=0.0;
for(int i=0;i <Size;i++)
{
for(int j=0;j <Dim;j++)
{
this.AverV+=Math.abs(this.Particle[i].getV()[j]);
}
}
this.AverV=this.AverV/(Size*Dim);
}
void GetAverVBest() //获取最佳微粒的平均速度
{
this.Particle[this.GBestIndex].setAverVBest(0.0);
for(int j=0;j <Dim;j++)
{
this.Particle[this.GBestIndex].setAverVBest(this.Particle[this.GBestIndex].getAverVBest()+Math.abs(this.Particle[this.GBestIndex].V[j]));
}
this.Particle[this.GBestIndex].setAverVBest(this.Particle[this.GBestIndex].getAverVBest()/Dim);
}
void GetDiversity() //以群体平均点为中心计算多样性
{
this.Diversity=0.0;
double DiverTemp;
for(int j=0;j <Dim;j++)
{
this.AverP[j]=0.0;
for(int i=0;i <Size;i++)
{
this.AverP[j]+=this.Particle[i].getX()[j];
}
this.AverP[j]=this.AverP[j]/Size;
}
for(int i=0;i <Size;i++)
{
DiverTemp=0.0;
for(int j=0;j <Dim;j++)
{
DiverTemp+=(this.Particle[i].getX()[j]-this.AverP[j])*(this.Particle[i].getX()[j]-this.AverP[j]);
}
this.Diversity+=Math.sqrt(DiverTemp);
}
this.Diversity=this.Diversity/(Size*2*b*Math.sqrt(Dim));
}
//各算法统计数据
public void abc() {
int runnum=30;
double start,finish;
double[] totaltime=new double[runnum];
double avetime=0.0;
int[] congen=new int[runnum];
int avegen=0;
double[] confit=new double[runnum];
double avefit=0.0;
double Gbestfit=50.0;
double Gworstfit=0.0;
int successN=0;
double SD=0.0;
double e=0.1; //认为算法成功收敛时所需满足的精度;注意各函数复杂程度不同,精度要求不同
for(int k=0;k <runnum;k++)
{
confit[k]=0.0;
congen[k]=0;
}
int i=0;
while(i <runnum){
start=System.currentTimeMillis();
Initswarm();
GetDiversity();
divers[0]=this.Diversity;
int j=1;
while(j <maxgen+1 ){
this.C1=0.5+2.0*(maxgen-j)/maxgen;
this.C2=0.5+2.0*j/maxgen;
ParticleFly();
GetDiversity();
divers[j]=this.Diversity;
j++;
}
finish=System.currentTimeMillis();
confit[i]=Math.abs(this.Particle[this.GBestIndex].getFitBest());
if(confit[i] <e) successN++;
if(confit[i]>Gworstfit) Gworstfit=confit[i];
if(confit[i] <Gbestfit) Gbestfit=confit[i];
SD+=confit[i]*confit[i];
avefit+=confit[i];
avegen+=congen[i]; totaltime[i]=(double)(finish-start);
avetime+=totaltime[i];
i++;
}
System.out.println("最差适应值 "+Gworstfit);
System.out.println("最佳适应值"+Gbestfit);
avefit=avefit/runnum;
System.out.println(runnum+"次平均收敛适应值为:"+avefit);
SD=Math.sqrt(SD)/runnum;
System.out.println(runnum+"次方差为:"+SD);
avetime=avetime/runnum;
System.out.println(runnum+"次平均运行时间为:"+avetime);
System.out.println("次中成功收敛次数为:"+successN);
}
//get set方法…… 因为回帖字符限制有很多略去,也有很多是不必要的,没有精简
public double getW() {
return W;
}
public void setW(double w) {
W = w;
}
public double getC1() {
return C1;
}
public void setC1(double c1) {
C1 = c1;
}
public double getC2() {
return C2;
}
public void setC2(double c2) {
C2 = c2;
}
public double[] getXup() {
return Xup;
}
public void setXup(double[] xup) {
Xup = xup;
}
public double[] getXdown() {
return Xdown;
}
public void setXdown(double[] xdown) {
Xdown = xdown;
}
public double[] getVmax() {
return Vmax;
}
public void setVmax(double[] vmax) {
Vmax = vmax;
}
public double getDiversity() {
return Diversity;
}
public void setDiversity(double diversity) {
Diversity = diversity;
}
public double[] getAverP() {
return AverP;
}
public void setAverP(double[] averP) {
AverP = averP;
}
public double getAverV() {
return AverV;
}
public void setAverV(double averV) {
AverV = averV;
}
public double getAverFit() {
return AverFit;
}
public void setAverFit(double averFit) {
AverFit = averFit;
}
public bpso getBpso() {
return bpso;
}
public void setBpso(bpso bpso) {
this.bpso = bpso;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SWARM s=new SWARM();
s.setSize(100); //群体规模
s.setA(-600);
s.setB(600); //变量取值范围
s.setDim(50); //变量个数
s.setMaxgen(30); //可根据优化对象的复杂程度来设置
s.setSampnum(50);
s.abc();
}}