-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulate.java
More file actions
442 lines (362 loc) · 15.9 KB
/
Copy pathSimulate.java
File metadata and controls
442 lines (362 loc) · 15.9 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/* Kongo IoT Application Simulation.
* main() creates the world and calls loop to run the simulation for desired number of "hours" (turns).
* loop does all the work, unloads Goods (generated RFID unload events), loads Goods (generate RFID load events), moves trucks, generates sensor stream events and checks for Goods/sensor rules violations.
*
* Version 1.0: Paul Brebner, Instaclustr.com, February 2018
*
* This is a simplistic stand-alone monolithic version which combines the simulation and rules checking, and is not particularly efficient or scalable.
*/
package com.instaclustr.kongo;
import java.util.*;
public class Simulate {
// Turn rules on or off for Goods movement control. Even with rules turned on there may be some violations as some things are random (e.g. truck accelerate and vibrations).
static boolean enforceTempRules = false; // enforce goods temperature category rules for movements from/to trucks/warehouses
static boolean enforceHazardousRules = false; // enforce goods co-location rules when loading trucks
static boolean checkGoods = true; // turn on/off co-location rules checking during simulation
static boolean debug = false;
static boolean verbose = true;
// global data. all goods in the system (in trucks or warehouses)
static HashMap<String, Goods> allGoods = new HashMap<String, Goods>();
// all warehouses
static HashMap<String, Warehouses> allWarehouses = new HashMap<String, Warehouses>();
// all goods that are in warehouses
static HashMap<String, String> goodsInWarehouses = new HashMap<String, String>();
// all goods that are in trucks
static HashMap<String, String> goodsInTrucks = new HashMap<String, String>();
// all trucks
static HashMap<String, Trucks> allTrucks = new HashMap<String, Trucks>();
// trucks at each warehouse
static HashMap<String, String> trucksAtWarehouses = new HashMap<String, String>();
// truckKey and keep1 are hacks used in Goods loading code in the simulation loop.
static String truckKey;
// keep truck key first time, and then randomly 50% of time for others
public static void keep1(String s)
{
if (truckKey == null || rand.nextBoolean())
truckKey = s;
}
static Random rand = new Random();
// simulation loop, simulates Goods and Trucks movement for required number of rounds (hours)
// assumes everything has been created already.
public static void loop(int hours)
{
long t0 = System.currentTimeMillis();
long totalEvents = 0;
// repeat for hours
System.out.println("Simulation started");
// loop the loop
for (int time=0; time < hours; time++)
{
System.out.println("************** Time = " + time);
// 1 UNLOAD trucks: move goods from trucks to warehouse where truck docked
if (debug) System.out.println("Unloading goods from Trucks...");
// Unload all Goods that are in trucks
Iterator<String> it = goodsInTrucks.keySet().iterator();
while (it.hasNext())
{
String goodsKey = it.next();
String trucksKey = goodsInTrucks.get(goodsKey);
// find warehouse where the truck is
String warehouse = trucksAtWarehouses.get(trucksKey);
// change location of goods to warehouse
goodsInWarehouses.put(goodsKey, warehouse);
if (debug) System.out.println("Unloaded " + goodsKey + " from " + trucksKey + " at " + warehouse);
// generate UNLOAD RFID event
String s = time + " RFID " + warehouse + ": UNLOAD " + goodsKey + " from " + trucksKey;
if (verbose) System.out.println(s);
totalEvents++;
it.remove();
// forget categories of loaded goods
Trucks t = allTrucks.get(trucksKey);
t.resetCats();
}
// 2 LOAD Goods from warehouse to trucks currently docked at warehouse
if (debug) System.out.println("Loading goods onto trucks");
it = goodsInWarehouses.keySet().iterator();
while (it.hasNext())
{
String goodsKey = it.next();
String warehouseKey = goodsInWarehouses.get(goodsKey);
// randomly decide if we want to load this good, check if there is a truck at the warehouse, load it, remove it from warehouse
if (debug) System.out.println("Found goods " + goodsKey + " in " + warehouseKey + " try and load it? ");
if (rand.nextDouble() > 0.5)
{
truckKey = null;
Map<String, String> map = trucksAtWarehouses;
map.entrySet()
.stream()
.filter(x -> x.getValue().equals(warehouseKey))
// hack need to pick 1 truck at random
.forEach(x -> keep1(x.getKey()));
// if a truck was found...
if (truckKey != null)
{
if (debug) System.out.println("Found a truck at warehouse " + truckKey);
// load truck, remove goods from warehouse
Trucks t = allTrucks.get(truckKey);
Goods g = allGoods.get(goodsKey);
// can we load the Goods onto it?
boolean load = false;
load = g.allowedInTruck(t.categoriesOnBoard);
if (verbose && load) System.out.println("Goods allowed in truck, goods cats=" + g.categories + " no conflict with truck cats=" + t.categoriesOnBoard.allCategories());
else if (verbose && !load) System.out.println("Goods NOT ALLOWED in truck, goods cats=" + g.categories + " conflict with truck cats=" + t.categoriesOnBoard.allCategories());
// keep loading if we can load it or we don't care about enforcing rules
if (!enforceHazardousRules || enforceHazardousRules && load)
{
// check temperature control rules
if (load = g.truckTempRules(t))
if (verbose) System.out.println("Goods allowed on truck for temperature rules check");
else if (verbose) System.out.println("Goods NOT ALLOWED on truck for temperature rules check");
if (!enforceTempRules || enforceTempRules && load)
{
t.updateCategories(g);
goodsInTrucks.put(goodsKey, truckKey);
it.remove();
if (debug) System.out.println("Loading " + goodsKey + " onto " + truckKey);
// generate RFID LOAD event
String s = time + " RFID " + warehouseKey + ": LOAD " + goodsKey + " onto " + truckKey;
if (verbose) System.out.println(s);
totalEvents++;
}
}
}
}
}
// 3 Move TRUCKS
// create shuffled list of warehouses to select destination warehouse from
List<String> keyList = new ArrayList<String>(allWarehouses.keySet());
Collections.shuffle( keyList );
Iterator<String> randKeys = keyList.iterator();
for (Map.Entry<String, String> entry : trucksAtWarehouses.entrySet())
{
String truckKey = entry.getKey();
Trucks truck = allTrucks.get(truckKey);
String currentLoc = entry.getValue();
String destination = null;;
// find a warehouse with a compatible temperature control
Warehouses w = null;
while (destination == null)
{
if (randKeys.hasNext())
destination = randKeys.next();
else
{
// else start again
randKeys = keyList.iterator();
destination = randKeys.next();
}
// does destination warehouse have compatible climate control to the truck temp control?
w = allWarehouses.get(destination);
if (!enforceTempRules || truck.canDeliverToWarehouse(w))
break;
// no good so keep looking
else destination = null;
}
trucksAtWarehouses.put(truckKey, destination);
if (verbose) System.out.println(time + " Truck " + truckKey + " temp cat=" + truck.tempRange + " moving from " + currentLoc + " to " + destination + " with temp cat=" + w.tempRange);
}
// 4 SENSOR stream, simple version, each warehouse and truck produce only out one value per sensor metric per location per hour
// This is a very inefficient implementation as it checks rules for all goods in each truck every sensor event
Sensor sensor;
// Truck SENSOR stream
for (String truckskey : allTrucks.keySet())
{
Trucks truck = allTrucks.get(truckskey);
sensor = new Sensor(time, "SENSOR TRUCK", truckskey, "temp", truck.temp.randomTempInRange());
if (verbose) sensor.print();
checkGoodsInTruck(truckskey, sensor);
sensor = new Sensor(time, "SENSOR TRUCK", truckskey, "humidity", randBetween(0, 100));
if (verbose) sensor.print();
checkGoodsInTruck(truckskey, sensor);
// lux https://en.wikipedia.org/wiki/Lux range 0 - 100,000 (direct sunlight), 500 is office lighting, unit is lux
sensor = new Sensor(time, "SENSOR TRUCK", truckskey, "illuminance", randBetween(0, 100000));
if (verbose) sensor.print();
checkGoodsInTruck(truckskey, sensor);
// acceleration, in standard gravities i.e. 0, 1, 100? Normal should be < 1g? fast car accel if about 0.5g
// roller coaster is 3-4g
// car https://physics.info/acceleration/ F1 could be up to 3g! A truck should be < 1g
sensor = new Sensor(time, "SENSOR TRUCK", truckskey, "acceleration", randBetween(0, 100));
if (verbose) sensor.print();
checkGoodsInTruck(truckskey, sensor);
// vibration has amplitude and frequency (but sensors produce data for multiple frequencies!)
// freq is Hz (0-100000), amp is ms-2 (0-?)
sensor = new Sensor(time, "SENSOR TRUCK", truckskey, "vibrationDisplacement", randBetween(0, 1000));
if (verbose) sensor.print();
checkGoodsInTruck(truckskey, sensor);
sensor = new Sensor(time, "SENSOR TRUCK", truckskey, "vibrationVelocity", randBetween(0, 1000));
if (verbose) sensor.print();
checkGoodsInTruck(truckskey, sensor);
totalEvents += 6;
}
// Warehouse SENSOR stream
for (String warehouseKey : allWarehouses.keySet())
{
Warehouses warehouse = allWarehouses.get(warehouseKey);
sensor = new Sensor(time, "SENSOR WAREHOUSE", warehouseKey, "temp", warehouse.temp.randomTempInRange());
if (verbose) sensor.print();
checkGoodsInWarehouse(warehouseKey, sensor);
sensor = new Sensor(time, "SENSOR WAREHOUSE", warehouseKey, "humidity", randBetween(0, 100));
if (verbose) sensor.print();
checkGoodsInWarehouse(warehouseKey, sensor);
sensor = new Sensor(time, "SENSOR WAREHOUSE", warehouseKey, "illuminance", randBetween(0, 100000));
if (verbose) sensor.print();
checkGoodsInWarehouse(warehouseKey, sensor);
// Nasty gases: ozone, particulate matter, toxic gas (Propane, Butane, LPG and Carbon Monoxide.), sulfur dioxide, and nitrous oxide
sensor = new Sensor(time, "SENSOR WAREHOUSE", warehouseKey, "ozone", randBetween(0, 10000));
if (verbose) sensor.print();
checkGoodsInWarehouse(warehouseKey, sensor);
sensor = new Sensor(time, "SENSOR WAREHOUSE", warehouseKey, "particles", randBetween(0, 10000));
if (verbose) sensor.print();
checkGoodsInWarehouse(warehouseKey, sensor);
sensor = new Sensor(time, "SENSOR WAREHOUSE", warehouseKey, "toxicGas", randBetween(0, 10000));
if (verbose) sensor.print();
checkGoodsInWarehouse(warehouseKey, sensor);
sensor = new Sensor(time, "SENSOR WAREHOUSE", warehouseKey, "sulfurDioxide", randBetween(0, 10));
if (verbose) sensor.print();
checkGoodsInWarehouse(warehouseKey, sensor);
sensor = new Sensor(time, "SENSOR WAREHOUSE", warehouseKey, "nitrousOxides", randBetween(0, 10));
if (verbose) sensor.print();
checkGoodsInWarehouse(warehouseKey, sensor);
totalEvents += 8;
}
}
System.out.println("Simulation ended");
long t1 = System.currentTimeMillis();
double duration = (t1 - t0)/1000.0;
System.out.println("Simulation duration (s) = " + duration);
double eventsSec = totalEvents/duration;
System.out.println("Events = " + totalEvents + ". Rate (Events/s) = " + eventsSec);
}
// for every goods in this truck check all sensor rules against this new sensor value and print warning
private static void checkGoodsInTruck(String truckskey, Sensor sensor)
{
if (!checkGoods)
return;
for (String goodsKey: goodsInTrucks.keySet())
{
// which truck is this goods in?
String truck = goodsInTrucks.get(goodsKey);
// same location as sensor data?
if (truck.equals(truckskey))
{
Goods goods = allGoods.get(goodsKey);
String v = goods.violatedSensorCatRules(sensor);
if (!v.equals(""))
System.out.println("SENSOR RULE VIOLATION for goods=" + goodsKey + " " + goods.allCategories() + " in truck " + truckskey + " violations: " + v);
}
}
}
// for every goods in warehouse check all sensor rules against new sensor value and print warning
private static void checkGoodsInWarehouse(String warehousekey, Sensor sensor)
{
if (!checkGoods)
return;
for (String goodsKey: goodsInWarehouses.keySet())
{
// which warehouse is this goods in?
String warehouse = goodsInWarehouses.get(goodsKey);
// same location as sensor data?
if (warehouse.equals(warehousekey))
{
Goods goods = allGoods.get(goodsKey);
String v = goods.violatedSensorCatRules(sensor);
if (!v.equals(""))
System.out.println("SENSOR RULE VIOLATION for goods=" + goodsKey + " " + goods.allCategories() + " in warehouse " + warehousekey + " violations: " + v);
}
}
}
public static double randBetween(double min, double max)
{
return (rand.nextDouble() * (max-min)) + min;
}
public static void main(String[] args)
{
// Parameters, how many Goods, warehouse locations and trucks, and hours to run simulation.
int numGoods = 1000;
int maxX = 10;
int maxY = 10;
int numWarehouses = maxX * maxY;
int numTrucks = numWarehouses*2;
int loops = 10;
// CREATION
// create random Goods in a hashMap
for (int i = 0; i < numGoods; i++)
{
Goods g = new Goods();
allGoods.put(g.tag, g);
String s = g.toStr();
System.out.println(s);
}
System.out.println("Goods created = " + numGoods);
// create warehouses
String aWarehouse = null;
for (int x = 0; x < maxX; x++)
{
for (int y = 0; y < maxY; y++)
{
Warehouses w = new Warehouses(x, y);
if (aWarehouse == null)
aWarehouse = w.id;
allWarehouses.put(w.id, w);
String s = w.toStr();
System.out.println(s);
}
}
System.out.println("Warehouses created = " + maxX*maxY);
// find warehouses with compatible environmental controls to put Goods in
for (String goodskey : allGoods.keySet())
{
Goods g = allGoods.get(goodskey);
boolean found = false;
for (String warehouseKey: allWarehouses.keySet())
{
Warehouses w = allWarehouses.get(warehouseKey);
if (g.warehouseTempRules(w))
{
goodsInWarehouses.put(goodskey, warehouseKey);
found = true;
break;
}
}
// can't find anywhere just put goods in 1st warehouse
if (!found) goodsInWarehouses.put(goodskey, aWarehouse);
}
for (Map.Entry<String, String> entry : goodsInWarehouses.entrySet())
{
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + " in " + value);
}
System.out.println("Goods locations created");
// Create Trucks to move Goods around
for (int i = 0; i < numTrucks; i++)
{
Trucks t = new Trucks();
t.resetCats();
allTrucks.put(t.id, t);
String s = t.toStr();
System.out.println(s);
}
System.out.println("Trucks created");
// set Trucks locations to warehouses
Iterator it = allWarehouses.keySet().iterator();
String warehousekey;
for (String truckskey : allTrucks.keySet())
{
if (it.hasNext())
warehousekey = (String) it.next();
else
warehousekey = aWarehouse; // else use first warehouse
trucksAtWarehouses.put(truckskey, warehousekey);
}
for (Map.Entry<String, String> entry : trucksAtWarehouses.entrySet())
{
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + " at " + value);
}
System.out.println("Truck locations created");
// Run the simulation for loops hours
loop(loops);
}
}