-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAncestry.py
349 lines (299 loc) · 11.5 KB
/
Ancestry.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
import math
import random
import numpy as np
import matplotlib.pyplot as plt
def distance(xy1, xy2):
# dx = xy2[0] - xy1[0]
# dy = xy2[1] - xy1[1]
# return (dx**2 + dy**2) ** 0.5
return np.linalg.norm(xy1 - xy2)
def average(xy1, xy2, alpha=0.5):
return alpha * xy1 + (1 - alpha) * xy2
class Person:
N_BASES = 50
MINIMUM_REPRODUCTION_AGE = 3
MIN_SCORE = float("-inf")
def __init__(self, dna, land, location):
self.dna = dna
self.land = land
self.location = np.array(location)
self.age = 0
self.person_id = "{}-{}-{}".format(int(location[0]), int(location[1]), Person.value_gene_binary(self.dna))
# genetic attributes
self.similarity_dna = self.dna[:]
self.similarity_weights = self.get_similarity_weights()
self.genetic_age = Person.value_gene_binary(self.dna[5:11])
self.neighbor_radius = 1 + Person.sum_gene(self.dna[2:11])
self.max_neighbors = Person.sum_gene(self.dna[33:43])
self.ideal_similarity = Person.value_gene_01(self.dna[9:24])
self.reproduction_probability = Person.value_gene_01(self.dna[41:47])
self.wanderlust_probability = Person.value_gene_01(self.dna[0:8])
def __repr__(self):
return "Person ID {}\ncoords {}\nage {}\nDNA {}\n".format(self.person_id, self.location, self.age, self.dna)
def get_similarity_weights(self):
s = self.similarity_dna
raw_weights = []
offset = 7
determiner_length = 5
for i in range(len(s)):
determiner = "".join([s[(i + offset + j) % len(s)] for j in range(determiner_length)])
raw_weight = Person.value_gene_01(determiner)
raw_weights.append(raw_weight)
total_weight = sum(raw_weights)
if total_weight == 0:
total_weight = 1
return [w / total_weight for w in raw_weights]
def select_mate(self, options):
best_score = Person.MIN_SCORE
best_option = None
options = options[:]
random.shuffle(options)
for option in options:
score = self.score(option)
# print("score {}".format(score))
if score > best_score:
# print("BETTER")
best_score = score
best_option = option
return best_option
def score(self, other):
if other.dna == self.dna:
# no mating with yourself or your clone
return Person.MIN_SCORE
# select part of the dna for use as "similarity", not all of it
d = Person.genetic_distance(self.similarity_dna, other.similarity_dna, weights=self.similarity_weights)
s = self.ideal_similarity
diff = abs(d - s)
while diff == 0:
# screw dealing with 1/0, just add some uncertainty so it's random among the perfect candidates
diff = random.uniform(-1e-3, 1e-3)
return 1/diff
def reproduce(self, mate):
new_dna = Person.combine_dna(self.dna, mate.dna)
new_location = average(self.location, mate.location, alpha=random.random())
return Person(new_dna, self.land, new_location)
def maybe_reproduce(self):
# decide whether to reproduce (based on something in DNA)
offspring = None
options = self.land.get_mating_options(self)
will_reproduce = (not self.is_baby()) and self.check_dna_will_reproduce()
if will_reproduce:
# print("will reproduce")
mate = self.select_mate(options)
if mate is not None:
# print("got one!")
offspring = self.reproduce(mate)
else:
# print("but got none")
offspring = None
self.move() # find more fertile area
self.age += 1
# print("returning offspring {}".format(offspring))
return offspring
def check_dna_will_reproduce(self):
return random.random() < self.reproduction_probability
def is_baby(self):
return self.age < Person.MINIMUM_REPRODUCTION_AGE
def maybe_die(self):
# use self.age as well as a certain gene
# make sure there are random elements allowing improbable survivals
improbable_survival = random.random() < 0.1 # x% chance of living another year against the odds
if improbable_survival:
return False
return self.is_old() or self.is_overcrowded()
def is_old(self):
# if self.is_baby():
# return False
return random.uniform(0, self.age) > self.genetic_age
def is_overcrowded(self):
neighbors = self.land.get_mating_options(self)
return random.uniform(0, len(neighbors)) > self.max_neighbors
def maybe_move(self):
# prevent clones from just clustering together in the same place
if random.random() < self.wanderlust_probability:
self.move()
def move(self):
if self.is_baby():
return
self.location = self.land.get_random_location()
@staticmethod
def genetic_distance(dna1, dna2, weights=None):
assert len(dna1) == len(dna2)
return sum((dna1[i] != dna2[i]) * weights[i] for i in range(len(dna1))) / len(dna1)
@staticmethod
def sum_gene(gene):
return sum(x == "1" for x in gene)
@staticmethod
def value_gene_01(gene):
return Person.sum_gene(gene) / len(gene)
@staticmethod
def value_gene_binary(gene):
n = 0
for i in range(len(gene)):
power = 2 ** i
digit = int(gene[i]) # little-endian because whatevs
n += digit * power
return n
@staticmethod
def generate_dna():
return "".join([random.choice("01") for _ in range(Person.N_BASES)])
@staticmethod
def combine_dna(p1, p2):
assert len(p1) == len(p2)
s = ""
for i in range(len(p1)):
s += random.choice([p1[i], p2[i]])
return s
class Land:
def __init__(self, x_max, y_max):
self.x_max = x_max
self.y_max = y_max
self.people = []
self.year = 0
self.population_history = [0]
self.next_person_id = 0
def populate(self, n_people):
for _ in range(n_people):
self.add_random_person()
self.population_history[-1] = len(self.people)
def add_random_person(self):
dna = Person.generate_dna()
land = self
location = self.get_random_location()
p = Person(dna, land, location)
self.add_person(p)
def get_random_location(self):
x = random.uniform(0, self.x_max)
y = random.uniform(0, self.y_max)
return np.array([x, y])
def add_person(self, person):
self.people.append(person)
# print("born {} --> population {}".format(person, len(self.people)))
def remove_person(self, person):
self.people.remove(person)
# print("died {} --> population {}".format(person, len(self.people)))
def get_mating_options(self, person):
return [p for p in self.people if distance(person.location, p.location) <= person.neighbor_radius]
def go_to_next_generation(self):
print("year {}".format(self.year))
self.move_people()
self.add_people()
self.kill_people()
self.year += 1
print("made it to year {}. there are now {} people".format(self.year, len(self.people)))
self.population_history.append(len(self.people))
def move_people(self):
for p in self.people:
p.maybe_move()
def add_people(self):
people_to_add = []
for person in self.people:
p = person.maybe_reproduce()
if p is not None:
# print("got baby {}".format(p))
people_to_add.append(p)
for p in people_to_add:
self.add_person(p)
def kill_people(self):
people_to_kill = []
for person in self.people:
if person.maybe_die():
people_to_kill.append(person)
for person in people_to_kill:
self.remove_person(person)
def plot_people_locations(self):
xs = [p.location[0] for p in self.people]
ys = [p.location[1] for p in self.people]
ages = [p.age for p in self.people]
max_age = max(ages)
age_cmap = plt.get_cmap("gist_rainbow")
age_nums = [256 * age / max_age for age in ages]
colors = [age_cmap(n) for n in age_nums]
plt.scatter(xs, ys, alpha=0.5, c=colors)
# circles for their radii
for p in self.people:
circle = plt.Circle(p.location, p.neighbor_radius, color="r", alpha=0.05)
plt.gca().add_artist(circle)
plt.title("people locations")
plt.show()
def plot_ages(self):
plt.hist([p.age for p in self.people])
plt.title("ages")
plt.show()
def plot_population_history(self):
xs = list(range(len(self.population_history)))
plt.plot(xs, self.population_history)
plt.yscale("log")
plt.title("population history")
plt.show()
def report_genomes(self):
print("--- genome report at year {} ---".format(self.year))
if len(self.people) == 0:
print("everyone is dead")
else:
genomes = sorted(p.dna for p in self.people)
d = {}
for genome in genomes:
if genome not in d:
d[genome] = 0
d[genome] += 1
for genome in sorted(d):
if d[genome] > 1:
print("{} (total clones: {})".format(genome, d[genome]))
else:
print("{} (unique)".format(genome))
self.plot_genome_average()
print("--- end of genome report for year {} ---".format(self.year))
def plot_genome_average(self):
g = [[] for _ in range(Person.N_BASES)]
for p in self.people:
for i, b in enumerate(p.dna):
g[i].append(int(b))
g_avgs = []
g_stds = []
g_0_count = []
g_1_count = []
for lst in g:
g_avgs.append(np.mean(lst))
g_stds.append(np.std(lst))
g_0_count.append(lst.count(0))
g_1_count.append(lst.count(1))
digs = math.ceil(math.log10(len(self.people)))
for i in range(Person.N_BASES):
s = "bit "
s += str(i).rjust(2)
s += ": (0 * "
s += str(g_0_count[i]).rjust(digs)
s += ") "
n_dashes = 25
s_dashes = "-" * n_dashes
avg_position = math.floor(g_avgs[i] * n_dashes)
s_dashes = s_dashes[:avg_position] + "X" + s_dashes[avg_position+1:]
s += s_dashes
s += " (1 * "
s += str(g_1_count[i]).rjust(digs)
s += ")"
print(s)
g_minus_1_sd = [max(0, g_avgs[i] - g_stds[i]) for i in range(Person.N_BASES)]
g_plus_1_sd = [min(1, g_avgs[i] + g_stds[i]) for i in range(Person.N_BASES)]
xs = list(range(Person.N_BASES))
plt.bar(xs, g_plus_1_sd, color="#59ba57")
plt.bar(xs, g_avgs, color="#e5cd19")
plt.bar(xs, g_minus_1_sd, color="white")
plt.title("genome bit values")
plt.show()
def report(self):
self.plot_people_locations()
self.plot_ages()
self.report_genomes()
if __name__ == "__main__":
land = Land(30, 30)
land.populate(100)
for _ in range(50):
land.go_to_next_generation()
if land.year % 10 == 0:
# land.report()
pass
for person in land.people:
print(person)
land.report()