MCPcopy Create free account
hub / github.com/dimvasdim/Thesis / Particle

Class Particle

pso.py:16–52  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

14
15
16class Particle:
17 def __init__(self, position, velocity):
18 self.position = position
19 self.velocity = velocity
20 self.pbest_position = position
21 self.gbest_position = position
22 self.pbest_fitness = 0
23 self.gbest_fitness = 0
24
25 def evaluate(self, obj_function, steady, cost, model):
26 fitness = obj_function(self.position, steady, cost, model)
27 # Check the personal best
28 if fitness > self.pbest_fitness:
29 self.pbest_fitness = fitness
30 self.pbest_position = self.position
31 # Check the global best
32 if fitness > self.gbest_fitness:
33 self.gbest_fitness = fitness
34 self.gbest_position = self.position
35
36 def update_velocity(self, w, c1, c2):
37 r1 = random()
38 r2 = random()
39 cognitive = c1 * r1 * (self.pbest_position - self.position)
40 social = c2 * r2 * (self.gbest_position - self.position)
41 self.velocity = (w * self.velocity) + cognitive + social
42
43 def update_position(self, bounds):
44 low_bound = bounds[0]
45 high_bound = bounds[1]
46 self.position = self.position + self.velocity
47 # adjust minimum position if needed
48 if self.position < low_bound:
49 self.position = low_bound
50 # adjust maximum position if needed
51 if self.position > high_bound:
52 self.position = high_bound
53
54
55def particle_swarm(number_of_particles, c1, c2, w_min, w_max, iterations, obj_function, steady, cost, model, bounds):

Callers 1

particle_swarmFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected