optunity.solvers.ParticleSwarm module

class optunity.solvers.ParticleSwarm.ParticleSwarm(num_particles, num_generations, max_speed=None, phi1=2.0, phi2=2.0, **kwargs)[source]

Bases: optunity.solvers.util.Solver

Please refer to Particle Swarm Optimization for details on this algorithm.

Initializes a PSO solver.

Parameters:
  • num_particles (int) – number of particles to use
  • num_generations (int) – number of generations to use
  • max_speed (float or None) – maximum velocity of each particle
  • phi1 (float) – parameter used in updating position based on local best
  • phi2 (float) – parameter used in updating position best on global best
  • kwargs ({‘name’: [lb, ub], ...}) – box constraints for each hyperparameter

The number of function evaluations it will perform is num_particles`*`num_generations. The search space is rescaled to the unit hypercube before the solving process begins.

>>> solver = ParticleSwarm(num_particles=10, num_generations=5, x=[-1, 1], y=[0, 2])
>>> solver.bounds['x']
[-1, 1]
>>> solver.bounds['y']
[0, 2]
>>> solver.num_particles
10
>>> solver.num_generations
5

Warning

This solver is not explicitly constrained. The box constraints that are given are used for initialization, but solver may leave the specified region during iterations. If this is unacceptable, you must manually constrain the domain of the objective function prior to using this solver (cfr. Domain constraints).

class Particle(position, speed, best, fitness, best_fitness)[source]

Constructs a Particle.

clone()[source]

Clones this Particle.

ParticleSwarm.bounds[source]
ParticleSwarm.desc_brief = 'particle swarm optimization'
ParticleSwarm.desc_full = ['Maximizes the function using particle swarm optimization.', ' ', 'This is a two-phase approach:', '1. Initialization: randomly initializes num_particles particles.', ' Particles are randomized uniformly within the box constraints.', '2. Iteration: particles move during num_generations iterations.', ' Movement is based on their velocities and mutual attractions.', ' ', 'This function requires the following arguments:', '- num_particles: number of particles to use in the swarm', '- num_generations: number of iterations used by the swarm', '- max_speed: maximum speed of the particles in each direction (in (0, 1])', '- box constraints via key words: constraints are lists [lb, ub]', ' ', 'This solver performs num_particles*num_generations function evaluations.']
ParticleSwarm.generate()[source]

Generate a new Particle.

ParticleSwarm.max_speed[source]
ParticleSwarm.maximize(f, pmap=<built-in function map>)

Maximizes f.

Parameters:
  • f (callable) – the objective function
  • pmap (callable) – the map() function to use
Returns:

  • the arguments which optimize f
  • an optional solver report, can be None

ParticleSwarm.minimize(f, pmap=<built-in function map>)

Minimizes f.

Parameters:
  • f (callable) – the objective function
  • pmap (callable) – the map() function to use
Returns:

  • the arguments which optimize f
  • an optional solver report, can be None

ParticleSwarm.name = 'particle swarm'
ParticleSwarm.num_generations[source]
ParticleSwarm.num_particles[source]
ParticleSwarm.optimize(f, maximize=True, pmap=<built-in function map>)[source]

Optimizes f.

Parameters:
  • f (callable) – the objective function
  • maximize (boolean) – do we want to maximizes?
  • pmap (callable) – the map() function to use
Returns:

  • the arguments which optimize f
  • an optional solver report, can be None

ParticleSwarm.particle2dict(particle)[source]
ParticleSwarm.phi1[source]
ParticleSwarm.phi2[source]
ParticleSwarm.smax[source]
ParticleSwarm.smin[source]
static ParticleSwarm.suggest_from_box(num_evals, **kwargs)[source]

Create a configuration for a ParticleSwarm solver.

Parameters:
  • num_evals (int) – number of permitted function evaluations
  • kwargs ({‘param’: [lb, ub], ...}) – box constraints
>>> config = ParticleSwarm.suggest_from_box(200, x=[-1, 1], y=[0, 1])
>>> config['x']
[-1, 1]
>>> config['y']
[0, 1]
>>> config['num_particles'] > 0
True
>>> config['num_generations'] > 0
True
>>> solver = ParticleSwarm(**config)
>>> solver.bounds['x']
[-1, 1]
>>> solver.bounds['y']
[0, 1]
ParticleSwarm.updateParticle(part, best, phi1, phi2)[source]

Update the particle.

Table Of Contents

This Page

Previous topic

optunity.solvers.NelderMead module

Next topic

optunity.solvers.RandomSearch module