Skip to content

Commit

Permalink
Merge branch 'master' of github.com:DEAP/deap
Browse files Browse the repository at this point in the history
  • Loading branch information
fmder committed Jul 24, 2023
2 parents 87f465e + b60130a commit 60913c5
Show file tree
Hide file tree
Showing 23 changed files with 103 additions and 83 deletions.
64 changes: 40 additions & 24 deletions deap/benchmarks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,11 @@ def h1(individual):
* - Global optima
- :math:`\mathbf{x} = (8.6998, 6.7665)`, :math:`f(\mathbf{x}) = 2`\n
* - Function
- :math:`f(\mathbf{x}) = \frac{\sin(x_1 - \frac{x_2}{8})^2 + \
\sin(x_2 + \frac{x_1}{8})^2}{\sqrt{(x_1 - 8.6998)^2 + \
(x_2 - 6.7665)^2} + 1}`
- .. math::
f(\mathbf{x}) = \frac{\sin(x_1 - \frac{x_2}{8})^2 + \
\sin(x_2 + \frac{x_1}{8})^2}{\sqrt{(x_1 - 8.6998)^2 + \
(x_2 - 6.7665)^2} + 1}
.. plot:: code/benchmarks/h1.py
:width: 67 %
Expand All @@ -167,8 +169,10 @@ def ackley(individual):
* - Global optima
- :math:`x_i = 0, \forall i \in \lbrace 1 \ldots N\rbrace`, :math:`f(\mathbf{x}) = 0`
* - Function
- :math:`f(\mathbf{x}) = 20 - 20\exp\left(-0.2\sqrt{\frac{1}{N} \
\sum_{i=1}^N x_i^2} \right) + e - \exp\left(\frac{1}{N}\sum_{i=1}^N \cos(2\pi x_i) \right)`
- .. math::
f(\mathbf{x}) = 20 - 20\exp\left(-0.2\sqrt{\frac{1}{N}
\sum_{i=1}^N x_i^2} \right) + e - \exp\left(\frac{1}{N}\sum_{i=1}^N \cos(2\pi x_i) \right)
.. plot:: code/benchmarks/ackley.py
:width: 67 %
Expand All @@ -192,8 +196,10 @@ def bohachevsky(individual):
* - Global optima
- :math:`x_i = 0, \forall i \in \lbrace 1 \ldots N\rbrace`, :math:`f(\mathbf{x}) = 0`
* - Function
- :math:`f(\mathbf{x}) = \sum_{i=1}^{N-1}(x_i^2 + 2x_{i+1}^2 - \
0.3\cos(3\pi x_i) - 0.4\cos(4\pi x_{i+1}) + 0.7)`
- .. math::
f(\mathbf{x}) = \sum_{i=1}^{N-1}(x_i^2 + 2x_{i+1}^2 -
0.3\cos(3\pi x_i) - 0.4\cos(4\pi x_{i+1}) + 0.7)
.. plot:: code/benchmarks/bohachevsky.py
:width: 67 %
Expand All @@ -216,8 +222,10 @@ def griewank(individual):
* - Global optima
- :math:`x_i = 0, \forall i \in \lbrace 1 \ldots N\rbrace`, :math:`f(\mathbf{x}) = 0`
* - Function
- :math:`f(\mathbf{x}) = \frac{1}{4000}\sum_{i=1}^N\,x_i^2 - \
\prod_{i=1}^N\cos\left(\frac{x_i}{\sqrt{i}}\right) + 1`
- .. math::
f(\mathbf{x}) = \frac{1}{4000}\sum_{i=1}^N\,x_i^2 -
\prod_{i=1}^N\cos\left(\frac{x_i}{\sqrt{i}}\right) + 1
.. plot:: code/benchmarks/griewank.py
:width: 67 %
Expand Down Expand Up @@ -252,9 +260,11 @@ def rastrigin(individual):
def rastrigin_scaled(individual):
r"""Scaled Rastrigin test objective function.
:math:`f_{\text{RastScaled}}(\mathbf{x}) = 10N + \sum_{i=1}^N \
\left(10^{\left(\frac{i-1}{N-1}\right)} x_i \right)^2 - \
10\cos\left(2\pi 10^{\left(\frac{i-1}{N-1}\right)} x_i \right)`
.. math::
f_{\text{RastScaled}}(\mathbf{x}) = 10N + \sum_{i=1}^N
\left(10^{\left(\frac{i-1}{N-1}\right)} x_i \right)^2 -
10\cos\left(2\pi 10^{\left(\frac{i-1}{N-1}\right)} x_i \right)`
"""
N = len(individual)
return 10 * N + sum((10 ** (i / (N - 1)) * x) ** 2 -
Expand All @@ -264,13 +274,18 @@ def rastrigin_scaled(individual):
def rastrigin_skew(individual):
r"""Skewed Rastrigin test objective function.
:math:`f_{\text{RastSkew}}(\mathbf{x}) = 10N + \sum_{i=1}^N \left(y_i^2 - 10 \cos(2\pi x_i)\right)`
.. math::
:math:`\text{with } y_i = \
\begin{cases} \
10\cdot x_i & \text{ if } x_i > 0,\\ \
x_i & \text{ otherwise } \
\end{cases}`
f_{\text{RastSkew}}(\mathbf{x}) =
10N + \sum_{i=1}^N \left(y_i^2 - 10 \cos(2\pi x_i)\right)
.. math::
\text{with } y_i =
\begin{cases}
10\cdot x_i & \text{ if } x_i > 0,\\ \\
x_i & \text{ otherwise }
\end{cases}
"""
N = len(individual)
return 10 * N + sum((10 * x if x > 0 else x) ** 2
Expand All @@ -291,9 +306,10 @@ def schaffer(individual):
* - Global optima
- :math:`x_i = 0, \forall i \in \lbrace 1 \ldots N\rbrace`, :math:`f(\mathbf{x}) = 0`
* - Function
- :math:`f(\mathbf{x}) = \sum_{i=1}^{N-1} (x_i^2+x_{i+1}^2)^{0.25} \cdot \
\left[ \sin^2(50\cdot(x_i^2+x_{i+1}^2)^{0.10}) + 1.0 \
\right]`
- .. math::
f(\mathbf{x}) = \sum_{i=1}^{N-1} (x_i^2+x_{i+1}^2)^{0.25} \cdot
\left[ \sin^2(50\cdot(x_i^2+x_{i+1}^2)^{0.10}) + 1.0 \right]
.. plot:: code/benchmarks/schaffer.py
:width: 67 %
Expand All @@ -316,9 +332,9 @@ def schwefel(individual):
* - Global optima
- :math:`x_i = 420.96874636, \forall i \in \lbrace 1 \ldots N\rbrace`, :math:`f(\mathbf{x}) = 0`
* - Function
- :math:`f(\mathbf{x}) = 418.9828872724339\cdot N - \
\sum_{i=1}^N\,x_i\sin\left(\sqrt{|x_i|}\right)`
- .. math::
f(\mathbf{x}) = 418.9828872724339\cdot N -
\sum_{i=1}^N\,x_i\sin\left(\sqrt{|x_i|}\right)
.. plot:: code/benchmarks/schwefel.py
:width: 67 %
Expand Down
6 changes: 3 additions & 3 deletions deap/cma.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ class StrategyOnePlusLambda(object):
| ``d`` | ``1.0 + N / (2.0 * | Damping for step-size. |
| | lambda_)`` | |
+----------------+---------------------------+----------------------------+
| ``ptarg`` | ``1.0 / (5 + sqrt(lambda_)| Target success rate. |
| ``ptarg`` | ``1.0 / (5 + sqrt(lambda_)| Target success rate. |
| | / 2.0)`` | |
+----------------+---------------------------+----------------------------+
| ``cp`` | ``ptarg * lambda_ / (2.0 +| Step size learning rate. |
Expand All @@ -243,7 +243,7 @@ class StrategyOnePlusLambda(object):
+----------------+---------------------------+----------------------------+
.. [Igel2007] Igel, Hansen, Roth, 2007. Covariance matrix adaptation for
multi-objective optimization. *Evolutionary Computation* Spring;15(1):1-28
multi-objective optimization. *Evolutionary Computation* Spring;15(1):1-28
"""
def __init__(self, parent, sigma, **kargs):
Expand Down Expand Up @@ -349,7 +349,7 @@ class StrategyMultiObjective(object):
+================+===========================+============================+
| ``d`` | ``1.0 + N / 2.0`` | Damping for step-size. |
+----------------+---------------------------+----------------------------+
| ``ptarg`` | ``1.0 / (5 + 1.0 / 2.0)`` | Target success rate. |
| ``ptarg`` | ``1.0 / (5 + 1.0 / 2.0)`` | Target success rate. |
+----------------+---------------------------+----------------------------+
| ``cp`` | ``ptarg / (2.0 + ptarg)`` | Step size learning rate. |
+----------------+---------------------------+----------------------------+
Expand Down
2 changes: 1 addition & 1 deletion deap/creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def __init__(self):
.. warning::
If your are inheriting from :class:`numpy.ndarray` see the
:doc:`tutorials/advanced/numpy` tutorial and the
:doc:`/tutorials/advanced/numpy` tutorial and the
:doc:`/examples/ga_onemax_numpy` example.
"""
Expand Down
2 changes: 1 addition & 1 deletion doc/api/gp.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Genetic Programming
.. autoclass:: deap.gp.Terminal
:members:

.. autoclass:: deap.gp.Ephemeral
.. autoclass:: deap.gp.MetaEphemeral
:members:

.. autofunction:: deap.gp.compile
Expand Down
2 changes: 1 addition & 1 deletion doc/code/benchmarks/ackley.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def ackley_arg0(sol):
X = np.arange(-30, 30, 0.5)
Y = np.arange(-30, 30, 0.5)
X, Y = np.meshgrid(X, Y)
Z = np.fromiter(map(ackley_arg0, zip(X.flat,Y.flat)), dtype=np.float, count=X.shape[0]*X.shape[1]).reshape(X.shape)
Z = np.fromiter(map(ackley_arg0, zip(X.flat,Y.flat)), dtype=float, count=X.shape[0]*X.shape[1]).reshape(X.shape)

ax.plot_surface(X, Y, Z, rstride=1, cstride=1, norm=LogNorm(), cmap=cm.jet, linewidth=0.2)

Expand Down
2 changes: 1 addition & 1 deletion doc/code/benchmarks/bohachevsky.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def bohachevsky_arg0(sol):
X = np.arange(-15, 15, 0.5)
Y = np.arange(-15, 15, 0.5)
X, Y = np.meshgrid(X, Y)
Z = np.fromiter(map(bohachevsky_arg0, zip(X.flat,Y.flat)), dtype=np.float, count=X.shape[0]*X.shape[1]).reshape(X.shape)
Z = np.fromiter(map(bohachevsky_arg0, zip(X.flat,Y.flat)), dtype=float, count=X.shape[0]*X.shape[1]).reshape(X.shape)


ax.plot_surface(X, Y, Z, rstride=1, cstride=1, norm=LogNorm(), cmap=cm.jet, linewidth=0.2)
Expand Down
2 changes: 1 addition & 1 deletion doc/code/benchmarks/griewank.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def griewank_arg0(sol):
X = np.arange(-50, 50, 0.5)
Y = np.arange(-50, 50, 0.5)
X, Y = np.meshgrid(X, Y)
Z = np.fromiter(map(griewank_arg0, zip(X.flat,Y.flat)), dtype=np.float, count=X.shape[0]*X.shape[1]).reshape(X.shape)
Z = np.fromiter(map(griewank_arg0, zip(X.flat,Y.flat)), dtype=float, count=X.shape[0]*X.shape[1]).reshape(X.shape)

ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.jet, linewidth=0.2)

Expand Down
2 changes: 1 addition & 1 deletion doc/code/benchmarks/h1.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def h1_arg0(sol):
X = np.arange(-25, 25, 0.5)
Y = np.arange(-25, 25, 0.5)
X, Y = np.meshgrid(X, Y)
Z = np.fromiter(map(h1_arg0, zip(X.flat,Y.flat)), dtype=np.float, count=X.shape[0]*X.shape[1]).reshape(X.shape)
Z = np.fromiter(map(h1_arg0, zip(X.flat,Y.flat)), dtype=float, count=X.shape[0]*X.shape[1]).reshape(X.shape)

ax.plot_surface(X, Y, Z, rstride=1, cstride=1, norm=LogNorm(), cmap=cm.jet, linewidth=0.2)

Expand Down
2 changes: 1 addition & 1 deletion doc/code/benchmarks/himmelblau.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def himmelblau_arg0(sol):
X = np.arange(-6, 6, 0.1)
Y = np.arange(-6, 6, 0.1)
X, Y = np.meshgrid(X, Y)
Z = np.fromiter(map(himmelblau_arg0, zip(X.flat,Y.flat)), dtype=np.float, count=X.shape[0]*X.shape[1]).reshape(X.shape)
Z = np.fromiter(map(himmelblau_arg0, zip(X.flat,Y.flat)), dtype=float, count=X.shape[0]*X.shape[1]).reshape(X.shape)

ax.plot_surface(X, Y, Z, rstride=1, cstride=1, norm=LogNorm(), cmap=cm.jet, linewidth=0.2)

Expand Down
2 changes: 1 addition & 1 deletion doc/code/benchmarks/movingsc1.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
X = np.arange(0, 100, 1.0)
Y = np.arange(0, 100, 1.0)
X, Y = np.meshgrid(X, Y)
Z = np.fromiter(map(lambda x: mp(x)[0], zip(X.flat,Y.flat)), dtype=np.float, count=X.shape[0]*X.shape[1]).reshape(X.shape)
Z = np.fromiter(map(lambda x: mp(x)[0], zip(X.flat,Y.flat)), dtype=float, count=X.shape[0]*X.shape[1]).reshape(X.shape)

ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.jet, linewidth=0.2)

Expand Down
2 changes: 1 addition & 1 deletion doc/code/benchmarks/rastrigin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def rastrigin_arg0(sol):
X = np.arange(-5, 5, 0.1)
Y = np.arange(-5, 5, 0.1)
X, Y = np.meshgrid(X, Y)
Z = np.fromiter(map(rastrigin_arg0, zip(X.flat,Y.flat)), dtype=np.float, count=X.shape[0]*X.shape[1]).reshape(X.shape)
Z = np.fromiter(map(rastrigin_arg0, zip(X.flat,Y.flat)), dtype=float, count=X.shape[0]*X.shape[1]).reshape(X.shape)

ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.jet, linewidth=0.2)

Expand Down
2 changes: 1 addition & 1 deletion doc/code/benchmarks/rosenbrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def rosenbrock_arg0(sol):
X = np.arange(-2, 2, 0.1)
Y = np.arange(-1, 3, 0.1)
X, Y = np.meshgrid(X, Y)
Z = np.fromiter(map(rosenbrock_arg0, zip(X.flat,Y.flat)), dtype=np.float, count=X.shape[0]*X.shape[1]).reshape(X.shape)
Z = np.fromiter(map(rosenbrock_arg0, zip(X.flat,Y.flat)), dtype=float, count=X.shape[0]*X.shape[1]).reshape(X.shape)

ax.plot_surface(X, Y, Z, rstride=1, cstride=1, norm=LogNorm(), cmap=cm.jet, linewidth=0.2)

Expand Down
2 changes: 1 addition & 1 deletion doc/code/benchmarks/schaffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def schaffer_arg0(sol):
X = np.arange(-25, 25, 0.25)
Y = np.arange(-25, 25, 0.25)
X, Y = np.meshgrid(X, Y)
Z = np.fromiter(map(schaffer_arg0, zip(X.flat,Y.flat)), dtype=np.float, count=X.shape[0]*X.shape[1]).reshape(X.shape)
Z = np.fromiter(map(schaffer_arg0, zip(X.flat,Y.flat)), dtype=float, count=X.shape[0]*X.shape[1]).reshape(X.shape)

ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.jet, linewidth=0.2)

