-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bunny_model_0.6.py
43 lines (34 loc) · 1.06 KB
/
Bunny_model_0.6.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
import random as rnd
class Individual:
def __init__(self):
self.age = 0
self.resources = 10
self.max_age = rnd.randint(5,10)
def get_older(self):
self.age += 1
self.resources -= 3
def search_food(self):
chance_to_find_food = 0.3
if rnd.random() < chance_to_find_food:
n_carrots = rnd.randint(5,10)
self.eat(n_carrots)
def eat(self,n_carrots):
self.resources += n_carrots
bunnypop = []
startpop = 10
for n in range(startpop):
bunnypop.append(Individual())
maxtime = 10
for t in range(maxtime):
oldpop = bunnypop[:]
del bunnypop[:]
for bunny in oldpop:
bunny.get_older()
bunny.search_food()
if bunny.age > bunny.max_age:
print 'A bunny died of old age'
elif bunny.resources < 0:
print 'A bunny died of starvation'
else:
bunnypop.append(bunny)
print len(bunnypop),' bunnies survived'