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, bounds, bh_strat):
"""Test if method gives the expected shape and range"""
bh = BoundaryHandler(strategy=bh_strat)
p = P.compute_position(swarm, bounds, bh)
assert p.shape == swarm.velocity.shape
if bounds is not None:
assert (bounds[0] <= p).all() and (bounds[1] >= p).all()
def test_input_swarm(self, swarm, bh_strat):
"""Test if method raises AttributeError with wrong swarm"""
bh = BoundaryHandler(strategy=bh_strat)
with pytest.raises(AttributeError):
P.compute_position(swarm, bounds=([-5, -5], [5, 5]), bh=bh)
def compute_position(
self, swarm, bounds=None, bh=BoundaryHandler(strategy="periodic")
):
"""Update the position matrix
This method updates the position matrix given the current position and
the velocity. If bounded, it waives updating the position.
Parameters
----------
swarm : pyswarms.backend.swarms.Swarm
a Swarm instance
bounds : tuple of numpy.ndarray or list
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
:code:`(dimensions,)`.
bh : pyswarms.backend.handlers.BoundaryHandler
a BoundaryHandler instance
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 compute_position(
self, swarm, bounds=None, bh=BoundaryHandler(strategy="periodic")
):
"""Update the position matrix
This method updates the position matrix given the current position and
the velocity. If bounded, it waives updating the position.
Parameters
----------
swarm : pyswarms.backend.swarms.Swarm
a Swarm instance
bounds : tuple of :code:`np.ndarray` or list (default is :code:`None`)
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
:code:`(dimensions,)`.
bh : a BoundaryHandler instance
def compute_position(
self, swarm, bounds=None, bh=BoundaryHandler(strategy="periodic")
):
"""Update the position matrix
This method updates the position matrix given the current position and
the velocity. If bounded, it waives updating the position.
Parameters
----------
swarm : pyswarms.backend.swarms.Swarm
a Swarm instance
bounds : tuple of :code:`np.ndarray` or list (default is :code:`None`)
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
:code:`(dimensions,)`.
bh : pyswarms.backend.handlers.BoundaryHandler
a BoundaryHandler instance
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__
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 compute_position(
self, swarm, bounds=None, bh=BoundaryHandler(strategy="periodic")
):
"""Update the position matrix
This method updates the position matrix given the current position and
the velocity. If bounded, it waives updating the position.
Parameters
----------
swarm : pyswarms.backend.swarms.Swarm
a Swarm instance
bounds : tuple of :code:`np.ndarray` or list (default is :code:`None`)
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
:code:`(dimensions,)`.
bh : pyswarms.backend.handlers.BoundaryHandler
a BoundaryHandler instance