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@20
|
29 def move(self, bacteria, delta_t, radius, delta, bact_r): |
ksenia_yashina@20
|
30 if abs(self.position) > radius - (bact_r + delta): |
ksenia_yashina@20
|
31 self.velocity = self.velocity.angleToCoord(-(pi/2 - |
ksenia_yashina@20
|
32 self.velocity.angle())) |
ksenia_yashina@20
|
33 self.position += self.velocity*delta_t |
ksenia_yashina@20
|
34 |
ksenia_yashina@20
|
35 def collision(self, other, bacteria, delta_t, radius, delta, bact_r): |
ksenia_yashina@20
|
36 self.velocity, other.velocity = other.velocity, self.velocity |
ksenia_yashina@20
|
37 self.move(bacteria, delta_t, radius, delta, bact_r) |
ksenia_yashina@20
|
38 other.move(bacteria, delta_t, radius, delta, bact_r) |
ksenia_yashina@20
|
39 |
ksenia_yashina@20
|
40 def reprod(self, type, env, p_max): |
ksenia_yashina@20
|
41 if type == 's': |
ksenia_yashina@20
|
42 prob = 1 - p_max*(env/100) |
ksenia_yashina@20
|
43 else: |
ksenia_yashina@20
|
44 prob = 0.001 |
ksenia_yashina@20
|
45 return random.random() < prob |
ksenia_yashina@20
|
46 |
ksenia_yashina@20
|
47 def asexual(self, bacteria, delta_t, radius, delta, bact_r, full_lifetime): |
ksenia_yashina@20
|
48 if self.find_place_a(bacteria, delta, bact_r): |
ksenia_yashina@20
|
49 pos1=self.position + vector.Vector(-(bact_r + 0.75*delta),0) |
ksenia_yashina@20
|
50 vel1=vector.Vector(1,0).angleToCoord(self.velocity.angle()) |
ksenia_yashina@20
|
51 bacteria.append(Bacteria(pos1, self.rnd_velocity(pi), |
ksenia_yashina@20
|
52 full_lifetime, self.if_mutated_a())) |
ksenia_yashina@20
|
53 pos2=self.position + vector.Vector((bact_r + 0.75*delta),0) |
ksenia_yashina@20
|
54 bacteria.append(Bacteria(pos2, self.rnd_velocity(0), |
ksenia_yashina@20
|
55 full_lifetime, self.if_mutated_a())) |
ksenia_yashina@20
|
56 bacteria.remove(self) |
ksenia_yashina@20
|
57 return |
ksenia_yashina@20
|
58 self.move(bacteria,delta_t,radius,delta,bact_r) |
ksenia_yashina@20
|
59 |
ksenia_yashina@20
|
60 |
ksenia_yashina@20
|
61 def sexual (self, other, bacteria, delta_t, radius, delta, bact_r, |
ksenia_yashina@20
|
62 full_lifetime): |
ksenia_yashina@20
|
63 center = (self.position+other.position)*0.5 |
ksenia_yashina@20
|
64 if self.find_place_s(other, bacteria, delta, bact_r): |
ksenia_yashina@20
|
65 center = (self.position + other.position)*0.5 |
ksenia_yashina@20
|
66 bacteria.append(Bacteria(center + |
ksenia_yashina@20
|
67 vector.Vector(-(bact_r*sqrt(2) + 0.75*delta),0), |
ksenia_yashina@20
|
68 self.rnd_velocity(pi), full_lifetime, |
ksenia_yashina@20
|
69 self.if_mutated_s(other))) |
ksenia_yashina@20
|
70 bacteria.append(Bacteria(center + |
ksenia_yashina@20
|
71 vector.Vector((bact_r*sqrt(2) + 0.75*delta),0), |
ksenia_yashina@20
|
72 self.rnd_velocity(0), full_lifetime, |
ksenia_yashina@20
|
73 self.if_mutated_s(other))) |
ksenia_yashina@20
|
74 bacteria.append(Bacteria(center + |
ksenia_yashina@20
|
75 vector.Vector(0, -(bact_r*sqrt(2) + 0.75*delta)), |
ksenia_yashina@20
|
76 self.rnd_velocity(3*pi/2), full_lifetime, |
ksenia_yashina@20
|
77 self.if_mutated_s(other))) |
ksenia_yashina@20
|
78 bacteria.append(Bacteria(center + |
ksenia_yashina@20
|
79 vector.Vector(0, (bact_r*sqrt(2) + 0.75*delta)), |
ksenia_yashina@20
|
80 self.rnd_velocity(pi/2), full_lifetime, |
ksenia_yashina@20
|
81 self.if_mutated_s(other))) |
ksenia_yashina@20
|
82 bacteria.remove(other) |
ksenia_yashina@20
|
83 bacteria.remove(self) |
ksenia_yashina@20
|
84 else: |
ksenia_yashina@20
|
85 self.collision(other,bacteria,delta_t,radius,delta,bact_r) |
ksenia_yashina@20
|
86 |
ksenia_yashina@20
|
87 def find_place_a(self, bacteria, delta, bact_r): #searches for place for children |
ksenia_yashina@20
|
88 for bact in bacteria: |
ksenia_yashina@20
|
89 if 0 < abs(bact.position - self.position) < (bact_r*3 + delta*2): |
ksenia_yashina@20
|
90 return False |
ksenia_yashina@20
|
91 return True |
ksenia_yashina@20
|
92 |
ksenia_yashina@20
|
93 |
ksenia_yashina@20
|
94 def find_place_s(self, other, bacteria, delta, bact_r): |
ksenia_yashina@20
|
95 for bact in bacteria: |
ksenia_yashina@20
|
96 if bact!=self and bact!=other: |
ksenia_yashina@20
|
97 if 0 < abs(bact.position - (self.position + other.position)*0.5)\ |
ksenia_yashina@20
|
98 < ((sqrt(2) + 1)*bact_r*2 + delta*2): |
ksenia_yashina@20
|
99 return False |
ksenia_yashina@20
|
100 return True |
ksenia_yashina@20
|
101 |
ksenia_yashina@20
|
102 |
ksenia_yashina@20
|
103 def rnd_velocity(self,angle): |
ksenia_yashina@20
|
104 vel=vector.Vector(1,0) |
ksenia_yashina@20
|
105 return vel.angleToCoord(angle + pi*random.randrange(-4,5)/16) |
ksenia_yashina@20
|
106 |
ksenia_yashina@20
|
107 def if_mutated_a(self): |
ksenia_yashina@20
|
108 return self.mutated |
ksenia_yashina@20
|
109 |
ksenia_yashina@20
|
110 def if_mutated_s(self,other): |
ksenia_yashina@20
|
111 if self.mutated + other.mutated == 0: |
ksenia_yashina@20
|
112 return False |
ksenia_yashina@20
|
113 return random.randrange(0, 3 - (self.mutated+other.mutated)) == 0 |