日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

粒子群PSO优化算法学习笔记 及其python实现(附讲解如何使用python语言sko.PSO工具包)

發布時間:2024/9/30 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 粒子群PSO优化算法学习笔记 及其python实现(附讲解如何使用python语言sko.PSO工具包) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

算法描述

粒子群算法思想來源于實際生活中鳥捕食的過程。假設在一個n維的空間中,有一群鳥(m只)在捕食,食物位于n維空間的某個點上,對于第i只鳥某一時刻來說,有兩個向量描述,一個是鳥的位置向量,第二個是鳥的速度。假設鳥能夠判斷一個位置的好壞,所謂“好壞”,就是離食物更近了還是更遠了。鳥在捕食的過程中會根據自己的經驗以及鳥群中的其他鳥的位置決定自己的速度,根據當前的位置和速度,可以得到下一刻的位置,這樣每只鳥通過向自己和鳥群學習不斷的更新自己的速度位置,最終找到食物,或者離食物足夠近的點。更新速度和位置的表達式如下。


其效果圖見下面:

sko.PSO 工具包講解

python語言
首先要下載這個工具包。
這個anaconda下載大家都會
參數詳解
如代碼:
pso = PSO(func=demo_func, dim=3, pop=40, max_iter=150, lb=[0, -1, 0.5], ub=[1, 1, 1], w=0.8, c1=0.5, c2=0.5)

參數說明
func類型function, 所要求得優化函數
dim類型int,維數,即函數的參數數
pop類型int,種群的大小,也就是粒子的數量。我們使用“pop”來與GA保持一致。默認40
max_iter類型int,iter迭代的最大值 默認150
lb類型列表,下限。每個參數的下限
ub類型列表,上限。每個參數的上限
W對應公式里的慣性權重,默認0.8
C1學習因子1,默認0.5
C2學習因子2,默認0.5
屬性說明
pbest_xarray_like, shape is (pop,dim)歷史上每個粒子的最佳位置
pbest_yarray_like, shape is (pop,1)歷史上最好的粒子圖像
gbest_xarray_like, shape is (1,dim)general best location for all particles in history
gbest_yfloat歷史上所有粒子的最佳圖像
gbest_y_histlist每個迭代的gbest_y

算例:有限制的粒子群

來源于官方文檔例子

第一步,定義問題

def demo_func(x):x1, x2, x3 = xreturn x1 ** 2 + (x2 - 0.05) ** 2 + x3 ** 2

第二步,做粒子群算法

from sko.PSO import PSOpso = PSO(func=demo_func, dim=3, pop=40, max_iter=150, lb=[0, -1, 0.5], ub=[1, 1, 1], w=0.8, c1=0.5, c2=0.5) pso.run() print('best_x is ', pso.gbest_x, 'best_y is', pso.gbest_y)

結果:

可以發現x的值落在啦限制區間內。
第三步,畫出結果

import matplotlib.pyplot as pltplt.plot(pso.gbest_y_hist) plt.show()

算例:無限制的粒子群

def demo_func(x):x1, x2, x3 = xreturn x1 ** 2 + (x2 - 0.05) ** 2 + x3 ** 2# %% Do PSO from sko.PSO import PSOpso = PSO(func=demo_func, dim=3) pso.run() print('best_x is ', pso.gbest_x, 'best_y is', pso.gbest_y)# %% Plot the result import matplotlib.pyplot as pltplt.plot(pso.gbest_y_hist) plt.show()

結果:

可以發現x1的值無限接近0,x2無限接近0.05,x3的值無限接近0.

所謂優化,我的理解是對一個問題求出它足夠好的解,即使這個解不是最優解。如題中解接近0而不是0。在現實生活中,這個解滿足要求。

數據批量做粒子群優化

#!/usr/bin/env python3 # -*- coding: utf-8 -*- # @Author: yudengwu # @Date : 2020/8/18 def demo_func(x):x1, x2, x3 = xreturn x1 ** 2 + (x2 - 0.05) ** 2 + x3 ** 2from sko.PSO import PSO import numpy as np import pandas as pddata={'lb':[[0,-1,0.5],[1,1,1],[2,3,4]],'ub':[[1,1,1],[2,2,2],[4,5,6]]} data=pd.DataFrame(data)print(data.shape[0])def pso(lb,ub):pso = PSO(func=demo_func, dim=3, pop=40, max_iter=150, lb=lb, ub=ub, w=0.8, c1=0.5, c2=0.5)pso.run()print('best_x is ', pso.gbest_x, 'best_y is', pso.gbest_y)for i in range(data.shape[0]):pso(data['lb'][i], data['ub'][i])

結果

python 實現粒子群

這是一個簡單算例,通過該算例簡單感受下粒子群
求取 函數x + 16 * np.sin(5 * x) + 10 * np.cos(4 * x) 的最大值

import numpy as np import matplotlib.pyplot as plt # 粒子(鳥) class particle:def __init__(self):self.pos = 0 # 粒子當前位置self.speed = 0self.pbest = 0 # 粒子歷史最好位置class PSO:def __init__(self):self.w = 0.5 # 慣性因子self.c1 = 1 # 自我認知學習因子self.c2 = 1 # 社會認知學習因子self.gbest = 0 # 種群當前最好位置self.N = 20 # 種群中粒子數量self.POP = [] # 種群self.iter_N = 100 # 迭代次數# 適應度值計算函數def fitness(self, x):return x + 16 * np.sin(5 * x) + 10 * np.cos(4 * x)# 找到全局最優解def g_best(self, pop):for bird in pop:if bird.fitness > self.fitness(self.gbest):self.gbest = bird.pos# 初始化種群def initPopulation(self, pop, N):for i in range(N):bird = particle()#初始化鳥bird.pos = np.random.uniform(-10, 10)#均勻分布bird.fitness = self.fitness(bird.pos)bird.pbest = bird.fitnesspop.append(bird)# 找到種群中的最優位置self.g_best(pop)# 更新速度和位置def update(self, pop):for bird in pop:# 速度更新speed = self.w * bird.speed + self.c1 * np.random.random() * (bird.pbest - bird.pos) + self.c2 * np.random.random() * (self.gbest - bird.pos)# 位置更新pos = bird.pos + speedif -10 < pos < 10: # 必須在搜索空間內bird.pos = posbird.speed = speed# 更新適應度bird.fitness = self.fitness(bird.pos)# 是否需要更新本粒子歷史最好位置if bird.fitness > self.fitness(bird.pbest):bird.pbest = bird.pos# 最終執行def implement(self):# 初始化種群self.initPopulation(self.POP, self.N)# 迭代for i in range(self.iter_N):# 更新速度和位置self.update(self.POP)# 更新種群中最好位置self.g_best(self.POP)pso = PSO() pso.implement()best_x=0 best_y=0 for ind in pso.POP:#print("x=", ind.pos, "f(x)=", ind.fitness)if ind.fitness>best_y:best_y=ind.fitnessbest_x=ind.pos print(best_y) print(best_x)x = np.linspace(-10, 10, 100000)def fun(x):return x + 16 * np.sin(5 * x) + 10 * np.cos(4 * x) y=fun(x) plt.plot(x, y)plt.scatter(best_x,best_y,c='r',label='best point') plt.legend() plt.show()

這段代碼有參考https://blog.csdn.net/saltriver/article/details/63680543

電氣專業的計算機小白,寫博文不容易。如果你覺得本文對你有用,請點個贊支持下。謝謝


與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的粒子群PSO优化算法学习笔记 及其python实现(附讲解如何使用python语言sko.PSO工具包)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。