标题:一个关于粒子群算法的程序
只看楼主
enjoylhl
Rank: 1
等 级:新手上路
帖 子:18
专家分:4
注 册:2013-9-23
结帖率:60%
已结贴  问题点数:30 回复次数:4 
一个关于粒子群算法的程序
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#define P_num 30
#define dm 160
#define iter_num 500
#define DT 1100
#define V_max 20  /*待定*/
#define c1 2
#define c2 2
#define w 0.6
#define maxwccm 25
#define c 0.001
#define c0 20
#define m 85
#define c3 0.2
#define c4 0.2
double particle[P_num][dm];
double particle_loc_best[P_num][dm];
double particle_loc_fit[P_num];
double particle_glo_best[dm];
double gfit;
double particle_v[P_num][dm];
double particle_fit[P_num];

void main()
{
    void initial();
    void renew_particle();
    void renew_var();
    double cost(double a[]);
    double fitness(double a[]);
    int i=0;
    srand((unsigned)time(NULL));
    initial();
    while(i<iter_num)
    {
        renew_particle();
        i++;
    }
    printf("最优值为%.10lf\n", gfit);
    for(i=0;i<dm;i++)
    {
        printf("%f\n",particle_glo_best[i]);
    }
}


double fitness(double a[])
{
    double cost(double a[]);
    return cost(a);
}


void initial()
{
    int a[160]={0,40,52,76,88,112,124,164,
        40,80,92,116,128,152,164,204,
        80,120,132,156,168,192,204,244,
        120,160,172,196,208,232,244,284,
        160,200,212,236,248,272,284,324,
        200,240,252,276,288,312,324,364,
        240,280,292,316,328,352,364,404,
        280,320,332,356,368,392,404,444,
        320,360,372,396,408,432,444,484,
        360,400,412,436,448,472,484,524,
        400,440,452,476,488,512,524,564,
        440,480,492,516,528,552,564,604,
        480,520,532,556,568,592,604,644,
        520,560,572,596,608,632,644,684,
        560,600,612,636,648,672,684,724,
        600,640,652,676,688,712,724,764,
        640,680,692,716,728,752,764,804,
        680,720,732,756,768,792,804,844,
        720,760,772,796,808,832,844,884,
        760,800,812,836,848,872,884,924};
    int i,j;
    double b0,b1,b2,b3,b4,b5,b6,b7;
    for(i=1;i<P_num;i++)
    {
        while(1)
        {
            b0=particle[0][0];
            b1=particle[0][1]+rand()%10;
            b2=particle[0][2]+rand()%10;
            b3=particle[0][3]+rand()%10;
            b4=particle[0][4]+rand()%10;
            b5=particle[0][5]+rand()%10;
            b6=particle[0][6]+rand()%10;
            b7=particle[0][7]+rand()%10;
            if(b1-b0>40&&b1-b0<48&&b2-b1>9&&b2-b1<15&&b3-b2>24&&b3-b2<32&&b4-b3>9&&b4-b3<15&&
            b5-b4>24&&b5-b4<32&&b6-b5>15&&b6-b5<20&&b7-b6>40&&b7-b6<48) break;
        }
        particle[i][0]=b0;
        particle[i][1]=b1;
        particle[i][2]=b2;
        particle[i][3]=b3;
        particle[i][4]=b4;
        particle[i][5]=b5;
        particle[i][6]=b6;
        particle[i][7]=b7;

        for(j=1;j<20;j++)
        {
            while(1)
            {
                b0=particle[0][8*j]+rand()%10;
                b1=particle[0][8*j+1]+rand()%10;
                b2=particle[0][8*j+2]+rand()%10;
                b3=particle[0][8*j+3]+rand()%10;
                b4=particle[0][8*j+4]+rand()%10;
                b5=particle[0][8*j+5]+rand()%10;
                b6=particle[0][8*j+6]+rand()%10;
                b7=particle[0][8*j+7]+rand()%10;
                if(b1-b0>40&&b1-b0<48&&b2-b1>9&&b2-b1<15&&b3-b2>24&&b3-b2<32&&b4-b3>9&&b4-b3<15&&b5-b4>24&&
                    b5-b4<32&&b6-b5>15&&b6-b5<20&&b7-b6>40&&b7-b6<48
                    &&b0-particle[i][8*j-7]>0&&b0-particle[i][8*j-7]<5
                    &&b6-particle[i][8*j-1]>0&&b6-particle[i][8*j-1]<5)
                    break;
            }
            particle[i][8*j]=b0;
            particle[i][8*j+1]=b1;
            particle[i][8*j+2]=b2;
            particle[i][8*j+3]=b3;
            particle[i][8*j+4]=b4;
            particle[i][8*j+5]=b5;
            particle[i][8*j+6]=b6;
            particle[i][8*j+7]=b7;
        }
    }


    for(i=0; i<P_num; i++)
    {
        for(j=0; j<dm; j++)
        {
            particle_loc_best[i][j]=particle[i][j];
            particle_v[i][j]=V_max*rand()/RAND_MAX;   /*速度初始化*/
        }
    }

    for(i=0; i<P_num; i++)
    {
        particle_fit[i]=fitness(particle[i]);     /*初始化各粒子适应值*/
        particle_loc_fit[i]=particle_fit[i];      /*初始化各粒子局部最优值*/
    }
    gfit=particle_loc_fit[0];                     /*初始化全局最优值*/


    j=0;
    for(i=1;i<P_num;i++)
    {
        if(particle_loc_fit[i]<gfit)
        {
            gfit = particle_loc_fit[i];                 /*求出当前全局最优值*/
            j = i;
        }
    }


    for(i=0; i<dm; i++)
    {
        particle_glo_best[i]=particle_loc_best[j][i];   /*初始化全局最优位置*/
    }
}



