2023 Huashu Cup Mathematical Modeling Ideas - Case: Particle Swarm Optimization

# 0 Ideas for the competition

(Share on CSDN as soon as the competition questions come out)

https://blog.csdn.net/dc_sinor?type=blog

1 What is particle swarm algorithm?

Particle Swarm Optimization (PSO) is an evolutionary algorithm developed by imitating the foraging behavior of birds and fish. Its concept is simple, easy to program and realize, with high operating efficiency and relatively few parameters, so it is widely used. The particle swarm optimization algorithm was proposed in 1995, and it has a history of 24 years (2019).
  
  The position of each particle in the PSO algorithm represents a candidate solution to the problem to be sought. The quality of the position of each particle in the space is determined by the fitness value of the particle's position in the problem to be sought. The position of each particle in the next generation is determined by its position in this generation and its own velocity vector, and its velocity determines the direction and distance of each flight of the particle. During the flight, the particle will record the optimal position P that it has been to, and the group will also update the optimal position G that the group has been to. The flying speed of the particle is determined by its current position, the optimal position that the particle itself has been to, the optimal position that the group has been to, and the speed of the particle at this time.

insert image description here

2 give an example

insert image description here
There are two people in a lake who can communicate with each other and can detect the lowest point of their position. The initial position is shown in the figure above. Since the right side is relatively deep, the person on the left will move the boat to the right.

insert image description here

Now the left side is darker, so the person on the right will move the boat to the left

Repeat the process all the way until finally the two boats will meet

insert image description here
A local optimal solution is obtained
insert image description hereand each individual is represented as a particle. The position of each individual at a certain moment is expressed as x(t), and the direction is expressed as v(t)

insert image description here

p(t) is the optimal solution of individual x at time t, g(t) is the optimal solution of all individuals at time t, v(t) is the direction of the individual at time t, and x(t) is the individual position at time t

insert image description here

The next position is determined by x, p, and g as shown in the figure above

insert image description here

The particles in the population can find the optimal solution to the problem by continuously learning from the historical information of themselves and the population.

3 is still an example

Particle swarm optimization is an algorithm derived from the foraging behavior of birds. Now, our protagonist is replaced by a flock of birds.
insert image description here

The goal of the little birds is simple, to find a location with the most food in this area to settle down and recuperate. Their search strategy in this place is as follows:
  1. Each bird randomly finds a place and evaluates the amount of food in this place.
  2. All the birds hold a meeting together and select the place with the most food as the candidate point G to settle down.
  3. Each bird looks back on its journey and remembers the place P where it has the most food.
  4. In order to find a place with more food, each bird flies towards G. However, I don’t know whether it is due to the difficulty of choosing or the nostalgia for P, or the distrust of G. Flying towards P, in fact, it does not know whether it is flying more towards G or towards P.
  5. It's time for the meeting again. If the birds decide to stop looking, they will choose the current G to settle down; otherwise, continue 2->3->4->5 to find their habitat.

insert image description here

In the case of strategy 4 described in the figure above, a bird is at point A, point G is the place where the birds have found the most food, and point P is the place where it has visited the most food. V is its current flying speed (velocity is a vector, with direction and magnitude), and now it decides to fly towards P and G, but this is a Buddhist bird, how much it flies depends on circumstances. If there is no speed V, it should fly to point B. With the influence of speed V, its combined speed finally makes it fly to point C, which is its next destination. If C is better than P, then C will be the next P, and if C is better than G, then it will be the next G.

Algorithm process

insert image description here

Algorithm implementation

Here the seniors use python to demonstrate the optimal solution of the particle swarm solution function

insert image description here

import numpy as np
import matplotlib.pyplot as plt
import random


# 定义“粒子”类
class parti(object):
    def __init__(self, v, x):
        self.v = v                    # 粒子当前速度
        self.x = x                    # 粒子当前位置
        self.pbest = x                # 粒子历史最优位置

