1515search space to minimize distance to target while avoiding obstacles.
1616"""
1717import numpy as np
18- import matplotlib
1918import matplotlib .pyplot as plt
2019import matplotlib .animation as animation
2120import matplotlib .patches as patches
@@ -41,25 +40,25 @@ class Particle:
4140 max_velocity: Maximum velocity allowed in each dimension (5% of search space range)
4241 position: Current 2D position [x, y] in search space
4342 velocity: Current velocity vector [vx, vy]
44- personla_best_position : Personal best position found so far
45- personla_best_value : Fitness value at personal best position
43+ personal_best_position : Personal best position found so far
44+ personal_best_value : Fitness value at personal best position
4645 path: List of all positions visited by this particle
4746 """
4847 def __init__ (self , search_bounds , spawn_bounds ):
4948 self .search_bounds = search_bounds
5049 self .max_velocity = np .array ([(b [1 ] - b [0 ]) * 0.05 for b in search_bounds ])
5150 self .position = np .array ([np .random .uniform (b [0 ], b [1 ]) for b in spawn_bounds ])
5251 self .velocity = np .random .randn (2 ) * 0.1
53- self .personla_best_position = self .position .copy ()
54- self .personla_best_value = np .inf
52+ self .personal_best_position = self .position .copy ()
53+ self .personal_best_value = np .inf
5554 self .path = [self .position .copy ()]
5655 def update_velocity (self , gbest_pos , w , c1 , c2 ):
5756 """Update particle velocity using PSO equation:
58- v = w*v + c1*r1*(personla_best - x) + c2*r2*(gbest - x)
57+ v = w*v + c1*r1*(personal_best - x) + c2*r2*(gbest - x)
5958 """
6059 r1 = np .random .rand (2 )
6160 r2 = np .random .rand (2 )
62- cognitive = c1 * r1 * (self .personla_best_position - self .position )
61+ cognitive = c1 * r1 * (self .personal_best_position - self .position )
6362 social = c2 * r2 * (gbest_pos - self .position )
6463 self .velocity = w * self .velocity + cognitive + social
6564 self .velocity = np .clip (self .velocity , - self .max_velocity , self .max_velocity )
@@ -120,7 +119,7 @@ def check_collision(self, start, end, obstacle):
120119 d = end - start
121120 f = start - center
122121 a = np .dot (d , d )
123- # FIX: Guard against zero-length steps to prevent ZeroDivisionError
122+ # Guard against zero-length steps to prevent ZeroDivisionError
124123 if a < 1e-10 : # Near-zero length step
125124 # Check if start point is inside obstacle
126125 return np .linalg .norm (f ) <= r
@@ -149,9 +148,9 @@ def step(self):
149148 for particle in self .particles :
150149 value = self .fitness (particle .position )
151150 # Update personal best
152- if value < particle .personla_best_value :
153- particle .personla_best_value = value
154- particle .personla_best_position = particle .position .copy ()
151+ if value < particle .personal_best_value :
152+ particle .personal_best_value = value
153+ particle .personal_best_position = particle .position .copy ()
155154 # Update global best
156155 if value < self .gbest_value :
157156 self .gbest_value = value
@@ -213,6 +212,7 @@ def main():
213212 # pragma: no cover
214213 if show_animation :
215214 # Visualization setup
215+ signal .signal (signal .SIGINT , signal_handler )
216216 fig , ax = plt .subplots (figsize = (10 , 10 ))
217217 ax .set_xlim (SEARCH_BOUNDS [0 ])
218218 ax .set_ylim (SEARCH_BOUNDS [1 ])
0 commit comments