カオス粒子群アルゴリズム

21030 ワード

#include 
#include 
#include 
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)
            {
                for(j=0;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)
                {
                    for(j=0;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)
                {
                    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;jw[i][j];
                    }
                }
                cqpso_p();
                b=chaotic_whether( );
                if(b==true)
                    flag=flag+1;
                else flag=0;
                
                if(flag==chaotic_count)
                {
                    chaotic();
                    flag=0;
                }
                
            }
            cout<resen(g[0],g[1],g[2],g[3]))<<"\t"<0]<<"\t"<1]<<"\t"<2]<<"\t"<3]<<endl;
            //cout<
        }
        //        
        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)
            {
                y[i]=random();
            }
            for(j=1;j)
            {
                if(j==1)
                {
                    for(i=0;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)
                    {
                        yyy[i]=y[i];
                    }
                }
                if(j>1)
                {
                    for(i=0;i)
                    {
                        y[i]=4*y[i]*(1-y[i]);
                    }
                    for(i=0;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)
                        {
                            yyy[i]=y[i];
                        }

                    }
                }

            }
            
            if(f_chaoticresen(g[0],g[1],g[2],g[3])))
            {
                for(i=0;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)
            {
                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)
            {
                if(f[i]<temp)
                {
                    temp=f[i];
                }
            }
            for(i=0;i)
            {
                if(temp==f[i])
                {
                    for(j=0;j)
                    {
                        g[j]=p[i][j];
                    }
                    break;
                }
            }
        }

        //       
        void cqpso_update(int count )
        {
            int i,j;
            for(i=0;i)
            {
                for(j=0;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)
            {
                for(j=0;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)
    qo->CQPSOmethod(i);
}

 
転載先:https://www.cnblogs.com/dongzhuangdian/p/5151769.html