pyswarms.discrete package¶
The pyswarms.discrete
module implements various techniques in
discrete optimization. These are techniques that can be applied to a
discrete searchspace.
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 positionvelocity update method. Unlike
pyswarms.single.gb
and pyswarms.single.lb
, this technique
is often applied to discrete binary problems such as jobshop scheduling,
sequencing, and the like.
The update rule for the velocity is still similar, as shown in the proceeding equation:
For the velocity update rule, a particle compares its current position with respect to its neighbours. The nearest neighbours are being determined by a kDtree given a distance metric, similar to localbest PSO. The neighbours are computed for every iteration. However, this whole behavior can be modified into a globalbest 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:
Where the function \(S(x)\) is the sigmoid function defined as:
This enables the algorithm to output binary positions rather than a stream of continuous values as seen in globalbest or localbest 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]¶ Bases:
pyswarms.base.base_discrete.DiscreteSwarmOptimizer

__init__
(n_particles, dimensions, options, init_pos=None, velocity_clamp=None, vh_strategy='unmodified', ftol=inf, ftol_iter=1)[source]¶ Initialize the swarm

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 pnorm to use. 1 is the sumofabsolute 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 outofbounds particles. Only the “unmodified” and the “adjust” strategies are allowed.
Type: String


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 iterationsiter.
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
Returns: the local best cost and the local best position among the swarm.
Return type:
