Skip to content

Commit

Permalink
Add binary generators
Browse files Browse the repository at this point in the history
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 <ljvmiranda@gmail.com>
  • Loading branch information
ljvmiranda921 committed Jun 7, 2018
1 parent b50df56 commit 2b5cfd8
Showing 1 changed file with 52 additions and 6 deletions.
58 changes: 52 additions & 6 deletions pyswarms/backend/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)

0 comments on commit 2b5cfd8

Please sign in to comment.