void renew_particle()
{
    void renew_var();
    int i,j;
    double a[160];
    double b0,b1,b2,b3,b4,b5,b6,b7;
    double h;
    int k,l;


    for(i=0;i<P_num;i++)
    {
        k=1+(int)14*rand()/RAND_MAX;   /*将精炼环节时长大的炉次随机排序(不从第一炉次开始)*/
        for(j=k;j<k+5;j++)
            particle[i][8*j+3]=particle[i][8*j+3]+8;


        l=(int)2*rand()/RAND_MAX;      /*将VD环节时长大的炉次随机排序*/
        for(j=k+l;j<k+l+3;j++)
            particle[i][8*j+5]=particle[i][8*j+5]+8;


        while(1) /*对第一炉次赋值*/
        {
            renew_var();
            b0=particle[i][0];
            b1=particle[i][1]+particle_v[i][1];
            b2=particle[i][2]+particle_v[i][2];
            b3=particle[i][3]+particle_v[i][3];
            b4=particle[i][4]+particle_v[i][4];
            b5=particle[i][5]+particle_v[i][5];
            b6=particle[i][6]+particle_v[i][6];
            b7=particle[i][7]+particle_v[i][7];
            if(b1>0&&b2>0&&b3>0&&b4>0&&b5>0&&b6>0&&b7>0&&b1-b0>40&&b1-b0<50&&b2-b1>9&&b2-b1<15&&b3-b2>24&&b3-b2<32&&
            b4-b3>9&&b4-b3<15&&b5-b4>24&&b5-b4<32&&b6-b5>15&&b5-b4<20&&b7-b6>40&&b7-b6<50) break;
        }
        particle[i][0]=b0;
        particle[i][1]=b1;
        particle[i][2]=b2;
        particle[i][3]=b3;
        particle[i][4]=b4;
        particle[i][5]=b5;
        particle[i][6]=b6;
        particle[i][7]=b7;

        for(j=1;j<20;j++)   /*对余下炉次赋值*/
        {
            if(particle[i][8*j+3]-particle[i][8*j+2]>32)
            {
                if(particle[i][8*j+5]-particle[i][8*j+4]>32)
                {
                    while(1)
                    {
                        renew_var();
                        b0=particle[i][8*j]+particle_v[i][8*j+1];
                        b1=particle[i][8*j+1]+particle_v[i][8*j+1];
                        b2=particle[i][8*j+2]+particle_v[i][8*j+2];
                        b3=particle[i][8*j+3]+particle_v[i][8*j+3];
                        b4=particle[i][8*j+4]+particle_v[i][8*j+4];
                        b5=particle[i][8*j+5]+particle_v[i][8*j+5];
                        b6=particle[i][8*j+6]+particle_v[i][8*j+6];
                        b7=particle[i][8*j+7]+particle_v[i][8*j+7];
                        if(b0>0&&b1>0&&b2>0&&b3>0&&b4>0&&b5>0&&b6>0&&b7>0&&
                            b1-b0>40&&b1-b0<50&&b2-b1>9&&b2-b1<15&&
                            b3-b2>32&&b3-b2<40&&b4-b3>9&&b4-b3<15&&
                            b5-b4>32&&b5-b4<40&&b6-b5>15&&b6-b5<20&&
                            b7-b6>40&&b7-b6<50&&
                            b0-particle[i][8*j-7]>0&&b0-particle[i][8*j-7]<5&&
                            b2-particle[i][8*j-5]>0&&b2-particle[i][8*j-5]<5&&
                            b4-particle[i][8*j-3]>0&&b4-particle[i][8*j-3]<5&&
                            b6-particle[i][8*j-1]>0&&b6-particle[i][8*j-1]<5)  break;
                    }
                    particle[i][8*j]=b0;
                    particle[i][8*j+1]=b1;
                    particle[i][8*j+2]=b2;
                    particle[i][8*j+3]=b3;
                    particle[i][8*j+4]=b4;
                    particle[i][8*j+5]=b5;
                    particle[i][8*j+6]=b6;
                    particle[i][8*j+7]=b7;
                }
                else
                {
                    while(1)
                    {
                        renew_var();
                        b0=particle[i][8*j]+particle_v[i][8*j+1];
                        b1=particle[i][8*j+1]+particle_v[i][8*j+1];
                        b2=particle[i][8*j+2]+particle_v[i][8*j+2];
                        b3=particle[i][8*j+3]+particle_v[i][8*j+3];
                        b4=particle[i][8*j+4]+particle_v[i][8*j+4];
                        b5=particle[i][8*j+5]+particle_v[i][8*j+5];
                        b6=particle[i][8*j+6]+particle_v[i][8*j+6];
                        b7=particle[i][8*j+7]+particle_v[i][8*j+7];
                        if(b0>0&&b1>0&&b2>0&&b3>0&&b4>0&&b5>0&&b6>0&&b7>0&&
                            b1-b0>40&&b1-b0<50&&b2-b1>9&&b2-b1<15&&
                            b3-b2>32&&b3-b2<40&&b4-b3>9&&b4-b3<15&&
                            b5-b4>24&&b5-b4<32&&b6-b5>15&&b6-b5<20&&
                            b7-b6>40&&b7-b6<50&&
                            b0-particle[i][8*j-7]>0&&b0-particle[i][8*j-7]<5&&
                            b2-particle[i][8*j-5]>0&&b2-particle[i][8*j-5]<5&&
                            b4-particle[i][8*j-3]>0&&b4-particle[i][8*j-3]<5&&
                            b6-particle[i][8*j-1]>0&&b6-particle[i][8*j-1]<5)  break;
                    }
                    particle[i][8*j]=b0;
                    particle[i][8*j+1]=b1;
                    particle[i][8*j+2]=b2;
                    particle[i][8*j+3]=b3;
                    particle[i][8*j+4]=b4;
                    particle[i][8*j+5]=b5;
                    particle[i][8*j+6]=b6;
                    particle[i][8*j+7]=b7;
                }
            }
            else
            {
                while(1)
                {
                    renew_var();
                    b0=particle[i][8*j]+particle_v[i][8*j+1];
                    b1=particle[i][8*j+1]+particle_v[i][8*j+1];
                    b2=particle[i][8*j+2]+particle_v[i][8*j+2];
                    b3=particle[i][8*j+3]+particle_v[i][8*j+3];
                    b4=particle[i][8*j+4]+particle_v[i][8*j+4];
                    b5=particle[i][8*j+5]+particle_v[i][8*j+5];
                    b6=particle[i][8*j+6]+particle_v[i][8*j+6];
                    b7=particle[i][8*j+7]+particle_v[i][8*j+7];
                    if(b0>0&&b1>0&&b2>0&&b3>0&&b4>0&&b5>0&&b6>0&&b7>0&&
                        b1-b0>40&&b1-b0<50&&b2-b1>9&&b2-b1<15&&
                        b3-b2>24&&b3-b2<32&&b4-b3>9&&b4-b3<15&&
                        b5-b4>24&&b5-b4<32&&b6-b5>15&&b6-b5<20&&
                        b7-b6>40&&b7-b6<50&&
                        b0-particle[i][8*j-7]>0&&b0-particle[i][8*j-7]<5&&
                        b2-particle[i][8*j-5]>0&&b2-particle[i][8*j-5]<5&&
                        b4-particle[i][8*j-3]>0&&b4-particle[i][8*j-3]<5&&
                        b6-particle[i][8*j-1]>0&&b6-particle[i][8*j-1]<5)  break;
                }
                    particle[i][8*j]=b0;
                    particle[i][8*j+1]=b1;
                    particle[i][8*j+2]=b2;
                    particle[i][8*j+3]=b3;
                    particle[i][8*j+4]=b4;
                    particle[i][8*j+5]=b5;
                    particle[i][8*j+6]=b6;
                    particle[i][8*j+7]=b7;
            }
        }
    }


        for(j=0;j<dm;j++)          /*对最后一炉次完工期超过交付期限的粒子处理方法*/
        {
            a[j]=particle[i][j];
        }

        if(a[159]>DT)
        {
            h=50*rand()/RAND_MAX;
            for(j=0;j<dm;j++)
                a[j]=DT-50-h;
        }
        for(j=0;j<dm;j++)
            particle[i][j]=a[j];
}




