| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 366 人关注过本帖
标题:粒子群算法测试函数问题
取消只看楼主 加入收藏
cockcans
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2014-5-11
结帖率:0
收藏
已结贴  问题点数:20 回复次数:0 
粒子群算法测试函数问题
#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.。。求大神指点。。。

搜索更多相关主题的帖子: include double 
2014-05-20 16:14
快速回复:粒子群算法测试函数问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017421 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved