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

« back to all changes in this revision

Viewing changes to doc/code/tutorials/part_4/4_4_Using_Cpp_NSGA.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 random
 
17
 
 
18
from deap import algorithms
 
19
from deap import base
 
20
from deap import creator
 
21
from deap import tools
 
22
from deap import cTools
 
23
 
 
24
import sortingnetwork as sn
 
25
 
 
26
INPUTS = 6
 
27
 
 
28
def evalEvoSN(individual, dimension):
 
29
    network = sn.SortingNetwork(dimension, individual)
 
30
    return network.assess(), network.length, network.depth
 
31
 
 
32
def genWire(dimension):
 
33
    return (random.randrange(dimension), random.randrange(dimension))
 
34
    
 
35
def genNetwork(dimension, min_size, max_size):
 
36
    size = random.randint(min_size, max_size)
 
37
    return [genWire(dimension) for i in xrange(size)]
 
38
    
 
39
def mutWire(individual, dimension, indpb):
 
40
    for index, elem in enumerate(individual):
 
41
        if random.random() < indpb:
 
42
            individual[index] = genWire(dimension)      
 
43
 
 
44
def mutAddWire(individual, dimension):
 
45
    index = random.randint(0, len(individual))
 
46
    individual.insert(index, genWire(dimension))
 
47
 
 
48
def mutDelWire(individual):
 
49
    index = random.randrange(len(individual))
 
50
    del individual[index]
 
51
 
 
52
creator.create("FitnessMin", base.Fitness, weights=(-1.0, -1.0, -1.0))
 
53
creator.create("Individual", list, fitness=creator.FitnessMin)
 
54
 
 
55
toolbox = base.Toolbox()
 
56
 
 
57
# Gene initializer
 
58
toolbox.register("network", genNetwork, dimension=INPUTS, min_size=9, max_size=12)
 
59
 
 
60
# Structure initializers
 
61
toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.network)
 
62
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
 
63
 
 
64
toolbox.register("evaluate", evalEvoSN, dimension=INPUTS)
 
65
toolbox.register("mate", tools.cxTwoPoint)
 
66
toolbox.register("mutate", mutWire, dimension=INPUTS, indpb=0.05)
 
67
toolbox.register("addwire", mutAddWire, dimension=INPUTS)
 
68
toolbox.register("delwire", mutDelWire)
 
69
toolbox.register("select", cTools.selNSGA2)
 
70
 
 
71
def main():
 
72
    random.seed(64)
 
73
 
 
74
    population = toolbox.population(n=300)
 
75
    hof = tools.ParetoFront()
 
76
    
 
77
    stats = tools.Statistics(lambda ind: ind.fitness.values)
 
78
    stats.register("Avg", tools.mean)
 
79
    stats.register("Std", tools.std)
 
80
    stats.register("Min", min)
 
81
    stats.register("Max", max)
 
82
 
 
83
    CXPB, MUTPB, ADDPB, DELPB, NGEN = 0.5, 0.2, 0.01, 0.01, 40
 
84
    
 
85
    # Evaluate every individuals
 
86
    fitnesses = toolbox.map(toolbox.evaluate, population)
 
87
    for ind, fit in zip(population, fitnesses):
 
88
        ind.fitness.values = fit
 
89
    
 
90
    hof.update(population)
 
91
    stats.update(population)
 
92
    
 
93
    # Begin the evolution
 
94
    for g in xrange(NGEN):
 
95
        print "-- Generation %i --" % g
 
96
        offspring = [toolbox.clone(ind) for ind in population]
 
97
    
 
98
        # Apply crossover and mutation
 
99
        for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
 
100
            if random.random() < CXPB:
 
101
                toolbox.mate(ind1, ind2)
 
102
                del ind1.fitness.values
 
103
                del ind2.fitness.values
 
104
        
 
105
        # Note here that we have a different sheme of mutation than in the
 
106
        # original algorithm, we use 3 different mutations subsequently.
 
107
        for ind in offspring:
 
108
            if random.random() < MUTPB:
 
109
                toolbox.mutate(ind)
 
110
                del ind.fitness.values
 
111
            if random.random() < ADDPB:
 
112
                toolbox.addwire(ind)
 
113
                del ind.fitness.values
 
114
            if random.random() < DELPB:
 
115
                toolbox.delwire(ind)
 
116
                del ind.fitness.values
 
117
                
 
118
        # Evaluate the individuals with an invalid fitness
 
119
        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
 
120
        fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
 
121
        for ind, fit in zip(invalid_ind, fitnesses):
 
122
            ind.fitness.values = fit
 
123
        
 
124
        print "  Evaluated %i individuals" % len(invalid_ind)
 
125
        
 
126
        population = toolbox.select(population+offspring, len(offspring))
 
127
        hof.update(population)
 
128
        stats.update(population)
 
129
 
 
130
        print "  Min %s" % stats.Min[0][-1][0]
 
131
        print "  Max %s" % stats.Max[0][-1][0]
 
132
        print "  Avg %s" % stats.Avg[0][-1][0]
 
133
        print "  Std %s" % stats.Std[0][-1][0]
 
134
 
 
135
    best_network = sn.SortingNetwork(INPUTS, hof[0])
 
136
    print best_network
 
137
    print best_network.draw()
 
138
    print "%i errors, length %i, depth %i" % hof[0].fitness.values
 
139
    
 
140
    return population, stats, hof
 
141
 
 
142
if __name__ == "__main__":
 
143
    main()
 
144