Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.cmm.msu.su/hg/petri_dish/annotate/f55481ff4bb5/bacteria.py
Дата изменения: Unknown
Дата индексирования: Fri Feb 28 08:33:44 2014
Кодировка:
petri_dish: bacteria.py annotate

petri_dish

annotate bacteria.py @ 21:f55481ff4bb5

fixed reproduction near dish walls
author Yashina Ksenia <ksenia_yashina@kodomo.fbb.msu.ru>
date Mon, 20 Dec 2010 02:38:11 +0300
parents 2df48c61bd42
children 69a4724e7cac
rev   line source
ksenia_yashina@20 1 import random
ksenia_yashina@20 2 from math import *
ksenia_yashina@20 3 import vector
ksenia_yashina@20 4
ksenia_yashina@20 5 class Bacteria (object):
ksenia_yashina@20 6 def __init__(self, position, velocity, lifetime, mutated):
ksenia_yashina@20 7 self.position = position
ksenia_yashina@20 8 self.velocity = velocity
ksenia_yashina@20 9 self.lifetime = lifetime
ksenia_yashina@20 10 self.mutated = mutated
ksenia_yashina@20 11
ksenia_yashina@20 12 def __eq__(self,other):
ksenia_yashina@20 13 if self.position==other.position and\
ksenia_yashina@20 14 self.velocity==other.velocity and\
ksenia_yashina@20 15 self.lifetime==other.lifetime and\
ksenia_yashina@20 16 self.mutated==other.mutated:
ksenia_yashina@20 17 return True
ksenia_yashina@20 18 return False
ksenia_yashina@20 19
ksenia_yashina@20 20 def __ne__(self,other):
ksenia_yashina@20 21 return not(self==other)
ksenia_yashina@20 22
ksenia_yashina@20 23 def check_collision(self, bacteria, delta, bact_r,delta_t):
ksenia_yashina@20 24 for bact in bacteria:
ksenia_yashina@20 25 if 0 < abs(self.position - bact.position) < delta + 2*bact_r:
ksenia_yashina@20 26 return bact
ksenia_yashina@20 27 return False
ksenia_yashina@20 28
ksenia_yashina@21 29 def check_dish_wall(self,position,radius,delta,bact_r):
ksenia_yashina@21 30 if abs(position) > radius - (bact_r + delta):
ksenia_yashina@21 31 return True
ksenia_yashina@21 32 return False
ksenia_yashina@21 33
ksenia_yashina@20 34 def move(self, bacteria, delta_t, radius, delta, bact_r):
ksenia_yashina@21 35 if self.check_dish_wall(self.position,radius,delta,bact_r):
ksenia_yashina@20 36 self.velocity = self.velocity.angleToCoord(-(pi/2 -
ksenia_yashina@20 37 self.velocity.angle()))
ksenia_yashina@20 38 self.position += self.velocity*delta_t
ksenia_yashina@20 39
ksenia_yashina@20 40 def collision(self, other, bacteria, delta_t, radius, delta, bact_r):
ksenia_yashina@20 41 self.velocity, other.velocity = other.velocity, self.velocity
ksenia_yashina@20 42 self.move(bacteria, delta_t, radius, delta, bact_r)
ksenia_yashina@20 43 other.move(bacteria, delta_t, radius, delta, bact_r)
ksenia_yashina@20 44
ksenia_yashina@20 45 def reprod(self, type, env, p_max):
ksenia_yashina@20 46 if type == 's':
ksenia_yashina@20 47 prob = 1 - p_max*(env/100)
ksenia_yashina@20 48 else:
ksenia_yashina@20 49 prob = 0.001
ksenia_yashina@20 50 return random.random() < prob
ksenia_yashina@20 51
ksenia_yashina@20 52 def asexual(self, bacteria, delta_t, radius, delta, bact_r, full_lifetime):
ksenia_yashina@21 53 if self.find_place_a(bacteria, delta, bact_r)\
ksenia_yashina@21 54 and not(self.check_dish_wall(self.position,radius,delta,2*bact_r)):
ksenia_yashina@20 55 pos1=self.position + vector.Vector(-(bact_r + 0.75*delta),0)
ksenia_yashina@20 56 vel1=vector.Vector(1,0).angleToCoord(self.velocity.angle())
ksenia_yashina@20 57 bacteria.append(Bacteria(pos1, self.rnd_velocity(pi),
ksenia_yashina@20 58 full_lifetime, self.if_mutated_a()))
ksenia_yashina@20 59 pos2=self.position + vector.Vector((bact_r + 0.75*delta),0)
ksenia_yashina@20 60 bacteria.append(Bacteria(pos2, self.rnd_velocity(0),
ksenia_yashina@20 61 full_lifetime, self.if_mutated_a()))
ksenia_yashina@20 62 bacteria.remove(self)
ksenia_yashina@20 63 return
ksenia_yashina@20 64 self.move(bacteria,delta_t,radius,delta,bact_r)
ksenia_yashina@20 65
ksenia_yashina@20 66
ksenia_yashina@20 67 def sexual (self, other, bacteria, delta_t, radius, delta, bact_r,
ksenia_yashina@20 68 full_lifetime):
ksenia_yashina@20 69 center = (self.position+other.position)*0.5
ksenia_yashina@21 70 if self.find_place_s(other, bacteria, delta, bact_r) and\
ksenia_yashina@21 71 not(self.check_dish_wall(center,radius,delta,bact_r*(1+sqrt(2)))):
ksenia_yashina@20 72 bacteria.append(Bacteria(center +
ksenia_yashina@20 73 vector.Vector(-(bact_r*sqrt(2) + 0.75*delta),0),
ksenia_yashina@20 74 self.rnd_velocity(pi), full_lifetime,
ksenia_yashina@20 75 self.if_mutated_s(other)))
ksenia_yashina@20 76 bacteria.append(Bacteria(center +
ksenia_yashina@20 77 vector.Vector((bact_r*sqrt(2) + 0.75*delta),0),
ksenia_yashina@20 78 self.rnd_velocity(0), full_lifetime,
ksenia_yashina@20 79 self.if_mutated_s(other)))
ksenia_yashina@20 80 bacteria.append(Bacteria(center +
ksenia_yashina@20 81 vector.Vector(0, -(bact_r*sqrt(2) + 0.75*delta)),
ksenia_yashina@20 82 self.rnd_velocity(3*pi/2), full_lifetime,
ksenia_yashina@20 83 self.if_mutated_s(other)))
ksenia_yashina@20 84 bacteria.append(Bacteria(center +
ksenia_yashina@20 85 vector.Vector(0, (bact_r*sqrt(2) + 0.75*delta)),
ksenia_yashina@20 86 self.rnd_velocity(pi/2), full_lifetime,
ksenia_yashina@20 87 self.if_mutated_s(other)))
ksenia_yashina@20 88 bacteria.remove(other)
ksenia_yashina@20 89 bacteria.remove(self)
ksenia_yashina@20 90 else:
ksenia_yashina@20 91 self.collision(other,bacteria,delta_t,radius,delta,bact_r)
ksenia_yashina@20 92
ksenia_yashina@20 93 def find_place_a(self, bacteria, delta, bact_r): #searches for place for children
ksenia_yashina@20 94 for bact in bacteria:
ksenia_yashina@20 95 if 0 < abs(bact.position - self.position) < (bact_r*3 + delta*2):
ksenia_yashina@20 96 return False
ksenia_yashina@20 97 return True
ksenia_yashina@20 98
ksenia_yashina@20 99
ksenia_yashina@20 100 def find_place_s(self, other, bacteria, delta, bact_r):
ksenia_yashina@20 101 for bact in bacteria:
ksenia_yashina@20 102 if bact!=self and bact!=other:
ksenia_yashina@20 103 if 0 < abs(bact.position - (self.position + other.position)*0.5)\
ksenia_yashina@20 104 < ((sqrt(2) + 1)*bact_r*2 + delta*2):
ksenia_yashina@20 105 return False
ksenia_yashina@20 106 return True
ksenia_yashina@20 107
ksenia_yashina@20 108
ksenia_yashina@20 109 def rnd_velocity(self,angle):
ksenia_yashina@20 110 vel=vector.Vector(1,0)
ksenia_yashina@20 111 return vel.angleToCoord(angle + pi*random.randrange(-4,5)/16)
ksenia_yashina@20 112
ksenia_yashina@20 113 def if_mutated_a(self):
ksenia_yashina@20 114 return self.mutated
ksenia_yashina@20 115
ksenia_yashina@20 116 def if_mutated_s(self,other):
ksenia_yashina@20 117 if self.mutated + other.mutated == 0:
ksenia_yashina@20 118 return False
ksenia_yashina@20 119 return random.randrange(0, 3 - (self.mutated+other.mutated)) == 0