Expand Down
2 changes: 1 addition & 1 deletion doc/code/benchmarks/schwefel.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def schwefel_arg0(sol):
X = np.arange(-500, 500, 10)
Y = np.arange(-500, 500, 10)
X, Y = np.meshgrid(X, Y)
Z = np.fromiter(map(schwefel_arg0, zip(X.flat,Y.flat)), dtype=np.float, count=X.shape[0]*X.shape[1]).reshape(X.shape)
Z = np.fromiter(map(schwefel_arg0, zip(X.flat,Y.flat)), dtype=float, count=X.shape[0]*X.shape[1]).reshape(X.shape)

ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.jet, linewidth=0.2)

Expand Down
2 changes: 1 addition & 1 deletion doc/code/benchmarks/shekel.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def shekel_arg0(sol):
X = np.arange(0, 1, 0.01)
Y = np.arange(0, 1, 0.01)
X, Y = np.meshgrid(X, Y)
Z = np.fromiter(map(shekel_arg0, zip(X.flat,Y.flat)), dtype=np.float, count=X.shape[0]*X.shape[1]).reshape(X.shape)
Z = np.fromiter(map(shekel_arg0, zip(X.flat,Y.flat)), dtype=float, count=X.shape[0]*X.shape[1]).reshape(X.shape)

