# Path: pyswarms/backend/topology/von_neumann.py
# class VonNeumann(Ring):
#     def __init__(self, static=None):
#         # static = None is just an artifact to make the API consistent
#         # Setting it will not change swarm behavior
#         super(VonNeumann, self).__init__(static=True)
#         self.rep = Reporter(logger=logging.getLogger(__name__))
# 
#     def compute_gbest(self, swarm, p, r, **kwargs):
#         """Updates the global best using a neighborhood approach
# 
#         The Von Neumann topology inherits from the Ring topology and uses
#         the same approach to calculate the global best. The number of
#         neighbors is determined by the dimension and the range. This
#         topology is always a :code:`static` topology.
# 
#         Parameters
#         ----------
#         swarm : pyswarms.backend.swarms.Swarm
#             a Swarm instance
#         r : int
#             range of the Von Neumann topology
#         p: int {1,2}
#             the Minkowski p-norm to use. 1 is the
#             sum-of-absolute values (or L1 distance) while 2 is
#             the Euclidean (or L2) distance.
# 
#         Returns
#         -------
#         numpy.ndarray
#             Best position of shape :code:`(n_dimensions, )`
#         float
#             Best cost
#         """
#         k = VonNeumann.delannoy(swarm.dimensions, r)
#         return super(VonNeumann, self).compute_gbest(swarm, p, k)
# 
#     @staticmethod
#     def delannoy(d, r):
#         """Static helper method to compute Delannoy numbers
# 
#         This method computes the number of neighbours of a Von Neumann
#         topology, i.e. a Delannoy number, dependent on the range and the
#         dimension of the search space. The Delannoy numbers are computed
#         recursively.
# 
#         Parameters
#         ----------
#         d : int
#             dimension of the search space
#         r : int
#             range of the Von Neumann topology
# 
#         Returns
#         -------
#         int
#             Delannoy number"""
#         if d == 0 or r == 0:
#             return 1
#         else:
#             del_number = (
#                 VonNeumann.delannoy(d - 1, r)
#                 + VonNeumann.delannoy(d - 1, r - 1)
#                 + VonNeumann.delannoy(d, r - 1)
#             )
#             return del_number
# 
# Path: tests/backend/topology/abc_test_topology.py
# class ABCTestTopology(abc.ABC):
#     """Abstract class that defines various tests for topologies
# 
#     Whenever a topology inherits from ABCTestTopology,
#     you don't need to write down all tests anymore. Instead, you can just
#     specify all required fixtures in the test suite.
#     """
# 
#     @pytest.fixture
#     def topology(self):
#         """Return an instance of the topology"""
#         raise NotImplementedError("NotImplementedError::topology")
# 
#     @pytest.fixture
#     def options(self):
#         """Return a dictionary of options"""
#         raise NotImplementedError("NotImplementedError::options")
# 
#     @pytest.mark.parametrize("static", [True, False])
#     @pytest.mark.parametrize("clamp", [None, (0, 1), (-1, 1)])
#     def test_compute_velocity_return_values(
#         self, topology, swarm, clamp, static
#     ):
#         """Test if compute_velocity() gives the expected shape and range"""
#         topo = topology(static=static)
#         v = topo.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("static", [True, False])
#     @pytest.mark.parametrize(
#         "bounds",
#         [None, ([-5, -5, -5], [5, 5, 5]), ([-10, -10, -10], [10, 10, 10])],
#     )
#     def test_compute_position_return_values(
#         self, topology, swarm, bounds, static
#     ):
#         """Test if compute_position() gives the expected shape and range"""
#         topo = topology(static=static)
#         p = topo.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()
# 
#     @pytest.mark.parametrize("static", [True, False])
#     def test_neighbor_idx(self, topology, options, swarm, static):
#         """Test if the neighbor_idx attribute is assigned"""
#         topo = topology(static=static)
#         topo.compute_gbest(swarm, **options)
#         assert topo.neighbor_idx is not None
# 
#     @pytest.mark.parametrize("static", [True, False])
#     @pytest.mark.parametrize("swarm", [0, (1, 2, 3)])
#     def test_input_swarm(self, topology, static, swarm, options):
#         """Test if AttributeError is raised when passed with a non-Swarm instance"""
#         with pytest.raises(AttributeError):
#             topo = topology(static=static)
#             topo.compute_gbest(swarm, **options)


import numpy as np
import pytest
from pyswarms.backend.topology import VonNeumann
from .abc_test_topology import ABCTestTopology
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Import modules

# Import from pyswarms

np.random.seed(4135157)


class TestVonNeumannTopology(ABCTestTopology):
    @pytest.fixture
    def topology(self):

