Skip to content

Commit

Permalink
Improve documentation (#16)
Browse files Browse the repository at this point in the history
This is a massive documentation update in order to make the
ReadTheDocs website as appealing it should be. In summary, the
following were undertaken:
  - Small patch fix for importing modules
  - Small modification in console_utils for console output
  - Modified ReadTheDocs' sidebar theme with an overriding css file.
  - Added a use-case example on Basic Optimization
  - Updated docstrings for most module implementations to make
    it look nice in the API documentation.

Even though most of these updates occured at different files,
they were all done for the sole purpose of improving documentation
in ReadTheDocs and introducing an API documentation at the end.

Author: ljvmiranda921
  • Loading branch information
ljvmiranda921 committed Aug 4, 2017
1 parent 13f462b commit 29bcb92
Show file tree
Hide file tree
Showing 15 changed files with 880 additions and 16 deletions.
2 changes: 1 addition & 1 deletion HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ History
* First release on PyPI.

0.1.1 (2017-7-25)
------------------
~~~~~~~~~~~~~~~~~

* Bug fixes
* Implemented Local Best PSO
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ built-in sphere function, :code:`pyswarms.utils.functions.sphere_func()`, and th
.. code-block:: python
import pyswarms as ps
from pyswarms.utils.functions import sphere_func
from pyswarms.utils.functions import single_obj as fx
# Set-up hyperparameters
options = {'c1': 0.5, 'c2': 0.3, 'm':0.9}
Expand All @@ -69,7 +69,7 @@ built-in sphere function, :code:`pyswarms.utils.functions.sphere_func()`, and th
optimizer = ps.single.GBestPSO(n_particles=10, dims=2, **options)
# Perform optimization
stats = optimizer.optimize(sphere_func, iters=100)
stats = optimizer.optimize(fx.sphere_func, iters=100)
Credits
Expand Down
17 changes: 17 additions & 0 deletions docs/_static/theme_overrides.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.wy-menu-vertical header, .wy-menu-vertical p.caption {
color: gold;
}

.wy-menu-vertical a {
color: white;
}

.wy-side-nav-search
{
color: #cacaca;
background: #074E68
}

.wy-side-nav-search input[type=text] {
border-color: rgba(7, 78, 104, 0.83);
}
5 changes: 5 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@
# "default.css".
html_static_path = ['_static']

def setup(app):
# overrides for wide tables in RTD theme
app.add_stylesheet('theme_overrides.css') # path relative to static


# If not '', a 'Last updated on:' timestamp is inserted at every page
# bottom, using the given strftime format.
html_last_updated_fmt = '%b %d, %Y'
Expand Down
186 changes: 186 additions & 0 deletions docs/examples/basic_optimization.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@

Basic Optimization
==================

In this example, we'll be performing a simple optimization of
single-objective functions using the global-best optimizer in
``pyswarms.single.GBestPSO`` and the local-best optimizer in
``pyswarms.single.LBestPSO``. This aims to demonstrate the basic
capabilities of the library when applied to benchmark problems.

.. code:: ipython3
import sys
sys.path.append('../')
.. code-block:: python
# Import modules
import numpy as np
# Import PySwarms
import pyswarms as ps
from pyswarms.utils.functions import single_obj as fx
# Some more magic so that the notebook will reload external python modules;
# see http://stackoverflow.com/questions/1907993/autoreload-of-modules-in-ipython
%load_ext autoreload
%autoreload 2
Optimizing a function
---------------------

First, let's start by optimizing the sphere function. Recall that the
minima of this function can be located at ``f(0,0..,0)`` with a value of
``0``. In case you don't remember the characteristics of a given
function, simply call ``help(<function>)``.

For now let's just set some arbitrary parameters in our optimizers.
There are, at minimum, three steps to perform optimization:

1. Set the hyperparameters to configure the swarm as a ``dict``.
2. Create an instance of the optimizer by passing the dictionary along
with the necessary arguments.
3. Call the ``optimize()`` method and have it store the optimal cost and
position in a variable.

The ``optimize()`` method returns a ``tuple`` of values, one of which
includes the optimal cost and position after optimization. You can store
it in a single variable and just index the values, or unpack it using
several variables at once.

.. code-block:: python
# Set-up hyperparameters
options = {'c1': 0.5, 'c2': 0.3, 'm':0.9}
# Call instance of PSO
gbest_pso = ps.single.GBestPSO(n_particles=10, dims=2, **options)
# Perform optimization
cost, pos = gbest_pso.optimize(fx.sphere_func, print_step=100, iters=1000, verbose=3)
.. parsed-literal::
Iteration 1/1000, cost: 0.0035824017918
Iteration 101/1000, cost: 1.02538653288e-08
Iteration 201/1000, cost: 9.95696087972e-13
Iteration 301/1000, cost: 8.22034343822e-16
Iteration 401/1000, cost: 3.7188438887e-19
Iteration 501/1000, cost: 1.23935292549e-25
Iteration 601/1000, cost: 6.03016193248e-28
Iteration 701/1000, cost: 3.70755768681e-34
Iteration 801/1000, cost: 2.64385328058e-37
Iteration 901/1000, cost: 1.76488833461e-40
================================
Optimization finished!
Final cost: 0.000
Best value: [-6.5732265560180066e-24, -7.4004230063696789e-22]
We can see that the optimizer was able to find a good minima as shown
above. You can control the verbosity of the output using the ``verbose``
argument, and the number of steps to be printed out using the
``print_step`` argument.

Now, let's try this one using local-best PSO:

.. code-block:: python
# Set-up hyperparameters
options = {'c1': 0.5, 'c2': 0.3, 'm':0.9}
# Call instance of PSO
lbest_pso = ps.single.LBestPSO(n_particles=10, dims=2, k=2,p=2, **options)
# Perform optimization
cost, pos = lbest_pso.optimize(fx.sphere_func, print_step=100, iters=1000, verbose=3)
.. parsed-literal::
Iteration 1/1000, cost: 0.190175474818
Iteration 101/1000, cost: 1.14470953523e-06
Iteration 201/1000, cost: 6.79485221069e-11
Iteration 301/1000, cost: 1.00691597113e-14
Iteration 401/1000, cost: 2.98301783945e-18
Iteration 501/1000, cost: 2.13856158282e-20
Iteration 601/1000, cost: 5.49351926815e-25
Iteration 701/1000, cost: 1.7673389214e-29
Iteration 801/1000, cost: 1.83082804473e-33
Iteration 901/1000, cost: 1.75920918448e-36
================================
Optimization finished!
Final cost: 3.000
Best value: [-8.2344756213578705e-21, -2.6563827831876976e-20]
Optimizing a function with bounds
---------------------------------

Another thing that we can do is to set some bounds into our solution, so
as to contain our candidate solutions within a specific range. We can do
this simply by passing a ``bounds`` parameter, of type ``tuple``, when
creating an instance of our swarm. Let's try this using the global-best
PSO with the Rastrigin function (``rastrigin_func`` in
``pyswarms.utils.functions.single_obj``).

Recall that the Rastrigin function is bounded within ``[-5.12, 5.12]``.
If we pass an unbounded swarm into this function, then a ``ValueError``
might be raised. So what we'll do is to create a bound within the
specified range. There are some things to remember when specifying a
bound:

- A bound should be of type tuple with length 2.
- It should contain two ``numpy.ndarrays`` so that we have a
``(min_bound, max_bound)``
- Obviously, all values in the ``max_bound`` should always be greater
than the ``min_bound``. Their shapes should match the dimensions of
the swarm.

What we'll do now is to create a 10-particle, 2-dimensional swarm. This
means that we have to set our maximum and minimum boundaries with the
shape of 2. In case we want to initialize an n-dimensional swarm, we
then have to set our bounds with the same shape n. A fast workaround for
this would be to use the ``numpy.ones`` function multiplied by a
constant.

.. code-block:: python
# Create bounds
max_bound = 5.12 * np.ones(2)
min_bound = - max_bound
bounds = (min_bound, max_bound)
.. code-block:: python
# Initialize swarm
options = {'c1': 0.5, 'c2': 0.3, 'm':0.9}
# Call instance of PSO with bounds argument
optimizer = ps.single.GBestPSO(n_particles=10, dims=2, bounds=bounds, **options)
# Perform optimization
cost, pos = optimizer.optimize(fx.rastrigin_func, print_step=100, iters=1000, verbose=3)
.. parsed-literal::
Iteration 1/1000, cost: 10.3592595923
Iteration 101/1000, cost: 0.00381030608321
Iteration 201/1000, cost: 1.31982446305e-07
Iteration 301/1000, cost: 1.16529008665e-11
Iteration 401/1000, cost: 0.0
Iteration 501/1000, cost: 0.0
Iteration 601/1000, cost: 0.0
Iteration 701/1000, cost: 0.0
Iteration 801/1000, cost: 0.0
Iteration 901/1000, cost: 0.0
================================
Optimization finished!
Final cost: 0.000
Best value: [8.9869507154871327e-10, -2.7262405947023504e-09]
8 changes: 8 additions & 0 deletions docs/examples/usecases.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Use-case examples
=================
Below are some of the applications where PySwarms can be used.
If you wish to check the actual Jupyter Notebooks, please go to this `link <https://github.com/ljvmiranda921/pyswarms/tree/master/examples>`_

.. toctree::

basic_optimization
31 changes: 31 additions & 0 deletions docs/features.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
========
Features
========



Single-Objective Optimizers
---------------------------

These are standard optimization techniques that aims to find the optima of a single objective function.

Continuous
~~~~~~~~~~

Single-objective optimization where the search space is continuous.

* :mod:`pyswarms.single.gb` - global-best Particle Swarm Optimization algorithm with a star-topology. Every particle compares itself with the best-performing particle in the swarm.

* :mod:`pyswarms.single.lb` - local-best Particle Swarm Optimization algorithm with a ring-topology. Every particle compares itself only with its nearest-neighbours as computed by a distance metric.


Utilities
---------

Test Functions
~~~~~~~~~~~~~~

These functions can be used as benchmark tests for assessing the performance of the optimization
algorithm.

* :mod:`pyswarms.utils.functions.single_obj` - single-objective test functions
32 changes: 30 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
@@ -1,13 +1,41 @@
Welcome to PySwarms's documentation!
======================================

Contents:
.. image:: https://img.shields.io/pypi/v/pyswarms.svg
:target: https://pypi.python.org/pypi/pyswarms

.. image:: https://img.shields.io/travis/ljvmiranda921/pyswarms.svg
:target: https://travis-ci.org/ljvmiranda921/pyswarms

.. image:: https://readthedocs.org/projects/pyswarms/badge/?version=latest
:target: https://pyswarms.readthedocs.io/en/latest/?badge=latest
:alt: Documentation Status

.. image:: https://pyup.io/repos/github/ljvmiranda921/pyswarms/shield.svg
:target: https://pyup.io/repos/github/ljvmiranda921/pyswarms/
:alt: Updates

PySwarms is a simple, Python-based, Particle Swarm Optimization (PSO) library.

* Free software: MIT license
* Github repository: https://github.com/ljvmiranda921/pyswarms

Launching pad
-------------

* If you don't know what Particle Swarm Optimization is, read up this short `Introduction <http://pyswarms.readthedocs.io/en/latest/intro.html>`_! Then, if you plan to use PySwarms in your project, check the `Installation guide <https://pyswarms.readthedocs.io/en/latest/installation.html>`_ and the `use-case examples <https://pyswarms.readthedocs.io/en/latest/examples/usecases.html>`_ in this documentation.

* If you are a researcher in the field of swarm intelligence, and would like to include your technique in our list of optimizers, check our `contributing <https://pyswarms.readthedocs.io/en/latest/contributing.html>`_ page to see how to implement your optimizer using the current base classes in the library.

* If you are an open-source contributor, and would like to help PySwarms grow, be sure to check our `Issues <https://github.com/ljvmiranda921/pyswarms/issues>`_ page in Github, and see the open issues with the tag `help-wanted <https://github.com/ljvmiranda921/pyswarms/labels/help%20wanted>`_. Moreover, we accommodate contributions from first-time contributors! Just check our `first-timers-only <https://github.com/ljvmiranda921/pyswarms/labels/first-timers-only>`_ tag for open issues (Don't worry! We're happy to help you make your first PR!).


.. toctree::
:maxdepth: 2
:caption: General

readme
intro
features
installation
authors
history
Expand Down
Loading

0 comments on commit 29bcb92

Please sign in to comment.