~ubuntu-branches/ubuntu/saucy/deap/saucy

« back to all changes in this revision

Viewing changes to examples/ga_onemax_numpy.py

  • 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
#    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 numpy
 
17
import sys
 
18
import random
 
19
import logging
 
20
 
 
21
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)
 
22
 
 
23
from deap import algorithms
 
24
from deap import base
 
25
from deap import creator
 
26
from deap import tools
 
27
 
 
28
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
 
29
creator.create("Individual", numpy.ndarray, fitness=creator.FitnessMax)
 
30
 
 
31
toolbox = base.Toolbox()
 
32
 
 
33
toolbox.register("attr_bool", random.randint, 0, 1)
 
34
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, n=100)
 
35
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
 
36
 
 
37
def evalOneMax(individual):
 
38
    return numpy.sum(individual),
 
39
 
 
40
toolbox.register("evaluate", evalOneMax)
 
41
toolbox.register("mate", tools.cxTwoPoints)
 
42
toolbox.register("mutate", tools.mutFlipBit, indpb=0.05)
 
43
toolbox.register("select", tools.selTournament, tournsize=3)
 
44
 
 
45
def main():
 
46
    random.seed(64)
 
47
    
 
48
    pop = toolbox.population(n=300)
 
49
    hof = tools.HallOfFame(1)
 
50
    stats = tools.Statistics(lambda ind: ind.fitness.values)
 
51
    stats.register("Avg", tools.mean)
 
52
    stats.register("Std", tools.std)
 
53
    stats.register("Min", min)
 
54
    stats.register("Max", max)
 
55
    
 
56
    algorithms.eaSimple(toolbox, pop, cxpb=0.5, mutpb=0.2, ngen=40, stats=stats, halloffame=hof)
 
57
    
 
58
    return pop, stats, hof
 
59
 
 
60
if __name__ == "__main__":
 
61
    main()