粒子群算法测试函数问题
#include"stdio.h"#include<iostream.h>
#include"stdlib.h"
#include"time.h"
#include"math.h"
#include <fstream.h>//处理文件
#include <windows.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 40
#define DIMENSION 10 //维数
#define MAXITER 1000
//全局变量定义
double w=0.9;
double c1=1.8;
double c2=1.8;
float VMAX=100;
float VMIN=-100;
float XMIN=-100;
float XMAX=100;
float P[DIMENSION];
float PBEST;
struct indi
{float number[DIMENSION];
float best[DIMENSION];
float bestfitness;
float fitness;
float speed[DIMENSION];
}individual[POPSIZE];
void initiate(void);
void calculation(int number);
void globalbest(int number);
void localbest(int number);
//程序初始化定义
void initiate()
{int i,j;
for(i=0;i<POPSIZE;i++)
{
for(j=0;j<DIMENSION;j++)
{
individual[i].number[j]=rdft()*(XMAX-XMIN)+XMIN;
}
}
for(i=0;i<POPSIZE;i++)
{
for(j=0;j<DIMENSION;j++)
{
individual[i].speed[j]=(VMAX-VMIN)*rdft()+VMIN;
}
}
for(i=0;i<POPSIZE;i++)
{
for(j=0;j<DIMENSION;j++)
{
individual[i].best[j]=individual[i].number[j];
}
}
for(i=0;i<POPSIZE;i++)
{
calculation(i);
}
for(i=0;i<POPSIZE;i++)
{
individual[i].bestfitness=individual[i].fitness;
}
globalbest(0);
}
//微粒历史最优位置修改程序
void localbest(int number)
{
int i;
if (individual[number].bestfitness>individual[number].fitness)
{
for(i=0;i<DIMENSION;i++)
individual[number].best[i]=individual[number].number[i];
}
individual[number].bestfitness=individual[number].fitness;
}
//种群历史最优位置修改程序
void globalbest(int number)
{
int i,j,flag;
float s=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(i=0;i<DIMENSION;i++)
{
P[i]=individual[flag].number[i];
}
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 calculation(int num)
{
double s=0.0;
for(int i=0;i<DIMENSION;i++)
{
s+=pow(individual[num].number[i],2);
}
individual[num].fitness=s;
}
//更新速度和位置
void UpdateSpeedandPosition()
{
int i,j;
i=0;j=0;
for(i=0;i<POPSIZE;i++)
{
for(j=0;j<DIMENSION;j++)
{ individual[i].speed[j]=w*individual[i].speed[j]+c1*rdft()*(individual[i].best[j]-individual[i].number[j])+c2*rdft()*(P[j]-individual[i].number[j]);
if(individual[i].speed[j]>VMAX)
{
individual[i].speed[j]=VMAX;
}
if(individual[i].speed[j]<-VMAX)
{
individual[i].speed[j]=-VMAX;
}
individual[i].number[j]=individual[i].number[j]+individual[i].speed[j];
if(individual[i].number[j]<-XMAX)
{
individual[i].number[j]=-XMAX;
}
if(individual[i].number[j]>XMAX)
{
individual[i].number[j]=XMAX;
}
}
}
}
//主程序
void main()
{
int i,j,total;
i=0;
j=0;
total=0;
ofstream fout("data.txt");//将数据输入到C:\data.txt
SYSTEMTIME mytime;
GetLocalTime(&mytime);
fout<<"程序开始执行时间是:"<<mytime.wYear<<"年"<<mytime.wMonth<<"月"
<<mytime.wDay<<"日"<<mytime.wHour<<"时"<<mytime.wMinute<<"分"
<<mytime.wSecond<<"秒"<<mytime.wMilliseconds<<"毫秒 "<<"今天是星期"<<mytime.wDayOfWeek<<endl;
initiate();//初始化每个粒子
for (i=0;i<MAXITER;i++)
{
w=0.9-i*0.5/MAXITER;//动态修改权重
for (j=0;j<POPSIZE;j++) //计算每个粒子的适应值
{
calculation(j);
}
for (j=0;j<POPSIZE;j++) //计算更新粒子个体历史最优解
{
localbest(j);
}
for (j=0;j<POPSIZE;j++) //更新僵局最优解保存到PBEST和P[DIMENSION]中
{
globalbest(j);
}
UpdateSpeedandPosition();
total++;
cout<<"第"<<total<<"次迭代结果是:"<<"PBEST= "<<PBEST<<" "<<endl;
fout<<total<<" "<<PBEST<<endl;
}
GetLocalTime(&mytime);
fout<<"程序结束执行时间是:"<<mytime.wYear<<"年"<<mytime.wMonth<<"月"
<<mytime.wDay<<"日"<<mytime.wHour<<"时"<<mytime.wMinute<<"分"
<<mytime.wSecond<<"秒"<<mytime.wMilliseconds<<"毫秒 "<<"今天是星期"<<mytime.wDayOfWeek<<endl;
cout<<"最优解是:"<<PBEST<<endl;
}
这个测试函数为x(x1,x2,x3,...,xi)的平方连加形式,其最小值是当x1=x2=x3=...=xi=0时。y=0。该粒子群算法中维数就是i
问题来了,应该无论i是多少,最小值都应该是0才对。当我把维数改成20,就是i=20,迭代最优结果却是y=10000.。。求大神指点。。。