The installation commands below should be run in a DOS or Unix
command shell (not in a Python shell).

Under Windows (version 7 and earlier), a command shell can be obtained
by running cmd.exe (through the Run… menu item from the Start
menu). Under Unix (Linux, Mac OS X,…), a Unix shell is available when
opening a terminal (in Mac OS X, the Terminal program is found in the
Utilities folder, which can be accessed through the Go menu in the
Finder).

Alternatively, you can simply download the package archive from the
Python Package Index (PyPI) and unpack it. The package can then be
installed by going into the unpacked directory
(pyswarm-...), and running the provided setup.py
program with

You can also simply move the pyswarm-py* directory
that corresponds best to your version of Python to a location that
Python can import from (directory in which scripts using
pyswarm are run, etc.); the chosen
pyswarm-py* directory should then be renamed
pyswarm. Python 3 users should then run 2to3-w.
from inside this directory so as to automatically adapt the code to
Python 3.

To illustrate how pyswarm is to be best utilized, we’ll start with a
complete example, which will be explained step-by-step afterwards:

frompyswarmimportpsodefbanana(x):x1=x[0]x2=x[1]returnx1**4-2*x2*x1**2+x2**2+x1**2-2*x1+5defcon(x):x1=x[0]x2=x[1]return[-(x1+0.25)**2+0.75*x2]lb=[-3,-1]ub=[2,6]xopt,fopt=pso(banana,lb,ub,f_ieqcons=con)# Optimum should be around x=[0.5, 0.76] with banana(x)=4.5 and con(x)=0

Now let’s walk through each section of code. We start off with any necessary
imports, in this case it’s just the optimizer function pso:

frompyswarmimportpso

Then we define the objective function to be minimized, which should be defined
like myfunction(x,*args,**kwargs). In other words, it takes as its first
argument an 1-d array-like object, followed by any other (optional) arguments
and (again, optional) keyword arguments. The function should return a single
scalar value that is minimized. In this example, the banana
function:

defbanana(x):x1=x[0]x2=x[1]returnx1**4-2*x2*x1**2+x2**2+x1**2-2*x1+5

Optimizing with constraints is optional, but we include one here to illustrate
how it might be done in the con function which has the same call
syntax as the objective, but returns an array of values (even if it only has a
single value in it):

defcon(x):x1=x[0]x2=x[1]return[-(x1+0.25)**2+0.75*x2]

Rather than specify a starting point for the algorithm, we define the limits
of the input variables that the optimizer is allowed to search within. For the
sake of clarity, we have defined them prior to calling the optimizer in the
objects lb and ub, which stand for lower-bound and upper-bound,
respectively:

lb=[-3,-1]ub=[2,6]

That is really all that needs to be defined to run pso, so we then
call the optimizer:

xopt,fopt=pso(banana,lb,ub,f_ieqcons=con)

Using the kwarg f_ieqcons tells the routine that there’s a single
constraint function that returns an array object.

Scaling factor to search away from the particle’s best known position
(Default: 0.5)

phig :scalar

Scaling factor to search away from the swarm’s best known position
(Default: 0.5)

maxiter :int

The maximum number of iterations for the swarm to search (Default: 100)

minstep :scalar

The minimum stepsize of swarm’s best position before the search
terminates (Default: 1e-8)

minfunc :scalar

The minimum change of swarm’s best objective value before the search
terminates (Default: 1e-8)

debug :boolean

If True, progress statements will be displayed every iteration
(Default: False)

We could have written the constraint function to return a scalar value instead
of an array-like object, like:

defcon(x):x1=x[0]x2=x[1]return-(x1+0.25)**2+0.75*x2

In which case, we would have utilized the keyword-argument ieqcons, which
takes an array of function handles, like:

xopt,fopt=pso(banana,lb,ub,ieqcons=[con])

The parameters args and kwargs are used to pass any additional
parameters to the objective and constraint functions and are not changed during
the optimization process.

The parameters omega, phig and phip are a way of controlling how
closely the particles move away from their own best known position and the best
known position of all the particles in the swarm. These can take any scalar
value, but values between 0 and 1 seem to work best.

Finally, the parameters maxiter, minstep and minfunc are used to
tell the swarm when to stop searching. The last parameter debug can be
used to print out progress statements about the swarm.