首页 > 编程语言 >粒子群算法(Particle Swarm Optimization, PSO)

粒子群算法(Particle Swarm Optimization, PSO)

时间:2023-09-17 22:46:31浏览次数:49  
标签:PSO Particle gBest Iter Swarm value np param Best

Particle Swarm Optimization

算法原理参考: https://zhuanlan.zhihu.com/p/404198434

Question

使用PSO算法计算函数$ f(x) = x_1^2 + 3 x_2^2 - x_1 + 2 x_2 - 5 $ 在 \(x \in [-100, 100]^2\) 上的最小值

Code

import numpy as np
from typing import List

Parameters

dim = 2      # varible dimension
N = 200       # number of partials 
max_v = 20   # the upper bound of velocity
max_iter = 50 # max iter
boundary = np.array([[-100, 100], [-100, 100]]) # [[dimK Lower Bound, dimK Upper Bound]]

c1 = 0.9     # inertia weight
c2 = 2       # neighborhood accelerate coefficient
c3 = 2       # global best accelerate coefficient

param = {
    'c1' : c1,
    'c2' : c2,
    'c3' : c3,
    'boundary': boundary
}


## compute the f
def f(t: List[float]) -> float:
    
    # x = t[0]
    # y = t[1]
    # z =((1*np.cos((1+1)*x+1))+(2*np.cos((2+1)*x+2))+(3*np.cos((3+1)*x+3))+ \
    #     (4*np.cos((4+1)*x+4))+(5*np.cos((5+1)*x+5)))*((1*np.cos((1+1)*y+1))+ \
    #     (2*np.cos((2+1)*y+2))+(3*np.cos((3+1)*y+3))+(4*np.cos((4+1)*y+4))+(5*np.cos((5+1)*y+5)))

    z = t[0]**2 + 3 * t[1]**2 - t[0] + 2 * t[1] - 5
    return z

PSO algorithm

## limit position
def limit_p(P, boundary):
    N = P.shape[0]
    # for each dimension
    def fun(a):
        if a < boundary[i][0]:
            return boundary[i][0]
        elif a > boundary[i][1]:
            return boundary[i][1]
        else:
            return a
    

    for i in range(boundary.shape[0]):
        P[:, i] = np.array(list(map(fun, P[:, i])))

    return P


## limit velocity
def limit_v(V, boundary):
    N = V.shape[0]
    # for each dimension
    for i in range(boundary.shape[0]):
        # the max velocity may not proceed 10% boundary size
        lim = 0.1 * (boundary[i][1] - boundary[i][0])
        V[:, i] = np.array(list(map(min, [lim] * N, V[:, i])))
    return V


## update the inertia weight
def update_param(param: dict, max_iter: int) -> dict:
    param['c1'] = 0.9 - 0.4 / max_iter * param['c1']
    return param


## update position and velocity
def update_p_v(P, V, pBest, gBest, param):
    V = param['c1'] * V + \
        param['c2'] * (pBest - P) * np.random.random() + \
        param['c3'] * (gBest - P) * np.random.random()
    V = limit_v(V, param['boundary'])
    P = limit_p(P + V, param['boundary'])
    return P, V


## compute nBest gBest
def compute_pB_gB(P, f, pBest, gBest):
    y = np.array(list(map(f, P)))
    for i in range(len(y)):
        if f(pBest[i]) > y[i]:
            pBest[i] = P[i]
    if min(y) < f(gBest):
        gBest = P[np.argmin(y)]
    return pBest, gBest
P = np.random.random(size=[N, dim]) * 200 - 100     # initial positions
V = np.random.random(size=[N, dim]) * 20 - 10       # initial velocties
pBest = P                                           # initial personal best
gBest = P[0]                                        # initial the fake global best
## iteration
# gBest_his = []
for i in range(max_iter):
    pBest, gBest = compute_pB_gB(P, f, pBest, gBest)
    P, V = update_p_v(P, V, pBest, gBest, param)
    param = update_param(param, max_iter)

    print("Iter {}, Best value = {:.3f}".format(i, f(gBest)))
    # gBest_his.append(f(gBest))

