-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbiological_model.py
39 lines (36 loc) · 933 Bytes
/
biological_model.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
import random as r
def select_partner(fits, pop):
"""
Mimics weighted selection with replacement.
"""
totals = []
running_total = 0
for f in fits:
running_total += f
totals.append(running_total)
rnd = r.random() * running_total
for i, t in enumerate(totals):
if rnd < t:
return pop[i]
def crossover(chromosome, partner):
"""
in: parents [2]
out: children [1]
rate: 0.5
Crosses two chromosomes at their mid point.
"""
# yes = r.randrange(0, 1)
# if yes:
c = chromosome[:400] + partner[400:len(partner)]
return c
# elif not yes:
# c = r.choice([chromosome, partner])
# return c
def mutate(chromosome):
"""
mutates a random allel with rate of 1.
"""
position = r.randrange(0, len(chromosome))
newvalue = r.randrange(-255, 255)
chromosome[position] = newvalue
return chromosome