磷虾群优化问题但是结果不理想,望各位大神帮忙看看
//库文件#include"stdio.h"
#include"stdlib.h"
#include"time.h"
#include"math.h"
//随机数定义
#define rdint(i) (rand()%(int)(i))
#define rdft() (float)((double)rdint(16384)/(16383.0))
#define rnd(a,b) (rdint((int)(b)-(int)(a)+1)+(int)(a))
//宏定义
#define POPSIZE 10
#define DIMENSION 5
#define Imax 50 //迭代次数
#define L -100.0 //下限
#define U 100.0 //上限
//全局变量定义
float Nmax=0.01;//最大诱导速度
float Vf=0.02;//最大觅食速度
float Dmax=0.005;//最大扩散速度
float P[DIMENSION];//全局最优位置
float PBEST;//全局最优的适应度值
float PWORST;//全局最差的适应度值
float alocal[DIMENSION];//邻居的影响的速度矢量
float atarget[DIMENSION];//最优个体的影响的速度矢量
int Best;
int worst;
float xij[DIMENSION];
float Bfood[DIMENSION];
float bbest[DIMENSION];
float e=0.0005;//最小正数
int ibest;
//定义结构体
struct indi
{
float number[DIMENSION];
float best[DIMENSION];
float bestfitness;
float fitness;
float N[DIMENSION];
float F[DIMENSION];
float D[DIMENSION];
}individual[POPSIZE+1];
//函数定义
void initiate();//初始化函数
void calculation(int number);//计算适应值
void globalbest(int number);//全局最优
void globalworst();//全局最差
void localbest(int number);//局部最优
float Kij(int num,int n);
void Xij(int num,int n);
void Alocal(int num);
void Atarget(int I,int num);
void Induced_Motion(int I,int num);//受诱导运动
void x_food();
void Bbest(int num);
void Foraging_Motion(int I,int num);//觅食行为
void Physical_Motion(int I,int num);//随机物理扩散
void crossover(int num);//交叉算子
void Mutation(int num);//突变算子
void Motion_Process(int num);//状态更新
//主函数
void main(){
int i,k;
srand(time(NULL));
initiate();
for(k=0;k<Imax;k++){
for(i=0;i<POPSIZE;i++)
{
calculation(i);//计算适应度
localbest(i);
Induced_Motion(k,i);//诱导运动
Foraging_Motion(k,i);//觅食运动
Physical_Motion(k,i);//随机物理扩散
crossover(i);
Mutation(i);
calculation(i);//计算适应度
Motion_Process(i);
}//所有个体产生了
globalbest(1);
printf("第%d代最优值:%f\n",k+1,PBEST);
}
printf("\n");
printf("全局最优值,%f\n",PBEST);
}
//程序初始化定义
void initiate()
{
int i,j;
for(i=0;i<POPSIZE;i++){
for(j=0;j<DIMENSION;j++){
individual[i].number[j]=rdft()*(U-L)+L;
individual[i].N[j]=Nmax*rdft();
individual[i].F[j]=Vf*rdft();
individual[i].D[j]=Dmax*rdft();
if(individual[i].number[j]<L)
individual[i].number[j]=2*L-individual[i].number[j];
if(individual[i].number[j]>U)
individual[i].number[j]=2*U-individual[i].number[j];
individual[i].best[j]=individual[i].number[j];
}
}
for(i=0;i<POPSIZE;i++){
calculation(i);
individual[i].bestfitness=individual[i].fitness;
}
globalbest(0);
}
//微粒历史最优位置修改程序
void localbest(int number)
{
int j;
if(individual[number].bestfitness>individual[number].fitness)
for(j=1;j<DIMENSION;j++){
individual[number].best[j]=individual[number].number[j];
individual[number].bestfitness=individual[number].fitness;
}
}
//种群历史最优位置修改程序
void globalbest(int number)
{
int i,j;
float s=0;
int flag=0;
if(number==0)
{
s=individual[0].fitness;
flag=0;
for(i=1;i<POPSIZE;i++)
if(individual[i].fitness<s)
{
s=individual[i].fitness;
flag=i;
}
for(j=0;j<DIMENSION;j++)
P[i]=individual[flag].number[j];
PBEST=individual[flag].fitness;
}
else
{
for(i=0;i<POPSIZE;i++)
if(individual[i].bestfitness<PBEST)
{
for(j=0;j<DIMENSION;j++)
P[j]=individual[i].best[j];
PBEST=individual[i].bestfitness;
}
}
}
//种群历史最差位置修改程序
void globalworst()
{
int i,j;
float s=0;
PWORST=individual[0].fitness;
for(i=1;i<POPSIZE;i++)
if(individual[i].bestfitness>PWORST){
for(j=0;j<DIMENSION;j++)
PWORST=individual[i].bestfitness;
worst=i;
}
}
//Kij n是num的邻居
float Kij(int num,int n){
float kij;
kij=(individual[num].fitness-individual[n].fitness)/(PWORST-PBEST);
return kij;
}
//Xij n是num的邻居
void Xij(int num,int n){
int j;
float d1=0.0,d2=0.0;
for(j=0;j<DIMENSION;j++){
d1=d1+(individual[num].number[j]-individual[n].number[j])*(individual[num].number[j]-individual[n].number[j]);
}
d2=sqrt(d1);
for(j=0;j<DIMENSION;j++){
xij[j]=(individual[n].number[j]-individual[num].number[j])/(d2+e);
}
}
//计算alocal
void Alocal(int num){
int i,j;
float d=0;//每个磷虾的感知距离
float d1=0,d2=0;
float kij;//适应度
for( i=0;i<POPSIZE;i++){
for(j=0;j<DIMENSION;j++){
d1=d1+(individual[num].number[j]-individual[i].number[j])*(individual[num].number[j]-individual[i].number[j]);
}
d=d+sqrt(d1);
}
d=d/(5*POPSIZE);
for( i=0;i<POPSIZE;i++){
for(j=0;j<DIMENSION;j++){
d1=d1+(individual[num].number[j]-individual[i].number[j])*(individual[num].number[j]-individual[i].number[j]);
}
d2=sqrt(d1);
if(d2<d){
kij=Kij(num,i);
Xij(num,i);
for(j=0;j<DIMENSION;j++){
alocal[j]=alocal[j]+kij*xij[j];
}
}
}
}
//计算atarget
void Atarget(int I,int num){
float c_best;
float kij;
c_best=2*(rdft()+I/Imax);
kij=Kij(num,Best);
Xij(num,Best);
for(int j=0;j<DIMENSION;j++){
atarget[j]=c_best*kij*xij[j];
}
}
//计算受诱导运动的速度
void Induced_Motion(int I,int num)
{
int i,j;
Alocal(num);
Atarget(I,num); //最好个体的影响
for(j=0;j<DIMENSION;j++)
individual[num].N[j]=Nmax*(alocal[j]+atarget[j])+rdft()*individual[num].N[j];
}
//计算中心食物的位置
void x_food(){
float fit=0;
int i,j;
for(j=0;j<DIMENSION;j++){
for(i=0;i<POPSIZE;i++){
individual[POPSIZE].number[j]=individual[POPSIZE].number[j]+individual[i].number[j]/individual[i].fitness;
}
}
for(i=0;i<POPSIZE;i++){
fit=fit+1/individual[i].fitness;
}
for(j=0;j<DIMENSION;j++)
individual[POPSIZE].number[j]=individual[POPSIZE].number[j]/fit;
}
void Bbest(int num){
int j;
float d1=0.0,d2=0.0;
for(j=0;j<DIMENSION;j++){
d1=d1+(individual[num].number[j]-individual[num].best[j])*(individual[num].number[j]-individual[num].best[j]);
}
d2=sqrt(d1);
for(j=0;j<DIMENSION;j++){
xij[j]=(individual[num].number[j]-individual[num].best[j])/(d2+e);
}
}
//觅食运动的影响
void Foraging_Motion(int I,int num){//I是当前迭代次数
int j;
float Bfood[DIMENSION];
float Cfood=2*(1-I/Imax);
float kij;
float fit;
x_food(); //确定食物的位置,将食
kij=Kij(num,POPSIZE);
Xij(num,POPSIZE);
for(j=0;j<DIMENSION;j++){
Bfood[j]=Cfood*kij*xij[j];
}
fit=(individual[num].fitness-individual[num].bestfitness)/(PWORST-PBEST);
Bbest(num);
for(j=0;j<DIMENSION;j++){
bbest[j]=fit*xij[j];
individual[num].F[j]=Vf*(Bfood[j]+bbest[j])+rdft()*individual[num].F[j];
}
}
//随机扩散
void Physical_Motion(int I,int num){
int j;
float s;
for(j=0;j<DIMENSION;j++)
s=rand()%3+(-1);
individual[num].D[j]=Dmax*(1-I/Imax)*s;
}
//状态更新
void Motion_Process(int num){
int j;
float t=0,t1=0;
t1=U-L;
t=2*rdft()*t1*DIMENSION;
for(j=0;j<DIMENSION;j++)
individual[num].number[j]=individual[num].number[j]+t*(individual[num].N[j]+individual[num].F[j]+ individual[num].D [j]);
}
//交叉算子
void crossover(int num){
int j,flag=1;
float Cr;
float kij;
int r=rand()%POPSIZE+1;
kij=Kij(num,Best);
Cr=0.2*kij;
if(rand()<Cr){
while(r!=num){
for(j=0;j<DIMENSION;j++){
individual[num].number[j]=individual[r].number[j];
}
if(j==DIMENSION){
break;
}
}
}
}
//突变算子
void Mutation(int num){
int j;
float Mu;
float kij;
int p=rand()%POPSIZE+1;
int q=rand()%POPSIZE+1;
kij=Kij(num,Best);
Mu=0.05/kij;
if(rand()<Mu){
while(p!=num&&q!=num&&p!=q){
for(j=0;j<DIMENSION;j++){
individual[num].number[j]=individual[Best].number[j]+rdft()*(individual[p].number[j]-individual[q].number[j]);
}
if(j==DIMENSION){
break;
}
}
}
}
//计算适应度值
void calculation(int num){
int j;
float s=0.0;
for(j=0;j<DIMENSION;j++){
s=s+individual[num].number[j]*individual[num].number[j];
individual[num].fitness=s;
}
}