Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_return_values(self, swarm, clamp):
"""Test if method gives the expected shape and range"""
vh = VelocityHandler(strategy="unmodified")
v = P.compute_velocity(swarm, clamp, vh)
assert v.shape == swarm.position.shape
if clamp is not None:
assert (clamp[0] <= v).all() and (clamp[1] >= v).all()def test_input_swarm(self, swarm, vh_strat):
"""Test if method raises AttributeError with wrong swarm"""
vh = VelocityHandler(strategy=vh_strat)
with pytest.raises(AttributeError):
P.compute_velocity(swarm, clamp=(0, 1), vh=vh)def test_missing_kwargs(self, swarm, options, vh_strat):
"""Test if method raises KeyError with missing kwargs"""
vh = VelocityHandler(strategy=vh_strat)
with pytest.raises(KeyError):
swarm.options = options
clamp = (0, 1)
P.compute_velocity(swarm, clamp, vh)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__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)
self.vh = VelocityHandler(strategy=vh_strategy)
self.name = __name__def compute_velocity(
self,
swarm,
clamp=None,
vh=VelocityHandler(strategy="unmodified"),
bounds=None,
):
"""Compute the velocity matrix
This method updates the velocity matrix using the best and current
positions of the swarm. The velocity matrix is computed using the
cognitive and social terms of the swarm.
A sample usage can be seen with the following:
.. code-block :: python
import pyswarms.backend as P
from pyswarms.backend.swarm import Swarm
from pyswarms.backend.handlers import VelocityHandler
from pyswarms.backend.topology import Pyramiddimensions=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 compute_velocity(
self,
swarm,
clamp=None,
vh=VelocityHandler(strategy="unmodified"),
bounds=None,
):
"""Compute the velocity matrix
This method updates the velocity matrix using the best and current
positions of the swarm. The velocity matrix is computed using the
cognitive and social terms of the swarm.
A sample usage can be seen with the following:
.. code-block :: python
import pyswarms.backend as P
from pyswarms.backend.swarm import Swarm
from pyswarms.backend.handlers import VelocityHandler
from pyswarms.backend.topology import Randomdef compute_velocity(
self,
swarm,
clamp=None,
vh=VelocityHandler(strategy="unmodified"),
bounds=None,
):
"""Compute the velocity matrix
This method updates the velocity matrix using the best and current
positions of the swarm. The velocity matrix is computed using the
cognitive and social terms of the swarm.
A sample usage can be seen with the following:
.. code-block :: python
import pyswarms.backend as P
from pyswarms.backend.swarm import Swarm
from pyswarms.backend.handlers import VelocityHandler
from pyswarms.backend.topology import Ringoptions=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__