view bacteria.py @ 23:97768e4bbe61
help
author |
Yashina Ksenia <ksenia_yashina@kodomo.fbb.msu.ru> |
date |
Thu, 23 Dec 2010 17:08:18 +0300 |
parents |
f55481ff4bb5 |
children |
|
line source
5 class Bacteria (object):
6 def __init__(self, position, velocity, lifetime, mutated):
7 self.position = position
8 self.velocity = velocity
9 self.lifetime = lifetime
10 self.mutated = mutated
12 def __eq__(self,other):
13 if self.position==other.position and\
14 self.velocity==other.velocity and\
15 self.lifetime==other.lifetime and\
16 self.mutated==other.mutated:
20 def __ne__(self,other):
21 return not(self==other)
23 def check_collision(self, bacteria, delta, bact_r,delta_t):
25 if 0 < abs(self.position - bact.position) < delta + 2*bact_r:
29 def check_dish_wall(self,position,radius,delta,bact_r):
30 if abs(position) > radius - (bact_r + delta):
34 def move(self, bacteria, delta_t, radius, delta, bact_r):
35 if self.check_dish_wall(self.position,radius,delta,bact_r):
36 self.velocity = self.velocity.angleToCoord(-(pi/2 -
37 self.velocity.angle()))
38 self.position += self.velocity*delta_t
40 def collision(self, other, bacteria, delta_t, radius, delta, bact_r):
41 self.velocity, other.velocity = other.velocity, self.velocity
42 self.move(bacteria, delta_t, radius, delta, bact_r)
43 other.move(bacteria, delta_t, radius, delta, bact_r)
45 def reprod(self, type, env, p_max):
47 prob = 1 - p_max*(env/100)
50 return random.random() < prob
52 def asexual(self, bacteria, delta_t, radius, delta, bact_r, full_lifetime):
53 if self.find_place_a(bacteria, delta, bact_r)\
54 and not(self.check_dish_wall(self.position,radius,delta,3*bact_r)):
55 pos1=self.position + vector.Vector(-(bact_r + 0.75*delta),0)
56 vel1=vector.Vector(1,0).angleToCoord(self.velocity.angle())
57 bacteria.append(Bacteria(pos1, self.rnd_velocity(pi),
58 full_lifetime, self.if_mutated_a()))
59 pos2=self.position + vector.Vector((bact_r + 0.75*delta),0)
60 bacteria.append(Bacteria(pos2, self.rnd_velocity(0),
61 full_lifetime, self.if_mutated_a()))
67 def sexual (self, other, bacteria, delta_t, radius, delta, bact_r,
70 center = (self.position+other.position)*0.5
71 if self.find_place_s(other, bacteria, delta, bact_r) and\
72 not(self.check_dish_wall(center,radius,delta,bact_r*3)):
73 bacteria.append(Bacteria(center +
74 vector.Vector(-(bact_r*sqrt(2) + 0.75*delta),0),
75 self.rnd_velocity(pi), full_lifetime,
76 self.if_mutated_s(other,num_of_mut)))
77 bacteria.append(Bacteria(center +
78 vector.Vector((bact_r*sqrt(2) + 0.75*delta),0),
79 self.rnd_velocity(0), full_lifetime,
80 self.if_mutated_s(other,num_of_mut)))
81 bacteria.append(Bacteria(center +
82 vector.Vector(0, -(bact_r*sqrt(2) + 0.75*delta)),
83 self.rnd_velocity(3*pi/2), full_lifetime,
84 self.if_mutated_s(other,num_of_mut)))
85 bacteria.append(Bacteria(center +
86 vector.Vector(0, (bact_r*sqrt(2) + 0.75*delta)),
87 self.rnd_velocity(pi/2), full_lifetime,
88 self.if_mutated_s(other,num_of_mut)))
89 bacteria.remove(other)
93 self.collision(other,bacteria,delta_t,radius,delta,bact_r)
96 def find_place_a(self, bacteria, delta, bact_r): #searches for place for children
98 if 0 < abs(bact.position - self.position) < (bact_r*3 + delta*2):
103 def find_place_s(self, other, bacteria, delta, bact_r):
104 for bact in bacteria:
105 if bact!=self and bact!=other:
106 if 0 < abs(bact.position - (self.position + other.position)*0.5)\
107 < ((sqrt(2) + 1)*bact_r*2 + delta*2):
112 def rnd_velocity(self,angle):
113 vel=vector.Vector(1,0)
114 return vel.angleToCoord(angle + pi*random.randrange(-4,5)/16)
116 def if_mutated_a(self):
119 def if_mutated_s(self,other,n):
120 if self.mutated==False and other.mutated == False:
122 r = (random.randint(0, 3) == 0)