-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMacrophage.java
More file actions
executable file
·286 lines (204 loc) · 5.77 KB
/
Macrophage.java
File metadata and controls
executable file
·286 lines (204 loc) · 5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
public class Macrophage extends Microbe
{
private double moveTime;
private double eatTime;
private double divideTime;
public Macrophage(Environment _world)
{
super(_world);
moveTime = 0;
scheduleNextMove();
divideTime = 0;
scheduleNextDivide();
eatTime = Double.MAX_VALUE;
}
public Macrophage(Environment _world, double _currentTime)
{
super(_world);
moveTime = _currentTime;
scheduleNextMove(); // generate a future move time
divideTime = _currentTime;
scheduleNextDivide(); // generate a future divide time
eatTime = Double.MAX_VALUE;
}
public double getNextEventTime()
{
return Math.min(divideTime, Math.min(moveTime, eatTime));
}
public void executeNextEvent()
{
if (moveTime < eatTime && moveTime < divideTime) {
move();
} else if (eatTime < divideTime) {
eat();
} else {
divide();
}
}
public void scheduleNextMove()
{
moveTime += nextExp(Parameters.MACRO_INTER_MOVE);
}
public void scheduleNextEat(double currentTime)
{
eatTime = currentTime;
}
public void scheduleNextDivide()
{
divideTime += nextExp(Parameters.MACRO_INTER_DIVIDE);
}
public void move()
{
int iteration = 0;
int numBacterium = 0;
int numMacrophages = 0;
// iterate through the surroundings to count microbe types
for (int rowOff = -1; rowOff <= 1; rowOff++)
{
for (int colOff = -1; colOff <= 1; colOff++)
{
surroundings[iteration] = 0;
// skip iteration of current position
if (rowOff == 0 && colOff == 0)
{
surroundings[iteration] = MACROPHAGE_PRESENT;
iteration++;
continue;
}
// surroundings[iteration] = world.getBacterium(position[0] + rowOff, position[1] + colOff); // get what's at current position
// if the current spot has a macrophage increment macrophage count
if (world.containsMacrophage(position[0] + rowOff, position[1] + colOff))
{
numMacrophages++;
surroundings[iteration] = MACROPHAGE_PRESENT;
}
// if the current spot has a macrophage increment bacterium count
else if (world.containsBacterium(position[0] + rowOff, position[1] + colOff))
{
numBacterium++;
surroundings[iteration] = BACTERIUM_PRESENT;
}
iteration++;
}
} // end count of microbe types
// randomly choose which bacterium to move to and eat
if (numBacterium > 0)
{
int nBacterium = random.nextInt(numBacterium); // randomly choose which bacteria to move to
int bacteriaPosition;
for (bacteriaPosition = 0; nBacterium >= 0; bacteriaPosition++) // find the nth bacterium to move to
{
if (surroundings[bacteriaPosition] == BACTERIUM_PRESENT)
{
nBacterium--;
}
}
bacteriaPosition--;
int rowOff = 0;
int colOff = 0;
// adjust the row offset
if (bacteriaPosition < 3)
rowOff--;
else if (bacteriaPosition > 5)
rowOff++;
// adjust the column offset
if (bacteriaPosition % 3 == 0)
colOff--;
else if (bacteriaPosition % 3 == 2)
colOff++;
// Eat the bacterium we have found
scheduleNextEat(moveTime);
System.out.printf("Macrophage %d moving %d, %d\n", this.id, rowOff, colOff);
position = world.moveMicrobe(this, rowOff, colOff);
}
// No bacteria present so randomly choose which empty cell to move into
else if (numMacrophages < 8)
{
int numEmptySpaces = 8 - numMacrophages;
// randomly choose which bacteria to move to
int n = random.nextInt(numEmptySpaces);
int emptySpace;
for (emptySpace = 0; n >= 0; emptySpace++) // find the nth bacterium to move to
{
if (emptySpace == 4) continue;
if (surroundings[emptySpace] == 0)
{
n--;
}
}
emptySpace--;
int rowOff = 0;
int colOff = 0;
// adjust the row offset
if (emptySpace < 3)
rowOff--;
else if (emptySpace > 5)
rowOff++;
// adjust the column offset
if (emptySpace % 3 == 0)
colOff--;
else if (emptySpace % 3 == 2)
colOff++;
//System.out.printf("Microbe %d moving %d, %d\n", this.id, rowOff, colOff);
position = world.moveMicrobe(this, rowOff, colOff);
}
scheduleNextMove();
}
public void eat()
{
Bacterium food = world.getBacterium(position[0],position[1]);
if (food != null) {
//System.out.printf("Macrophage %d eating Microbe %d\n", this.id, food.id);
world.removeBacterium(food);
}
scheduleNextEat(Double.MAX_VALUE);
}
public void divide()
{
if(numBacteriumInSurroundings() < Parameters.MIN_BACT_TO_DIVIDE)
{
//System.out.printf("Macrophage %d skipped dividing not enough bacterium\n",this.id);
scheduleNextDivide();
return;
}
//System.out.printf("Macrophage %d dividing\n",this.id);
int randomSpace = random.nextInt(9);
int colOff;
int rowOff;
int currentSpace = randomSpace;
do
{
colOff = (currentSpace % 3) - 1;
rowOff = (currentSpace / 3) - 1;
if (world.isEmpty(position[0] + rowOff, position[1] + colOff))
{
Macrophage offspring = new Macrophage(world, divideTime);
//System.out.printf("\tcreated macrophage %d\n", offspring.id);
// add macrophage to empty space
world.addMicrobe(offspring, position[0] + rowOff, position[1] + colOff);
break; // found place to move to so break out of loop
}
currentSpace = (currentSpace + 1) % 9;
} while (currentSpace != randomSpace);
scheduleNextDivide();
//System.out.printf("\tNext move: %f, Next Divide: %f\n", moveTime, divideTime);
}
public int numBacteriumInSurroundings()
{
int numBacterium = 0;
int colOff;
int rowOff;
int currentSpace = 0;
do
{
colOff = (currentSpace % 3) - 1;
rowOff = (currentSpace / 3) - 1;
if (world.containsBacterium(position[0] + rowOff, position[1] + colOff))
{
numBacterium++;
}
currentSpace = (currentSpace + 1) % 9;
} while (currentSpace != 0);
return numBacterium;
}
}