-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_model.R
68 lines (55 loc) · 1.98 KB
/
get_model.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
require(data.table)
require(plyr)
require(docopt)
source("src/Utils.R")
source("src/Models.R")
# Get Parameters
"
Usage:
get_model (--adaptive | --markovian) --order <order> --input <input> --output <output> [--cores <num_cores> --minimum_events <events>]
get_model (-h | --help)
Description: A program that construct either Markovian or Adaptive Memory models of a given order
Options:
-h --help Show this screen.
-i <input>, --input <input> Three column input CSV FILE in the form of (agent id, timestamp, location id).
-o <output>, --output <output> Output file where to store the model.
-a, --adaptive Adaptive memory model.
-m, --markovian Markovian model.
-n <order>, --order <order> Order of the model.
-c <num_cores>, --cores <num_cores> Number of CPU cores used to compute the model [default: 1]
--minimum_events <events> Minimum number of evets to consider [default: <order> + 1]
" -> doc
opts <- docopt(doc)
input_filename <- opts$input
output_filename <- opts$output
order <- opts$order
if (!is.na(as.numeric(order))){
order <- as.numeric(order)
}else{
stop("Order must be a number.")
}
if (order < 2 && opts$adaptive) {
stop("Order must be greater than 2 to generate adaptive memory model")
}
num_cores <- opts$cores
if (!is.na(as.numeric(num_cores))){
num_cores <- as.numeric(num_cores)
}else{
message("Cores must be a number.")
return(-1)
}
min_events <- opts$"--minimum_events"
if (!is.na(as.numeric(min_events))){
min_events <- as.numeric(min_events)
}else{
# stop("the number of miniumum events must be a number.")
min_events = order + 1
}
parallel <- ifelse(num_cores > 1, T, F)
if (parallel){
require(doMC)
doMC::registerDoMC(cores = num_cores)
}
model <- ifelse(opts$adaptive, "adaptive", "markov")
edgelist <- compute_model(input_filename, model, order, parallel, min_events)
write.table(edgelist, output_filename, row.names = F, sep = ',')