print("The min_f reached at", gBest)
# from matplotlib import pyplot as plt
# plt.plot(gBest_his)
# plt.show()
Iter 0, Best value = 137.194
Iter 1, Best value = -5.293
Iter 2, Best value = -5.293
Iter 3, Best value = -5.293
Iter 4, Best value = -5.293
Iter 5, Best value = -5.293
Iter 6, Best value = -5.293
Iter 7, Best value = -5.293
Iter 8, Best value = -5.293
Iter 9, Best value = -5.293
Iter 10, Best value = -5.293
Iter 11, Best value = -5.293
Iter 12, Best value = -5.293
Iter 13, Best value = -5.293
Iter 14, Best value = -5.336
Iter 15, Best value = -5.336
Iter 16, Best value = -5.336
Iter 17, Best value = -5.336
Iter 18, Best value = -5.336
Iter 19, Best value = -5.439
Iter 20, Best value = -5.439
Iter 21, Best value = -5.439
Iter 22, Best value = -5.500
Iter 23, Best value = -5.500
Iter 24, Best value = -5.500
Iter 25, Best value = -5.500
Iter 26, Best value = -5.581
Iter 27, Best value = -5.581
Iter 28, Best value = -5.581
Iter 29, Best value = -5.581
Iter 30, Best value = -5.581
Iter 31, Best value = -5.581
Iter 32, Best value = -5.581
Iter 33, Best value = -5.581
Iter 34, Best value = -5.581
Iter 35, Best value = -5.581
Iter 36, Best value = -5.581
Iter 37, Best value = -5.581
Iter 38, Best value = -5.581
Iter 39, Best value = -5.582
Iter 40, Best value = -5.582
Iter 41, Best value = -5.582
Iter 42, Best value = -5.582
Iter 43, Best value = -5.582
Iter 44, Best value = -5.582
Iter 45, Best value = -5.582
Iter 46, Best value = -5.582
Iter 47, Best value = -5.582
Iter 48, Best value = -5.583
Iter 49, Best value = -5.583
The min_f reached at [ 0.4941146  -0.33489041]

标签:PSO,Particle,gBest,Iter,Swarm,value,np,param,Best
From: https://www.cnblogs.com/zhang-js/p/17709992.html

相关文章

  • 米联客MLK-CM03-7EG-7EV AMD MPSOC核心模块硬件手册
    1整体概述MLK-CM03-7EG-7EV-1156核心模块是米联客电子ZynqUltraScale+MPSOC系列开发平台的全新高端产品。其核心模块集成电源管理:0.85V核心电源,最大输出48A。用户基于核心模块设计功能底板(提供功能底板设计方案)。降低项目功能底板设计难度和生产成本,加速项目开发。其应用领域......
  • 米联客MLK-CM02-2CG-3EG-4EV-AMD MPSOC核心模块硬件手册
    1产品概述MLK-CM02-2CG-3EG-4EV(MILIANKE-8X)是米联客电子ZynqUltraScale+MPSOC系列开发平台的全新高端产品。其核心模块集成电源管理:0.85V核心电源,最大输出12A。用户基于核心模块设计功能底板(提供功能底板设计方案)。降低项目功能底板设计难度和生产成本,加速项目开发。其应用领......
  • 米联客MPSOC MLK-F20-CM02-2CG-3EG-4EV开发板硬件手册
    1整体概述MLK-F20-CM02-2CG-3EG-4EV(MLK-8X)系列开发平台是米联客ZynqUltraScale+系列开发平台的全新高端产品。其核心模块集成电源管理:0.85V核心电源,最大输出12A。用户基于核心模块设计功能底板(提供功能底板设计方案)。降低项目功能底板设计难度和生产成本,加速项目开发。其应用......
  • 时间序列预测 | Matlab 粒子群优化长短期记忆网络(PSO-LSTM)的时间序列预测
    ✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,matlab项目合作可私信。......
  • 8步轻松晋级AMD MPSoC Linux内核调试专家
    8步轻松晋级AMDMPSoCLinux内核调试专家介绍AMDMPSoCLinux一般使用PetaLinux编译Linux系统,包括Linux内核、DTS、文件系统。PetaLinux内部集成Yocto,自动下载、配置、编译各种软件包。它简化了编译流程,也导致有些工程师找不到软件包的源代码,不知道如何调试软件。在PetaLinux......
  • 多元回归预测 | Matlab 粒子群算法优化随机森林(PSO-RF)回归预测
    ✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,matlab项目合作可私信。......
  • Docker-Swarm启动服务一直处于New状态
    一、情况描述​ 近期有个项目的开发环境需要迁移nas挂载盘,需要把开发环境的服务停止,待迁移完成后重启服务。​ 该环境使用的docker-swarm启动服务,之前考虑的是swarm是docker原生支持的,启动方便,命令也较为简单,能够满足使用需求。待更换nas盘完成,通知我启动服务。​ 按照正常......
  • docker-swarm的负载均衡(LB)策略-dnsrr与vip
    在DockerSwarm中,DNSRR(DNSRoundRobin)和VIP(VirtualIP)是两种常见的负载均衡策略,它们各自有不同的作用和特点。DNSRR(DNSRoundRobin)负载均衡策略:作用:DNSRR策略通过对域名解析返回多个后端服务的IP地址来实现负载均衡。每个请求都会被依次分发到这些IP地址中的一个。特点:简单......
  • 多元时间序列 | Matlab粒子群算法优化深度置信网络(PSO-DBN)多变量时间序列预测
    ✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,matlab项目合作可私信。......
  • Kubernetes、Docker Swarm和Rancher的特点 - 容器编排平台比较
    本文将介绍三种流行的容器编排平台:Kubernetes、DockerSwarm和Rancher。我们将比较它们的特点,包括架构、功能、性能和生态系统。通过了解这些平台的优势和劣势,读者可以更好地选择适合自己需求的容器编排平台。引言随着容器技术的快速发展,容器编排平台的需求也日益增长。容器编排......