粒子群算法(PSO)的C实现

来源:互联网 发布:下载文件网页源码 编辑:程序博客网 时间:2024/06/09 15:09
/******************************************************* Copyright(c), 2017 Co.,Ltd.* All rights reserved.** FileName:     main.c* Description:  粒子群算法应用(PSO),计算函数 sin(x) 在x 0~3的范围的最大值* Author    :   Zpeg* Modified  :* Reviewer  :   http://www.cnblogs.com/lyrichu/p/6151272.html* Date      :   2017-04-12* Record    :*******************************************************/#include <stdio.h>#include <math.h>#include <time.h>#include <stdlib.h>#include <string.h>typedef struct{    int id;             // 粒子标识    double present;     // 粒子所在位置    double velocity;    // 粒子当前速度    double fitness;     // 粒子当前位置所得结果} ST_POP;#define MAX_TEST_NUM    30      // 最大寻优次数#define POP_SIZE        3      // 粒子群大小#define W               1       // 惯性系数#define C1              1.49445 // 群体加速度系数#define C2              1.49445 // 个体加速度系数#define MAX_POP_PRES    3       // 个体最大取值#define MIN_POP_PRES    0       // 个体最小取值#define MAX_POP_VELO    0.5     // 速度最大值#define MIN_POP_VELO    -0.5    // 速度最小值ST_POP st_pop_list[POP_SIZE];       // 粒子群ST_POP st_pop_pbest_list[POP_SIZE]; // 粒子的最优记录ST_POP st_pop_gbest;                // 当前粒子群最优粒子// 随机初始化void rand_init(){    srand(time(NULL));}// 适应度函数 get fitness valuedouble func(double x){    return sin(x);}// 粒子群计算适应值void pop_compute(){    int cnt = 0;    for(cnt = 0; cnt < POP_SIZE; cnt ++)    {        st_pop_list[cnt].fitness = func(st_pop_list[cnt].present);    }}// 粒子群初始化void pop_init(){    int cnt = 0;    for(cnt = 0; cnt < POP_SIZE; cnt ++)    {        memset(&st_pop_list[cnt], 0, sizeof(ST_POP));        memset(&st_pop_pbest_list[cnt], 0, sizeof(ST_POP));        st_pop_list[cnt].id = cnt;        st_pop_list[cnt].present = ((double)rand()/RAND_MAX) * 3;       //粒子位置范围  0~3        st_pop_list[cnt].velocity = ((double)rand()/RAND_MAX - 0.5);    //粒子速度范围  -0.5~0.5    }    pop_compute();}// 获取粒子群中最优粒子void PSO_GetGbest(){    int cnt = 0;    memset(&st_pop_gbest, 0 , sizeof(ST_POP));    for(cnt = 0; cnt < POP_SIZE; cnt ++)    {        if(st_pop_list[cnt].fitness > st_pop_gbest.fitness)        {            st_pop_gbest.id = st_pop_list[cnt].id;            st_pop_gbest.present = st_pop_list[cnt].present;            st_pop_gbest.fitness = st_pop_list[cnt].fitness;        }    }}// 历史位置中最优位置void PSO_GetPbest(){    int cnt = 0;    for(cnt = 0; cnt < POP_SIZE; cnt ++)    {        if(st_pop_list[cnt].fitness > st_pop_pbest_list[cnt].fitness)        {            st_pop_pbest_list[cnt].id = st_pop_list[cnt].id;            st_pop_pbest_list[cnt].present = st_pop_list[cnt].present;            st_pop_pbest_list[cnt].fitness = st_pop_list[cnt].fitness;        }    }}// 粒子群寻优函数void PSO_start(){    int num = 0;    // 寻优次数    int cnt = 0;    // 粒子索引    double prand = 0;    double grand = 0;    pop_init();    PSO_GetGbest();    PSO_GetPbest();    // 迭代寻优    for(num = 0; num < MAX_TEST_NUM; num++)    {        for(cnt = 0; cnt < POP_SIZE; cnt ++)        {            // 更新速度            prand = (double)rand()/RAND_MAX; // 0~1之间随机            grand = (double)rand()/RAND_MAX;            st_pop_list[cnt].velocity = W*st_pop_list[cnt].velocity +                    C1*prand*(st_pop_pbest_list[cnt].present - st_pop_list[cnt].present) +                    C2*grand*(st_pop_gbest.present - st_pop_list[cnt].present);            if(st_pop_list[cnt].velocity > MAX_POP_VELO)                st_pop_list[cnt].velocity = MAX_POP_VELO;            else if(st_pop_list[cnt].velocity < MIN_POP_VELO)                st_pop_list[cnt].velocity = MIN_POP_VELO;            // 更新位置            st_pop_list[cnt].present += st_pop_list[cnt].velocity;            if(st_pop_list[cnt].present > MAX_POP_PRES)                st_pop_list[cnt].present = MAX_POP_PRES;            else if(st_pop_list[cnt].present < MIN_POP_PRES)                st_pop_list[cnt].present = MIN_POP_PRES;        }        pop_compute();        PSO_GetGbest();        PSO_GetPbest();        if(st_pop_gbest.fitness > 0.9995)        {            printf("==========================\n");            printf("Test num : %d\n", num);            printf("Best present %f!\n", st_pop_gbest.present);            printf("Get effective fitness %f!\n", st_pop_gbest.fitness);            printf("==========================\n");            break;        }        else        {            printf("%d time \n", num);            printf("present: %f!\n", st_pop_gbest.present);            printf("Get fitness: %f \n", st_pop_gbest.fitness);            printf("\n");        }    }}int main(){    rand_init();    PSO_start();    return 0;}
0 0
原创粉丝点击