From 2b5cfd84e5bcb5d0b40a3b41a30e29d4ec765d23 Mon Sep 17 00:00:00 2001 From: ljvmiranda921 Date: Thu, 7 Jun 2018 14:13:02 +0900 Subject: [PATCH] Add binary generators In this commit, we add a set of binary generators to generate discrete/binary versions of the swarm. Signed-off-by: Lester James V. Miranda --- pyswarms/backend/generators.py | 58 ++++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 6 deletions(-) diff --git a/pyswarms/backend/generators.py b/pyswarms/backend/generators.py index 7ec2df5f..0f867803 100644 --- a/pyswarms/backend/generators.py +++ b/pyswarms/backend/generators.py @@ -64,6 +64,42 @@ def generate_swarm(n_particles, dimensions, bounds=None, center=1.00, init_pos=N else: return pos +def generate_discrete_swarm(n_particles, dimensions, binary=False, init_pos=None): + """Generates a discrete swarm + + Parameters + ---------- + n_particles : int + number of particles to be generated in the swarm. + dimensions: int + number of dimensions to be generated in the swarm. + binary : bool (default is :code:`False`) + generate a binary matrix + init_pos : :code:`numpy.ndarray` (default is :code:`None`) + option to explicitly set the particles' initial positions. Set to + :code:`None` if you wish to generate the particles randomly. + """ + try: + if init_pos is not None: + # There is user-defined initial position + if binary: + # Check if the initialized position is binary + if not len(np.unique(init_pos)) == 2: + raise ValueError('User-defined init_pos is not binary!') + pos = init_pos + else: + pos = init_pos + else: + # There is no user-defined initial position + if binary: + pos = np.random.randint(2, size=(n_particles, dimensions)) + else: + pos = np.random.random_sample(size=(n_particles, dimensions)).argsort(axis=1) + except ValueError: + raise + else: + return pos + def generate_velocity(n_particles, dimensions, clamp=None): """Initializes a velocity vector @@ -72,7 +108,7 @@ def generate_velocity(n_particles, dimensions, clamp=None): n_particles : int number of particles to be generated in the swarm. dimensions: int - number of dimensions to be generated in the swarm + number of dimensions to be generated in the swarm. clamp : tuple of floats (default is :code:`None`) a tuple of size 2 where the first entry is the minimum velocity and the second entry is the maximum velocity. It @@ -93,8 +129,8 @@ def generate_velocity(n_particles, dimensions, clamp=None): else: return velocity -def create_swarm(n_particles, dimensions, discrete=False, options=None, - bounds=None, center=1.0, clamp=None): +def create_swarm(n_particles, dimensions, discrete=False, binary=False, + options=None, bounds=None, center=1.0, init_pos=None, clamp=None): """Abstracts the generate_swarm() and generate_velocity() methods Parameters @@ -107,12 +143,17 @@ def create_swarm(n_particles, dimensions, discrete=False, options=None, Swarm options, for example, c1, c2, etc. discrete : bool (default is :code:`False`) Creates a discrete swarm + binary : bool (default is :code:`False`) + generate a binary matrix 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,)`. - center : :code:`numpy.ndarrray` (default is :code:`1`) + center : :code:`numpy.ndarray` (default is :code:`1`) a list of initial positions for generating the swarm + init_pos : :code:`numpy.ndarray` (default is :code:`None`) + option to explicitly set the particles' initial positions. Set to + :code:`None` if you wish to generate the particles randomly. clamp : tuple of floats (default is :code:`None`) a tuple of size 2 where the first entry is the minimum velocity and the second entry is the maximum velocity. It @@ -123,6 +164,11 @@ def create_swarm(n_particles, dimensions, discrete=False, options=None, pyswarms.backend.swarms.Swarm a Swarm class """ - position = generate_swarm(n_particles, dimensions, bounds, center) - velocity = generate_velocity(n_particles, dimensions, clamp) + if discrete: + position = generate_discrete_swarm(n_particles, dimensions, binary=binary) + else: + position = generate_swarm(n_particles, dimensions, bounds=bounds, + center=center, init_pos=init_pos) + + velocity = generate_velocity(n_particles, dimensions, clamp=clamp) return Swarm(position, velocity, options=options) \ No newline at end of file