~ubuntu-branches/ubuntu/quantal/deap/quantal

« back to all changes in this revision

Viewing changes to doc/ea_bases.rst

  • Committer: Package Import Robot
  • Author(s): Miriam Ruiz, Jakub Wilk, Miriam Ruiz
  • Date: 2011-11-17 11:53:15 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20111117115315-k9lkwpqcbsq8n0q7
Tags: 0.7.1-1
[ Jakub Wilk ]
* Add Vcs-* fields.

[ Miriam Ruiz ]
* New Upstream Release
* Upgraded Standards-Version from 3.9.1 to 3.9.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
.. _ea-bases:
2
 
 
3
 
============================
4
 
Evolutionary Algorithm Bases
5
 
============================
6
 
 
7
 
.. automodule:: eap.base
8
 
 
9
 
.. _containers:
10
 
 
11
 
Container Types
12
 
===============
13
 
 
14
 
As of version 0.5.0, eap does not provide base types that wrap python base types, instead it is possible to create your own types that inherits from whatever type is more convenient or appropriate, for example, :class:`list`, :class:`array`, :class:`set`, :class:`dict`, etc. As most of those types are initialized using an iterable, the :mod:`~eap.creator` allows to create these objects using internal generators in order to produce objects with different content. See the :mod:`~eap.creator` module for more information.
15
 
 
16
 
.. autoclass:: eap.base.Tree([content])
17
 
   :members:
18
 
 
19
 
Fitness
20
 
=======
21
 
 
22
 
.. autoclass:: eap.base.Fitness
23
 
   :members:
24
 
 
25
 
Creator
26
 
=======
27
 
 
28
 
.. automodule:: eap.creator
29
 
 
30
 
.. autofunction:: eap.creator.create(name, base[, attribute[, ...]])
31
 
 
32
 
   For example, using ::
33
 
   
34
 
       creator("MyType", object, value=4, data=lambda: random.random())
35
 
       
36
 
   is the same as defining in the module :mod:`eap.creator` ::
37
 
   
38
 
       class MyType(object):
39
 
           value = 4
40
 
           def __init__(self):
41
 
               self.data = random.random()
42
 
   
43
 
 
44
 
Population, Individual and Other Structures
45
 
============================================
46
 
 
47
 
All application specific structures may be built using the :func:`~eap.creator.create` function and types defined in python or the :mod:`~eap.base` module. Here are some simple recipes to build very simple types.
48
 
 
49
 
Fitness
50
 
+++++++
51
 
 
52
 
As described earlier, the :class:`eap.base.Fitness` instantiate by default a minimizing fitness. This can be changed using the :mod:`~eap.creator` and its :func:`eap.creator.create` function. A maximizing fitness can be created using ::
53
 
 
54
 
    create("FitnessMax", base.Fitness, weights=(1.0,))
55
 
 
56
 
 
57
 
Individual
58
 
++++++++++
59
 
 
60
 
There is only one rule when building an individual, it **must** contain a :attr:`fitness` attribute. The following all produce valid individuals that are suited for most evolutionary algorithms.
61
 
 
62
 
Individual List
63
 
---------------
64
 
 
65
 
The individual list is suited for binary, integer, float and even funky individuals. It may contain any type and/or any type mix that is needed. The :class:`IndividualList` type is created with ::
66
 
 
67
 
    create("IndividualList", list, fitness=creator.FitnessMax)
68
 
 
69
 
and an individual of *size* 5 is instantiated with ::
70
 
 
71
 
    content = [random.random() for i in xrange(5)]
72
 
    ind = creator.IndividualList(content)
73
 
 
74
 
.. note::
75
 
   For individuals containing only a single numeric type, it may be more suited to use the :class:`array` base class, as the copy operation is way more efficient.
76
 
 
77
 
Individual Indices
78
 
------------------
79
 
 
80
 
The individual indices is almost the same as the individual list, except for its content. Here we will use the maximizing fitness describes earlier ::
81
 
 
82
 
    create("IndividualIndices", list, fitness=creator.FitnessMax)
83
 
 
84
 
and an individual indices of *size* 5 is instantiated with ::
85
 
 
86
 
    content = random.sample(xrange(5), 5)
87
 
    ind = creator.IndividualIndices(content)
88
 
 
89
 
Individual Tree
90
 
---------------
91
 
 
92
 
The individual tree is a bit harder to create. We first must define a primitive set and the operator we need in our post-fix trees. ::
93
 
 
94
 
    pset = gp.PrimitiveSet("MAIN", 1)
95
 
    pset.addPrimitive(operator.add, 2)
96
 
    pset.addPrimitive(operator.sub, 2)
97
 
    pset.addPrimitive(operator.mul, 2)
98
 
 
99
 
Then it is just as easy as other types, the tree class may be initialized from a iterable. The :mod:`~eap.gp` module contains some helper functions to build trees. For example, the :func:`~eap.gp.generate_full` will produce a full tree. ::
100
 
 
101
 
    creator.create("IndividualTree", gp.PrimitiveTree, fitness=creator.FitnessMax, pset=pset)
102
 
    ind = creator.IndividualTree(gp.generate_full(pset=pset, min=3, max=5))
103
 
 
104
 
Population
105
 
++++++++++
106
 
 
107
 
A population is usually a list of individuals or sub-populations, it is no more complicated to create than an individual. When using a :class:`~eap.toolbox.Toolbox`, it is often not necessary to create a class :class:`Population`, it is made here just to show how it would be created. ::
108
 
 
109
 
    create("Population", list)
110
 
    
111
 
A population of 10 individuals of indices is instanciated using ::
112
 
 
113
 
    ind_content = lambda: random.sample(xrange(5), 5)
114
 
    pop_content = [creator.IndividualIndices(ind_content()) for i in xrange(10)]
115
 
    pop = creator.Population(pop_content)
116
 
    
117
 
.. note::
118
 
   A deme (sub-population) is no more than a population, it is created the same way as a population (or any other list type).
119
 
 
120
 
.. seealso::
121
 
    
122
 
    The :ref:`First Steps of Evolution <first-steps>` shows how to combine the :mod:`~eap.creator` and the :mod:`~eap.toolbox` to initialize types.