-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimal.py
More file actions
64 lines (42 loc) · 1.36 KB
/
Animal.py
File metadata and controls
64 lines (42 loc) · 1.36 KB
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
import random
class Animal:
def __init__(self, name, fertility, childRatio, env, age=0):
self.name = name
self.childRatio = childRatio
self.childs = []
self.partner = None
self.fertility = fertility
self.gender = None
self.age = age
self.environment = env
self.mutationRate = 0.05
def addEnvironment(self, env):
self.environment = env
def setRatio(self, ratio):
self.childRatio = ratio
def getRatio(self):
return self.childRatio
def getChilds(self):
return self.childs
def getFertility(self):
return self.fertility
def getName(self):
return self.name
def older(self):
self.age = 1 + self.age
def mutate(self):
if random.uniform(0,1)<self.mutationRate:
if random.random()<0.5:
self.childRatio += 0.04
else:
self.childRatio -=0.04
if random.uniform(0,1)<0.0005:
if random.random()<0.5:
self.fertility += 1
else:
if self.fertility>=1:
self.fertility -=1
def addChild(self, child):
self.childs.append(child)
def _eq_(self, other):
return isinstance(other, self.__class__) and self.name == other.name