Skip to content

Commit

Permalink
Merge pull request DEAP#120 from Ogaday/doc
Browse files Browse the repository at this point in the history
Update code line references. Fixes DEAP#99
  • Loading branch information
fmder committed Feb 17, 2016
2 parents eb3c951 + da326e4 commit 7b35c11
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
26 changes: 13 additions & 13 deletions doc/examples/ga_onemax.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ provides a convenient method for creating types called the creator.
First of all, we need to import some modules.

.. literalinclude:: /../examples/ga/onemax.py
:lines: 16-20
:lines: 20-24

-------
Creator
Expand All @@ -48,7 +48,7 @@ from. Finally the optional arguments are members to add to the new type, for
example a :attr:`fitness` for an individual or :attr:`speed` for a particle.

.. literalinclude:: /../examples/ga/onemax.py
:lines: 22-23
:lines: 26-27

The first line creates a maximizing fitness by replacing, in the base type
:class:`~deap.base.Fitness`, the pure virtual
Expand All @@ -75,7 +75,7 @@ methods, :meth:`~deap.base.Toolbox.register` and
:meth:`~deap.base.Toolbox.unregister` that are used to do the tricks.

.. literalinclude:: /../examples/ga/onemax.py
:lines: 25-31
:lines: 29,31,36,38,41-42,45

In this code block we registered a generation function and two initialization
functions. The generator :meth:`toolbox.attr_bool` when called, will draw a
Expand Down Expand Up @@ -108,7 +108,7 @@ The evaluation function is pretty simple in this case, we need to count the
number of ones in the individual. This is done by the following lines of code.

.. literalinclude:: /../examples/ga/onemax.py
:lines: 33-34
:lines: 48-49

The returned value must be an iterable of length equal to the number of
objectives (weights).
Expand All @@ -127,7 +127,7 @@ Registering the operators and their default arguments in the toolbox is done
as follow.

.. literalinclude:: /../examples/ga/onemax.py
:lines: 37-40
:lines: 55,58,62,68

The evaluation is given the alias evaluate. Having a single argument being the
individual to evaluate we don't need to fix any, the individual will be given
Expand Down Expand Up @@ -155,15 +155,15 @@ Before evolving it, we need to instantiate a population. This step is done
effortless using the method we registered in the toolbox.

.. literalinclude:: /../examples/ga/onemax.py
:lines: 42, 45
:lines: 72,77


``pop`` will be a :class:`list` composed of 300 individuals, *n* is the
parameter left open earlier in the toolbox. The next thing to do is to
evaluate this brand new population.

.. literalinclude:: /../examples/ga/onemax.py
:lines: 50-53
:lines: 90-93

We first :func:`map` the evaluation function to every individual, then assign
their respective fitness. Note that the order in ``fitnesses`` and
Expand All @@ -178,7 +178,7 @@ we want to evolve for a fixed number of generation :data:`NGEN`, the
evolution will then begin with a simple for statement.

.. literalinclude:: /../examples/ga/onemax.py
:lines: 57-59
:lines: 97-99

Is that simple enough? Lets continue with more complicated things, selecting,
mating and mutating the population. The crossover and mutation operators
Expand All @@ -189,7 +189,7 @@ individuals.
In a simple GA, the first step is to select the next generation.

.. literalinclude:: /../examples/ga/onemax.py
:lines: 61-64
:lines: 101-104

This step creates an offspring list that is an exact copy of the selected
individuals. The :meth:`toolbox.clone` method ensure that we don't own a
Expand All @@ -202,19 +202,19 @@ mutation with probability :data:`MUTPB`. The ``del`` statement simply
invalidate the fitness of the modified individuals.

.. literalinclude:: /../examples/ga/onemax.py
:lines: 66-76
:lines: 106-107,110-111,115-118,121-123

The population now needs to be re-evaluated, we then apply the evaluation as
seen earlier, but this time only on the individuals with an invalid fitness.

.. literalinclude:: /../examples/ga/onemax.py
:lines: 78-82
:lines: 125-129

And finally, last but not least, we replace the old population by the
offspring.

.. literalinclude:: /../examples/ga/onemax.py
:lines: 87
:lines: 134

This is the end of the evolution part, it will continue until the predefined
number of generation are accomplished.
Expand All @@ -223,7 +223,7 @@ Although, some statistics may be gathered on the population, the following
lines print the min, max, mean and standard deviation of the population.

.. literalinclude:: /../examples/ga/onemax.py
:lines: 89-100
:lines: 136-147

A :class:`~deap.tools.Statistics` object has been defined to facilitate how
statistics are gathered. It is not presented here so that we can focus on the
Expand Down
8 changes: 5 additions & 3 deletions examples/ga/onemax.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,20 @@

toolbox = base.Toolbox()

# Attribute generator: define 'attr_bool' to be an attribute ('gene')
# Attribute generator
# define 'attr_bool' to be an attribute ('gene')
# which corresponds to integers sampled uniformly
# from the range [0,1] (i.e. 0 or 1 with equal
# probability)
toolbox.register("attr_bool", random.randint, 0, 1)

# Structure initializers: define 'individual' to be an individual
# Structure initializers
# define 'individual' to be an individual
# consisting of 100 'attr_bool' elements ('genes')
toolbox.register("individual", tools.initRepeat, creator.Individual,
toolbox.attr_bool, 100)

# define the population to be a list of 'individual's
# define the population to be a list of individuals
toolbox.register("population", tools.initRepeat, list, toolbox.individual)

# the goal ('fitness') function to be maximized
Expand Down

0 comments on commit 7b35c11

Please sign in to comment.