-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGetPredictions.R
108 lines (81 loc) · 4.33 KB
/
GetPredictions.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
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
library(TrafficBDE) #The prediction package (https://github.com/ptzenos/TrafficBDE)
library(lubridate) #allow usage of floor_date, ceiling_date
GetPredictions <- function(executor_number, total_executors){
df = data.frame(matrix(vector(), 0, 6, dimnames=list(c(), c("Link_id", "Direction","Exec_Timestamp","Step","Pred_Timestamp","Pred"))),stringsAsFactors=F)
#get traindata
print("Getting OSM links...")
OSM_Links <- data.table::fread("data/OSM_Main_Links_small.csv")
OSM_Links <- as.data.frame(OSM_Links[,])
print("OK. The number of links is:")
print(nrow(OSM_Links))
print("Fetching the latest complete train dataset...")
Data<-try(as.data.frame(read.table("http://feed.opendata.imet.gr:23577/fcd/traindata_complete.csv?offset=0&limit=-1", header = TRUE, sep = ";"),silent = TRUE))
print("OK. The number of rows in this dataset was:")
print(nrow(Data))
#set next dt target
next_dt <- as.POSIXct(Data[which.max(as.POSIXct(Data$Date)),3]) + 3600
print("Calculating predictions for:")
print(next_dt)
#insert the same execution timestamp for all links
exec_tstamp <- floor_date(Sys.time(),unit="15 minutes")
#Produce a prediction for every link
i <- 0;
from_number <- (((nrow(OSM_Links)/total_executors)*(executor_number - 1)) + 1)
to_number <- (((nrow(OSM_Links)/total_executors)*(executor_number - 1)) + (nrow(OSM_Links)/total_executors))
print(paste("This executor will handle links from: ",from_number," to ", to_number,sep=""))
for (i in from_number:to_number){
print(paste("Iteration: ",i," of ",to_number,sep="" ))
pred <- try(kStepsForward(Data = Data, Link_id = OSM_Links[i,1], direction = OSM_Links[i,2], datetime = next_dt, predict = "Mean_speed", steps = 4),silent=TRUE)
if (class(pred) == "try-warning" || class(pred) == "try-error") {
#smth went wrong, continue
print("Training failed")
}
else{
#We got some results. Set exec_timestamp for all predictions produced in the current run
#Comment the following line to include the actual exec_timestamp for each link in the returned dataset
print("Training successful")
pred[,3] = as.character(exec_tstamp)
df <- rbind(df,pred)
}
i <- i+1;
}
tt=strftime(Sys.time(), "%Y%m%d%H%M%S");
write.csv(df, file = paste("output/output_all",tt,".csv",sep=""));
return(df);
}
GetPredictionOneLink <- function(Link_id,Direction){
df = data.frame(matrix(vector(), 0, 6, dimnames=list(c(), c("Link_id", "Direction","Exec_Timestamp","Step","Pred_Timestamp","Pred"))),stringsAsFactors=F)
#get traindata
print("Fetching the latest complete train dataset...")
Data<-try(as.data.frame(read.table("http://feed.opendata.imet.gr:23577/fcd/traindata_complete.csv?offset=0&limit=-1", header = TRUE, sep = ";"),silent = TRUE))
print("OK. The number of rows in this dataset was:")
print(nrow(Data))
#set next dt target
next_dt <- as.POSIXct(Data[which.max(as.POSIXct(Data$Date)),3]) + 3600
print("Calculating predictions for:")
print(next_dt)
#insert the same execution timestamp for all links
exec_tstamp <- floor_date(Sys.time(),unit="15 minutes")
#Produce a prediction for every link
i <- 0;
pred <- try(kStepsForward(Data = Data, Link_id = Link_id, direction = Direction, datetime = next_dt, predict = "Mean_speed", steps = 4),silent=TRUE)
if (class(pred) == "try-warning" || class(pred) == "try-error") {
#smth went wrong, continue
print("Training failed")
}
else{
#We got some results. Set exec_timestamp for all predictions produced in the current run
#Comment the following line to include the actual exec_timestamp for each link in the returned dataset
print("Training successful")
pred[,3] = as.character(exec_tstamp)
df <- rbind(df,pred)
tt=strftime(Sys.time(), "%Y%m%d%H%M%S");
write.csv(df, file = paste("output/output_",Link_id,"_",Direction,"_",tt,".csv",sep=""));
}
return(df)
}
### TEST CALLS BELOW ###
#exec one instance for all links in the file /data/OSM_Main_Links_small.csv (testing). To add new links and directions, fill in the corresponding link_id and Direction and just set zero to all other fields.
GetPredictions(1,1);
#exec one instance for a specific link_id, direction (testing Ιωάννη Τσιμισκή (primary) - Ioanni Tsimiski)
GetPredictionOneLink(176665188,1);