more refactoring
[imago.git] / imago_pack / pso.py
index 6d6fada..061fe3e 100644 (file)
@@ -7,6 +7,7 @@ from functools import partial
 import lhs
 
 def particle(dimension, bound, v_max, func_d, pos=None):
+    """Create a new particle."""
     if not pos:
         position = [2 * bound * random.random() - bound for _ in xrange(dimension)]
     else:
@@ -16,6 +17,7 @@ def particle(dimension, bound, v_max, func_d, pos=None):
     return value, position, velocity, value, position
 
 def move(particle, omega, phi_p, phi_g, v_max, global_best, func_d):
+    """Move the *particle*."""
     _, position, velocity, best_value, best_position = particle
     position = [p + v for (p, v) in zip(position, velocity)]
     velocity = [omega * v 
@@ -30,6 +32,8 @@ def move(particle, omega, phi_p, phi_g, v_max, global_best, func_d):
     return value, position, velocity, best_value, best_position
 
 def optimize(dimension, boundary, function_d, n_parts, n_turns):
+    """Optimize *function_d* of given *dimension* in space bounded by
+    symmetrical *boundary*. Use *n_parts* particles for *n_turn* turns."""
     pool = multiprocessing.Pool(None)
     v_max = boundary
     particles = [particle(dimension, boundary, v_max, function_d, pos)