-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPopulation.py
48 lines (38 loc) · 1.42 KB
/
Population.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
import random
from numpy.random import choice
from Breeder import Breeder
from Cat import Cat
class Population:
def __init__(self):
self.females = []
self.males = []
def add_cat_from_data(self, data):
cat = Cat(name='test', **data)
self.add_cat(cat)
def add_cat(self, cat):
if cat.gender == 0:
self.females.append(cat)
else:
self.males.append(cat)
cat.describe()
def get_male_cat(self):
self.males.remove(random.choice(list(self.males)))
def get_female_cat(self):
self.males.remove(random.choice(list(self.males)))
def to_json(self):
females =[c.serialize() for c in self.females]
males =[c.serialize() for c in self.males]
return females + males
def breed_population(self):
next_population = Population()
for female in self.females:
fathers = choice([0, 1, 2], 1, p=[0.20, 0.30, 0.50])
if fathers == 1:
breeder = Breeder(female, random.choice(self.males))
for i in range(random.randint(3, 5)):
next_population.add_cat(breeder.breed())
elif fathers == 2:
breeder = Breeder(female, random.choice(self.males), random.choice(self.males))
for i in range(random.randint(3, 5)):
next_population.add_cat(breeder.breed())
return next_population