-
Notifications
You must be signed in to change notification settings - Fork 0
/
evolution.py
120 lines (92 loc) · 3.6 KB
/
evolution.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
import random
class Individual(object):
def __init__(self, species='', sex='', survivability = 0, reproductive_likelihood = 0, phenotype = [], genotype = []):
self.species = species
self.sex = sex
self.survivability = survivability
self.reproductive_likelihood = reproductive_likelihood
self.phenotype = phenotype
self.genotype = genotype
self.alive = True
self.hadsex = 0
def create_population(amount):
population = []
species_index = {'test_bird': {'species': 'test_bird'}}
sex_list = ('male', 'female')
for i in range(0, amount):
species = 'test_bird'
sex = random.choice(sex_list)
survivability = round(random.uniform(0, 1), 2)
# print(survivability)
reproductive_likelihood = round(random.uniform(0, 1), 2)
# print(reproductive_likelihood)
population.append(Individual(species, sex, survivability, reproductive_likelihood))
return population
def create_adult_population(population):
adult_population = []
death_rate = float(.5)
death_count = int(len(population)*death_rate)
max = float(0)
pix = float(0)
for individual in population:
max += individual.survivability
individual.surv_toprange = max
individual.surv_botrange = max - individual.survivability
print('Max: {0}, Top Range: {1}, Bot Range {2}'.format(max, individual.surv_toprange, individual.surv_botrange))
for i in range(0, death_count):
pix = round(random.uniform(0,max), 2)
print(pix)
print(i)
for individual in population:
if individual.surv_botrange < pix and pix < individual.surv_toprange:
individual.alive = False
print('ded')
return population
def create_weighted_sex():
max_s = float(0)
for individual in population:
if individual.alive:
if selection_mode == female:
if individual.sex = male:
max_s += individual.reproductive_likelihood
individual.sex_toprange = max_s
individual.sex_botrange = max_s - individual.reproductive_likelihood
def create_next_generation(population):
next_generation = []
selection_mode = female
for individual in adult_population:
if individual.alive == True:
if selection_mode == female:
while individual.sex == female:
def print_population_statistics(population):
"""
Prints the species, sex, survability, reproductive_likelyhood and
other stats of the members of a population.
:param population: population list object
:return:
"""
for individual in population:
print_individual_statistics(individual)
if individual.alive:
print('alive')
def print_individual_statistics(individual):
"""
Prints the statistics of an individual.
:param individual: Individual class object
:return:
"""
species = individual.species
sex = individual.sex
survivability = individual.survivability
reproductive_likelihood = individual.reproductive_likelihood
print('Species: {0} | Sex: {1} | Survivability {2} | Reproductive_Chance {3}'.format(species, sex, survivability, reproductive_likelihood))
def print_adult_population_statistics(adult_population):
for individual in population:
print_individual_statistics(individual)
if individual.alive:
print('Alive')
def test():
a = create_population(20)
## print_population_statistics(a)
b = create_adult_population(a)
print_population_statistics(b)