-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnvironment.java
More file actions
executable file
·227 lines (181 loc) · 5.63 KB
/
Environment.java
File metadata and controls
executable file
·227 lines (181 loc) · 5.63 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
import java.util.Random;
import java.util.ArrayList;
public class Environment {
int numRows;
int numCols;
int numMacrophages;
int numBacteria;
Cell[][] landscape;
Random random;
Driver driver;
public Environment(int _numRows,int _numCols,int _numMacrophages,int _numBacteria, Driver _driver) {
driver = _driver;
numRows = _numRows;
numCols = _numCols;
numMacrophages = _numMacrophages;
numBacteria = _numBacteria;
random = new Random();
landscape = new Cell[numRows][numCols];
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
landscape[i][j] = new Cell();
}
}
}
public int[] moveMicrobe(Microbe agent, int rowOff, int colOff) {
int currentRow = ( agent.getRow() + numRows ) % numRows;
int currentCol = ( agent.getCol() + numCols ) % numCols;
landscape[currentRow][currentCol].removeMicrobe(agent);
int newRow = (currentRow + rowOff + numRows) % numRows;
int newCol = (currentCol + colOff + numCols) % numCols;
landscape[newRow][newCol].setMicrobe(agent);
return new int[] {newRow, newCol};
}
// add a microbe to the environment in a random empty space
public void addMicrobe(Microbe agent) {
int row;
int col;
do {
row = random.nextInt(numRows);
col = random.nextInt(numCols);
} while (!isEmpty((row+numRows) % numRows,(col+numCols) % numCols));
landscape[(row+numRows) % numRows][(col+numCols) % numCols].setMicrobe(agent);
agent.setRowCol((row+numRows) % numRows, (col+numCols) % numCols);
}
// add a microbe to the environment in a specified space
// (as a result of a divide)
public void addMicrobe(Microbe agent, int row, int col) {
landscape[(row+numRows) % numRows][(col+numCols) % numCols].setMicrobe(agent);
agent.setRowCol((row+numRows) % numRows,(col+numCols) % numCols);
if (agent instanceof Bacterium) {
numBacteria++;
driver.addBacterium(agent);
}
else if (agent instanceof Macrophage) {
numMacrophages++;
driver.addMacrophage(agent);
}
}
public void removeBacterium(Bacterium agent)
{
landscape[(agent.getRow()+numRows) % numRows][(agent.getCol() + numCols) % numCols].removeMicrobe(agent);
numBacteria--;
driver.removeBacterium(agent);
}
public Bacterium getBacterium(int row, int col)
{
return landscape[(row+numRows) % numRows][(col+numCols) % numCols].getBacterium();
}
public Macrophage getMacrophage(int row, int col)
{
return landscape[(row+numRows) % numRows][(col+numCols) % numCols].getMacrophage();
}
public boolean containsBacterium(int row, int col)
{
Bacterium contents = landscape[(row+numRows) % numRows][(col+numCols) % numCols].getBacterium();
return (contents != null);
}
public boolean containsMacrophage(int row, int col)
{
Macrophage contents = landscape[(row+numRows) % numRows][(col+numCols) % numCols].getMacrophage();
return (contents != null);
}
public boolean isEmpty(int row, int col)
{
return ((!containsMacrophage(row,col)) && (!containsBacterium(row,col)));
}
public double harvest(double currentTime, int row, int col, boolean cont)
{
return landscape[(row+numRows) % numRows][(col+numCols) % numCols].harvestResource(currentTime, cont);
}
public double getRegrowth(int row, int col)
{
return landscape[(row+numRows) % numRows][(col+numCols) % numCols].getRegrowthRate();
}
// Inner class containing the contents of a landscape location.
// May contain a bacterium, a macrophage, or both.
private class Cell
{
Bacterium bacterium;
Macrophage macrophage;
double resource;
double regrowthRate;
double lastHarvestTime;
double maxResource;
public Cell() {
bacterium = null;
macrophage = null;
resource = 0;
lastHarvestTime = 0;
maxResource = random.nextGaussian() * Parameters.MAX_RESOURCE_SD + Parameters.MAX_RESOURCE_MEAN;
regrowthRate = random.nextGaussian() * Parameters.REGROWTH_RATE_SD + Parameters.REGROWTH_RATE_MEAN;
}
public Cell(Bacterium b)
{
bacterium = b;
macrophage = null;
resource = 0;
lastHarvestTime = 0;
maxResource = random.nextGaussian() * Parameters.MAX_RESOURCE_SD + Parameters.MAX_RESOURCE_MEAN;
regrowthRate = random.nextGaussian() * Parameters.REGROWTH_RATE_SD + Parameters.REGROWTH_RATE_MEAN;
}
public Cell(Macrophage m)
{
bacterium = null;
macrophage = m;
resource = 0;
lastHarvestTime = 0;
maxResource = random.nextGaussian() * Parameters.MAX_RESOURCE_SD + Parameters.MAX_RESOURCE_MEAN;
regrowthRate = random.nextGaussian() * Parameters.REGROWTH_RATE_SD + Parameters.REGROWTH_RATE_MEAN;
}
public double getRegrowthRate()
{
return regrowthRate;
}
public Bacterium getBacterium()
{
return bacterium;
}
public Macrophage getMacrophage()
{
return macrophage;
}
public void setMicrobe(Microbe m)
{
if (m instanceof Bacterium) {
bacterium = (Bacterium)m;
} else {
macrophage = (Macrophage)m;
}
}
public void removeMicrobe(Microbe m)
{
if (m instanceof Bacterium) {
bacterium = null;
} else {
macrophage = null;
}
}
public void setBacterum(Bacterium b)
{
bacterium = b;
}
public void setMacrophage(Macrophage m)
{
macrophage = m;
}
public double harvestResource(double currentTime, boolean continuous)
{
// amount of resource currently at the cell when the bacteria eats it
double resourceHarvested = resource + regrowthRate * (currentTime - lastHarvestTime);
if (!continuous && resourceHarvested > maxResource)
{
resourceHarvested = maxResource;
}
resource = 0;
lastHarvestTime = currentTime;
//System.out.printf("Harvested %f resource\n", resourceHarvested);
return resourceHarvested;
}
}
}