From b6d24e18badb39af9f4094fd3f70242a41758412 Mon Sep 17 00:00:00 2001 From: ljvmiranda921 Date: Wed, 6 Jun 2018 21:04:21 +0900 Subject: [PATCH] Add tests for topology module --- tests/backend/topology/__init__.py | 0 tests/backend/topology/conftest.py | 27 +++++++++++++++++++ tests/backend/topology/test_ring.py | 40 +++++++++++++++++++++++++++++ tests/backend/topology/test_star.py | 38 +++++++++++++++++++++++++++ 4 files changed, 105 insertions(+) create mode 100644 tests/backend/topology/__init__.py create mode 100644 tests/backend/topology/conftest.py create mode 100644 tests/backend/topology/test_ring.py create mode 100644 tests/backend/topology/test_star.py diff --git a/tests/backend/topology/__init__.py b/tests/backend/topology/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/backend/topology/conftest.py b/tests/backend/topology/conftest.py new file mode 100644 index 00000000..40228fc2 --- /dev/null +++ b/tests/backend/topology/conftest.py @@ -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) + diff --git a/tests/backend/topology/test_ring.py b/tests/backend/topology/test_ring.py new file mode 100644 index 00000000..bebf71e8 --- /dev/null +++ b/tests/backend/topology/test_ring.py @@ -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() diff --git a/tests/backend/topology/test_star.py b/tests/backend/topology/test_star.py new file mode 100644 index 00000000..affd6770 --- /dev/null +++ b/tests/backend/topology/test_star.py @@ -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() \ No newline at end of file