-
Notifications
You must be signed in to change notification settings - Fork 6
/
Social_Distancing_Simulations.R
69 lines (56 loc) · 2.27 KB
/
Social_Distancing_Simulations.R
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
library('doParallel')
args <- commandArgs(trailingOnly = TRUE)
cat(args,"\n")
perform_room_simulation <- function(x_length,y_length,num_people){
#given dimensions of the room, and number of people return all pairwise distances
x=runif(num_people, min = 0, max = x_length)
y=runif(num_people, min = 0, max = y_length)
df=t(rbind(x,y))
df_dist=dist(df)
#return(df_dist)
to_return=as.list(df_dist)
to_return=lapply(to_return,round,5)
return(to_return)
}
start.time <- Sys.time()
x_length=as.integer(args[2])
y_length=as.integer(args[3])
num_people=as.integer(args[4])
max_iterations=as.integer(args[5])
#are we running a single core simulation, or run multicore? If local simulation, run single core
#if we are using SageMaker Processing, using multicore
is_local=as.integer(args[6])
num_to_parallelize <- detectCores(all.tests = FALSE, logical = TRUE) #find out how many cores are on the machine
if (is_local==1){
num_to_parallelize=1
}
cl <- makeCluster(num_to_parallelize)
registerDoParallel(cl)
cat("x_length: ", x_length, "\n")
cat("y_length: ", y_length, "\n")
cat("num_people: ", num_people, "\n")
cat("max_iterations: ", max_iterations, "\n")
cat("Running in parallel with ", num_to_parallelize, "cores.\n")
start.time <- Sys.time()
result <- foreach (i =1:max_iterations) %dopar%
{
mini_result=perform_room_simulation(x_length=x_length,y_length=y_length,num_people=num_people)
mini_result=unlist(mini_result)
mini_result_pre=mini_result
num_violates=length(mini_result[mini_result <6]) #number of violations of social distancing
average_violations_per_person=num_violates/num_people
average_violations_per_person = average_violations_per_person *2
}
result=unlist(result)
end.time <- Sys.time()
time.taken <- end.time - start.time
time_taken=time.taken
print(time_taken)
#cat("Finished simulation in ",time_taken," seconds.\n")
cat ("Simulation Mean: ",mean(result),"\n")
cat("Simulation Standard Deviation: ", sd(result), "\n")
#Write out the results to S3 if using SageMaker processing
if (is_local !=1){
write(as.character(mean(result)), file = "/opt/ml/processing/output/output_result_mean.txt", append = TRUE)
}
#write(sd(result), file = "/opt/ml/processing/output/output_result_sd.txt", append = TRUE)