Saturday, December 5, 2015

Particle Swarm Optimization in Python

Here is a short and sweet particle swarm optimization implementation in Python. For more information on particle swarm optimization check out Particle swarm optimization in F#

Example usage is like so:
def simple_error_function(args):  
   return args[0]+args[1]  

number_of_parameters = 2  
max_iterations = 100  
best_parameters, best_error_score = particle_swarm_optimize(simple_error_function, 
                                         number_of_parameters, max_iterations)  

The result will be the parameters that resulted in the lowest value for the simpe_error_function

You can add custom parameter initialization:
def simple_error_function(args):
    return args[0]+args[1]

def parameter_init():
    return random.random()*0.5-10

number_of_parameters = 2
max_iterations = 100
best_parameters, best_error_score = particle_swarm_optimize(simple_error_function, number_of_parameters,
                                                            max_iterations, weight_init, parameter_init=parameter_init)

Other arguments


  •  stopping_error: float = 0.001 #stop when we have a particle with this score 
  • num_particles: int = 10 #number of particles to run 
  • max_iterations_without_improvement: int = 10 #stop when we have this many consecutive turns without improvement 
  • c1: float = 2.0 # c1 PSO parameter 
  • c2: float = 2.0 # c1 PSO parameter

No comments:

Post a Comment