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_invalid_clamp_type(self, clamp):
"""Test if method raises a TypeError given invalid clamp type"""
with pytest.raises(TypeError):
P.generate_velocity(n_particles=2, dimensions=3, clamp=clamp)
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_return_values(self, bounds, center):
"""Test if method returns expected values"""
pos = P.generate_swarm(
n_particles=2, dimensions=3, bounds=bounds, center=center
)
if bounds is None:
min_bounds, max_bounds = (0.0, 1.00)
else:
min_bounds, max_bounds = bounds
lower_bound = center * np.array(min_bounds)
upper_bound = center * np.array(max_bounds)
assert (pos <= upper_bound).all() and (pos >= lower_bound).all()
def test_input_swarm(self, swarm):
"""Test if method raises AttributeError with wrong swarm"""
with pytest.raises(AttributeError):
P.compute_pbest(swarm)
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_return_values(self, swarm):
"""Test if method gives the expected return values"""
expected_cost = np.array([1, 2, 2])
expected_pos = np.array([[1, 2, 3], [4, 5, 6], [1, 1, 1]])
pos, cost = P.compute_pbest(swarm)
assert (pos == expected_pos).all()
assert (cost == expected_cost).all()
def test_generate_discrete_binary_swarm(self, binary):
"""Test if binary=True returns expected values"""
dims = 3
pos = P.generate_discrete_swarm(
n_particles=2, dimensions=dims, binary=binary
)
if binary:
assert len(np.unique(pos)) <= 2 # Might generate pure 0 or 1
else:
assert (np.max(pos, axis=1) == dims - 1).all()
def test_bounds_wrong_size(self, bounds):
"""Test if method raises ValueError when bounds is of wrong shape"""
with pytest.raises(ValueError):
P.generate_swarm(n_particles=2, dimensions=3, bounds=bounds)