# Path: pyswarms/backend/topology/von_neumann.py
# class VonNeumann(Ring):
#     def __init__(self, static=None):
#         # static = None is just an artifact to make the API consistent
#         # Setting it will not change swarm behavior
#         super(VonNeumann, self).__init__(static=True)
#         self.rep = Reporter(logger=logging.getLogger(__name__))
# 
#     def compute_gbest(self, swarm, p, r, **kwargs):
#         """Updates the global best using a neighborhood approach
# 
#         The Von Neumann topology inherits from the Ring topology and uses
#         the same approach to calculate the global best. The number of
#         neighbors is determined by the dimension and the range. This
#         topology is always a :code:`static` topology.
# 
#         Parameters
#         ----------
#         swarm : pyswarms.backend.swarms.Swarm
#             a Swarm instance
#         r : int
#             range of the Von Neumann topology
#         p: int {1,2}
#             the Minkowski p-norm to use. 1 is the
#             sum-of-absolute values (or L1 distance) while 2 is
#             the Euclidean (or L2) distance.
# 
#         Returns
#         -------
#         numpy.ndarray
#             Best position of shape :code:`(n_dimensions, )`
#         float
#             Best cost
#         """
#         k = VonNeumann.delannoy(swarm.dimensions, r)
#         return super(VonNeumann, self).compute_gbest(swarm, p, k)
# 
#     @staticmethod
#     def delannoy(d, r):
#         """Static helper method to compute Delannoy numbers
# 
#         This method computes the number of neighbours of a Von Neumann
#         topology, i.e. a Delannoy number, dependent on the range and the
#         dimension of the search space. The Delannoy numbers are computed
#         recursively.
# 
#         Parameters
#         ----------
#         d : int
#             dimension of the search space
#         r : int
#             range of the Von Neumann topology
# 
#         Returns
#         -------
#         int
#             Delannoy number"""
#         if d == 0 or r == 0:
#             return 1
#         else:
#             del_number = (
#                 VonNeumann.delannoy(d - 1, r)
#                 + VonNeumann.delannoy(d - 1, r - 1)
#                 + VonNeumann.delannoy(d, r - 1)
#             )
#             return del_number
# 
# Path: tests/backend/topology/abc_test_topology.py
# class ABCTestTopology(abc.ABC):
#     """Abstract class that defines various tests for topologies
# 
#     Whenever a topology inherits from ABCTestTopology,
#     you don't need to write down all tests anymore. Instead, you can just
#     specify all required fixtures in the test suite.
#     """
# 
#     @pytest.fixture
#     def topology(self):
#         """Return an instance of the topology"""
#         raise NotImplementedError("NotImplementedError::topology")
# 
#     @pytest.fixture
#     def options(self):
#         """Return a dictionary of options"""
#         raise NotImplementedError("NotImplementedError::options")
# 
#     @pytest.mark.parametrize("static", [True, False])
#     @pytest.mark.parametrize("clamp", [None, (0, 1), (-1, 1)])
#     def test_compute_velocity_return_values(
#         self, topology, swarm, clamp, static
#     ):
#         """Test if compute_velocity() gives the expected shape and range"""
#         topo = topology(static=static)
#         v = topo.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("static", [True, False])
#     @pytest.mark.parametrize(
#         "bounds",
#         [None, ([-5, -5, -5], [5, 5, 5]), ([-10, -10, -10], [10, 10, 10])],
#     )
#     def test_compute_position_return_values(
#         self, topology, swarm, bounds, static
#     ):
#         """Test if compute_position() gives the expected shape and range"""
#         topo = topology(static=static)
#         p = topo.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()
# 
#     @pytest.mark.parametrize("static", [True, False])
#     def test_neighbor_idx(self, topology, options, swarm, static):
#         """Test if the neighbor_idx attribute is assigned"""
#         topo = topology(static=static)
#         topo.compute_gbest(swarm, **options)
#         assert topo.neighbor_idx is not None
# 
#     @pytest.mark.parametrize("static", [True, False])
#     @pytest.mark.parametrize("swarm", [0, (1, 2, 3)])
#     def test_input_swarm(self, topology, static, swarm, options):
#         """Test if AttributeError is raised when passed with a non-Swarm instance"""
#         with pytest.raises(AttributeError):
#             topo = topology(static=static)
#             topo.compute_gbest(swarm, **options)


# Path: tests/backend/topology/test_von_neumann.py
import numpy as np
import pytest
from pyswarms.backend.topology import VonNeumann
from .abc_test_topology import ABCTestTopology

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Import modules

# Import from pyswarms

np.random.seed(4135157)


class TestVonNeumannTopology(ABCTestTopology):
    @pytest.fixture
    def topology(self):

        return VonNeumann