~ubuntu-branches/ubuntu/utopic/deap/utopic-proposed

« back to all changes in this revision

Viewing changes to examples/ga/onemax_short.py

  • Committer: Package Import Robot
  • Author(s): Daniel Stender, Miriam Ruiz, Daniel Stender, Jakub Wilk
  • Date: 2014-07-06 00:03:41 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20140706000341-s7gij1ki3d8xz6t9
Tags: 1.0.1-1
[ Miriam Ruiz ]
* New Upstream Release. Closes: #675200
* Upgraded Standards-Version from 3.9.2 to 3.9.5
* Switched to dh_python2
* Upgraded debian/compat to 9
* Added build-arch and build-indep targets to debian/rules
* Using awk to remove connection from the documentation to google analytics
* Using jquery.js and underscore.js from libjs-jquery and libjs-underscore
* Added patches/doc.patch

[ Daniel Stender ]
* deb/control:
  + Added myself to Uploaders.
  + Added version to b-p on python-all.
  + Updated Homepage.
  + Wrapped and sorted.
* deb/copyright:
  + Changed copyright file towards DEP-5.
  + Updated Source URI (project source moved to Github).
  + Added email addresses for copyright holders.
  + Dropped license for eap/toolbox.py (not included anymore).
  + Extended copyrights to 2014.
  + Added myself to copyrights for deb/*.
  + Specified license location.
* Added watch file [initial version by Jackson Doak].

[ Jakub Wilk ]
* Use canonical URIs for Vcs-* fields.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#    This file is part of DEAP.
 
2
#
 
3
#    DEAP is free software: you can redistribute it and/or modify
 
4
#    it under the terms of the GNU Lesser General Public License as
 
5
#    published by the Free Software Foundation, either version 3 of
 
6
#    the License, or (at your option) any later version.
 
7
#
 
8
#    DEAP is distributed in the hope that it will be useful,
 
9
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 
11
#    GNU Lesser General Public License for more details.
 
12
#
 
13
#    You should have received a copy of the GNU Lesser General Public
 
14
#    License along with DEAP. If not, see <http://www.gnu.org/licenses/>.
 
15
 
 
16
import array
 
17
import random
 
18
 
 
19
import numpy
 
20
 
 
21
from deap import algorithms
 
22
from deap import base
 
23
from deap import creator
 
24
from deap import tools
 
25
 
 
26
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
 
27
creator.create("Individual", array.array, typecode='b', fitness=creator.FitnessMax)
 
28
 
 
29
toolbox = base.Toolbox()
 
30
 
 
31
# Attribute generator
 
32
toolbox.register("attr_bool", random.randint, 0, 1)
 
33
 
 
34
# Structure initializers
 
35
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, 100)
 
36
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
 
37
 
 
38
def evalOneMax(individual):
 
39
    return sum(individual),
 
40
 
 
41
toolbox.register("evaluate", evalOneMax)
 
42
toolbox.register("mate", tools.cxTwoPoint)
 
43
toolbox.register("mutate", tools.mutFlipBit, indpb=0.05)
 
44
toolbox.register("select", tools.selTournament, tournsize=3)
 
45
 
 
46
def main():
 
47
    random.seed(64)
 
48
    
 
49
    pop = toolbox.population(n=300)
 
50
    hof = tools.HallOfFame(1)
 
51
    stats = tools.Statistics(lambda ind: ind.fitness.values)
 
52
    stats.register("avg", numpy.mean)
 
53
    stats.register("std", numpy.std)
 
54
    stats.register("min", numpy.min)
 
55
    stats.register("max", numpy.max)
 
56
    
 
57
    pop, log = algorithms.eaSimple(pop, toolbox, cxpb=0.5, mutpb=0.2, ngen=40, 
 
58
                                   stats=stats, halloffame=hof, verbose=True)
 
59
    
 
60
    return pop, log, hof
 
61
 
 
62
if __name__ == "__main__":
 
63
    main()