Welcome to simplepso’s documentation!

simplePSO is a full implementation of the PSO algorithm. It requires an objective function to minimize that returns a scalar value, starting position, bounds on how far you want to search around the starting position, and max speeds each particle can travel. Then, you set the number of particles in the swarm, the number of iterations to run, and the number of processors you want to use. That’s it!

# Here we initial the class
# We must provide the cost function and a starting value
optimizer = PSO(start=start_position)

# We also must set bounds of the parameter space
optimizer.set_bounds(parameter_range)
# set speed limits on updating particles
optimizer.set_speed(speed_min, speed_max)

# Now we run the pso algorithm
optimizer.run(num_particles, num_iterations, num_processors,
              cost_function)
# get best parameter fit
best = optimizer.best
# parameter set
best.pos
# fitness of parameter set
best.fitness

We provide two examples in the examples folder training PySB models to time course data using ODE simulations and one example training a model to a distribution of species concentrations using stochastic simulations.

_images/training_earm.gif
class simplepso.pso.PSO(start=None, save_sampled=False, verbose=False, shrink_steps=True)[source]

Simple interface to run particle swarm optimization

It can optimize parameters for a function using two run methods.
run()cost function gets a parameter vector and returns a scalar. Can

be used with any callable function.

run_ssa()cost function gets multiple trajectories of a PySB model

and returns a scalar. These trajectories are provided as a pandas.DataFrame. PySB dependent function.

__init__(start=None, save_sampled=False, verbose=False, shrink_steps=True)[source]
Parameters
startlist

Starting position

save_sampledbool

Save each particles position and fitness over all iterations.

verbosebool
shrink_stepsbool

Shrink max distance traveled by each particle as the number of iterations increases

get_best_value()[source]

Returns the best fitness value of the population

get_history()[source]

Returns the history of the run

print_stats(iteration, fitness)[source]
return_ranked_populations()[source]

Returns a ranked population of the particles

Returns
np.array, np.array
run(num_particles, num_iterations, cost_function=None, num_processors=1, save_samples=False, stop_threshold=1e-05, max_iter_no_improv=None)[source]

Run optimization

Parameters
num_particlesint

Number of particles in population, ~20 is a good starting place

num_iterationsint

Number of iterations to perform.

cost_functioncallable function

Takes a parameter set and returns a scalar (particles fitness)

num_processorsint

Number of processors to run on. If using scipy, note that you may need to set OMP_NUM_THREADS=1 to prevent each process from using more than one CPU.

save_samplesbool

Save ALL positions of particles over time, can require large memory if num_particles, num_iterations, and len(parameters) is large.

stop_thresholdfloat

Standard deviation of the particles’ cost function at which the optimization is stopped

max_iter_no_improv: int

Maximum steps allowed without improvement before the optimization stops.

run_ssa(model, num_particles, num_iterations, num_sim, cost_function, simulator, save_samples=False, stop_threshold=0)[source]

Run PSO for a stochastic simulator

Parameters
modelpysb.Model
num_particlesint

Number of particles in the swarm.

num_iterationsint

Number of iterations to perform

num_simint

Number of SSA simulations to run for each particle.

cost_functionfunction

Takes a pandas dataframe of PySB trajectories created by running multiple SSA simulations. Function must return a scalar.

simulatorpysb.Simulator

PySB simulator (CudaSSASimulator or OpenclSSASimulator)

save_samplesbool
stop_thresholdfloat
set_bounds(parameter_range=None, lower=None, upper=None)[source]

Set the search space bounds that the parameters may search.

This can be a single float, in which the particles can search plus or minus the starting values around this. It can also be an array that must be the same length of the starting position.

Parameters
parameter_rangefloat

If provided parameter_range, the range will be set by the starting position +/- this value. To set each parameter manually, use lower and upper args

lowerarray

Lower bounds for parameters

upperarray

Upper bounds for parameters

set_speed(speed_min=- 10000, speed_max=10000)[source]

Sets the max and min speed of the particles.

This is usually a fraction of the bounds set with set_bounds. So if one sets the bound to be +/- 1 order of magnitude, you can set the speed to be -.1 and .1, allow the particles to update in 1/10 the parameter space. This keeps particles near their local position rather than jumping across parameter space quickly.

Parameters
speed_minfloat
speed_maxfloat
set_start_position(position)[source]

Set the starting position for the population of particles.

Parameters
positionarray
all_fitness

History of all particles finesses over all iterations. Only saved if save_sampled=True

all_history

History of all particles positions over all iterations. Only saved if save_sampled=True

best

Best Particle of population

history

Best particle for each iteration

population

Population of particles

population_fitness

Fitness values of population of particles

start

Starting position

values

Fitness values of the best particle for each iteration

Indices and tables