混沌粒子群算法

 

#include <iostream>
#include <math.h>
#include <time.h>
using namespace std;

#define M 50  //群体数目50
#define N 4   //每个粒子的维数4
#define NN 500 //迭代次数
#define chaotic_count 3    //判断是否进入停滞状态
#define gama 0.001      
#define R 0.8
#define chaotic_counts 100   //混沌搜索的迭代次数
//测试类
class TestFunction
{
    public:
        double resen(double x1,double x2,double x3,double x4)
        {
            double s=0;
            s=100*(x2-x1*x1)*(x2-x1*x1)+(1-x1)*(1-x1)+s;
            s=100*(x3-x2*x2)*(x3-x2*x2)+(1-x2)*(1-x2)+s;
            s=100*(x4-x3*x3)*(x4-x3*x3)+(1-x3)*(1-x3)+s;
            return s;
        }
};

class CQPSO
{
    private:
        double (*w)[N];// = new double[50][4]; //总体粒子
        double *f;//=new double[M];//适应度值
        double *ff;//=new double[M];//相对f的比较值
        double (*p)[N];//=new double[M][N];
        double (*v)[N];//粒子更新速度
        double *g;//=new double[N];
        double c1;
        double c2;
        int flag;//监测是否进入混沌状态
        TestFunction *tf;// = new TestFunction;
        double random()
        {
            double s;
            s=(abs(rand())%10000+10000)/10000.0-1.0;    
            return s;
        }
    public:
        CQPSO( )
        {
            int i,j;
            w=new double[M][N];
            v=new double[M][N];
            f=new double[M];
            ff=new double[M];
            p=new double[M][N];
            g=new double[N];
            tf=new TestFunction;
            for(i=0;i<M;i++)
            {
                for(j=0;j<N;j++)
                {
                    w[i][j]=random();
                    v[i][j]=random();
                }
            }
            c1=2;
            c2=2;
            flag=0;
        }

        void CQPSOmethod(int count)
        {
            int i,j;
            bool b;
            if(count==1)
            {
                for(i=0;i<M;i++)
                {
                    for(j=0;j<N;j++)
                    {
                        p[i][j]=w[i][j];
                    }
                    f[i]=tf->resen(w[i][0],w[i][1],w[i][2],w[i][3]);
                }
                cqpso_p();//得出全局最优
            }

            if(count>1)
            {
                cqpso_update(count);
                for(i=0;i<M;i++)
                {
                    ff[i]=tf->resen(w[i][0],w[i][1],w[i][2],w[i][3]);
                    if(ff[i]<f[i])
                    {    
                        f[i]=ff[i];
                        for(j=0;j<N;j++) p[i][j]=w[i][j];
                    }
                }
                cqpso_p();
                b=chaotic_whether( );
                if(b==true)
                    flag=flag+1;
                else flag=0;
                
                if(flag==chaotic_count)
                {
                    chaotic();
                    flag=0;
                }
                
            }
            cout<<(tf->resen(g[0],g[1],g[2],g[3]))<<"\t"<<g[0]<<"\t"<<g[1]<<"\t"<<g[2]<<"\t"<<g[3]<<endl;
            //cout<<g[0]<<"\t"<<g[1]<<"\t"<<g[2]<<"\t"<<g[3]<<endl;
        }
        //混沌搜索核心算法
        void chaotic()
        {
            int i,j;
            double *y=new double[N];
            double *yy=new double[N];
            double *yyy=new double[N];
            double f_chaotic;//*f_chaotic=new double[chaotic_counts];
            double ff_chaotic;
            for(i=0;i<N;i++)
            {
                y[i]=random();
            }
            for(j=1;j<chaotic_counts;j++)
            {
                if(j==1)
                {
                    for(i=0;i<N;i++)
                    {
                        yy[i]=g[i]+R*(2*y[i]-1);
                    }
                    f_chaotic=tf->resen(yy[0],yy[1],yy[2],yy[3]);
                    for(i=0;i<N;i++)
                    {
                        yyy[i]=y[i];
                    }
                }
                if(j>1)
                {
                    for(i=0;i<N;i++)
                    {
                        y[i]=4*y[i]*(1-y[i]);
                    }
                    for(i=0;i<N;i++)
                    {
                        yy[i]=g[i]+R*(2*y[i]-1);
                    }
                    ff_chaotic=tf->resen(yy[0],yy[1],yy[2],yy[3]);
                    if(ff_chaotic<f_chaotic)
                    {
                        f_chaotic=ff_chaotic;
                        for(i=0;i<N;i++)
                        {
                            yyy[i]=y[i];
                        }

                    }
                }

            }
            
            if(f_chaotic<(tf->resen(g[0],g[1],g[2],g[3])))
            {
                for(i=0;i<N;i++)
                {
                    g[i]=yyy[i];
                }
            }            
        }

       //判断是否进入混沌状态
        bool chaotic_whether( )
        {
            double Fbest;
            Fbest=tf->resen(g[0],g[1],g[2],g[3]);
            double temp=ff[0];
            int i;//,j;
            for(i=1;i<M;i++)
            {
                if(ff[i]<temp)
                {
                    temp=ff[i];
                }
            }
            if(((temp-Fbest)/temp)<gama)
                return true;
            else return false;
        }

        double ww(int count)
        {
            double wmax=0.9;
            double wmin=0.1;
            double wx=0.9-count*(0.8/NN);
            return wx;
        }
        //得到个体最优中最小值——全局最优
        void cqpso_p()
        {
            double temp=f[0];
            int i,j;
            for(i=1;i<M;i++)
            {
                if(f[i]<temp)
                {
                    temp=f[i];
                }
            }
            for(i=0;i<M;i++)
            {
                if(temp==f[i])
                {
                    for(j=0;j<N;j++)
                    {
                        g[j]=p[i][j];
                    }
                    break;
                }
            }
        }

        //粒子的更新过程
        void cqpso_update(int count )
        {
            int i,j;
            for(i=0;i<M;i++)
            {
                for(j=0;j<N;j++)
                    v[i][j]=ww(count)*v[i][j]+c1*random()*(p[i][j]-w[i][j])+c2*random()*(g[j]-w[i][j]);
            }
            for(i=0;i<M;i++)
            {
                for(j=0;j<N;j++)
                    w[i][j]=w[i][j]+v[i][j];
            }
        }   
};

int main()
{
    int i;
    srand((unsigned)time(0)); 
    CQPSO *qo = new CQPSO();
    for(i=1;i<NN;i++)
    qo->CQPSOmethod(i);
}

 

posted @ 2016-01-22 16:55  小德cyj  阅读(3656)  评论(0编辑  收藏  举报