Implementing your own optimizer¶
PySwarms aims to be the go-to library for various PSO implementations, so if you are a researcher in swarm intelligence or a developer who wants to contribute, then read on this guide!
As a preliminary, here is a checklist whenever you will implement an optimizer:
- Propose an optimizer
- Write optimizer by inheriting from base classes
- Write a unit test
Proposing an optimizer¶
We wanted to make sure that PySwarms is highly-usable, and thus it is important that optimizers included in this library are either (1) classic textbook-PSO techniques or (2) highly-cited, published, optimization algorithms.
In case you wanted to include your optimization algorithm in this library, please raise an issue and add a short abstract on what your optimizer does. A link to a published paper (it’s okay if it’s behind a paywall) would be really helpful!
Inheriting from base classes¶
Most optimizers in this library inherit its attributes and methods from a set of built-in
base classes. You can check the existing classes in
For example, if we take the
pyswarms.base.base_single class, a base-class for standard single-objective
continuous optimization algorithms such as global-best PSO (
local-best PSO (
pyswarms.single.local_best), we can see that it inherits a set of methods as
The required methods can be seen in the base classes, and will raise a
if not called. Additional methods, private or not, can also be added depending on the needs of your
A short note on keyword arguments¶
The role of keyword arguments, or kwargs in short, is to act as a container for all other parameters
needed for the optimizer. You can define these things in your code, and create assertions to make all
of them required. However, note that in some implementations, required
options might include
w. This is the case in
pyswarms.base.bases for instance.
A short note on
You might notice that in most base classes, an
assertions() method is being called. This aims
to check if the user-facing input are correct. Although the method is called “assertions”, please make
all user-facing catches as raised Exceptions.
A short note on
We make sure that everything can be imported when the whole
pyswarms library is called. Thus,
please make sure to also edit the accompanying
__init__.py file in the directory you are working
For example, if you write your optimizer class
MyOptimizer inside a file called
and you are working under the
/single directory, please update the
from .global_best import GlobalBestPSO from .local_best import LocalBestPSO # Add your module from .my_optimizer import MyOptimizer __all__ = [ "GlobalBestPSO", "LocalBestPSO", "MyOptimizer" # Add your class ]
This ensures that it will be automatically initialized when the whole library is imported.
Writing unit tests¶
Testing is an important element of developing PySwarms, and we wanted everything to be as smooth as
possible, especially when doing the build and integrating. In this case, we provide the
module in the package. In case you add a test for your optimizer, simply name them with the same
convention as in those tests.
You can perform separate checks by
$ python -m unittest tests.optimizers.<test_myoptimizer>