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:
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
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)