├── iter0.png
├── iter5.png
├── iter10.png
├── iter15.png
├── iter20.png
├── iter25.png
├── particle_swarm_optimisation.jpg
├── README.md
└── main.py
/iter0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dawn0123/gbest_method_for_a_particle_swarm_optimisation_problem/HEAD/iter0.png
--------------------------------------------------------------------------------
/iter5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dawn0123/gbest_method_for_a_particle_swarm_optimisation_problem/HEAD/iter5.png
--------------------------------------------------------------------------------
/iter10.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dawn0123/gbest_method_for_a_particle_swarm_optimisation_problem/HEAD/iter10.png
--------------------------------------------------------------------------------
/iter15.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dawn0123/gbest_method_for_a_particle_swarm_optimisation_problem/HEAD/iter15.png
--------------------------------------------------------------------------------
/iter20.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dawn0123/gbest_method_for_a_particle_swarm_optimisation_problem/HEAD/iter20.png
--------------------------------------------------------------------------------
/iter25.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dawn0123/gbest_method_for_a_particle_swarm_optimisation_problem/HEAD/iter25.png
--------------------------------------------------------------------------------
/particle_swarm_optimisation.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dawn0123/gbest_method_for_a_particle_swarm_optimisation_problem/HEAD/particle_swarm_optimisation.jpg
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # GBest Method for a Particle Swarm Optimisation Problem
2 |
3 | ## Particle Swarm Optimisation Problem
4 |
5 |
6 |
7 |
8 |
9 | Particle swarm optimization (PSO) is one of the bio-inspired algorithms and it is a simple one to search for an optimal solution in the solution space. It is different from other optimization algorithms in such a way that only the objective function is needed and it is not dependent on the gradient or any differential form of the objective. It also has very few hyperparameters.
10 |
11 | ## Algorithm
12 |
13 | Assume we have $P$ particles and we denote of particle $i$ at iteration $t$ as $X^i(t)$, which in the example of above, we have it as a coordinate $X^i(t)=(x^i(t), y^i(t))$. Besides the position, we also have a velocity for each particle, denoted as $V^i(t)=(v^i_x(t), v^i_y(t))$. At the next iteration, the position of each particle would be updated as $$X^i(t+1)=X^i(t)+V^i(t+1)$$ or, equivalently, $$x^i(t+1)=x^i(t)+v^i_x(t+1)$$ $$y^i(t+1)=y^i(t)+v^i_y(t+1)$$ and at the same time, the velocities are also updated by the rule $$V^i(t+1)=wV^i(t)+c_1r_1(pbest^i-X^i(t))+c_2r_2(gbest-X^i(t))$$ where $r_1$ and $r_2$ are random numbers between 0 and 1, constants $w, c_1$ and $c_2$ are parameters to the PSO algorithm, and $pbest^i$ is the position that gives the best $f(X)$ value ever explored by particle $i$ and $gbest$ is that explored by all the particles in the swarm.
14 |
15 | ## Results
16 |
17 | This shows results about the following function
18 |
19 | $$f(x,y)=(x-3.14)^2+(y-2.72)^2+sin(3x+1.41)+sin(4y-1.73)$$
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/main.py:
--------------------------------------------------------------------------------
1 | '''
2 | Implementation of gbest method for solving a particle swarm optimisation problem.
3 | '''
4 | import numpy as np
5 | import matplotlib.pyplot as plt
6 | X1_MIN, X1_MAX, X2_MIN, X2_MAX = 0., 6., 0., 6.
7 |
8 |
9 | def f(x1, x2):
10 | return (x1 - 3.14)**2 + (x2 - 2.72)**2 + np.sin(3 * x1 + 1.41) + np.sin(4 * x2 + 1.73)
11 |
12 |
13 | def print_vectors(V, heading, name='x'):
14 | print(heading, end='')
15 | for i, v in enumerate(V):
16 | print("%s%d = [%.3f, %.3f]" % (name, i+1, v[0], v[1]), end=' ')
17 | print("")
18 |
19 |
20 | def plot_contour(X, gbest, title=""):
21 | x, y = np.array(np.meshgrid(np.linspace(X1_MIN, X1_MAX, 100),
22 | np.linspace(X2_MIN, X2_MAX, 100)))
23 | z = f(x, y)
24 | plt.figure(figsize=(6, 6))
25 | plt.title(title)
26 | plt.xlabel("x1")
27 | plt.ylabel("x2")
28 | plt.xlim(X1_MIN, X1_MAX)
29 | plt.ylim(X2_MIN, X2_MAX)
30 | plt.contour(x, y, z, 10)
31 | plt.scatter(X[..., 0], X[..., 1], cmap='bo')
32 | plt.plot(gbest[0], gbest[1], 'r*')
33 | plt.show()
34 |
35 |
36 | def solve(X, V, w, c1, c2, r1, r2, nIter):
37 | print_vectors(X, "Initial positions: ", 'x')
38 | print_vectors(V, "Initial velocities: ", 'v')
39 | plot_contour(X, np.array([X1_MIN-1, X2_MIN-1]), "Iteration t = 0")
40 | size = len(X)
41 | pbest = X
42 | f_pbest = f(X[..., 0], X[..., 1])
43 | print("Values of the objective function: ",
44 | " ".join(map(lambda i, x: "f%d = %.3f" % (i+1, x), range(size), f_pbest)))
45 | gbest = X[np.argmin(f_pbest)]
46 | f_gbest = np.min(f_pbest)
47 | for it in range(1, nIter+1):
48 | print("--------------------Iteration %02d--------------------" % (it))
49 | V = w * V + c1 * r1 * (pbest - X) + c2 * r2 * (gbest - X)
50 | X = X + V
51 | print_vectors(X, "Positions: ", 'x')
52 | print_vectors(V, "Velocities: ", 'v')
53 | f_pcurrent = f(X[..., 0], X[..., 1])
54 | print("Values of the objective function: ",
55 | " ".join(map(lambda i, x: "f%d = %.3f" % (i+1, x), range(size), f_pcurrent)))
56 | f_gcurrent = np.min(f_pcurrent)
57 | for i, flag in enumerate(f_pcurrent < f_pbest):
58 | if flag:
59 | print(
60 | f"Particle {i + 1} updates its personal best performance.")
61 | pbest = np.where((f_pbest < f_pcurrent)[..., np.newaxis], pbest, X)
62 | f_pbest = np.where(f_pbest < f_pcurrent, f_pbest, f_pcurrent)
63 | if f_gbest > f_gcurrent:
64 | gbest = X[np.argmin(f_pcurrent)]
65 | f_gbest = f_gcurrent
66 | print("The swarm updates its collective best performance.")
67 | if it % 5 == 0:
68 | plot_contour(X, gbest, f"Iteration t = {it}")
69 | return gbest, f_gbest
70 |
71 |
72 | if __name__ == '__main__':
73 | w = 0.8
74 | c1, c2 = 0.1, 0.1
75 | r1, r2 = 0.4, 0.6
76 | nIter = 25
77 | X = np.array([
78 | [5.951, 4.533],
79 | [3.486, 0.172],
80 | [4.859, 1.868],
81 | [3.347, 4.523]
82 | ])
83 | V = np.array([
84 | [-0.653, -0.986],
85 | [-0.219, 0.412],
86 | [-0.876, -0.223],
87 | [0.087, 0.970]
88 | ])
89 | (x1, x2), fx = solve(X, V, w, c1, c2, r1, r2, nIter)
90 | print("Solution: x1 = %.3f, x2 = %.3f, f(x1, x2) = %.3f" % (x1, x2, fx))
91 |
--------------------------------------------------------------------------------