PySwarms is a Python library for particle swarm optimization (PSO). Use it when a candidate solution can be represented as a position and assigned a cost: the swarm searches for a position with a low cost. It provides ready-made optimizers alongside the option to implement PSO yourself. section 9.2.4
A PySwarms objective must handle a batch of particle positions and return one cost per particle. For the Michalewicz function, reshaping a single position into one row lets the same function evaluate one candidate or many; summing along the row produces the costs. section 9.2.4
Minimize a two-dimensional function
After defining `michalewicz_function`, set `dimension = 2` and `bounds = (np.zeros(dimension), np.pi * np.ones(dimension))`. Then run `optimizer = ps.single.GlobalBestPSO(n_particles=100, dimensions=dimension, options={'w': 0.9, 'c1': 0.5, 'c2': 0.3}, bounds=bounds)` followed by `cost, pos = optimizer.optimize(michalewicz_function, iters=1000)`. The shown run returns a position near `[2.203, 1.571]` with cost `-1.8013`. Here, `w` is inertia; `c1` and `c2` weight the personal-best and swarm-best influences. section 9.2.4
!pip install pyswarms
import pyswarms as ps #1
dimension = 2 #2
bounds = (np.zeros(dimension), np.pi * np.ones(dimension)) #3
options = {'w': 0.9, 'c1': 0.5, 'c2': 0.3} #4
optimizer = ps.single.GlobalBestPSO(n_particles=100, dimensions=dimension,
options=options, bounds=bounds) #5
cost, pos = optimizer.optimize(michalewicz_function, iters=1000) #6
print(f"Optimal solution: {np.round(pos,3)}") #7
print(f"Minimum value: {np.round(cost,4)}") #7
#1 Import the PSO solver from pyswarms.
#2 Dimension of the Michalewicz function
#3 Create bounds for the search space.
#4 Set up the optimizer.
#5 Create an instance of the optimizer.
#6 Optimize the Michalewicz function
#7 Print the optimal solution and minimum value of the function after running PSO.
The complete runnable pattern shows installation, NumPy bounds, optimizer construction, and the `optimize` call. section 9.2.4
PySwarms can also search neural-network weights rather than function coordinates. For a penguin-species classifier, each particle represents a flattened set of weights and biases for a four-input, ten-hidden-unit, three-output network. The loss function unpacks those parameters, computes predictions, and returns a loss for each particle. The example compares global-best, local-best, and binary PSO; its reported accuracies differ substantially, so the variant is a choice to test rather than assume. section 9.7
X = penguins[['bill_length_mm', 'bill_depth_mm', 'flipper_length_mm',
'body_mass_g']].to_numpy() #1
num_samples = X.shape[0] #2
n_inputs = X.shape[1] #2
n_hidden = 10 #2
n_classes = len(np.unique(y)) #2
dimensions = (n_inputs * n_hidden) + (n_hidden * n_classes) + n_hidden + n_
classes #3
PSO_varaints = ['globalBest', 'localBest', 'binaryPSO'] #4
options = {'w':0.79, 'c1': 0.9, 'c2': 0.5, 'k': 8, 'p': 2} #5
for algorithm in PSO_varaints: #6
if algorithm == 'globalBest':
optimizer = ps.single.GlobalBestPSO(n_particles=150,
dimensions=dimensions, options=options)
cost, pos = optimizer.optimize(particle_loss, iters=2000)
print("#"*30)
print(f"PSO varaints: {algorithm}")
print(f"Best average accuracy: {100*round((predict(pos) == y).mean(),3)} %")
print()
elif algorithm == 'localBest':
optimizer = ps.single.LocalBestPSO(n_particles=150,
dimensions=dimensions, options=options)
cost, pos = optimizer.optimize(particle_loss, iters=2000)
print("#"*30)
print(f"PSO varaints: {algorithm}")
print(f"Best average accuracy: {100*round((predict(pos) == y).mean(),3)} %")
print()
elif algorithm == 'binaryPSO':
optimizer = ps.discrete.BinaryPSO(n_particles=150,
dimensions=dimensions, options=options)
cost, pos = optimizer.optimize(particle_loss, iters=2000)
print("#"*30)
print(f"PSO varaints: {algorithm}")
print(f"Best average accuracy: {100*round((predict(pos) == y).mean(),3)} %")
print()
#1 Get the feature vector
#2 Set up the number of training samples, inputs, hidden layers, and outputs.
#3 Set up the dimensions of the problem.
#4 Define the variants of PSO.
#5 Set up the PSO hyperparameters
#6 Train the NN using different variants of PSO, and print the best accuracy.
The training setup connects network parameter count, optimizer options, and the loop over PSO variants. section 9.7
