1
# This file is part of DEAP.
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.
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.
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/>.
16
"""This example contains the generalizing test from *Potter, M. and De Jong,
17
K., 2001, Cooperative Coevolution: An Architecture for Evolving Co-adapted
18
Subcomponents.* section 4.2.2. Varying the *NUM_SPECIES* in :math:`[1, \ldots,
19
4]` will produce the results for one to four species respectively.
24
import matplotlib.pyplot as plt
30
from deap import algorithms
31
from deap import tools
35
IND_SIZE = coop_base.IND_SIZE
36
SPECIES_SIZE = coop_base.SPECIES_SIZE
40
noise = "*##*###*###*****##*##****#*##*###*#****##******##*#**#*#**######"
41
schematas = ("1##1###1###11111##1##1111#1##1###1#1111##111111##1#11#1#11######",
42
"1##1###1###11111##1##1000#0##0###0#0000##000000##0#00#0#00######",
43
"0##0###0###00000##0##0000#0##0###0#0000##001111##1#11#1#11######")
45
toolbox = coop_base.toolbox
47
# This will allow to plot the match strength of every target schemata
48
toolbox.register("evaluate_nonoise", coop_base.matchSetStrengthNoNoise)
50
def main(extended=True, verbose=True):
53
stats = tools.Statistics(lambda ind: ind.fitness.values)
54
stats.register("avg", numpy.mean)
55
stats.register("std", numpy.std)
56
stats.register("min", numpy.min)
57
stats.register("max", numpy.max)
59
logbook = tools.Logbook()
60
logbook.header = "gen", "species", "evals", "std", "min", "avg", "max"
65
for i in range(len(schematas)):
66
size = int(TARGET_SIZE/len(schematas))
67
target_set.extend(toolbox.target_set(schematas[i], size))
69
species = [toolbox.species() for _ in range(NUM_SPECIES)]
71
# Init with random a representative for each species
72
representatives = [random.choice(s) for s in species]
75
# We must save the match strength to plot them
76
t1, t2, t3 = list(), list(), list()
79
# Initialize a container for the next generation representatives
80
next_repr = [None] * len(species)
81
for i, s in enumerate(species):
82
# Vary the species individuals
83
s = algorithms.varAnd(s, toolbox, 0.6, 1.0)
85
# Get the representatives excluding the current species
86
r = representatives[:i] + representatives[i+1:]
88
ind.fitness.values = toolbox.evaluate([ind] + r, target_set)
90
record = stats.compile(s)
91
logbook.record(gen=g, species=i, evals=len(s), **record)
96
# Select the individuals
97
species[i] = toolbox.select(s, len(s)) # Tournament selection
98
next_repr[i] = toolbox.get_best(s)[0] # Best selection
103
# Compute the match strength without noise for the
104
# representatives on the three schematas
105
t1.append(toolbox.evaluate_nonoise(representatives,
106
toolbox.target_set(schematas[0], 1), noise)[0])
107
t2.append(toolbox.evaluate_nonoise(representatives,
108
toolbox.target_set(schematas[1], 1), noise)[0])
109
t3.append(toolbox.evaluate_nonoise(representatives,
110
toolbox.target_set(schematas[2], 1), noise)[0])
112
representatives = next_repr
114
for r in representatives:
115
# print individuals without noise
116
print("".join(str(x) for x, y in zip(r, noise) if y == "*"))
119
# Do the final plotting
120
plt.plot(t1, '-', color="k", label="Target 1")
121
plt.plot(t2, '--', color="k", label="Target 2")
122
plt.plot(t3, ':', color="k", label="Target 3")
123
plt.legend(loc="lower right")
124
plt.axis([0, ngen, 0, max(max(t1), max(t2), max(t3)) + 1])
125
plt.xlabel("Generations")
126
plt.ylabel("Number of matched bits")
129
if __name__ == "__main__":