Skip to content

Commit

Permalink
Add tests for topology module
Browse files Browse the repository at this point in the history
  • Loading branch information
ljvmiranda921 committed Jun 6, 2018
1 parent 6019a39 commit b6d24e1
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
Empty file.
27 changes: 27 additions & 0 deletions tests/backend/topology/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""Fixtures for tests"""

# Import modules
import pytest
import numpy as np

# Import from package
from pyswarms.backend.swarms import Swarm

@pytest.fixture
def swarm():
"""A contrived instance of the Swarm class at a certain timestep"""
attrs_at_t = {
'position' : np.array([[5,5,5], [3,3,3], [1,1,1]]),
'velocity' : np.array([[1,1,1], [1,1,1], [1,1,1]]),
'current_cost' : np.array([2,2,2]),
'pbest_cost' : np.array([1,2,3]),
'pbest_pos' : np.array([[1,2,3], [4,5,6], [7,8,9]]),
'best_cost' : 1,
'best_pos' : np.array([1,1,1]),
'behavior' : {'c1' : 0.5, 'c2': 1, 'w': 2}
}
return Swarm(**attrs_at_t)

40 changes: 40 additions & 0 deletions tests/backend/topology/test_ring.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Import modules
import pytest
import numpy as np

# Import from package
from pyswarms.backend.topology import Ring


@pytest.mark.parametrize('k', [1,2,3])
@pytest.mark.parametrize('p', [1,2])
def test_update_gbest_neighborhood(swarm, p, k):
"""Test if update_gbest_neighborhood gives the expected return values"""
topology = Ring()
pos, cost = topology.compute_gbest(swarm, p=p, k=k)
expected_pos = np.array([1,2,3])
expected_cost = 1
assert (pos == expected_pos).all()
assert cost == expected_cost

@pytest.mark.parametrize('clamp', [None, (0,1), (-1,1)])
def test_compute_velocity_return_values(swarm, clamp):
"""Test if compute_velocity() gives the expected shape and range"""
topology = Ring()
v = topology.compute_velocity(swarm, clamp)
assert v.shape == swarm.position.shape
if clamp is not None:
assert (clamp[0] <= v).all() and (clamp[1] >= v).all()

@pytest.mark.parametrize('bounds', [None, ([-5,-5,-5],[5,5,5]),
([-10, -10, -10],[10, 10, 10])])
def test_compute_position_return_values(swarm, bounds):
"""Test if compute_position() gives the expected shape and range"""
topology = Ring()
p = topology.compute_position(swarm, bounds)
assert p.shape == swarm.velocity.shape
if bounds is not None:
assert (bounds[0] <= p).all() and (bounds[1] >= p).all()
38 changes: 38 additions & 0 deletions tests/backend/topology/test_star.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Import modules
import pytest
import numpy as np

# Import from package
from pyswarms.backend.topology import Star


def test_compute_gbest_return_values(swarm):
"""Test if compute_gbest() gives the expected return values"""
topology = Star()
expected_cost = 1
expected_pos = np.array([1,2,3])
pos, cost = topology.compute_gbest(swarm)
assert cost == expected_cost
assert (pos == expected_pos).all()

@pytest.mark.parametrize('clamp', [None, (0,1), (-1,1)])
def test_compute_velocity_return_values(swarm, clamp):
"""Test if compute_velocity() gives the expected shape and range"""
topology = Star()
v = topology.compute_velocity(swarm, clamp)
assert v.shape == swarm.position.shape
if clamp is not None:
assert (clamp[0] <= v).all() and (clamp[1] >= v).all()

@pytest.mark.parametrize('bounds', [None, ([-5,-5,-5],[5,5,5]),
([-10, -10, -10],[10, 10, 10])])
def test_compute_position_return_values(swarm, bounds):
"""Test if compute_position() gives the expected shape and range"""
topology = Star()
p = topology.compute_position(swarm, bounds)
assert p.shape == swarm.velocity.shape
if bounds is not None:
assert (bounds[0] <= p).all() and (bounds[1] >= p).all()

0 comments on commit b6d24e1

Please sign in to comment.