ax.plot_surface(X, Y, Z, rstride=1, cstride=1, norm=LogNorm(), cmap=cm.jet, linewidth=0.2)

Expand Down
24 changes: 14 additions & 10 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# All configuration values have a default; values that are commented out
# serve to show the default.

import sys, time
import sys, time, os

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
Expand All @@ -35,8 +35,7 @@
except ImportError:
pass
else:
extensions += ['matplotlib.sphinxext.only_directives',
'matplotlib.sphinxext.plot_directive']
extensions += ['matplotlib.sphinxext.plot_directive']

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
Expand Down Expand Up @@ -110,6 +109,9 @@
# Reload the cached values every 5 days
intersphinx_cache_limit = 5

# Align mathjax equations
mathjax3_config = {'chtml': {'displayAlign': 'left'}}

# -- Options for pyplot extension ----------------------------------------------

# Default value for the include-source option
Expand All @@ -128,13 +130,15 @@

# -- Options for extlinks extension ----------------------------------------------
import subprocess
try:
tree = subprocess.check_output(["git", "rev-parse", "HEAD"]).strip()
except OSError:
import warnings
warnings.warn("Cannot link examples because we cannot retrieve the git version", Warning)
else:
extlinks = {'example': ('https://github.com/DEAP/deap/blob/{tree}/examples/%s.py'.format(tree=tree), "examples/")}
tree = os.getenv('GITHUB_COMMIT')
if tree is None:
try:
tree = subprocess.check_output(["git", "rev-parse", "HEAD"], text=True).strip()
except OSError:
import warnings
warnings.warn("Cannot link examples because we cannot retrieve the git version", Warning)
if tree:
extlinks = {'example': ('https://github.com/DEAP/deap/blob/{tree}/examples/%s.py'.format(tree=tree), "examples/%s")}
# -- Options for HTML output ---------------------------------------------------

