粒子群算法代码.多维.python

# coding: utf-8
import numpy as np
import random

# ----------------------PSO参数设置---------------------------------
class PSO():
    def __init__(self, pN, dim, max_iter):  # 初始化类  设置粒子数量  位置信息维度  最大迭代次数
        self.w = 1
        self.ws = 0.95
        self.we = 0.4
        self.c1 = 1.8
        self.c2 = 1.8
        self.r1 = 0.6
        self.r2 = 0.4
        self.pN = pN  # 粒子数量
        self.dim = dim  # 搜索维度
        self.max_iter = max_iter  # 迭代次数
        self.X = np.zeros((self.pN, self.dim))  # 所有粒子的位置(还要确定取值范围)
        self.Xmax = 100
        self.Xmin = -100
        self.V =  np.zeros((self.pN, self.dim))  # 所有粒子的速度(还要确定取值范围)
        self.Vmax = 2
        self.Vmin = -2
        self.pbest = np.zeros((self.pN, self.dim))  # 个体经历的最佳位置
        self.gbest = np.zeros((1, self.dim))  # 全局最佳位置
        self.p_fit = np.zeros(self.pN)  # 每个个体的历史最佳适应值
        self.fit = 0.001  # 全局最佳适应值

    # ---------------------目标函数Sphere函数-----------------------------
    def function(self, x):
        y = 0
        for i in range(self.dim):
            y = y + x[i]**2
        return y

    # ---------------------初始化种群----------------------------------
    def init_Population(self):
        for i in range(self.pN):  # 遍历所有粒子

            for j in range(self.dim):  # 每一个粒子的纬度
                self.X[i][j] = random.uniform(-100, 100)  # 给每一个粒子的位置赋一个初始随机值(在一定范围内)
                self.V[i][j] = random.uniform(-0.2, 0.2)  # 给每一个粒子的速度给一个初始随机值(在一定范围内)

            self.pbest[i] = self.X[i]  # 把当前粒子位置作为这个粒子的最优位置
            tmp = self.function(self.X[i])  # 计算这个粒子的适应度值
            self.p_fit[i] = tmp  # 当前粒子的适应度值作为个体最优值

            if (tmp > self.fit):  # 与当前全局最优值做比较并选取更佳的全局最优值
                self.fit = tmp
                self.gbest = self.X[i]


     # ---------------------更新粒子位置----------------------------------

    def iterator(self):
        fitness = []
        for t in range(self.max_iter):
            #w = self.ws - (self.ws - self.we) * (t / self.max_iter)
            for i in range(self.pN):

                # 更新速度
                self.V[i] = self.w * self.V[i] + self.c1 * random.uniform(0,1) * (self.pbest[i] - self.X[i]) + (self.c2 * random.uniform(0,1) * (
                self.gbest - self.X[i]))
                for j in range(self.dim):
                    if self.V[i][j] > self.Vmax:
                        self.V[i][j] = self.Vmax
                    elif self.V[i][j] < self.Vmin:
                        self.V[i][j] = self.Vmin

                # 更新位置
                self.X[i] = self.X[i] + self.V[i]
                for j in range(self.dim):
                    if self.X[i][j] > self.Xmax:
                        self.X[i][j] = self.Xmax
                    elif self.X[i][j]  < self.Xmin:
                        self.X[i][j]  = self.Xmin

            for i in range(self.pN):  # 更新gbest\pbest

                temp = self.function(self.X[i])

                if (temp > self.p_fit[i]):  # 更新个体最优
                    self.pbest[i] = self.X[i]
                    self.p_fit[i] = temp

                if (temp > self.fit):  # 更新全局最优
                    self.gbest = self.X[i]
                    self.fit = temp

            fitness.append(self.fit)
            print('最优值为:', self.fit)  # 输出最优值
        return fitness


# ----------------------程序执行-----------------------
my_pso = PSO(pN=30, dim=20, max_iter=500)
my_pso.init_Population()
my_pso.iterator()

参考https://www.omegaxyz.com/2018/01/12/python_pso/

你可能感兴趣的:(python)