2023 Higher Education Society Cup Mathematical Modeling Ideas - Case: Particle Swarm Optimization

# 0 Question ideas

(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 flocks of birds and fish. Its concept is simple, easy to program and implement, has high operating efficiency, relatively few parameters, and is widely used. The particle swarm algorithm was proposed in 1995 and has a history of 24 years ago (2019).
  
  The position of each particle in the particle swarm algorithm represents a candidate solution to the problem to be solved. The quality of each particle's position in space is determined by the fitness value of the particle's position in the problem to be solved. The position of each particle in the next generation is determined by its position in this generation and its own velocity vector. Its velocity determines the direction and distance of each particle's flight. During the flight, the particles will record the optimal position P that they have visited, and the group will also update the optimal position G that the group has visited. The flight speed of a 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

Keep repeating this process and eventually 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 birds is simple: to find a place 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 that place.
  2. All the birds met together and selected 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 with the most food it has ever visited.
  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 difficulty in choosing, nostalgia for P, or distrust of G. When the bird flies towards G, it also flies towards G from time to time. It flies towards P. In fact, it doesn’t 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 searching, they will choose the current G to make their home; otherwise, continue 2->3->4->5 to find their habitat.

Insert image description here

In the situation of strategy 4 described in the figure above, a bird is at point A, point G is the location where the birds have found the most food, and point P is the location where it has visited the most food. V is its current flying speed (speed is a vector, with direction and magnitude). Now it decides to fly towards P and G, but this is a Buddhist bird, and the specific amount of flight depends on the circumstances. Without 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 senior uses Python to demonstrate the optimal solution using 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

Modeling information

Data Sharing: The strongest modeling data
Insert image description here
Insert image description here

Guess you like

Origin blog.csdn.net/math_assistant/article/details/132585142