This repository has been archived by the owner on Jun 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaxOnes_AFPO.py
254 lines (181 loc) · 7.16 KB
/
MaxOnes_AFPO.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
#!/usr/bin/env python3.6
# AFPO.py
# Author: Shawn Beaulieu
# July 20th, 2018
import copy
import math
import random
def Save_Data(content):
"""
Store data for later use.
"""
keys = sorted(content.keys())
with open("AFPO_History.csv", "a+") as f:
# AGE, FITNESS, ID, ORIGIN
# . . . .
# . . . .
# . . . .
L = ",".join([str(content[key]) for key in keys if key != 'genome'])
f.write(L)
f.write("\n")
class AFPO():
P = {
'popsize': 100,
'max_gene_len': 20,
'target': [1]*20,
'max_generations': 500
}
def __init__(self, parameters=dict()):
# Receive parameters dictionary from Run_AFPO.py
# and convert all entries to self.key = value:
# e.g. parameters = {'popsize':100}
# >> self.popsize = 100
# *: non-keyworded dynamic update
# **: key-worded dynamic update
self.__dict__.update(AFPO.P, **parameters)
self.current_gen = 0
self.idx = 0
self.children = self.Initialize_Population(self.popsize, self.max_gene_len)
self.Evolve()
def Initialize_Population(self, size, length):
"""
Creates a list of size "size" of individuals with genomes
of length "length".
"""
pop = []
for p in range(size):
child = {
'genome': [random.choice(range(2)) for _ in range(length)],
'fitness': 0,
'age': 0,
'origin': self.current_gen,
'id': self.idx
}
pop.append(child)
self.idx += 1
return(pop)
def Evolve(self):
"""
This function loops through the number of generations, calling
other functions that dictate evolution. Champions are printed at
the end of every generation.
Logic flow:
(1) Initialize parents
(2) Evaluate children
(3) Filter by AFPO
(4) Obtain new parents
(5) Spawn new children based on (4)
"""
self.parents = list()
for g in range(self.max_generations):
for child in self.children:
child['fitness'] = self.Evaluate(child)
# Write information to file:
Save_Data(child)
self.Selection()
# Report current champion fitness:
champ = self.champion['fitness']
print("Generation {0}/{1}: Champion={2}".format(self.current_gen, self.max_generations, champ))
self.current_gen += 1
# Spawn a new population of individuals
self.Spawn()
def Evaluate(self, child):
"""
Finds the ratio of correct to incorrect entries
"""
score = sum([int(child['genome'][x] == self.target[x]) for x in range(self.max_gene_len)])
return(float(score)/len(child['genome']))
def Selection(self):
"""
Filtration.
If the current generation == 0, then just take the fittest individual to be
the sole parent for the next generation. Otherwise, call AFPO.
"""
if self.current_gen == 0:
# For first generation, just take the fittest individual
self.Find_Champion(seed=0.0)
self.Mature()
self.parents.append(self.champion)
else:
self.Age_Fitness_Selection()
self.Mature()
self.Find_Champion(seed=self.champion['fitness'])
def Mature(self):
"""
Increments the age of the surviving parents.
"""
for parent in self.parents:
parent['age'] += 1
def Find_Champion(self, seed):
"""
Locates the current champion
"""
max_score = seed
for child in self.children:
if child['fitness'] > max_score:
max_score = child['fitness']
self.champion = child
def Age_Fitness_Selection(self):
"""
Looks for domination by age and fitness. If an individual is non-dominated
(i.e. no other individual is both younger and fitter) then it survives into
the next generation and gives birth to offspring.
"""
# Held out (non-dominated) parents during evaluation
self.children = self.parents + self.children
self.parents = list()
for i in range(len(self.children)):
candidate = self.children[i]
# Find evidence to reverse prior:
dominated = False
# Slice population for comparing "candidate" to remaining individuals
# candidate vs. challenger
other = list(range(len(self.children)))
competitors = other[:i] + other[i+1:]
for j in competitors:
challenger = self.children[j]
# If challenger is fitter, candidate must be younger
# Individuals are only dominated if the challenger has
# (1) greater fitness and lower age;
# and (2) greater fitness and same age
# If challenger has lower age AND lower fitness, the candidate
# is non-dominated.
if challenger['fitness'] > candidate['fitness']:
if not candidate['age'] < challenger['age']:
dominated = True
break
# If identical, take the individual more recently generated (phenotype maps)
elif (challenger['age'] == candidate['age']) and (candidate['fitness'] == challenger['fitness']):
if j < i:
dominated = True
break
# If individual survives tests above, they survive (non-dominated)
if not dominated:
self.parents.append(candidate)
# Print age and fitness for validation
#for p in self.parents:
# print(p['age'], p['fitness'])
def Spawn(self):
"""
Fills out the rest of the population with mutated versions of the non-dominated
parents. One slot is saved for a "baby" individual, with age 0 and randomized genome.
"""
self.children = list()
# Only need to evaluate (popsize-len(parents)) strings:
while len(self.children) < (self.popsize - len(self.parents) - 1):
# Randomly select a parent to copy and mutate
progenitor = self.parents[random.choice(range(len(self.parents)))]
# Add mutated parent to new child population:
new_child = self.Mutate(copy.deepcopy(progenitor))
self.children.append(new_child)
# Add baby
self.children += self.Initialize_Population(1, self.max_gene_len)
def Mutate(self, child):
"""
Bit-flipper
"""
# S:ingle N:ucleotide P:olymorphism
# Single gene mutation (bit flip)
SNP = random.choice(range(self.max_gene_len))
child['genome'][SNP] = abs(child['genome'][SNP]-1)
return(child)