void renew_var()
{
    int i,j;
    for(i=0;i<P_num;i++)
    {
        particle_fit[i]=fitness(particle[i]);
        if(particle_fit[i]<particle_loc_fit[i])
        {
            particle_loc_fit[i]=particle_fit[i];
            for(j=0;j<dm;j++)
            {
                particle_loc_best[i][j]=particle[i][j];
            }
        }
    }


    for(i=0,j=-1;i<P_num; i++)
    {
        if(particle_loc_fit[i]<gfit)
        {
            gfit=particle_loc_fit[i];
            j=i;
        }
    }


    if(j!=-1)
    {
        for(i=0;i<dm;i++)
        {
            particle_glo_best[i]=particle_loc_best[j][i];
        }
    }



    for(i=0;i<P_num;i++)
    {
        for(j=0;j<dm;j++)
        {
            particle_v[i][j]=w*particle_v[i][j]+
                c1*rand()/RAND_MAX*(particle_loc_best[i][j]-particle[i][j])+
                c2*rand()/RAND_MAX*(particle_glo_best[j]-particle[i][j]);
            if(particle_v[i][j]>V_max)
                particle_v[i][j]=V_max;
            else
                if(particle_v[i][j]<-V_max)
                particle_v[i][j]=-V_max;
                else
                    particle_v[i][j]=particle_v[i][j];
        }
    }
}


