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

« back to all changes in this revision

Viewing changes to examples/gp_multiplexer.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 EAP.
2
 
#
3
 
#    EAP 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
 
#    EAP 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 EAP. If not, see <http://www.gnu.org/licenses/>.
15
 
 
16
 
import sys
17
 
import random
18
 
import operator
19
 
import logging
20
 
 
21
 
from deap import algorithms
22
 
from deap import base
23
 
from deap import creator
24
 
from deap import tools
25
 
from deap import gp
26
 
 
27
 
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)
28
 
 
29
 
def if_then_else(condition, out1, out2):
30
 
    return out1 if condition else out2
31
 
 
32
 
# Initialize Multiplexer problem input and output vectors
33
 
 
34
 
MUX_SELECT_LINES = 3
35
 
MUX_IN_LINES = 2 ** MUX_SELECT_LINES
36
 
MUX_TOTAL_LINES = MUX_SELECT_LINES + MUX_IN_LINES
37
 
 
38
 
# input : [A0 A1 A2 D0 D1 D2 D3 D4 D5 D6 D7] for a 8-3 mux
39
 
inputs = [[0] * MUX_TOTAL_LINES for i in range(2 ** MUX_TOTAL_LINES)]
40
 
outputs = [None] * (2 ** MUX_TOTAL_LINES)
41
 
 
42
 
for i in range(2 ** MUX_TOTAL_LINES):
43
 
    value = i
44
 
    divisor = 2 ** MUX_TOTAL_LINES
45
 
    # Fill the input bits
46
 
    for j in range(MUX_TOTAL_LINES):
47
 
        divisor /= 2
48
 
        if value >= divisor:
49
 
            inputs[i][j] = 1
50
 
            value -= divisor
51
 
    
52
 
    # Determine the corresponding output
53
 
    indexOutput = MUX_SELECT_LINES
54
 
    for j, k in enumerate(inputs[i][:MUX_SELECT_LINES]):
55
 
        if k:   indexOutput += 2 ** j
56
 
    outputs[i] = inputs[i][indexOutput]
57
 
 
58
 
pset = gp.PrimitiveSet("MAIN", MUX_TOTAL_LINES, "IN")
59
 
pset.addPrimitive(operator.and_, 2)
60
 
pset.addPrimitive(operator.or_, 2)
61
 
pset.addPrimitive(operator.not_, 1)
62
 
pset.addPrimitive(if_then_else, 3)
63
 
pset.addTerminal(1)
64
 
pset.addTerminal(0)
65
 
 
66
 
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
67
 
creator.create("Individual", gp.PrimitiveTree, fitness=creator.FitnessMax, pset=pset)
68
 
 
69
 
toolbox = base.Toolbox()
70
 
toolbox.register("expr", gp.genFull, pset=pset, min_=2, max_=4)
71
 
toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.expr)
72
 
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
73
 
toolbox.register("lambdify", gp.lambdify, pset=pset)
74
 
 
75
 
def evalMultiplexer(individual):
76
 
    func = toolbox.lambdify(expr=individual)
77
 
    good = sum((func(*(inputs[i])) == outputs[i] for i in range(2 ** MUX_TOTAL_LINES)))
78
 
    return good,
79
 
 
80
 
toolbox.register("evaluate", evalMultiplexer)
81
 
toolbox.register("select", tools.selTournament, tournsize=7)
82
 
toolbox.register("mate", gp.cxUniformOnePoint)
83
 
toolbox.register("expr_mut", gp.genGrow, min_=0, max_=2)
84
 
toolbox.register("mutate", gp.mutUniform, expr=toolbox.expr_mut)
85
 
 
86
 
def main():
87
 
    random.seed()
88
 
    pop = toolbox.population(n=500)
89
 
    hof = tools.HallOfFame(1)
90
 
    stats = tools.Statistics(lambda ind: ind.fitness.values)
91
 
    stats.register("Avg", tools.mean)
92
 
    stats.register("Std", tools.std)
93
 
    stats.register("Min", min)
94
 
    stats.register("Max", max)
95
 
    
96
 
    algorithms.eaSimple(toolbox, pop, 0.8, 0.1, 40, stats, halloffame=hof)
97
 
    
98
 
    logging.info("Best individual is %s, %s", gp.evaluate(hof[0]), hof[0].fitness)
99
 
    
100
 
    return pop, stats, hof
101
 
 
102
 
if __name__ == "__main__":
103
 
    main()
104