-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpso.py
More file actions
84 lines (65 loc) · 2.75 KB
/
pso.py
File metadata and controls
84 lines (65 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Basic implementation of a particle swarm optimization algorithm
# @tz2lala
import numpy as np
from typing import List, Tuple
from optbase import OptimizerPopulationBased
from testfuns import EGG_HOLDER
class PSO(OptimizerPopulationBased):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.best_pos_p = None
self.best_val_p = None
self.best_pos_g = None
self.best_val_g = None
def optimize(self, max_gen: int,
c1: float = 0.1, c2: float = 0.1, inertia: float = 1.0,
init_velocity_scale: float = 0.1):
if self.n_pop is None:
raise ValueError("Population not initialized")
self._cost_trace = np.zeros(max_gen)
# initialize best position storage
self._pop = self.x0
self.evaluate_best()
# initialize velocity
velocity = np.random.randn(self.n_pop, self.n_var) * init_velocity_scale
# iterations
for igen in range(max_gen):
# update velocity
r1 = np.random.rand(self.n_pop, self.n_var) # random scalings
r2 = np.random.rand(self.n_pop, self.n_var)
velocity = inertia * velocity + \
c1 * r1 * (self.best_pos_p - self._pop) + \
c2 * r2 * (self.best_pos_g - self._pop)
# update position
self._pop += velocity
# clipping
lower = np.array([pair[0] for pair in self.bounds])
higher = np.array([pair[1] for pair in self.bounds])
self._pop = np.clip(self._pop, lower, higher)
# update best positions
self.evaluate_best()
self._cost_trace[igen] = self.best_val_g
self._x_est = self.best_pos_g
def evaluate_best(self):
if self.best_pos_p is None:
self.best_pos_p = self._pop
self.best_val_p = self.evaluate_population(self._pop)
self.best_pos_g = self.best_val_p[self.best_val_p.argmin()]
self.best_val_g = self.best_val_p.min()
else:
func_val = self.evaluate_population(self._pop)
# update particle best
update_ids = func_val < self.best_val_p
self.best_val_p[update_ids] = func_val[update_ids]
self.best_pos_p[update_ids] = self._pop[update_ids]
# update global best
if self.best_val_p.min() < self.best_val_g:
self.best_val_g = self.best_val_p.min()
self.best_pos_g = self.best_pos_p[self.best_val_p.argmin()]
if __name__ == '__main__':
# Run an example
de = PSO(objective_function=EGG_HOLDER['obj'],
bounds=EGG_HOLDER['bounds'])
de.initialize_population(30)
de.optimize(inertia=0.8, max_gen=100)
de.plot_trace()