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

本文详细介绍了如何使用C语言实现粒子群优化算法(PSO),包括算法的基本原理、关键步骤以及C代码实现,帮助读者理解并掌握PSO算法在实际问题中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/******************************************************
* 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 value
double 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;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值