# Add any paths that contain custom themes here, relative to this directory.
Expand Down
16 changes: 8 additions & 8 deletions doc/examples/eda.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ The basic concept concept behind EDA is to sample :math:`\lambda` individuals
with a certain distribution and estimate the problem distribution from the
:math:`\mu` best individuals. This really simple concept adhere to the
generate-update logic. The strategy contains a random number generator which
is adapted from the population. The following :class:`EDA` class do just that.
is adapted from the population. The following :class:`EMNA` class do just that.

.. literalinclude:: /../examples/eda/fctmin.py
:pyobject: EDA
.. literalinclude:: /../examples/eda/emna.py
:pyobject: EMNA

A normal random number generator is initialized with a certain mean
(*centroid*) and standard deviation (*sigma*) for each
(*centroid*) and standard deviation (*sigma*) for each
dimension. The :meth:`generate` method uses numpy to generate *lambda_*
sequences in *dim* dimensions, then the sequences are used to initialize
individuals of class given in the *ind_init* argument. Finally, the
Expand All @@ -37,19 +37,19 @@ Two classes are needed, a minimization fitness and a individual that will
combine the fitness and the real values. Moreover, we will use
:class:`numpy.ndarray` as base class for our individuals.

.. literalinclude:: /../examples/eda/fctmin.py
.. literalinclude:: /../examples/eda/emna.py
:lines: 28-29

