Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.cmm.msu.ru/hg/petri_dish/file/97768e4bbe61/bacteria.py
Дата изменения: Unknown
Дата индексирования: Sun Feb 3 06:02:02 2013
Кодировка:
petri_dish: 97768e4bbe61 bacteria.py

petri_dish

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
1 import random
2 from math import *
3 import vector
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:
17 return True
18 return False
20 def __ne__(self,other):
21 return not(self==other)
23 def check_collision(self, bacteria, delta, bact_r,delta_t):
24 for bact in bacteria:
25 if 0 < abs(self.position - bact.position) < delta + 2*bact_r:
26 return bact
27 return False
29 def check_dish_wall(self,position,radius,delta,bact_r):
30 if abs(position) > radius - (bact_r + delta):
31 return True
32 return False
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):
46 if type == 's':
47 prob = 1 - p_max*(env/100)
48 else:
49 prob = 0.001
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()))
62 bacteria.remove(self)
63 return 1
64 return 0
67 def sexual (self, other, bacteria, delta_t, radius, delta, bact_r,
68 full_lifetime):
69 num_of_mut=1
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)
90 bacteria.remove(self)
91 return num_of_mut
92 else:
93 self.collision(other,bacteria,delta_t,radius,delta,bact_r)
94 return 0
96 def find_place_a(self, bacteria, delta, bact_r): #searches for place for children
97 for bact in bacteria:
98 if 0 < abs(bact.position - self.position) < (bact_r*3 + delta*2):
99 return False
100 return True
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):
108 return False
109 return True
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):
117 return self.mutated
119 def if_mutated_s(self,other,n):
120 if self.mutated==False and other.mutated == False:
121 return False
122 r = (random.randint(0, 3) == 0)
123 if r:
124 n+=1
125 return r