petri_dish
diff bacteria.py @ 20:2df48c61bd42
now cells can move,reproduce
author | Yashina Ksenia <ksenia_yashina@kodomo.fbb.msu.ru> |
---|---|
date | Mon, 20 Dec 2010 01:44:18 +0300 |
parents | |
children | f55481ff4bb5 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/bacteria.py Mon Dec 20 01:44:18 2010 +0300 1.3 @@ -0,0 +1,113 @@ 1.4 +import random 1.5 +from math import * 1.6 +import vector 1.7 + 1.8 +class Bacteria (object): 1.9 + def __init__(self, position, velocity, lifetime, mutated): 1.10 + self.position = position 1.11 + self.velocity = velocity 1.12 + self.lifetime = lifetime 1.13 + self.mutated = mutated 1.14 + 1.15 + def __eq__(self,other): 1.16 + if self.position==other.position and\ 1.17 + self.velocity==other.velocity and\ 1.18 + self.lifetime==other.lifetime and\ 1.19 + self.mutated==other.mutated: 1.20 + return True 1.21 + return False 1.22 + 1.23 + def __ne__(self,other): 1.24 + return not(self==other) 1.25 + 1.26 + def check_collision(self, bacteria, delta, bact_r,delta_t): 1.27 + for bact in bacteria: 1.28 + if 0 < abs(self.position - bact.position) < delta + 2*bact_r: 1.29 + return bact 1.30 + return False 1.31 + 1.32 + def move(self, bacteria, delta_t, radius, delta, bact_r): 1.33 + if abs(self.position) > radius - (bact_r + delta): 1.34 + self.velocity = self.velocity.angleToCoord(-(pi/2 - 1.35 + self.velocity.angle())) 1.36 + self.position += self.velocity*delta_t 1.37 + 1.38 + def collision(self, other, bacteria, delta_t, radius, delta, bact_r): 1.39 + self.velocity, other.velocity = other.velocity, self.velocity 1.40 + self.move(bacteria, delta_t, radius, delta, bact_r) 1.41 + other.move(bacteria, delta_t, radius, delta, bact_r) 1.42 + 1.43 + def reprod(self, type, env, p_max): 1.44 + if type == 's': 1.45 + prob = 1 - p_max*(env/100) 1.46 + else: 1.47 + prob = 0.001 1.48 + return random.random() < prob 1.49 + 1.50 + def asexual(self, bacteria, delta_t, radius, delta, bact_r, full_lifetime): 1.51 + if self.find_place_a(bacteria, delta, bact_r): 1.52 + pos1=self.position + vector.Vector(-(bact_r + 0.75*delta),0) 1.53 + vel1=vector.Vector(1,0).angleToCoord(self.velocity.angle()) 1.54 + bacteria.append(Bacteria(pos1, self.rnd_velocity(pi), 1.55 + full_lifetime, self.if_mutated_a())) 1.56 + pos2=self.position + vector.Vector((bact_r + 0.75*delta),0) 1.57 + bacteria.append(Bacteria(pos2, self.rnd_velocity(0), 1.58 + full_lifetime, self.if_mutated_a())) 1.59 + bacteria.remove(self) 1.60 + return 1.61 + self.move(bacteria,delta_t,radius,delta,bact_r) 1.62 + 1.63 + 1.64 + def sexual (self, other, bacteria, delta_t, radius, delta, bact_r, 1.65 + full_lifetime): 1.66 + center = (self.position+other.position)*0.5 1.67 + if self.find_place_s(other, bacteria, delta, bact_r): 1.68 + center = (self.position + other.position)*0.5 1.69 + bacteria.append(Bacteria(center + 1.70 + vector.Vector(-(bact_r*sqrt(2) + 0.75*delta),0), 1.71 + self.rnd_velocity(pi), full_lifetime, 1.72 + self.if_mutated_s(other))) 1.73 + bacteria.append(Bacteria(center + 1.74 + vector.Vector((bact_r*sqrt(2) + 0.75*delta),0), 1.75 + self.rnd_velocity(0), full_lifetime, 1.76 + self.if_mutated_s(other))) 1.77 + bacteria.append(Bacteria(center + 1.78 + vector.Vector(0, -(bact_r*sqrt(2) + 0.75*delta)), 1.79 + self.rnd_velocity(3*pi/2), full_lifetime, 1.80 + self.if_mutated_s(other))) 1.81 + bacteria.append(Bacteria(center + 1.82 + vector.Vector(0, (bact_r*sqrt(2) + 0.75*delta)), 1.83 + self.rnd_velocity(pi/2), full_lifetime, 1.84 + self.if_mutated_s(other))) 1.85 + bacteria.remove(other) 1.86 + bacteria.remove(self) 1.87 + else: 1.88 + self.collision(other,bacteria,delta_t,radius,delta,bact_r) 1.89 + 1.90 + def find_place_a(self, bacteria, delta, bact_r): #searches for place for children 1.91 + for bact in bacteria: 1.92 + if 0 < abs(bact.position - self.position) < (bact_r*3 + delta*2): 1.93 + return False 1.94 + return True 1.95 + 1.96 + 1.97 + def find_place_s(self, other, bacteria, delta, bact_r): 1.98 + for bact in bacteria: 1.99 + if bact!=self and bact!=other: 1.100 + if 0 < abs(bact.position - (self.position + other.position)*0.5)\ 1.101 + < ((sqrt(2) + 1)*bact_r*2 + delta*2): 1.102 + return False 1.103 + return True 1.104 + 1.105 + 1.106 + def rnd_velocity(self,angle): 1.107 + vel=vector.Vector(1,0) 1.108 + return vel.angleToCoord(angle + pi*random.randrange(-4,5)/16) 1.109 + 1.110 + def if_mutated_a(self): 1.111 + return self.mutated 1.112 + 1.113 + def if_mutated_s(self,other): 1.114 + if self.mutated + other.mutated == 0: 1.115 + return False 1.116 + return random.randrange(0, 3 - (self.mutated+other.mutated)) == 0