class PSO(object):
    def __init__(self, interval, tab='min', partisNum=10, iterMax=1000, w=1, c1=2, c2=2):
        self.interval = interval                                            # 给定状态空间 - 即待求解空间
        self.tab = tab.strip()                                              # 求解最大值还是最小值的标签: 'min' - 最小值;'max' - 最大值
        self.iterMax = iterMax                                              # 迭代求解次数
        self.w = w                                                          # 惯性因子
        self.c1, self.c2 = c1, c2                                           # 学习因子
        self.v_max = (interval[1] - interval[0]) * 0.1                      # 设置最大迁移速度
        #####################################################################
        self.partis_list, self.gbest = self.initPartis(partisNum)                 # 完成粒子群的初始化,并提取群体历史最优位置
        self.x_seeds = np.array(list(parti_.x for parti_ in self.partis_list))    # 提取粒子群的种子状态 ###
        self.solve()                                                              # 完成主体的求解过程
        self.display()                                                            # 数据可视化展示

    def initPartis(self, partisNum):
        partis_list = list()
        for i in range(partisNum):
            v_seed = random.uniform(-self.v_max, self.v_max)
            x_seed = random.uniform(*self.interval)
            partis_list.append(parti(v_seed, x_seed))
        temp = 'find_' + self.tab
        if hasattr(self, temp):                                             # 采用反射方法提取对应的函数
            gbest = getattr(self, temp)(partis_list)
        else:
            exit('>>>tab标签传参有误:"min"|"max"<<<')
        return partis_list, gbest

    def solve(self):
        for i in range(self.iterMax):
            for parti_c in self.partis_list:
                f1 = self.func(parti_c.x)
                # 更新粒子速度,并限制在最大迁移速度之内
                parti_c.v = self.w * parti_c.v + self.c1 * random.random() * (parti_c.pbest - parti_c.x) + self.c2 * random.random() * (self.gbest - parti_c.x)
                if parti_c.v > self.v_max: parti_c.v = self.v_max
                elif parti_c.v < -self.v_max: parti_c.v = -self.v_max
                # 更新粒子位置,并限制在待解空间之内
                if self.interval[0] <= parti_c.x + parti_c.v <=self.interval[1]:
                    parti_c.x = parti_c.x + parti_c.v
                else:
                    parti_c.x = parti_c.x - parti_c.v
                f2 = self.func(parti_c.x)
                getattr(self, 'deal_'+self.tab)(f1, f2, parti_c)             # 更新粒子历史最优位置与群体历史最优位置

    def func(self, x):                                                       # 状态产生函数 - 即待求解函数
        value = np.sin(x**2) * (x**2 - 5*x)
        return value

    def find_min(self, partis_list):                                         # 按状态函数最小值找到粒子群初始化的历史最优位置
        parti = min(partis_list, key=lambda parti: self.func(parti.pbest))
        return parti.pbest

    def find_max(self, partis_list):
        parti = max(partis_list, key=lambda parti: self.func(parti.pbest))   # 按状态函数最大值找到粒子群初始化的历史最优位置
        return parti.pbest

    def deal_min(self, f1, f2, parti_):
        if f2 < f1:                          # 更新粒子历史最优位置
            parti_.pbest = parti_.x
        if f2 < self.func(self.gbest):
            self.gbest = parti_.x            # 更新群体历史最优位置

    def deal_max(self, f1, f2, parti_):
        if f2 > f1:                          # 更新粒子历史最优位置
            parti_.pbest = parti_.x
        if f2 > self.func(self.gbest):
            self.gbest = parti_.x            # 更新群体历史最优位置

    def display(self):
        print('solution: {}'.format(self.gbest))
        plt.figure(figsize=(8, 4))
        x = np.linspace(self.interval[0], self.interval[1], 300)
        y = self.func(x)
        plt.plot(x, y, 'g-', label='function')
        plt.plot(self.x_seeds, self.func(self.x_seeds), 'b.', label='seeds')
        plt.plot(self.gbest, self.func(self.gbest), 'r*', label='solution')
        plt.xlabel('x')
        plt.ylabel('f(x)')
        plt.title('solution = {}'.format(self.gbest))
        plt.legend()
        plt.savefig('PSO.png', dpi=500)
        plt.show()
        plt.close()


if __name__ == '__main__':
    PSO([-9, 5], 'max')

Effect
insert image description here

Guess you like

Origin blog.csdn.net/dc_sinor/article/details/132043593