-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_fstatic.R
More file actions
2461 lines (2247 loc) · 101 KB
/
create_fstatic.R
File metadata and controls
2461 lines (2247 loc) · 101 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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
rm(list = ls()) # Clear variables from global environment before proceeding.
setwd("C:/Temp/Simulator")
############################################
## R script to create room files for the agent based simulator used in King et
## al. 2022 https://doi.org/10.1080/23249935.2021.2017510.
## Currently, environments are created manually by the user.
## Each function presented here creates a given environmental layout.
## The environments used in King et al. 2022 are created using the 'empty_room'
## and 'err_stud_room2' functions.
############################################
## Supplementary function used in assigning floor field values:
get.nns <- function(x, y, ni, nj, obstacles) {
### Finds the nearest (direct) neighbours to position (x,y) in grid that
### aren't obstacles or haven't already been assigned a floor field value.
### Requires:
### 1. x-coordinate of grid cell to find nearest neighbours.
### 2. y-coordinate of grid cell to find nearest neighbours.
### 3. The x-dimension of the grid.
### 4. The y-dimension of the grid.
### 5. The obstacle positions in the grid.
### Returns a matrix of the x- and y- coordinates of the nearest neighbour
### cells of (x,y) that aren't obstacles or already assigned a floor field.
# Coordinates of nearest neighbours of position (x,y).
xp <- c(-1, 0, 1, 0) + x
yp <- c(0, 1, 0, -1) + y
del <- vector() # This will remove coordinates that are invalid.
# For each element in the xp and yp vectors...
for (i in 1:length(xp)) {
#...if any nearest neighbour lies outside of the grid...
if (xp[i] > ni || xp[i] < 1 || yp[i] > nj || yp[i] < 1) {
# ...append index to del vector for removal.
del <- c(del, i)
} else { # Otherwise, if the nearest neighbours lie within the grid...
#...if the floor field at the nearest neighbours is an obstacle or already
# assigned...
if (obstacles[xp[i], yp[i]] >= 0) {
# ...append index to del vector for removal.
del <- c(del, i)
}
}
}
# If there are any nearest neighbours that need to be removed...
if (length(del) > 0) {
# ...remove them!
xp <- xp[-del]
yp <- yp[-del]
}
return(rbind(xp, yp))
}
## Supplementary function used in assigning floor field values:
get.diag <- function(x, y, ni, nj, obstacles) {
### Finds the distance for unassigned diagonal nearest neighbours to
### position (x,y) in grid, assigns a temporary distance value. This is then
### compared any current distance values assigned to the cell from this and/or
### previous iterations.
### Takes the following inputs:
### 1. x-coordinate of grid cell to find nearest neighbours.
### 2. y-coordinate of grid cell to find nearest neighbours.
### 3. The x-dimension of the grid.
### 4. The y-dimension of the grid.
### 5. The obstacle positions in the grid.
### Outputs the minimum distances of the diagonal nearest neighbour cells of (x,y)
### that aren't obstacles or lie outside of the grid.
# Coordinates of diagonal nearest neighbours of position (x,y).
xp <- c(-1, 1, 1, -1) + x
yp <- c(-1, 1, -1, 1) + y
del <- vector() # This will remove coordinates that are invalid.
# For each element in the xp and yp vectors...
for (i in 1:length(xp)) {
# If any nearest neighbour lies outside of the grid...
if (xp[i] > ni || xp[i] < 1 || yp[i] > nj || yp[i] < 1) {
# ...append index to del vector for removal.
del <- c(del, i)
} else { # Otherwise, if the nearest neighbour lies within the grid...
#...if floor field at nearest neighbour has not already been assigned...
if (obstacles[xp[i], yp[i]] <= 0) {
# ...append index to del vector for removal.
del <- c(del, i)
}
}
}
# If any of the diagonals has not been assigned a value...
if (length(del) < 4) { # There can only be up to four diagonal nearest neighbours.
if (length(del) > 0) {
#...remove them before assigning the minimum floor field in the
# following loop.
xp <- xp[-del]
yp <- yp[-del]
}
# Create a vector of distance values for the remaining assigned neighbours.
out <- rep(0, length(xp))
# For each assigned neighbour...
for (i in 1:length(xp)) {
#...find their current distance value.
out[i] <- obstacles[xp[i], yp[i]]
}
# Find the smallest floor field value.
temp <- min(out)
}
# Otherwise, if all the diagonal neighbours have had distance values assigned
# from previous iterations...
else {
#...return a ludicrously large value for create.static.
temp <- ni * nj
}
return(temp)
}
av_field_calc <- function(field, zone = 3) {
### This function calculates the average distance value of a cell over its
### nearest neighbourhood of a given size. Used to find the average distance
### so that obstacles have lower floor fields in their vicinity, making
### slightly more realistic movement of agents around obstacles.
### Requires the following inputs:
### 1. A matrix of values where each element will be averaged.
### 2. An integer specifying the size of the neighbourhood to perform averaging.
### Returns a matrix with the same dimensions as the input matrix with averaged
### values.
# Set the sizes of the grid to be averaged.
ni <- nrow(field)
nj <- ncol(field)
# Set the values of all neighbouring cells, including the central one.
abuts <- seq(-zone, zone)
# Initialise the matrix of averaged values.
out2 <- matrix(nrow = ni, ncol = nj)
# Initialise the vector of x-coordinates for all abutting cells.
xp <- numeric()
## Now set the (x, y) coordinates for all neighbouring cells.
# For each neighbour...
for (e in abuts) {
#...set the x-values.
z <- rep(e, times = length(abuts))
xp <- c(xp, z)
}
# Set the y-coordinates of all abuts.
yp <- rep(abuts, times = length(abuts))
## Now fill in the values of the averaged matrix.
# The first and last (neighbourhood size) rows and columns are set to 0, due
# to their being bounding walls around the environment of a given thickness
# which have zero floor field.
out2[1:zone,] <- field[1:zone,]
out2[, 1:zone] <- field[, 1:zone]
out2[, (nj - (zone - 1)):nj] <- field[, (nj - (zone - 1)):nj]
out2[(ni - (zone - 1)):ni,] <- field[(ni - (zone - 1)):ni,]
# For each non-zero row...
for (k in (zone + 1):(ni - zone)) {
#...for each non-zero column...
for (l in (zone + 1):(nj - zone)) {
#...collect up the values of every neighbour...
vals <- field[k + xp, l + yp]
#...and average them.
out2[k, l] <- mean(vals, na.rm = T)
}
}
return(out2)
}
max_field_calc <- function(field, zone = 3) {
### This function find the maximum floor field value of a cell over its nearest
### neighbourhood of a given size. Used to produce slightly more realistic
### movement of agents. Requires the following inputs:
### 1. A matrix of values to find the local maximum floor field.
### 2. An integer specifying the size of the neighbourhood to search for the
### maximum.
### Returns a matrix with the same dimensions as the input matrix with local
### maximum values.
# Set the sizes of the grid to be searched.
ni <- nrow(field)
nj <- ncol(field)
# Set the values of all neighbouring cells, including the central one.
abuts <- seq(-zone, zone)
# Initialise the matrix of maximal values.
out2 <- matrix(nrow = ni, ncol = nj)
# Initialise the vector of x-coordinates for all abutting cells.
xp <- numeric()
## Now set the (x, y) coordinates for all neighbouring cells.
# For each neighbour...
for (e in abuts) {
#...set the x-values.
z <- rep(e, times = length(abuts))
xp <- c(xp, z)
}
# Set the y-coordinates of all abuts.
yp <- rep(abuts, times = length(abuts))
## Now fill in the values of the maximal matrix.
# The first and last (neighbourhood size) rows and columns are set to 0, due
# to their being bounding walls around the environment of a given thickness
# which have zero floor field.
out2[1:zone,] <- field[1:zone,]
out2[, 1:zone] <- field[, 1:zone]
out2[, (nj - (zone - 1)):nj] <- field[, (nj - (zone - 1)):nj]
out2[(ni - (zone - 1)):ni,] <- field[(ni - (zone - 1)):ni,]
# For each non-zero row...
for (k in (zone + 1):(ni - zone)) {
#...for each non-zero column...
for (l in (zone + 1):(nj - zone)) {
#...collect up the values of every neighbour...
vals <- field[k + xp, l + yp]
#...and find the local maximum.
out2[k, l] <- max(vals, na.rm = T)
}
}
return(out2)
}
### Function to impose a static floor field onto an environment with obstacles,
### starting from a defined point.
create.static <- function(obstacles, target = matrix(c(1, 1), ncol = 1, nrow = 2)) {
### Generates the floor field and distance values for the discretised
### space for the microscopic, individual-level pedestrian simulator.
### Requires:
### 1. A matrix representing the simulated space, with 0 in elements
### corresponding to obstacles and 1's otherwise.
### 2. Coordinates for the source of the floor field/destination. Each column
### is the coordinates of one cell in the grid. The source could span more
### than one cell.
### Returns a list with the following matrices:
### 1. The floor field values for each cell of the spatial grid.
### 2. The distance of each grid cell from the source/destination.
# Set the floor field grid.
ni <- dim(obstacles)[1]
nj <- dim(obstacles)[2]
# Make floor field at obstacles = 0 and -1 otherwise.
dists <- matrix(rep(-1, ni * nj), ncol = nj, nrow = ni) * obstacles
# For each cell of the source (usually just one cell, but could be any number
# of cells in any configuration)...
for (i in 1:dim(target)[2]) {
#...set the field at the destination = 1.
dists[target[1, i], target[2, i]] <- 1
}
## Now find the shortest distances of each cell in the grid from the source.
## Starts at the destination cell, finds its nearest neighbours and records
## their grid coordinates. Then assigns them their shortest distances.
## These nearest neighbours then become the cells to find the nearest
## neighbours of in the next iteration, not counting the cells that have
## already been assigned a distance from a previous iteration.
# While there is a cell which has unevaluated neighbours or until every cell
# is assigned a shortest distance...
while (is.matrix(target) && length(as.vector(target)) > 0) {
#...initialise the matrix of the coordinates of nearest neighbour to each
# of the cells from the previous iteration.
# For each cell whose distance was assigned in the previous iteration...
for (i in 1:dim(target)[2]) {
#...find the coordinates of its nearest neighbours.
ass_cells_this_it <- get.nns(x = target[1, i], y = target[2, i], ni = ni,
nj = nj, obstacles = dists)
# If these cells are neighbours to the original source cell...
if (i == 1) {
#...add their coordinates of these cells to generate a considered cells
# matrix.
assigned_cells <- cbind(target, ass_cells_this_it)
}
# Otherwise, if these cells are not neighbours to the original source cell...
else {
#...add the coordinates of these cells to the considered cells matrix.
assigned_cells <- cbind(assigned_cells, ass_cells_this_it)
}
# If there are any cells with unassigned distances in the grid...
if (dim(ass_cells_this_it)[2] > 0) {
#...then for each of these cells...
for (j in 1:dim(ass_cells_this_it)[2]) {
#...calculate the distance from the source cell. If this cell is
# directly adjacent to the source cell, then the minimum distance is
# the distance of the selected cell + 1, if the cell is a diagonal
# neighbour to the selected cell, then the distance will be the
# distance of the selected cell + sqrt(2).
dists[ass_cells_this_it[1, j], ass_cells_this_it[2, j]] <-
min(dists[target[1, i], target[2, i]] + 1,(sqrt(2)) + get.diag(
x = ass_cells_this_it[1, j], y = ass_cells_this_it[2, j], ni = ni,
nj = nj, obstacles = dists
)
)
}
}
assigned_cells <- assigned_cells[, -1] # Remove the original destination cell.
}
# The cells that were selected in this iteration will be used in the next one.
target <- as.matrix(assigned_cells)
}
# This scales the values of the non-zero floor field to values between 0.1 and 1.
out <- dists
# Sort floor field values over entire space from highest to lowest.
lookup <- sort(unique(as.vector(dists)), decreasing = T)
lookup <- lookup[-which(lookup == 0)] # Remove the 0 value.
# Set interval values between 0.1 to 1 to map floor field values.
val <- seq(from = 0.1, to = 1, length = length(lookup))
# For each cell in the grid...
for (i in 1:ni) {
for (j in 1:nj) {
#...if the cell is not an obstacle...
if (dists[i, j] > 0) {
#...then assign it a non-zero floor field value.
out[i, j] <- val[which(lookup == dists[i, j])]
}
}
}
# This assignment blurs out the boundaries of the walls, so they must be put back.
out2 <- out * obstacles
# Returns the floor field and distance values for the given destination.
return(list(out2, dists))
}
oneexit.newnew <- function(size = 100, wt = 3, exitwidth = 10, plot = T,
writefile = F, name = "oneexitnew.txt") {
### Function to create a square room with one exit and a floor field towards this exit.
### updated version of oneexit.new.
### Takes the following inputs:
### 1. Size of the square room.
### 2. The thickness of the walls of the room, in number of matrix elements.
### 3. The width of the exit in matrix elements.
### 4. Whether the colour map of the floor field should be plotted.
### 5. Whether to write the floor field for the room to a file.
### 6. The name of the file to write said floor field, if needed.
# If the exit width is an even number...
if (exitwidth %% 2 == 0) {
# ...increase size of room for later.
size <- size + 1
}
# The total size of the space is 1.5 times the size of the room.
allsize <- round(1.5 * size)
# If the exit width is odd and the total size of the space is even...
if (exitwidth %% 2 != 0 && allsize %% 2 != 1) {
# ...make the size of the space odd.
allsize <- allsize + 1
}
# Initialise the matrix of the space.
grid <- matrix(rep(1, allsize * allsize), ncol = allsize, nrow = allsize)
## Create walls and door:
# For each row/column of wall thickness...
for (i in 1:wt) {
grid[i, ] <- rep(0, allsize) # Top wall.
grid[allsize + 1 - i, ] <- rep(0, allsize) # Bottom wall.
grid[, i] <- rep(0, allsize) # Left wall.
grid[, allsize + 1 - i] <- rep(0, allsize) # Right wall.
grid[, allsize - size + i] <- rep(0, allsize) # Wall at middle of room.
}
# Find the size of the actual room in number of matrix elements.
realroom <- length(which(grid[round(allsize / 2), (allsize - size + 1):(allsize)] > 0))
upto <- (allsize - realroom) / 2
# Set every element above and below the room = 0.
for (i in 1:upto) {
grid[i, ] <- rep(0, allsize)
grid[allsize + 1 - i, ] <- rep(0, allsize)
}
## Build door:
# For an even-width door...
if (exitwidth %% 2 == 0) {
#...set the width of the door.
width <- round(exitwidth / 2)
from <- round(allsize / 2 - width + 1)
upto <- round(allsize / 2 + width)
# If the exit width is wrong...
if (length(from:upto) != exitwidth) {
#...let the user know.
print("you don't get the right exitwidth")
print(length(from:upto))
}
# Place door halfway along the middle wall.
for (i in 1:wt) {
grid[from:upto, allsize - size + i] <- rep(1, length(from:upto))
}
} else { # If the exit width is odd...
#...set the width of the door.
width <- round((exitwidth - 1) / 2)
from <- round(allsize / 2) - width
upto <- round(allsize / 2) + width
# If the exit width is wrong...
if (length(from:upto) != exitwidth) {
#...let the user know.
print("you don't get the right exitwidth")
}
# Place door halfway along the middle wall.
for (i in 1:wt) {
grid[from:upto, allsize - size + i] <- rep(1, length(from:upto))
}
}
## Assign static floor field:
# Set the source of floor field to be at wall behind exit (bottom of plot).
tt <- rbind((allsize / 2):(allsize / 2 + 1), rep(wt + 1, 2))
# Create a static floor field with maximum at destination and the distance
# values of each cell from the destination.
test <- create.static(obstacles = grid, target = tt)
# Overlay the floor field onto the environment layout.
grid <- grid * test
# Plot the floor field if you want.
if (plot) {
image(grid)
}
#exitx <- rep(allsize - size, 4)
# Write the coordinates of the exit.
exitx <- rep(10, 4)
exity <- rep(round(allsize / 2), 4)
# If you want to write the output of this to a file...
if (writefile) {
#...collate all data into a table, including the size of the grid, the
# coordinates of the destination, and the values of the floor field.
out <- t(t(c("oneexit", allsize, exitx, exity, as.vector(grid))))
write.table(out, file = name, row.names = F, col.names = F, quote = F)
}
invisible(grid)
}
observational.routes1 <- function(plot = T, writefile = F, name = "routes1.txt",
shift1 = 0, shift2 = 0) {
### Functions to create a corridor with obstacles.
### Takes the following inputs:
### 1. Whether or not to plot the floor field.
### 2. Whether or not to write the floor field to a file.
### 3. The name of the file to write the floor field to, if applicable.
### 4. The amount to shift the x-position of the first destination cell.
### 5. The amount to shift the x-position of the second destination cell.
# Fix the thickness of the walls and the total size of the space.
wt <- 3
allsize <- 150
# Initialise the matrix of the space.
grid <- matrix(rep(1, allsize * allsize), ncol = allsize, nrow = allsize)
## Create walls:
for (i in 1:wt) {
grid[i, ] <- rep(0, allsize) # Top wall
grid[allsize + 1 - i, ] <- rep(0, allsize) # Bottom wall
grid[, i] <- rep(0, allsize) # Left wall
grid[, allsize + 1 - i] <- rep(0, allsize) # Right wall
}
# The top and bottom 58 rows are blocked out.
for (i in 1:58) {
grid[i, ] <- rep(0, allsize)
if (i < 58) {
grid[allsize + 1 - i, ] <- rep(0, allsize)
}
}
## Build obstacle.
for (i in 1:4) {
grid[(59 + 9 + 1):(93 - 9 - 1), allsize - 130 + i] <-
rep(0, length((58 + 9 + 1):(93 - 9 - 2)))
}
## Assign static floor field:
# Set destination as two cells at middle of space behind corridor exit.
tt <- rbind((allsize / 2):(allsize / 2 + 1), rep(wt + 1, 2))
tt[1, ] <- tt[1, ] + c(shift1, shift2)
# Create a static floor field with maximum at destination and the distance
# values of each cell from the destination.
test <- create.static(obstacles = grid, target = tt)
# Overlay the floor field onto the environment layout.
grid <- grid * test
# Plot the floor field if you want.
if (plot) {
image(grid)
}
# Write the coordinates of the exit.
exitx <- rep(allsize - 140, 4)
exity <- rep(allsize / 2, 4)
# If you want to write the output of this to a file...
if (writefile) {
#...collate all data into a table, including the size of the grid, the
# coordinates of the destination, and the values of the floor field.
out <- t(t(c("oneexit", allsize, exitx, exity, as.vector(grid))))
write.table(out, file = name, row.names = F, col.names = F, quote = F)
}
invisible(grid)
}
observational.routes2 <- function(gridsize, doorwidth,
wt = round(0.02 * gridsize), plot = T,
writefile = F,
name = paste0("Rooms/Jun et al_room_", gridsize, ".txt")) {
### Function to create a corridor with obstacles
### Takes the following inputs:
### 1. The size of the spatial grid (currently in 10s of centimetres).
### 2. The width of the door.
### 3. The wall thickness in number of grid cells.
### 4. Whether the colour map of the floor field should be plotted.
### 5. Whether to write the floor field for the room to a file.
### 6. The name of the file to write said floor field, if needed.
# Initialise the matrix of the space.
grid <- matrix(rep(1, gridsize * gridsize), ncol = gridsize, nrow = gridsize)
## Create walls and door:
for (i in 1:wt) {
grid[i, ] <- rep(0, gridsize)
grid[gridsize + 1 - i, ] <- rep(0, gridsize)
grid[, i] <- rep(0, gridsize)
grid[, gridsize + 1 - i] <- rep(0, gridsize)
}
## Build wall and door:
for (i in 1:wt) {
doorwall <- c((wt):((gridsize / 2) - (doorwidth / 2)),
((gridsize / 2) + (doorwidth / 2)):(gridsize - i)) # wall
grid[doorwall, (gridsize - round((13/15)*gridsize) + i)] <-
rep(0, length(doorwall)) # door
}
# Set the length of the obstacles.
obst1len <- 30
obst2len <- 50
## Build obstacles:
for (i in 1:4) {
obst1 <- c(round(3/8 * gridsize) - round(obst1len / 2)):(round(3/8 * gridsize) +
round(obst1len / 2))
grid[round(3/8 * gridsize) + i, obst1] <- rep(0, length(obst1))
grid[round(5/8 * gridsize) + i, obst1] <- rep(0, length(obst1))
obst2 <- (round(1/2 * gridsize) - (obst2len / 2)):(round(1/2 * gridsize) + (obst2len / 2))
grid[obst2, (3/4 * gridsize) + i] <- rep(0, length(obst2))
}
## Assign static floor field:
# Set the coordinates of the destinations.
unordered_targets <- matrix(c(round(gridsize / 2), round(gridsize / 15), round(gridsize / 2),
round(11/12 * gridsize), (round(3/8 * gridsize) - 5), round(3/8 * gridsize),
(round(5/8 * gridsize) + wt + 8), round(3/8 * gridsize)), nrow = 2,
ncol = 4)
#############################################################################
# This next section of code orders a matrix of destination coordinates.
# Sorts the destination coordinates in increasing x-coordinate first.
# Then it sorts, for each unique x-coordinate, the y-coordinates in
# increasing order. So we get a matrix with the first row ordered in
# increasing size and the second row is ordered in increasing order for each
# value in the first row.
# E.g. [1, 3, 2, 5, 3]
# [2, 3, 4, 5, 1]
# becomes:
# [1, 2, 3, 3, 5]
# [2, 4, 1, 3, 5]
# This is done because of a limitation in the subsequent GuiOutput.java when
# painting each destination a different colour.
# Initial unordered matrix.
new <-t(apply(unordered_targets,1,function(x) x[order(unordered_targets[1,])]))
dest_xs <- new[1,] # Collect up the freshly-ordered x-coordinates.
uniq_x <- unique(dest_xs) # Find each unique x-coordinate and...
#...iterate over them.
for (i in 1:length(uniq_x)) {
# Collect the columns of the destination matrix containing the x-coordinate.
temp <- new[, which(dest_xs == uniq_x[i])]
# If this x-coordinate is repeated...
if (dim(as.matrix(temp))[2] > 1) {
#...order the second row of this reduced matrix in increasing order.
temp <-t(apply(temp,1,function(x) x[order(temp[2,])]))
}
# If we've just started, then the sorted matrix is initialised.
if (i == 1) {targets <- temp}
# Otherwise, the sub-matrix is appended to the current matrix.
else {targets <- cbind(targets, temp)}
}
# If we are writing this room to a file...
if (writefile) {
#...write the name of the room first.
write("Jun et al", file = name)
# Then the grid dimensions.
write(gridsize, file = name, append = T)
# Followed by the number of destinations...
write(dim(targets)[2], file = name, append = T)
#...and the sorted destination coordinates.
write.table(t(targets), file = name, append = T, sep = ",", row.names = F,
col.names = F)
}
# For each destination...
for (i in 1:dim(targets)[2]) {
tt <- as.matrix(targets[,i])
#...create a static floor field with maximum at destination and the distance
# values of each cell from the destination.
test <- create.static(obstacles = grid, target = tt)
# Multiply by grid of obstacles to get 0 at obstacles.
field <- test[[1]] * grid
dists <- test[[2]] * grid
# Plot the floor field if you want.
if (plot) {
filled.contour(x = seq(10, (nrow(field) * 10), length.out = nrow(field)),
y = seq(10, (ncol(field) * 10), length.out = ncol(field)),
z = t(field),
color.palette = function(n) hcl.colors(n, "Oslo", rev = TRUE),
xlab = "x",
ylab = "y",
main = paste0("Jun et. al's setup target ", i, " floor field"))
}
# If you want to write the output of this to a file...
if (writefile) {
#...write the floor field of the destination...
write.table(field, file = name, row.names = F, col.names = F, quote = F,
sep = ",", append = T)
#...and the distances of each cell from the destination.
write.table(dists, file = name, row.names = F, col.names = F, quote = F,
sep = ",", append = T)
}
}
invisible(grid)
}
circle <- function(size = 100, wt = 3, plot = T, writefile = F, name = "circletest.txt") {
### A circular room:
### Takes the following inputs:
### 1. Size of the space/dimensions of the spatial grid.
### 2. Thickness of the wall.
### 3. Whether or not to plot the colour map of the floor field.
### 4. Whether or not to write the floor field values to a file.
### 5. The name of the file to write the output to, if applicable.
# Initialise spatial grid. Initial cell values = 0.5.
grid <- matrix(rep(0.5, size * size), ncol = size, nrow = size)
# Set the centre of the circle.
centrex <- size / 2
centrey <- size / 2
rad <- round(size / 2) - wt # circle radius.
# Assign position of each grid cell in metres. Top left cell at (0.5, 0.5).
pos <- seq(from = 0.5, to = size - 0.5, length = size)
## Create walls:
# For each row...
for (i in 1:size) {
#..for each column...
for (j in 1:size) {
#...find distance of cell from centre.
ddd <- sqrt((pos[i] - centrex)^2 + (pos[j] - centrey)^2)
# If cell outside room radius...
if (ddd >= rad) {
#...then it is a wall.
grid[i, j] <- 0
}
}
}
# Plot the floor field if you want.
if (plot) {
image(grid)
}
# Write the coordinates of the destination, for later use in simulator.
exitx <- rep(1, 4)
exity <- rep(1, 4)
# If you want to write the output of this to a file...
if (writefile) {
#...collate all data into a table, including the size of the grid, the
# coordinates of the destination, and the values of the floor field.
out <- t(t(c("circle", size, exitx, exity, as.vector(grid))))
write.table(out, file = name, row.names = F, col.names = F, quote = F)
}
invisible(grid)
}
classroom.new <- function(gridsize = 100, wt = round(0.02 * gridsize),
benchthick = wt,
exitwidth = round(0.15 * gridsize), plot = T,
writefile = F,
name = paste0("Rooms/classroom_", gridsize, ".txt")) {
### Function to create a square room with one destination and some bench-like
### obstacles (inspired by classroom setup).
### Takes the following inputs:
### 1. Size of room (not the environment created).
### 2. Thickness of the walls in spatial cells.
### 3. Thickness of benches.
### 4. Width of exit door in spatial cells.
### 5. Whether or not to plot the floor field.
### 6. Whether or not to write floor field values to a file.
### 7. Name of output file, if applicable.
# Initialise spatial grid. Default cell value = 1.
grid <- matrix(rep(1, gridsize * gridsize), ncol = gridsize, nrow = gridsize)
# Size of room.
size <- round(1/3 * gridsize)
## Create walls:
for (i in 1:wt) {
grid[i, ] <- rep(0, gridsize) # Top wall.
grid[gridsize + 1 - i, ] <- rep(0, gridsize) # Bottom wall.
grid[, i] <- rep(0, gridsize) # Left-most wall.
grid[, gridsize + 1 - i] <- rep(0, gridsize) # Right-most wall.
grid[, gridsize - size + i] <- rep(0, gridsize) # Dividing wall.
}
# Calculate the width of the room, starting from the dividing wall
# and ending at the right-most wall.
lect_width <- length(which(grid[round(gridsize / 2), (gridsize - size + 1):(gridsize)] > 0))
## Build door:
width <- round(exitwidth / 2)
# Where the door starts.
from <- round((gridsize / 2) - width + 1)
# Where the door ends.
upto <- round((gridsize / 2) + width)
# Create door.
for (i in 1:wt) {
grid[from:upto, gridsize - size + i] <- rep(1, length(from:upto))
}
## Build benches:
# Calculate the total number of benches.
bno <- floor(((gridsize - size) - 2 * wt) / (benchthick)) - 3
# Assign bench widths so that there is enough central space for agents to reach door.
upper_bench_width <- gridsize - wt - upto
lower_bench_width <- from - wt
# Index for keeping track of the destination.
dest_ind <- 1
# Y-coordinates for the destinations. Destinations are between the first and
# second benches.
bench_dest_y <- numeric()
# For each bench...
for (i in 1:bno) {
# If this is the first bench...
if (i %% 4 == 1) {
#...block out the row from the start of the first bench to the end of the last.
bench <- (wt + (i + 2) * benchthick + 1):
(wt + (i + 3) * benchthick)
grid[, bench] <- rep(0, gridsize)
# Set the empty space between benches.
grid[from:upto, bench] <- rep(1, length(from:upto))
# Set the destination y-coordinate between the benches.
bench_dest_y[dest_ind] <- wt + (i * benchthick)
dest_ind <- dest_ind + 1
}
}
# Set destination x-coordinates as midway along the benches.
bench_dest_upper_x <- round((gridsize - wt) - (upper_bench_width / 4))
bench_dest_x_lower <- round(lower_bench_width / 4)
# Create vector of destination coordinates with length = number of destinations / 2.
dests <- c(round(gridsize / 2), round(gridsize - (size / 2)),
bench_dest_upper_x, bench_dest_y[1], bench_dest_upper_x,
bench_dest_y[2], bench_dest_upper_x, bench_dest_y[3],
bench_dest_upper_x, bench_dest_y[4], bench_dest_upper_x,
bench_dest_y[5], bench_dest_upper_x, bench_dest_y[6],
bench_dest_upper_x, bench_dest_y[7], bench_dest_x_lower,
bench_dest_y[1], bench_dest_x_lower, bench_dest_y[2],
bench_dest_x_lower, bench_dest_y[3], bench_dest_x_lower,
bench_dest_y[4], bench_dest_x_lower, bench_dest_y[5],
bench_dest_x_lower, bench_dest_y[6], bench_dest_x_lower,
bench_dest_y[7])
#############################################################################
# This next section of code orders a matrix of destination coordinates.
# Sorts the destination coordinates in increasing x-coordinate first.
# Then it sorts, for each unique x-coordinate, the y-coordinates in
# increasing order. So we get a matrix with the first row ordered in
# increasing size and the second row is ordered in increasing order for each
# value in the first row.
# E.g. [1, 3, 2, 5, 3]
# [2, 3, 4, 5, 1]
# becomes:
# [1, 2, 3, 3, 5]
# [2, 4, 1, 3, 5]
# This is done because of a limitation in the subsequent GuiOutput.java when
# painting each destination a different colour.
# Convert to matrix such that row 1 is x-coordinates and second row is y.
unordered_targets <- matrix(dests, nrow = 2,
ncol = length(dests)/2)
# Sort by first row, maintaining corresponding values in row 2.
new <-t(apply(unordered_targets,1,function(x) x[order(unordered_targets[1,])]))
dest_xs <- new[1,] # Collect up the freshly-ordered x-coordinates.
uniq_x <- unique(dest_xs) # Find each unique x-coordinate and...
# ...iterate over them.
for (i in 1:length(uniq_x)) {
# Collect the columns of the destination matrix containing the x-coordinate.
temp <- new[, which(dest_xs == uniq_x[i])]
# If this x-coordinate is repeated...
if (dim(as.matrix(temp))[2] > 1) {
#...order the second row of this reduced matrix in increasing order.
temp <-t(apply(temp,1,function(x) x[order(temp[2,])]))
}
# If we've just started, then the sorted matrix is initialised.
if (i == 1) {targets <- temp}
# Otherwise, the sub-matrix is appended to the current matrix.
else {targets <- cbind(targets, temp)}
}
# If we are writing this room to a file...
if (writefile) {
#...write the name of the room first.
write("classroom", file = name)
# Then the grid dimensions.
write(gridsize, file = name, append = T)
# Followed by the number of destinations...
write(dim(targets)[2], file = name, append = T)
#...and the sorted destination coordinates.
write.table(t(targets), file = name, append = T, sep = ",", row.names = F,
col.names = F)
}
# For each destination...
for (i in 1:dim(targets)[2]) {
tt <- as.matrix(targets[,i])
#...create a static floor field with maximum at destination and the distance
# values of each cell from the destination.
test <- create.static(obstacles = grid, target = tt)
# multiply by grid of obstacles to get 0 at obstacles.
field <- test[[1]] * grid
dists <- test[[2]] * grid
# If you want to see what it looks like...
if (plot) {
filled.contour(x = seq(10, (nrow(field) * 10), length.out = nrow(field)),
y = seq(10, (ncol(field) * 10), length.out = ncol(field)),
z = t(field),
color.palette = function(n) hcl.colors(n, "Oslo", rev = TRUE),
xlab = "x",
ylab = "y",
main = paste0("Classroom setup destination ", i, " floor field"))
}
# If you want to write the output of this to a file...
if (writefile) {
#...write the floor field of the destination...
write.table(field, file = name, row.names = F, col.names = F, quote = F,
sep = ",", append = T)
#...and the distances of each cell from the destination.
write.table(dists, file = name, row.names = F, col.names = F, quote = F,
sep = ",", append = T)
}
}
invisible(grid)
}
officeblock.new <- function(size = 100, wt = 3, exitwidth = 10, plot = T,
writefile = F, name = "officeblocknew.txt") {
### Function to create a simple office block with up to five offices along a
### central corridor with a large room at the bottom. One destination is set
### within this bottom room.
### Takes the following input:
### 1. Size of the room (not the total environment) in units of 10s of centimetres.
### 2. Thickness of the walls in spatial cells.
### 3. Width of exit door in spatial cells.
### 4. Whether or not to plot the floor field.
### 5. Whether or not to write floor field values to file.
### 6. The name of the file to write said floor field, if needed.
# Environment is 1.5x room size.
allsize <- round(1.5 * size)
# A quick check to see if input exit width = output exit width. Not crucial
# to function success.
width <- round(exitwidth / 2)
from <- round(allsize / 2 - width + 1)
upto <- round(allsize / 2 + width)
# If the length of the exit door is not correct...
if (length(from:upto) != exitwidth) {
#...let the user know.
print("you don't get the right exitwidth")
}
# Initialise spatial grid. Default cell value = 1.
grid <- matrix(rep(1, allsize * allsize), ncol = allsize, nrow = allsize)
## Create walls:
for (i in 1:wt) {
grid[i, ] <- rep(0, allsize) # Top wall.
grid[allsize + 1 - i, ] <- rep(0, allsize) # Bottom wall.
grid[, i] <- rep(0, allsize) # Left-most wall.
grid[, allsize + 1 - i] <- rep(0, allsize) # Right-most wall.
grid[, allsize - size + i] <- rep(0, allsize) # Dividing wall.
grid[(from - 1 - i), (allsize - size + 1):allsize] <-
rep(0, length((allsize - size + 1):allsize)) # Bottom left wall
grid[(upto + 1 + i), (allsize - size + 1):allsize] <-
rep(0, length((allsize - size + 1):allsize)) # Bottom right wall
}
## Build central door:
for (i in 1:wt) {
grid[(from):(upto), allsize - size + i] <- rep(1, length((from):(upto)))
}
## Build offices:
# The number of offices on each side of the corridor depend on the size of
# the environment such that only a set number of offices of equal width that
# can fit into the remaining environment space are included.
offno <- floor((size - 2 * wt) / 30)
# Maximum of five offices in the space.
if (offno > 5) {
offno <- 5
}
# Set the width of each office so that they are large enough for agents to
# move around.
offwidth <- floor((size - 2 * wt) / offno)
# For each office...
for (i in 1:offno) {
#...if i is less than the office number...
if (i < offno) {
#...then set the section of each row of the grid that is an office space.
iis <- c(1:(from - 2), (upto + 2):allsize)
# Set the section of each column of the total space that is an office space.
jjs <- (allsize - (i) * offwidth - wt + 1):(allsize - (i) * offwidth)
# First, block out all the space.
grid[iis, jjs] <- matrix(rep(0, length(iis) * length(jjs)),
ncol = wt, nrow = length(iis))
}
# Now build the office space.
jjs <- (allsize - (i - 1) * offwidth - wt + 1 - 0 - exitwidth):
(allsize - (i - 1) * offwidth - wt + 1 - 1)
iis <- c((from - 1 - wt):(from - 2), (upto + 2):(upto + 1 + wt))
grid[iis, jjs] <- matrix(rep(1, length(iis) * length(jjs)),
ncol = length(jjs), nrow = length(iis))
}
## Assign static floor field:
# Assign destination as two cells in middle x-position next to bottom wall.
tt <- rbind((allsize / 2):(allsize / 2 + 1), rep(wt + 1, 2))
# Create a static floor field with maximum at destination and the distance
# values of each cell from the destination.
test <- create.static(obstacles = grid, target = tt)
grid <- test * grid
# Plot the floor field if you want.
if (plot) {
image(grid)
}
# Write the coordinates of the destination, for later use in simulator.
exitx <- rep(allsize - size - round(allsize / 6), 4)
exity <- rep(allsize / 2, 4)
# If you want to write the output of this to a file...
if (writefile) {
#...collate all data into a table, including the size of the grid, the
# coordinates of the destination, and the values of the floor field.
out <- t(t(c("officeblock", allsize, exitx, exity, as.vector(grid))))
write.table(out, file = name, row.names = F, col.names = F, quote = F)
}
invisible(grid)
}
vehicle.new <- function(size = 200, wt = 3, width = 26, exitwidth = 10,
plot = T, writefile = F, name = "vehiclenew.txt") {
### Function to create a public transport container, with a floor field towards
### two exits at either end of the container and some bench-like obstacles:
### Takes the following inputs:
### 1. Size of environment.
### 2. Thickness of walls.
### 3. Width of container.
### 4. Width of the exits.
### 4. Whether or not to plot floor field.
### 5. Whether or not to write floor field values to file.
### 6. Name of output file, if applicable.
### Container positioned in middle of environment.
# Set the start and end points for the container wall.
from <- round(size / 2) - width / 2 + 1
upto <- round(size / 2) + width / 2
# Initialise spatial grid. Default cell value = 1.