-
Notifications
You must be signed in to change notification settings - Fork 1
/
speciation.py
executable file
·355 lines (315 loc) · 11.7 KB
/
speciation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
from deap import base, creator, gp, tools
from random import choice
from numpy import average
from measure_tree import *
#Inicializa las poblacion con una sola especie
def init_species(population):
for ind in population:
ind.specie(1)
def count_species(population):
"""
This method counts the total number of species
in the population
:param population: a set of individuals
:return: The number of species
"""
specie = list()
for ind in population:
if ind.get_specie() is not None and ind.get_specie() not in specie:
specie.append(ind.get_specie())
if not specie:
return 0
else:
return len(specie)
def list_species(population):
"""
This method return a list of the species
in the population
:param population: a set of individuals
:return: A list of species
"""
specie = list()
for ind in population:
if ind.get_specie() is not None and ind.get_specie() not in specie:
specie.append(ind.get_specie())
if not specie:
return 0
else:
return specie
def ind_specie(population):
"""
This method count the individuals of each specie
:param population: a set of individuals
:return: A list with the specie and the total number of individuals in it.
"""
specie2 = list()
num = list()
for ind in population:
specie2.append(ind.get_specie())
specie = sorted(specie2)
if specie.count(specie[0]) == len(population):
num.append([specie[0], specie.count(specie[0])])
else:
for i in range(len(specie)):
if specie[i] != specie[i-1]:
num.append([specie[i], specie.count(specie[i])])
for ind in population:
set_numind(ind, num)
return num
def specie_gpo(population):
"""
This method return groups of individuals by specie
:param population: a set of individuals
:return: A list with the individuals grouped by specie
"""
specie2 = list()
gpos = list()
orderbyfit = list()
orderbyfit = sorted(population, key=lambda ind: ind.get_specie())
for i in range(1, len(orderbyfit)):
if orderbyfit[i-1].get_specie() == orderbyfit[i].get_specie():
specie2.append(orderbyfit[i-1])
if i==len(orderbyfit)-1:
specie2.append(orderbyfit[i])
gpos.append([specie2])
else:
specie2.append(orderbyfit[i-1])
gpos.append([specie2])
specie2 = list()
if i == len(orderbyfit)-1:
specie2.append(orderbyfit[i])
gpos.append([specie2])
return gpos
def get_specie_ind(individual, population):
"""
This method return the number of individuals in a given specie
:param individual: This individual will bring the number of specie to search
:param population: The list of individuals to compare
:return: A number of total individuals
"""
cont = 0
specie = individual.get_specie()
for ind in population:
if ind.get_specie() == specie:
cont += 1
return cont
def get_ind_specie(n_specie, population):
"""
This method return a group of individuals from a given specie
:param n_specie: The number of the specie
:param population: The list of individuals to compare
:return: A list of total individuals
"""
specie = n_specie
list_ind = []
for ind in population:
if ind.get_specie() == specie:
list_ind.append(ind)
return list_ind
def set_numind(ind, species):
"""
This method assign to an individual the number of individuals in the specie of his kind.
:param ind: This individual will bring the number of specie to search
:param species:
"""
for i in range(len(species)):
if species[i][0] == ind.get_specie() and ind.num_specie is not None:
ind.num_specie(species[i][1])
def intracluster(gpo_specie):
"""
Upgrade: May 11th
This method calculates the intracluster distance in a specie.
:param gpo_specie: The specie to calculate the intracluster
"""
list_distance = []
for j_ind in range(0, len(gpo_specie)):
list_d = []
for e_ind in range(0, len(gpo_specie)):
if j_ind != e_ind:
if len(gpo_specie[j_ind]) == 1 and len(gpo_specie[e_ind]) == 1:
d = 0
else:
d = distance(gpo_specie[j_ind], gpo_specie[e_ind], version=3, beta=0.5)
list_d.append(d)
try:
list_distance.append(min(list_d))
except ValueError:
print 'intra', list_distance
avg_distance = average(list_distance)
for ind in gpo_specie:
ind.set_intracluster(avg_distance)
return avg_distance
def calc_intracluster(population):
"""
Upgrade: May 11th
This method calculates the intracluster distance in each specie.
:param population: all the individuals
"""
list_s = list_species(population)
l_sp = []
for specie in list_s:
list_ind = get_ind_specie(specie, population)
if len(list_ind) >= 2:
l_sp.append([specie, intracluster(list_ind)])
else:
for ind_j in list_ind:
ind_j.set_intracluster(0.0)
l_sp.append([specie, 0.0])
return l_sp
def species_random(population, h, version, beta):
"""
Upgrade: May 10th
This is the speciation method.
This method compare each individual without specie with the rest of the
individual, selecting one random individual of the specie and calculating the distance to compare.
:param population: set of individuals without specie
:param h: distance measure
:param version: the way of comparasion between the individuals
1: Binary comparasion
2: Modify method
3: Original method
:param beta: measure to give a weight in the comparasion between number of nodes and
depth.
:return: a population with specie
"""
num_specie = count_species(population)
if num_specie >= 1:
for ind in population:
if ind.get_specie() is None:
if len(ind) == 1:
ind.specie(1)
else:
list_s = list_species(population)
for specie in list_s:
list_ind = get_ind_specie(specie, population)
r_ind = choice(list_ind)
if distance(ind, r_ind, version, beta) <= h:
ind.specie(r_ind.get_specie())
break
if ind.get_specie() is None:
ind.specie(num_specie+1)
num_specie += 1
else:
population[0].specie(1)
species_random(population, h, version, beta)
return population
def species(population, h, version, beta):
"""
This is the speciation method.
This method compare each individual with the rest
of the population to assign it a specie.
:param population: set of individuals without specie
:param h: distance measure
:param version: the way of comparasion between the individuals
1: Binary comparasion
2: Modify method
3: Original method
:return: a population with specie
"""
num_specie = count_species(population)
for ind in population:
if ind.get_specie() is None:
if len(ind) == 1:
ind.specie(1)
else:
for ind1 in population:
if ind1.get_specie() is not None:
# if distance(ind, ind1, 1) != distance(ind, ind1, 3):
# print 'ind:', ind
# print 'ind2:', ind1
# print 'D1:', distance(ind, ind1, 1)
# print ind.binary_rep_get()
# print ind1.binary_rep_get()
# print 'D2:', distance(ind, ind1, 3)
if distance(ind, ind1, version, beta) <= h:
ind.specie(ind1.get_specie())
break
if ind.get_specie() is None:
ind.specie(num_specie+1)
num_specie += 1
return population
def specie_ind(population,ind, h):
"""
This method assign a specie to an specific individual
:param population: set of individuals
:param ind: the individual to assign the specie
:param h: the distance measure
:return: The individual with a specie
"""
if (len(ind)==1):
ind.specie(1)
else:
for ind_p in population:
if ind_p.get_specie()!=None:
if distance(ind, ind_p, version=3)<=h:
ind.specie(ind_p.get_specie())
break
if ind.get_specie()==None:
num_specie=count_species(population)
ind.specie(num_specie+1)
return ind
def specie_parents_child(parents, offspring, h, version, beta):
"""
This method assign the specie to each children.
The assignation of the specie depends of the specie of the parent.
:param parents: set of parents
:param offspring: set of descendents without specie
:param h: distace to compare, deafult .15
:return: A set of descendent with specie.
"""
n_esp = count_species(parents)
for ind in offspring:
if ind.get_specie() is None:
if len(ind) == 1:
ind.specie(1)
else:
for parent in parents:
if parent.get_specie() is not None:
if distance(ind, parent, version, beta) <= h:
ind.specie(parent.get_specie())
break
if ind.get_specie() is None:
ind.specie(n_esp+1)
n_esp += 1
return offspring
def specie_offspring_random(parents, offspring, h, version, beta):
"""
Modified May 10th
This method assign the specie to each children.
The assignation of the specie depends of the specie of the parent.
:param parents: set of parents
:param offspring: set of descendents without specie
:param h: distace to compare, deafult .15
:return: A set of descendent with specie.
"""
n_esp = count_species(parents)
if n_esp >= 1:
for ind in offspring:
if ind.get_specie() is None:
if len(ind) == 1:
ind.specie(1)
else:
list_s = list_species(parents)
for specie in list_s:
list_ind = get_ind_specie(specie, parents)
r_ind = choice(list_ind)
if distance(ind, r_ind, version, beta) <= h:
ind.specie(r_ind.get_specie())
break
if ind.get_specie() is None:
off_specie = list_species(offspring)
if off_specie:
new_species = list(set(off_specie) - set(list_s))
if new_species:
for specie in new_species:
list_ind = get_ind_specie(specie, offspring)
r_ind = choice(list_ind)
if distance(ind, r_ind, version, beta) <= h:
ind.specie(r_ind.get_specie())
break
if ind.get_specie() is None:
ind.specie(n_esp+1)
n_esp += 1
else:
print ("Error en las especies de los padres/descencientes")
return offspring