Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
init_pos : numpy.ndarray, optional
option to explicitly set the particles' initial positions. Set to
:code:`None` if you wish to generate the particles randomly.
velocity_clamp : tuple, optional
a tuple of size 2 where the first entry is the minimum velocity
and the second entry is the maximum velocity. It
sets the limits for velocity clamping.
vh_strategy : String
a strategy for the handling of the velocity of out-of-bounds particles.
Only the "unmodified" and the "adjust" strategies are allowed.
ftol : float
relative error in objective_func(best_pos) acceptable for
convergence
"""
# Initialize logger
self.rep = Reporter(logger=logging.getLogger(__name__))
# Assign k-neighbors and p-value as attributes
self.k, self.p = options["k"], options["p"]
# Initialize parent class
super(BinaryPSO, self).__init__(
n_particles=n_particles,
dimensions=dimensions,
binary=True,
options=options,
init_pos=init_pos,
velocity_clamp=velocity_clamp,
ftol=ftol,
)
# Initialize the resettable attributes
self.reset()
# Initialize the topology
self.top = Ring(static=False)
speed of animation.
"""
# Import standard library
import logging
# Import modules
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import animation, cm
from mpl_toolkits.mplot3d import Axes3D
from ..reporter import Reporter
from .formatters import Animator, Designer
rep = Reporter(logger=logging.getLogger(__name__))
def plot_cost_history(
cost_history, ax=None, title="Cost History", designer=None, **kwargs
):
"""Create a simple line plot with the cost in the y-axis and
the iteration at the x-axis
Parameters
----------
cost_history : array_like
Cost history of shape :code:`(iters, )` or length :code:`iters` where
each element contains the cost for the given iteration.
ax : :obj:`matplotlib.axes.Axes`, optional
The axes where the plot is to be drawn. If :code:`None` is
passed, then the plot will be drawn to a new set of axes.
option to explicitly set the particles' initial positions. Set to
:code:`None` if you wish to generate the particles randomly.
"""
super(GeneralOptimizerPSO, self).__init__(
n_particles,
dimensions=dimensions,
options=options,
bounds=bounds,
velocity_clamp=velocity_clamp,
center=center,
ftol=ftol,
init_pos=init_pos,
)
# Initialize logger
self.rep = Reporter(logger=logging.getLogger(__name__))
# Initialize the resettable attributes
self.reset()
# Initialize the topology and check for type
if not isinstance(topology, Topology):
raise TypeError("Parameter `topology` must be a Topology object")
else:
self.top = topology
self.bh = BoundaryHandler(strategy=bh_strategy)
self.vh = VelocityHandler(strategy=vh_strategy)
self.name = __name__
def __init__(self, static=False):
"""Initialize the class
Parameters
----------
static : bool (Default is :code:`False`)
a boolean that decides whether the topology
is static or dynamic
"""
super(Pyramid, self).__init__(static)
self.rep = Reporter(logger=logging.getLogger(__name__))
def __init__(self, static=False):
"""Initializes the class
Parameters
----------
static : bool
a boolean that decides whether the topology
is static or dynamic. Defaulg is `False`
"""
super(Random, self).__init__(static)
self.rep = Reporter(logger=logging.getLogger(__name__))
This module abstracts how a swarm is generated. You can see its
implementation in our base classes. In addition, you can use all the methods
here to dictate how a swarm is initialized for your custom PSO.
"""
# Import standard library
import logging
# Import modules
import numpy as np
from ..utils.reporter import Reporter
from .swarms import Swarm
rep = Reporter(logger=logging.getLogger(__name__))
def generate_swarm(
n_particles, dimensions, bounds=None, center=1.00, init_pos=None
):
"""Generate a swarm
Parameters
----------
n_particles : int
number of particles to be generated in the swarm.
dimensions: int
number of dimensions to be generated in the swarm
bounds : tuple of numpy.ndarray or list, optional
a tuple of size 2 where the first entry is the minimum bound while
the second entry is the maximum bound. Each array must be of shape
def __init__(self, static, **kwargs):
"""Initializes the class"""
# Initialize logger
self.rep = Reporter(logger=logging.getLogger(__name__))
# Initialize attributes
self.static = static
self.neighbor_idx = None
if self.static:
self.rep.log(
"Running on `dynamic` topology,"
"set `static=True` for fixed neighbors.",
lvl=logging.DEBUG,
)
self.logger = logging.getLogger(__name__)
# Assign k-neighbors and p-value as attributes
self.k, self.p = options["k"], options["p"]
# Initialize parent class
super(LocalBestPSO, self).__init__(
n_particles=n_particles,
dimensions=dimensions,
options=options,
bounds=bounds,
velocity_clamp=velocity_clamp,
center=center,
ftol=ftol,
init_pos=init_pos,
)
# Initialize logger
self.rep = Reporter(logger=logging.getLogger(__name__))
# Initialize the resettable attributes
self.reset()
# Initialize the topology
self.top = Ring(static=static)
self.bh = BoundaryHandler(strategy=bh_strategy)
self.vh = VelocityHandler(strategy=vh_strategy)
self.name = __name__
def __init__(self, static=False):
"""Initializes the class
Parameters
----------
static : bool (Default is :code:`False`)
a boolean that decides whether the topology
is static or dynamic
"""
super(Ring, self).__init__(static)
self.rep = Reporter(logger=logging.getLogger(__name__))
option to explicitly set the particles' initial positions. Set to
:code:`None` if you wish to generate the particles randomly.
"""
super(GlobalBestPSO, self).__init__(
n_particles=n_particles,
dimensions=dimensions,
options=options,
bounds=bounds,
velocity_clamp=velocity_clamp,
center=center,
ftol=ftol,
init_pos=init_pos,
)
# Initialize logger
self.rep = Reporter(logger=logging.getLogger(__name__))
# Initialize the resettable attributes
self.reset()
# Initialize the topology
self.top = Star()
self.bh = BoundaryHandler(strategy=bh_strategy)
self.vh = VelocityHandler(strategy=vh_strategy)
self.name = __name__