1
# This file is part of EAP.
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.
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.
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/>.
21
from deap import algorithms
23
from deap import creator
24
from deap import tools
27
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)
29
def if_then_else(condition, out1, out2):
30
return out1 if condition else out2
32
# Initialize Multiplexer problem input and output vectors
35
MUX_IN_LINES = 2 ** MUX_SELECT_LINES
36
MUX_TOTAL_LINES = MUX_SELECT_LINES + MUX_IN_LINES
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)
42
for i in range(2 ** MUX_TOTAL_LINES):
44
divisor = 2 ** MUX_TOTAL_LINES
46
for j in range(MUX_TOTAL_LINES):
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]
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)
66
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
67
creator.create("Individual", gp.PrimitiveTree, fitness=creator.FitnessMax, pset=pset)
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)
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)))
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)
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)
96
algorithms.eaSimple(toolbox, pop, 0.8, 0.1, 40, stats, halloffame=hof)
98
logging.info("Best individual is %s, %s", gp.evaluate(hof[0]), hof[0].fitness)
100
return pop, stats, hof
102
if __name__ == "__main__":