Blame view

src/preypredator/PreyPredator2.java 7.73 KB
1b1e928cc   glaville   initial import of...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  package preypredator;
  import it.unimi.dsi.fastutil.ints.IntArrayList;
  
  import java.io.IOException;
  import java.util.Arrays;
  import java.util.Random;
  
  import org.perf4j.StopWatch;
  import org.perf4j.slf4j.Slf4JStopWatch;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  
  import joptsimple.OptionParser;
  import joptsimple.OptionSet;
1b1e928cc   glaville   initial import of...
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
  
  public class PreyPredator2 {
  	
  	private final static Logger logger = LoggerFactory.getLogger(PreyPredator2.class);
  	private final StopWatch watch = new Slf4JStopWatch();
  	private final StopWatch runWatch = new Slf4JStopWatch();
  	
  	//public static final int WIDTH = 1000;
  	//public static final int HEIGHT = 1000;
  	
  	public static final int GRASS_MIN = 0;
  	public static final int GRASS_MAX = 100;
  	public static final int GRASS_ORIG = 20;
  	public static final float GRASS_FACT = 1.0f;
  	public static final int   GRASS_GROWTH = 10;
  	
  	public static final int PREY_ORIG = 10;
  	//public static final int PREY_RADIUS = 20;
  	public static final int PREY_MAX = 50;
  	
  	public static final int PRED_ORIG = 10;
  	//public static final int PRED_RADIUS = 20;
  	public static final int PRED_MAX = 100;
  	
  	// Attributes
  	
  	private final PPGrid grass;
  	private final PPGrid preys;
  	private final PPGrid preds;
  	
  	
  	// Temporary buffers used in each step
  	
  	private final IntArrayList preysX;
  	private final IntArrayList preysY;
  	private final IntArrayList predsX;
  	private final IntArrayList predsY;
  	
  	private final int nbPreds;
  	private final int nbPreys;
  	private final int range;
  	private final PPRuntime runtime;
  	
  	//OCLChrono chrono = new OCLChrono("preypredator");
  	
  	private final int size;
  	
  	public PreyPredator2(int size, int nbPreys, int nbPredators, int range, PPRuntime runtime) {
  		this.grass = new PPGrid(size, size);
  		this.preys = new PPGrid(size, size);
  		this.preds = new PPGrid(size, size);
  		
  		this.size    = size;
  		this.nbPreds = nbPredators;
  		this.nbPreys = nbPreys;
  		this.range   = range;
  		
  		this.preysX = new IntArrayList();
  		this.preysY = new IntArrayList();
  		this.predsX = new IntArrayList();
  		this.predsY = new IntArrayList();
  		
  		this.runtime = runtime; 
  	}	
  	
  	public void setup() {
  		watch.start("pp_setup");
  		initGrid(grass.getStorage(), (int) (grass.getStorage().length * 0.8), GRASS_ORIG);
  		initGrid(preys.getStorage(), nbPreys, PREY_ORIG);
  		initGrid(preds.getStorage(), nbPreds, PRED_ORIG);
  		watch.stop();
  	}
  	
  	public void update_positions() {
  		//chrono.start("update_positions");
  		
  		preysX.clear();
  		preysY.clear();
  		predsX.clear();
  		predsY.clear();
  		
  		for (int j = 0; j < size; j++) {
  			for (int i = 0; i < size; i++) {
  				if (preys.get(i, j) > 0) {
  					preysX.add(i);
  					preysY.add(j);
  				}
  				
  				if (preds.get(i, j) > 0) {
  					predsX.add(i);
  					predsY.add(j);
  				}
  			}
  		}
  		
  		//System.out.println(chrono.stop());
  	}
  	
  	public void step() {
  		watch.start("pp_step");
  		//OCLChrono stepChrono = new OCLChrono("step");
  		//stepChrono.start();
  		
  		update_positions();
  		
  		runWatch.start("pp_grass");
  		runtime.growGrass(grass, GRASS_FACT, GRASS_GROWTH, GRASS_MIN, GRASS_MAX);
  		runWatch.stop();
  		
  		int[] newPreysX = new int[preysX.size()];
  		int[] newPreysY = new int[preysY.size()];
  		int[] newPredsX = new int[predsX.size()];
  		int[] newPredsY = new int[predsY.size()];
  		
  		runWatch.start("pp_move_preys");
  		if (preysX.size() > 0) {
  			
  			runtime.selectMaxTarget(grass, range,
  					preysX.toIntArray(), preysY.toIntArray(),
  					newPreysX, newPreysY);
  		}
  		runWatch.stop();
  		
  		/*
  		for (int i = 0; i < preysX.size(); i++) {
  			System.out.println("Prey: (" + preysX.get(i) + ", " + preysY.get(i) + ") => (" +
  					newPreysX[i] + ", " + newPreysY[i] + ")");	
  		}*/
  		
  		// PREYS
  		
  		for (int i = 0; i < preysX.size(); i++) {
  			final int oldPos = preys.offset(preysX.get(i), preysY.get(i));
  			final int newPos = preys.offset(newPreysX[i], newPreysY[i]);
  			
  			// Check an eventual conflict with another prey
  			if (preys.get(newPos) > 0) {
  				continue;
  			}
  			
  			// Movement
  			preys.set(newPos, preys.get(oldPos));
  			preys.set(oldPos,  0);
  			
  			// Eating
  			if (grass.get(newPos) >= 0) {
  				preys.set(newPos, clamp(preys.get(newPos) + grass.get(newPos), 0, PREY_MAX));
  				grass.set(newPos, 0);
  			}
  			
  			// Reproduction
  			if (preys.get(newPos) == PREY_MAX) {
  				preys.set(oldPos, PREY_ORIG);
  			}
  		}
  		
  		runWatch.start("pp_move_preds");
  		if (predsX.size() > 0) {
  			int [] predsXbis = predsX.toIntArray();
  			int [] predsYbis = predsY.toIntArray();
  			
  			runtime.selectMaxTarget(preys, (int) Math.round(range * 1.5),
  					predsXbis, predsYbis,
  					newPredsX, newPredsY);
  		}
  		runWatch.stop();
  		
  		/*for (int i = 0; i < predsX.size(); i++) {
  			System.out.println("Pred: (" + predsX.get(i) + ", " + predsY.get(i) + ") => (" +
  					newPredsX[i] + ", " + newPredsY[i] + ")");	
  		}*/
  		
  		// PREDATOR
  		
  		for (int i = 0; i < predsX.size(); i++) {
  			final int pos = predsY.get(i) * size + predsX.get(i);
  			final int newPos = newPredsY[i] * size + newPredsX[i];
  			
  			// Check an eventual conflict with another predator
  			if (preds.get(newPos) > 0) {
  				continue;
  			}
  			
  			// Movement
  			preds.set(newPos, preds.get(pos));
  			preds.set(pos, 0);
  			
  			// Eating
  			if (preys.get(newPos) >= 0) {
  				preds.set(newPos, clamp(preds.get(newPos) + preys.get(newPos), 0, PRED_MAX));
  				preys.set(newPos, 0);
  			}
  			
  			// Reproduction
  			if (preds.get(newPos) == PRED_MAX) {
  				preds.set(pos, PRED_ORIG);
  			}
  		}
  		
  		/*System.out.println(grass);
  		System.out.println(preys);*/
  		//System.out.println(preds);
  		//logger.info(stepChrono.stop().toString());
  		watch.stop();
  	}
  	
  	private void initGrid(int[] grid, int nbCells, int value) {
  		int remainingCells = nbCells;
  		Random rng = new Random();
  		
  		while (remainingCells > 0) {
  			final int pos = rng.nextInt(grid.length);
  			
  			if (grid[pos] == 0) {
  				grid[pos] = value;
  				remainingCells--;
  			}
  		}
  	}
  	
  	// Helpers
  	private int clamp(int value, int min, int max) {
  		return Math.max(min, Math.min(value, max));
  	}
  	
  	public static void main(String[] args) {
  		OptionParser parser = new OptionParser();
  		
d2e21dea1   glaville   Update preys-pred...
243
  		parser.acceptsAll(Arrays.asList("g", "gpu", "opencl"), "Enable OpenCL support")
1b1e928cc   glaville   initial import of...
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
  		.withOptionalArg().ofType(Boolean.class).defaultsTo(true);
  		
  		parser.acceptsAll(Arrays.asList("h", "help"), "Print this help");
  		
  		parser.accepts("size", "Size of the grid (side)").withRequiredArg()
  		.ofType(Integer.class).defaultsTo(1000);
  		
  		parser.accepts("preys", "Number of Preys to use").withRequiredArg()
  		.ofType(Integer.class).defaultsTo(10000);
  
  		parser.accepts("preds", "Number of Predators to use").withRequiredArg()
  		.ofType(Integer.class).defaultsTo(5000);
  		
  		parser.accepts("range", "Range to use for preys (x 1.5 for predators)").withRequiredArg()
  		.ofType(Integer.class).defaultsTo(100);
  
  		OptionSet options = parser.parse(args);
  		
  		if (options.has("help")) {
  			try {
  				parser.printHelpOn(System.out);
  				return;
  			} catch (IOException e) {
  				System.err.println("Failed to print help to standard output");
  				e.printStackTrace();
  			}
  		}
  		
  		PPRuntime runtime;
  		
  		if ((Boolean) (options.valueOf("gpu"))) {
d2e21dea1   glaville   Update preys-pred...
275
276
  			logger.info("Use OpenCL runtime on first available GPU");
  			runtime = new PPRuntimeGPU();
1b1e928cc   glaville   initial import of...
277
  		} else {
d2e21dea1   glaville   Update preys-pred...
278
  			logger.info("Use Java Runtime");
1b1e928cc   glaville   initial import of...
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
  			runtime = new PPRuntimeCPU();
  		}
  		
  		int size = (Integer) options.valueOf("size");
  		int nbPreys = (Integer) options.valueOf("preys");
  		int nbPreds = (Integer) options.valueOf("preds");
  		int range = (Integer) options.valueOf("range");
  		
  		logger.info(
  				"size: " + size + ", nbPreys: " + nbPreys + ", nbPreds: " + nbPreds + ", range: " + range
  		);
  		
  		//PPRuntime runtime = new PPRuntimeGPU(ContextType.ALL);
  		//PPRuntime runtime = new PPRuntimeCPU();
  		PreyPredator2 simulation = new PreyPredator2(size, nbPreys, nbPreds, range, runtime);
  		simulation.setup();
  		
  		for (int i = 0; i < 50; i++) {
  			simulation.step();
  		}
  	}
  
  }