9
9
from collections import deque
10
10
11
11
import numpy as np
12
+
13
+ sys .path .insert (0 , os .path .abspath ("../../../mesa" ))
12
14
from mesa .experimental .continuous_space import ContinuousSpaceAgent
13
15
16
+ class CellularAgent (ContinuousSpaceAgent ) :
17
+ def _random_move (self , speed = 1 ):
18
+ """Random walk in a 2D space."""
19
+ perturb = np .array (
20
+ [
21
+ self .random .uniform (- 0.5 , 0.5 ),
22
+ self .random .uniform (- 0.5 , 0.5 ),
23
+ ]
24
+ )
25
+ self .direction = self .direction + perturb
26
+ norm = np .linalg .norm (self .direction )
27
+ if norm > 0 :
28
+ self .direction /= norm
29
+ self .position += self .direction * speed
30
+
14
31
15
- class AntibodyAgent (ContinuousSpaceAgent ):
32
+ class AntibodyAgent (CellularAgent ):
16
33
"""An Antibody agent. They move randomly until they see a virus, go fight it.
17
34
If they lose, stay KO for a bit, lose health and back to random moving.
18
35
"""
@@ -113,17 +130,7 @@ def move(self):
113
130
114
131
# Random walk if no target
115
132
elif target is None :
116
- perturb = np .array (
117
- [
118
- self .random .uniform (- 0.5 , 0.5 ),
119
- self .random .uniform (- 0.5 , 0.5 ),
120
- ]
121
- )
122
- self .direction = self .direction + perturb
123
- norm = np .linalg .norm (self .direction )
124
- if norm > 0 :
125
- self .direction /= norm
126
- new_pos = self .position + self .direction * self .speed
133
+ self ._random_move ()
127
134
128
135
# Chase a valid virus target
129
136
else :
@@ -161,7 +168,7 @@ def engage_virus(self, virus) -> str:
161
168
return "ko"
162
169
163
170
164
- class VirusAgent (ContinuousSpaceAgent ):
171
+ class VirusAgent (CellularAgent ):
165
172
"""A virus agent: random movement, mutation, duplication, passive to antibodies."""
166
173
167
174
speed = 1
@@ -186,7 +193,7 @@ def __init__(
186
193
def step (self ):
187
194
if self .random .random () < self .duplication_rate :
188
195
self .duplicate ()
189
- self .move ()
196
+ self ._random_move ()
190
197
191
198
def duplicate (self ):
192
199
VirusAgent (
@@ -209,17 +216,4 @@ def generate_dna(self, dna=None):
209
216
dna [idx ] = (dna [idx ] - 1 ) % 10
210
217
return dna
211
218
212
- def move (self ):
213
- # Random walk
214
- perturb = np .array (
215
- [
216
- self .random .uniform (- 0.5 , 0.5 ),
217
- self .random .uniform (- 0.5 , 0.5 ),
218
- ]
219
- )
220
- self .direction = self .direction + perturb
221
- norm = np .linalg .norm (self .direction )
222
- if norm > 0 :
223
- self .direction /= norm
224
219
225
- self .position += self .direction * self .speed
0 commit comments