-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBacterium.java
More file actions
executable file
·201 lines (148 loc) · 4.69 KB
/
Bacterium.java
File metadata and controls
executable file
·201 lines (148 loc) · 4.69 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
public class Bacterium extends Microbe
{
private double moveTime;
private double divideTime;
private double dieTime;
private double prevTime;
private double consumptionRate;
private double resource;
public Bacterium(Environment _world)
{
super(_world);
moveTime = 0;
scheduleNextMove();
divideTime = 0;
scheduleNextDivide();
prevTime = 0;
resource = random.nextGaussian() * Parameters.INIT_RESOURCE_SD + Parameters.INIT_RESOURCE_MEAN;
dieTime = Double.MAX_VALUE;
consumptionRate = random.nextGaussian() * Parameters.CONSUMPTION_RATE_SD + Parameters.CONSUMPTION_RATE_MEAN;
}
public Bacterium(Environment _world, double _currentTime, double _resource, double _consumptionRate)
{
super(_world);
moveTime = _currentTime;
scheduleNextMove(); // generate a future move time
divideTime = _currentTime;
scheduleNextDivide(); // generate a future divide time
prevTime = _currentTime;
resource = _resource;
dieTime = Double.MAX_VALUE;
consumptionRate = _consumptionRate;
}
// Return the time of the next event
public double getNextEventTime()
{
return(Math.min(dieTime, Math.min(moveTime,divideTime)));
}
public void executeNextEvent()
{
prevTime = getNextEventTime();
if (moveTime < divideTime && moveTime < dieTime) {
move();
} else if (divideTime < dieTime) {
divide();
} else {
die();
}
}
public void scheduleNextMove()
{
moveTime += nextExp(Parameters.BACT_INTER_MOVE);
}
public void scheduleNextDivide()
{
divideTime += nextExp(Parameters.BACT_INTER_DIVIDE);
}
public void scheduleDeath()
{
double netResourceLossRate = consumptionRate - world.getRegrowth(position[0], position[1]);
if (netResourceLossRate > 0) {
dieTime = resource / netResourceLossRate + getNextEventTime();
//System.out.printf("Die time %f\n", dieTime);
} else {
dieTime = Double.MAX_VALUE;
}
}
public void die()
{
// System.out.printf("=============Bacterium %d Died============\n", this.id);
world.removeBacterium(this);
}
public void move()
{
resource += world.harvest(moveTime, position[0], position[1], true);
int randomSpace = random.nextInt(9);
int colOff;
int rowOff;
int currentSpace = randomSpace;
do {
colOff = (currentSpace % 3) - 1;
rowOff = (currentSpace / 3) - 1;
// Search for an adjacent space which does not contain another bacterium
if (!world.containsBacterium(position[0] + rowOff, position[1] + colOff))
{
// move bacteria to empty space
//System.out.printf("Bacterium %d moving %d, %d\n", this.id, rowOff, colOff);
position = world.moveMicrobe(this, rowOff, colOff);
// If you have moved into a macrophage, get eaten
Macrophage m = world.getMacrophage(position[0],position[1]);
if (m != null)
{
//System.out.printf("\tBacterium %d will be eaten by Macrophage %d\n", this.id, m.id);
m.scheduleNextEat(moveTime);
}
else {
// Only harvest if there is no macrophage in the space
resource += world.harvest(moveTime, position[0], position[1], false);
// consume some of your resource
resource -= (getNextEventTime() - prevTime) * consumptionRate;
// if you consumed all your resources, die
if (resource < 0)
{
die();
// get out of method if you die
return;
}
// if you are still alive, schedule a next death time based on current resources and consumuption rate
scheduleDeath();
}
break;
}
currentSpace = (currentSpace + 1) % 9;
} while (currentSpace != randomSpace);
// if no empty spaces, don't move
scheduleNextMove();
//System.out.printf("\tNext move: %f, Next Divide: %f, Die: %f\n", moveTime, divideTime, dieTime);
//System.out.printf("\tResource: %f\n", resource);
}
/*
*/
public void divide()
{
//System.out.printf("Bacterium %d dividing\n",this.id);
resource = resource / 2;
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))
{
Bacterium offspring = new Bacterium(world, divideTime, resource, consumptionRate);
//System.out.printf("\tcreated bacterium %d\n", offspring.id);
// add bacteria 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);
scheduleDeath();
scheduleNextDivide();
//System.out.printf("\tNext move: %f, Next Divide: %f, Die: %f\n", moveTime, divideTime, dieTime);
//System.out.printf("\tResource: %f\n", resource);
}
}