# pyswarms.discrete package¶

The pyswarms.discrete module implements various techniques in discrete optimization. These are techniques that can be applied to a discrete search-space.

## pyswarms.discrete.binary module¶

A Binary Particle Swarm Optimization (binary PSO) algorithm.

It takes a set of candidate solutions, and tries to find the best solution using a position-velocity update method. Unlike pyswarms.single.gb and pyswarms.single.lb, this technique is often applied to discrete binary problems such as job-shop scheduling, sequencing, and the like.

The update rule for the velocity is still similar, as shown in the proceeding equation:

$v_{ij}(t + 1) = w * v_{ij}(t) + c_{1}r_{1j}(t)[y_{ij}(t) − x_{ij}(t)] + c_{2}r_{2j}(t)[\hat{y}_{j}(t) − x_{ij}(t)]$

For the velocity update rule, a particle compares its current position with respect to its neighbours. The nearest neighbours are being determined by a kD-tree given a distance metric, similar to local-best PSO. The neighbours are computed for every iteration. However, this whole behavior can be modified into a global-best PSO by changing the nearest neighbours equal to the number of particles in the swarm. In this case, all particles see each other, and thus a global best particle can be established.

In addition, one notable change for binary PSO is that the position update rule is now decided upon by the following case expression:

$\begin{split}X_{ij}(t+1) = \left\{\begin{array}{lr} 0, & \text{if } \text{rand() } \geq S(v_{ij}(t+1))\\ 1, & \text{if } \text{rand() } < S(v_{ij}(t+1)) \end{array}\right\}\end{split}$

Where the function $$S(x)$$ is the sigmoid function defined as:

$S(x) = \dfrac{1}{1 + e^{-x}}$

This enables the algorithm to output binary positions rather than a stream of continuous values as seen in global-best or local-best PSO.

This algorithm was adapted from the standard Binary PSO work of J. Kennedy and R.C. Eberhart in Particle Swarm Optimization [SMC1997].

 [SMC1997] J. Kennedy and R.C. Eberhart, “A discrete binary version of particle swarm algorithm,” Proceedings of the IEEE International Conference on Systems, Man, and Cybernetics, 1997.
class pyswarms.discrete.binary.BinaryPSO(n_particles, dimensions, options, init_pos=None, velocity_clamp=None, vh_strategy='unmodified', ftol=-inf, ftol_iter=1)[source]
__init__(n_particles, dimensions, options, init_pos=None, velocity_clamp=None, vh_strategy='unmodified', ftol=-inf, ftol_iter=1)[source]

Initialize the swarm

n_particles

number of particles in the swarm.

Type: int
dimensions

number of dimensions in the space.

Type: int
options

a dictionary containing the parameters for the specific optimization technique

• c1 : float
cognitive parameter
• c2 : float
social parameter
• w : float
inertia parameter
• k : int
number of neighbors to be considered. Must be a positive integer less than n_particles
• 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.
Type: dict with keys {'c1', 'c2', 'w', 'k', 'p'}
init_pos

option to explicitly set the particles’ initial positions. Set to None if you wish to generate the particles randomly.

Type: numpy.ndarray, optional
velocity_clamp

a tuple of size 2 where the first entry is the minimum velocity and the second entry is the maximum velocity. It sets the limits for velocity clamping.

Type: tuple, optional
vh_strategy

a strategy for the handling of the velocity of out-of-bounds particles. Only the “unmodified” and the “adjust” strategies are allowed.

Type: String
ftol

relative error in objective_func(best_pos) acceptable for convergence

Type: float
ftol_iter

number of iterations over which the relative error in objective_func(best_pos) is acceptable for convergence. Default is 1

Type: int
optimize(objective_func, iters, n_processes=None, verbose=True, **kwargs)[source]

Optimize the swarm for a number of iterations

Performs the optimization to evaluate the objective function f for a number of iterations iter.

Parameters: objective_func (function) – objective function to be evaluated iters (int) – number of iterations n_processes (int, optional) – number of processes to use for parallel particle evaluation Defaut is None with no parallelization. verbose (bool) – enable or disable the logs and progress bar (default: True = enable logs) kwargs (dict) – arguments for objective function the local best cost and the local best position among the swarm. tuple