In this example, we will demonstrate three plotting methods available on PySwarms:
plot_cost_history
: for plotting the cost history of a swarm given a matrixplot_contour
: for plotting swarm trajectories of a 2D-swarm in two-dimensional spaceplot_surface
: for plotting swarm trajectories of a 2D-swarm in three-dimensional space
In [1]:
# Import modules
import matplotlib.pyplot as plt
import numpy as np
from IPython.display import Image
# Import PySwarms
import pyswarms as ps
from pyswarms.utils.functions import single_obj as fx
from pyswarms.utils.plotters import (plot_cost_history, plot_contour, plot_surface)
The first step is to create an optimizer. Here, we're going to use Global-best PSO to find the minima of a sphere function. As usual, we simply create an instance of its class pyswarms.single.GlobalBestPSO
by passing the required parameters that we will use. Then, we'll call the optimize()
method for 100 iterations.
In [2]:
options = {'c1':0.5, 'c2':0.3, 'w':0.9}
optimizer = ps.single.GlobalBestPSO(n_particles=50, dimensions=2, options=options)
cost, pos = optimizer.optimize(fx.sphere, iters=100)
To plot the cost history, we simply obtain the cost_history
from the optimizer
class and pass it to the cost_history
function. Furthermore, this method also accepts a keyword argument **kwargs
similar to matplotlib
. This enables us to further customize various artists and elements in the plot. In addition, we can obtain the following histories from the same class:
In [3]:
plot_cost_history(cost_history=optimizer.cost_history)
plt.show()
The plotters
module offers two methods to perform animation, plot_contour()
and plot_surface()
. As its name suggests, these methods plot the particles in a 2-D or 3-D space.
Each animation method returns a matplotlib.animation.Animation
class that still needs to be animated by a Writer
class (thus necessitating the installation of a writer module). For the proceeding examples, we will convert the animations into a JS script. In such case, we need to invoke some extra methods to do just that.
Lastly, it would be nice to add meshes in our swarm to plot the sphere function. This enables us to visually recognize where the particles are with respect to our objective function. We can accomplish that using the Mesher
class.
In [4]:
from pyswarms.utils.plotters.formatters import Mesher
In [5]:
# Initialize mesher with sphere function
m = Mesher(func=fx.sphere)
There are different formatters available in the pyswarms.utils.plotters.formatters
module to customize your plots and visualizations. Aside from Mesher
, there is a Designer
class for customizing font sizes, figure sizes, etc. and an Animator
class to set delays and repeats during animation.
We can obtain the swarm's position history using the pos_history
attribute from the optimizer
instance. To plot a 2D-contour, simply pass this together with the Mesher
to the plot_contour()
function. In addition, we can also mark the global minima of the sphere function, (0,0)
, to visualize the swarm's "target".
In [6]:
%%capture
# Make animation
animation = plot_contour(pos_history=optimizer.pos_history,
mesher=m,
mark=(0,0))
In [7]:
# Enables us to view it in a Jupyter notebook
animation.save('plot0.gif', writer='imagemagick', fps=10)
Image(url='plot0.gif')
Out[7]:
To plot in 3D space, we need a position-fitness matrix with shape (iterations, n_particles, 3)
. The first two columns indicate the x-y position of the particles, while the third column is the fitness of that given position. You need to set this up on your own, but we have provided a helper function to compute this automatically
In [8]:
# Obtain a position-fitness matrix using the Mesher.compute_history_3d()
# method. It requires a cost history obtainable from the optimizer class
pos_history_3d = m.compute_history_3d(optimizer.pos_history)
In [9]:
# Make a designer and set the x,y,z limits to (-1,1), (-1,1) and (-0.1,1) respectively
from pyswarms.utils.plotters.formatters import Designer
d = Designer(limits=[(-1,1), (-1,1), (-0.1,1)], label=['x-axis', 'y-axis', 'z-axis'])
In [10]:
%%capture
# Make animation
animation3d = plot_surface(pos_history=pos_history_3d, # Use the cost_history we computed
mesher=m, designer=d, # Customizations
mark=(0,0,0)) # Mark minima
In [11]:
animation3d.save('plot1.gif', writer='imagemagick', fps=10)
Image(url='plot1.gif')
Out[11]: