Revision: 3deaae3a3943
Branch: default
Author:
felix.anto...@gmail.com
Date: Wed Apr 9 01:49:36 2014 UTC
Log: Edited wiki page SimpleExample through web user interface.
http://code.google.com/p/deap/source/detail?r=3deaae3a3943&repo=wiki
Modified:
/SimpleExample.wiki
=======================================
--- /SimpleExample.wiki Wed Apr 4 15:24:24 2012 UTC
+++ /SimpleExample.wiki Wed Apr 9 01:49:36 2014 UTC
@@ -1,167 +1,4 @@
-#summary A very simple example of genetic algorithm in python using DEAP
+#summary Simple Genetic Algorithm in Python with DEAP
#labels Deprecated,Example
-*SEE THE DOCUMENTATION EXAMPLES INSTEAD*
-
-<wiki:toc max_depth="2" />
-
-= Introduction =
-
-This is an example to show how simple it is to use EAP. More examples are
presented in the tutorial part of the
[
http://doc.deap.googlecode.com/hg/0.6/index.html EAP User Guide].
-
-= The One Max Problem =
-
-This is the first complete program built with EAP. It will help new users
to overview some of the possibilities in EAP. The problem is very simple,
we search a 1 (`True`) filled vector individual. This problem is widely
used in the evolutionary computation community since it is very simple and
it illutrates well the potential of evolutionary algorithms.
-
-
-= Setting Things Up =
-
-Here we use the one max problem to show how simple can be an evolutionary
algorithm with EAP. The first thing to do is to ellaborate the structures
of the algorithm.
-<wiki:comment> It is pretty obvious in this case that an individual that
can contain a serie of `booleans` is the most interesting kind of structure
available. </wiki:comment>
-The most natural representation we could use is a vector of `booleans`.
-EAP does not contain any explicit individual structure since it is simply
a container of attributes associated with a fitness. Instead, the framework
provides a convenient method for creating types called the creator.
-
-== Creator ==
-
-The creator is a class factory that can build at runtime new classes that
inherit from a base class. It is very useful since an individual can be any
type of container from list to n-ary tree. The creator also allows to add
attributes to the new class.
-
-Let see an example of how to use the creator to setup an individual that
is an array of booleans and contains a miximizing fitness. First, we need
to import the `eap.base` and `eap.creator` modules.
-
-The creator module defines a single function called `create` that is used
to create types. The `create` function takes at least 2 arguments plus
optional keyword arguments. The first argument `name` is the actual name of
the type that we want to create: `Individual`. The second argument is a
base class that the new type created will inherit from. Finally, the
optional keyword arguments are added as attributes of the new type (this
subject is more detailed in the documentation, and out of the current
scope).
-
-{{{
-creator.create("FitnessMax", base.Fitness, weights=(1,0) )
-creator.create("Individual", list, fitness=creator.FitnessMax)
-}}}
-
-The first line creates a maximizing fitness by setting in the weights
member of the base class `Fitness` with (1.0,). The weights indicate how
many objectives will be optimized, and if they will be maximized "1.0" or
minimized "-1.0". The second line creates an `Individual` class that
inherits from `list` and has a `fitness` member of the type `FitnessMax`
that was just created.
-
-== Toolbox ==
-
-The `Toolbox` is an other convenience structure that stores functions with
their arguments. The `Toolbox` contains simple methods such as `register`
and `unregister`.
-
-{{{
-toolbox = base.Toolbox()
-
-# Attribute generator
-toolbox.register("attr_bool", random.randint, 0, 1)
-
-# Structure initializers
-toolbox.register("individual", creator.Individual, tools.initRepeat,
toolbox.attr_bool, n=100)
-toolbox.register("population", list, tools.initRepeat, toolbox.individual,
n=300)
-}}}
-
-The two last lines of code create two functions within the toolbox, the
first function registered, when called, will instantiate an individual and
the second will instantiate a list containing 300 individuals.
-
-= Objective Function =
-
-The evaluation function is pretty simple in this case, we need to count
the number of 1s in the individual and set the fitness as this value. The
fitness must be a `tuple`. This is done by the following lines of code.
-
-{{{
-def evalOneMax(individual):
- return sum(individual),
-}}}
-
-= Genetic Operators =
-
-There is two ways of using operators, the first one, is to call the
function from the `eap.toolbox` module and the second one is to register
them with their argument in a `Toolbox`. The most convenient way is to
register them in the toolbox, because it allows to easily switch between
operators if desired. The toolbox method is also used in the algorithms
[
http://doc.deap.googlecode.com/hg/short_ga_onemax.html one max short
version].
-
-Registering the operators and their default arguments in the toolbox is
done as follow.
-
-{{{
-toolbox.register('evaluate', evalOneMax)
-toolbox.register('mate', tools.cxTwoPoints)
-toolbox.register('mutate', tools.mutFlipBit, indpb=0.05)
-toolbox.register('select', tools.selTournament, tournsize=3)
-}}}
-
-= Evolving the Population =
-
-== Creating the Population ==
-
-Before evolving it, we need to instanciate a population. This step is done
effortless by using the method `population` we registered in the `Toolbox`.
-
-{{{
-pop = toolbox.population()
-}}}
-
-Then the population has to be evaluated.
-
-{{{
-for ind in pop:
- ind.fitness.values = toolbox.evaluate(ind)
-}}}
-
-== The Appeal of Evolution ==
-
-The evolution of the population is the last thing to do before getting
results. In this example we *do not* use the `eap.algorithms` module in
order to show how to manipulate the different features of EAP. Let say that
our evolution has a fixed number of generations (`MAXGEN`).
-
-{{{
-for g in range(MAXGEN):
-}}}
-
-Is that simple enough? We now need to select the individuals that will
participate to the production of the next generation. Those selected
individuals are cloned so that they are independant of their parents.
`Toolbox` also implements a method `clone` to easily duplicate any object.
-
-{{{
- # Select the next generation individuals
- offsprings = toolbox.select(pop, len(pop))
- # Clone the selected individuals
- offsprings = [toolbox.clone(ind) for ind in offsprings]
-}}}
-
-Lets continue with more complicated stuff, mating and mutating the
offsprings. The crossover and mutation operators provided with eap take
respectively 2 and 1 individual(s). Those individuals are modified in-place
and a reference to them is also returned by the functions. In the
following, a crossover is applied with a probability `CXPB` and a mutation
with a probability `MUTPB` on every individuals. When a crossover or a
mutation is applied, the fitness of the individual is invalidated by the
`del` statement.
-
-{{{
- # Apply crossover and mutation on the offsprings
- for child1, child2 in zip(offsprings[::2], offsprings[1::2]):
- if random.random() < CXPB:
- toolbox.mate(child1, child2)
- del child1.fitness.values
- del child2.fitness.values
-
- for mutant in offsprings:
- if random.random() < MUTPB:
- toolbox.mutate(mutant)
- del mutant.fitness.values
-}}}
-
-The offsprings now need to be evaluated, we apply the evaluation on every
individual in the population that has an invalid fitness.
-
-{{{
- for ind in offsprings:
- if not ind.fitness.valid:
- ind.fitness.values = toolbox.evaluate(ind)
-}}}
-
-Finally, we replace the whole population by the offsprings, as stated in
the simple GA algorithm.
-
-{{{
- pop = offsprings
-}}}
-
-Some statistics on the population may be gathered. The following lines
print the min, max, mean and standard deviation of the population.
-
-{{{
- # Gather all the fitnesses in one list and print the stats
- fits = [ind.fitness.values[0] for ind in pop]
-
- length = len(pop)
- mean = sum(fits) / length
- sum2 = sum(x*x for x in fits)
- std_dev = abs(sum2 / length - mean**2)**0.5
-
- print " Min %s" % min(fits)
- print " Max %s" % max(fits)
- print " Avg %s" % mean
- print " Std %s" % std_dev
-}}}
-
-= Conclusion =
-
-The whole example with the algorithm, a lots of blank lines, comments and
imports is about 70 lines of codes. If there is a genetic algorithm more
compact (but still readable) that allows the same level of freedom
available, send it to us at <deap-users at googlegroups dot com>!
-
-In other words, EAP is easy to use and allows great flexibility.
-
-The complete one max genetic algorithm is available at
http://doc.deap.googlecode.com/hg/0.7/examples/onemax.html and more
examples at
http://doc.deap.googlecode.com/hg/0.7/examples/index.html.
-
-Recommend this : <g:plusone size="medium"></g:plusone>
+[
http://deap.gel.ulaval.ca/doc/default/examples/ga_onemax.html *See the
documentation example instead*]