double cost(double a[])
{
    double sum=0;
    int i;
    for(i=0;i<20;i++)
        {
            if((a[8*i+6]-a[8*i+5])>maxwccm)
            {
                sum=sum+c0*(a[8*i+7]-a[8*i])+c*(DT-a[8*i+7])*m+c3*(a[8*i+6]-a[8*i+5])*m+c4*((a[8*i+6]-a[8*i+5])-maxwccm)*m;
            }
            else
            {
                sum=sum+c0*(a[8*i+7]-a[8*i])+c*(DT-a[8*i+7])*m+c3*(a[8*i+6]-a[8*i+5])*m;
            }
        }
    return sum;
}

问题就出在while循环内,也就是if括号后的内容,是粒子群算法的约束条件,我不知道是有死循环还是有其他原因,总之就是运行不出来结果;但是把这些约束都去掉的话就行。
搜索更多相关主题的帖子: 1100 double include 
2014-05-10 21:09
top398
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:2
帖 子:427
专家分:857
注 册:2014-5-2
得分:15 
                    while(1)
                    {
                        renew_var();
                        b0=particle[i][8*j]+particle_v[i][8*j+1];
                        b1=particle[i][8*j+1]+particle_v[i][8*j+1];
                        b2=particle[i][8*j+2]+particle_v[i][8*j+2];
                        b3=particle[i][8*j+3]+particle_v[i][8*j+3];
                        b4=particle[i][8*j+4]+particle_v[i][8*j+4];
                        b5=particle[i][8*j+5]+particle_v[i][8*j+5];
                        b6=particle[i][8*j+6]+particle_v[i][8*j+6];
                        b7=particle[i][8*j+7]+particle_v[i][8*j+7];
                        if(b0>0&&b1>0&&b2>0&&b3>0&&b4>0&&b5>0&&b6>0&&b7>0&&
                            b1-b0>40&&b1-b0<50&&b2-b1>9&&b2-b1<15&&
                            b3-b2>32&&b3-b2<40&&b4-b3>9&&b4-b3<15&&
                            b5-b4>32&&b5-b4<40&&b6-b5>15&&b6-b5<20&&
                            b7-b6>40&&b7-b6<50&&
                            b0-particle[i][8*j-7]>0&&b0-particle[i][8*j-7]<5&&
                            b2-particle[i][8*j-5]>0&&b2-particle[i][8*j-5]<5&&
                            b4-particle[i][8*j-3]>0&&b4-particle[i][8*j-3]<5&&
                            b6-particle[i][8*j-1]>0&&b6-particle[i][8*j-1]<5)  break;
                    }

是说这种语句吗?
究竟是产生的数据根本不会满足约束条件,还是约束条件描述错误?
写得实在让人难以看下去。
如果确实是这里的问题,建议你先将
                        renew_var();
每次生成的数据在调试器中查看,或打印出来(重定向到文件或直接写入文件),仔细检查,确保生成数据在正确的范围,然后才考虑条件判断的问题。
2014-05-10 23:57
enjoylhl
Rank: 1
等 级:新手上路
帖 子:18
专家分:4
注 册:2013-9-23
得分:0 
回复 2 楼 top398
就是这种语句,是模型中的约束条件;
2014-05-11 10:16
top398
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:2
帖 子:427
专家分:857
注 册:2014-5-2
得分:15 
例如这么一种判断:
b7-b6>40&&b7-b6<50
在代码中反复出现,你应该写一个判断函数,则上面的判断可变为:
range(b7-b6, 40, 50)
这不是更为简洁易读吗?
2014-05-11 11:41
enjoylhl
Rank: 1
等 级:新手上路
帖 子:18
专家分:4
注 册:2013-9-23
得分:0 
回复 4 楼 top398
恩恩!谢谢,受用了!!
2014-05-11 14:11



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-431335-1-1.html




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

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