Operators
=========

The :func:`~deap.algorithms.eaGenerateUpdate` algorithm requires to set in a
toolbox an evaluation function, an generation method and an update method.
We will use the method of an initialized :class:`EDA`. For the generate
We will use the method of an initialized :class:`EMNA`. For the generate
method, we set the class that the individuals are transferred in to our
:class:`Individual` class containing a fitness.

.. literalinclude:: /../examples/eda/fctmin.py
.. literalinclude:: /../examples/eda/emna.py
:pyobject: main

The complete :example:`eda/fctmin`.
The complete :example:`eda/fctmin`.
6 changes: 3 additions & 3 deletions doc/examples/ga_onemax_short.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ One Max Problem: Short Version
===============================

The short One Max genetic algorithm example is very similar to the full one
:ref:`-ga-onemax`. The only difference is that it makes use of the
:ref:`ga-onemax`. The only difference is that it makes use of the
:mod:`~deap.algorithms` module which implements some basic evolutionary
algorithms. The initializations are almost the same. We only have to import
some additional packages and modules.

.. literalinclude:: /../examples/ga/onemax_short.py
:lines: 16,19-21

In order to use the evolution functions implemented in :mod:`~deap.algorithms`,
we have to register some functions from the :mod:`~deap.tools` module:
:func:`evaluate`, :func:`mate`, :func:`mutate`, and :func:`~deap.Toolbox.select`.
Expand All @@ -21,7 +21,7 @@ we have to register some functions from the :mod:`~deap.tools` module:
:lines: 41-44

The toolbox is then passed to the algorithm and via ``stats`` it uses the
registered functions.
registered functions.

.. literalinclude:: /../examples/ga/onemax_short.py
:lines: 46, 49-58
Expand Down
Loading

0 comments on commit 60913c5

Please sign in to comment.