Skip to content

Commit

Permalink
Adjusted the cmaes_plotting example to the new logbook and adjusted t…
Browse files Browse the repository at this point in the history
…he documentation accordingly.

--HG--
branch : dev
  • Loading branch information
fmder committed Jun 29, 2013
1 parent 79ea3ff commit 9213a55
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
12 changes: 6 additions & 6 deletions doc/examples/cmaes_plotting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,35 @@ The beginning of this example is exactly the same as the :ref:`CMA-ES
:func:`~deap.algorithms.eaGenerateUpdate` is somewhat insufficient for our
purpose. We need to gather the required data on each generation. So instead of
using the :func:`~deap.algorithms.eaGenerateUpdate` function, we'll develop
it to get a grip on what is recorded. First, we'll created objects to record
it to get a grip on what is recorded. First, we'll create objects to record
our data. Here we want to plot, in addition to what the
:class:`~deap.Statistics` and :class:`~deap.HallOfFame` objects contain, the
:class:`~deap.tools.Logbook` and :class:`~deap.tools.HallOfFame` objects contain, the
step size, the axis ratio and the major axis of the covariace matrix, the best
value so far, the best coordinates so far and the standard deviation of the
all coordinates at each generation.

.. literalinclude:: /code/examples/es/cma_plotting.py
:lines: 56-61
:lines: 59-64

Once the objects are created, the evolution loop, based on a generational
stopping criterion, calls repeatedly the :meth:`generate`, :meth:`evaluate`
and :meth:`update` methods registered in the toolbox.

.. literalinclude:: /code/examples/es/cma_plotting.py
:lines: 63-72
:lines: 66-75

Then, the previoulsy created objects start to play their role. The according
data is recorded in each object on each generation.

.. literalinclude:: /code/examples/es/cma_plotting.py
:lines: 77,84-89
:lines: 80,81,88-93

Now that the data is recorded the only thing left to do is to plot it. We'll
use `matplotlib <http://matplotlib.org>`_ to generate the graphics from the
recorded data.

.. literalinclude:: /code/examples/es/cma_plotting.py
:lines: 92-120
:lines: 95-124

Which gives the following result.

Expand Down
10 changes: 7 additions & 3 deletions examples/es/cma_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ def main(verbose=True):
stats.register("std", numpy.std)
stats.register("min", numpy.min)
stats.register("max", numpy.max)

logbook = tools.Logbook()
logbook.header = "gen", "evals", "std", "min", "avg", "max"

# Objects that will compile the data
sigma = numpy.ndarray((NGEN,1))
Expand All @@ -74,10 +77,11 @@ def main(verbose=True):
# Update the hall of fame and the statistics with the
# currently evaluated population
halloffame.update(population)
stats.record(population, evals=len(population), gen=gen,)
record = stats.compile(population)
logbook.record(evals=len(population), gen=gen, **record)

if verbose:
print(stats.stream)
print(logbook.stream)

# Save more data along the evolution for latter plotting
# diagD is sorted and sqrooted in the update method
Expand All @@ -90,7 +94,7 @@ def main(verbose=True):

# The x-axis will be the number of evaluations
x = list(range(0, strategy.lambda_ * NGEN, strategy.lambda_))
avg, max_, min_ = stats.select("avg", "max", "min")
avg, max_, min_ = logbook.select("avg", "max", "min")
plt.figure()
plt.subplot(2, 2, 1)
plt.semilogy(x, avg, "--b")
Expand Down

0 comments on commit 9213a55

Please sign in to comment.