Coverage for cosmoHammer/pso/ParticleSwarmOptimizer.py : 88%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
''' Created on Sep 30, 2013
@author: J. Akeret '''
''' Optimizer using a swarm of particles
:param 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.
:param low: array of the lower bound of the parameter space :param high: array of the upper bound of the parameter space :param particleCount: the number of particles to use. :param 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.
:param 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.
'''
''' Constructor '''
self.pool = multiprocessing.Pool(self.threads)
""" Launches the PSO. Yields the complete swarm per iteration
:param maxIter: maximum iterations :param c1: cognitive weight :param c2: social weight :param p: stop criterion, percentage of particles to use :param m: stop criterion, difference between mean fitness and global best :param n: stop criterion, difference between norm of the particle vector and norm of the global best """
#if(self.isMaster()): #print("new global best found %i %s"%(i, self.gbest.__str__()))
particle.updatePBest()
print("max iteration reached! stoping") return
#w=0.72
""" Runs the complete optimiziation.
:param maxIter: maximum iterations :param c1: cognitive weight :param c2: social weight :param p: stop criterion, percentage of particles to use :param m: stop criterion, difference between mean fitness and global best :param 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 """
# If the `pool` property of the pso has been set (i.e. we want # to use `multiprocessing`), use the `pool`'s map method. Otherwise, # just use the built-in `map` function. mapFunction = self.pool.map else:
else:
# print( "best %f, meanFit %f, ration %f"%( self.gbest[0], meanFit, abs((self.gbest[0]-meanFit))))
#Andres N. Ruiz et al. sortedSwarm = [particle for particle in self.swarm] sortedSwarm.sort(key=lambda part: -part.fitness) bestOfBest = sortedSwarm[0:int(floor(self.particleCount*p))]
positions = [particle.position for particle in bestOfBest] means = numpy.mean(positions, axis=0) delta = numpy.mean((means-self.gbest.position)/self.gbest.position) return numpy.log10(delta) < -3.0
""" Implementation of a single particle
:param position: the position of the particle in the parameter space :param velocity: the velocity of the particle :param fitness: the current fitness of the particle
"""
def create(cls, paramCount): """ Creates a new particle without position, velocity and -inf as fitness """
numpy.array([[]]*paramCount), -numpy.Inf)
""" Sets the current particle representation as personal best """
""" Creates a copy of itself """ copy(self.velocity), self.fitness)
return "%f, pos: %s velo: %s"%(self.fitness, self.position, self.velocity)
return self.__str__() |