cosmoHammer.pso package

Submodules

cosmoHammer.pso.BestFitPositionGenerator module

Created on Oct 22, 2013

@author: J.Akeret

class cosmoHammer.pso.BestFitPositionGenerator.BestFitPositionGenerator(mpi=False, threads=1, particleCount=None, maxIter=None)[source]

Bases: object

A position generator which uses a particle swarm optimization algorithm to find the best fit value and the collapsed swarm to estimate the curvature matrix at that point. The optimization process can be parallelized over MPI and python multiprocessing.

Parameters:
  • mpi – True if a MPI implementation of the PSO should be used. Default is False
  • threads – Number of multiprocessing thread that should be started. Default is 1
  • particleCount – Number of particle to use for the optimization. If none the number is derrived according to the size of the parameter space. Default is None
  • maxIter – the maximal number of iterations. Default will be set to MAX_PSO_ITER
BEST_FILE_NAME = '_best_fit_global.out'
BEST_INFO_FILE_NAME = '_best_fit_info.out'
MAX_PSO_ITER = 1000
MIN_PARTICLE_COUNT = 20
generate()[source]

generates the positions by running the PSO and using the chain’s min and max and then calling the paraboloid fitter in order to estimate the covariance matrix. The position will then be generated by drawing position from a multivariant gaussian distribution defined by the best fit and the estimated covariance matrix. The progress of the PSO is successively stored to a the disk.

get_particle_count()[source]

Generates the number of particles to use by using a logarithmic function of the parameter count

setup(sampler)[source]

setup the generator

cosmoHammer.pso.CurvatureFitter module

Created on Oct 30, 2013

@author: J.Akeret

class cosmoHammer.pso.CurvatureFitter.CurvatureFitter(swarm, gbest)[source]

Bases: object

Fits a paraboloid centered around the global best fit of the PSO by estimating a curvarture matrix with the particle given in the swarm

Parameters:
  • swarm – list of particles
  • gbest – the global best particle at the last iteration
fit()[source]

Fits the paraboloid to the swarm particles

Returns:the mean = global best position and the estimated covariance matrix
minimize1(dim, theta, delta)[source]
minimize2(dim, theta, delta)[source]
cosmoHammer.pso.CurvatureFitter.bound(x)[source]
cosmoHammer.pso.CurvatureFitter.corr2cov(corrm, var)[source]
cosmoHammer.pso.CurvatureFitter.errfunc(p, theta, delta, thetabar)[source]

Error function defined by f(theta) - delta :param p: list of samples :param theta: the curvature matrix. see parabola def :param delta: the measured values

cosmoHammer.pso.CurvatureFitter.errfunc2(p, theta, delta, thetabar)[source]

Error function defined by f(theta) - delta :param p: the curvature matrix. see parabola def :param theta: list of samples :param delta: the measured values

cosmoHammer.pso.CurvatureFitter.parabola(p, theta, thetabar=1)[source]

Computation of the paraboloid for the given curvature matrix and samples. :param p: list of samples :param theta: vector containing the lower triangle of the matrix and the offset from the true mean

Returns:vector y from f(x,p)
cosmoHammer.pso.CurvatureFitter.rescale(_cov, v, dim)[source]
cosmoHammer.pso.CurvatureFitter.reverse(dim, R, vars)[source]

Transforms a symmetric matrix into a vector containig the lower triangle

Parameters:R – the symmetric matrix
Returns:the vector
cosmoHammer.pso.CurvatureFitter.transform(dim, p)[source]

Transforms a vector containg the lower triangle of a matrix into a symmetric matrix

Parameters:p – the vector
Returns:the matrix and left over values

cosmoHammer.pso.MpiParticleSwarmOptimizer module

Created on Oct 28, 2013

@author: J.Akeret

class cosmoHammer.pso.MpiParticleSwarmOptimizer.MpiParticleSwarmOptimizer(func, low, high, particleCount=25, threads=1)[source]

Bases: cosmoHammer.pso.ParticleSwarmOptimizer.ParticleSwarmOptimizer

PSO with support for MPI to distribute the workload over multiple nodes

isMaster()[source]

cosmoHammer.pso.ParticleSwarmOptimizer module

Created on Sep 30, 2013

@author: J. Akeret

class cosmoHammer.pso.ParticleSwarmOptimizer.Particle(position, velocity, fitness=0)[source]

Bases: object

Implementation of a single particle

Parameters:
  • position – the position of the particle in the parameter space
  • velocity – the velocity of the particle
  • fitness – the current fitness of the particle
copy()[source]

Creates a copy of itself

classmethod create(paramCount)[source]

Creates a new particle without position, velocity and -inf as fitness

updatePBest()[source]

Sets the current particle representation as personal best

class cosmoHammer.pso.ParticleSwarmOptimizer.ParticleSwarmOptimizer(func, low, high, particleCount=25, threads=1, pool=None)[source]

Bases: object

Optimizer using a swarm of particles

Parameters:
  • func – A function that takes a vector in the parameter space as input and returns the natural logarithm of the posterior probability for that position.
  • low – array of the lower bound of the parameter space
  • high – array of the upper bound of the parameter space
  • particleCount – the number of particles to use.
  • threads – (optional) The number of threads to use for parallelization. If threads == 1, then the multiprocessing module is not used but if threads > 1, then a Pool object is created and calls to lnpostfn are run in parallel.
  • pool – (optional) An alternative method of using the parallelized algorithm. If provided, the value of threads is ignored and the object provided by pool is used for all parallelization. It can be any object with a map method that follows the same calling sequence as the built-in map function.
isMaster()[source]
optimize(maxIter=1000, c1=1.193, c2=1.193, p=0.7, m=0.001, n=0.01)[source]

Runs the complete optimiziation.

Parameters:
  • maxIter – maximum iterations
  • c1 – cognitive weight
  • c2 – social weight
  • p – stop criterion, percentage of particles to use
  • m – stop criterion, difference between mean fitness and global best
  • n – stop criterion, difference between norm of the particle vector and norm of the global best
Return swarms, gBests:
 

the swarms and the global bests of all iterations

sample(maxIter=1000, c1=1.193, c2=1.193, p=0.7, m=0.001, n=0.01)[source]

Launches the PSO. Yields the complete swarm per iteration

Parameters:
  • maxIter – maximum iterations
  • c1 – cognitive weight
  • c2 – social weight
  • p – stop criterion, percentage of particles to use
  • m – stop criterion, difference between mean fitness and global best
  • n – stop criterion, difference between norm of the particle vector and norm of the global best

Module contents