-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimal_adaption.R
174 lines (154 loc) · 4.26 KB
/
optimal_adaption.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
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
#
# Author: Luis F. Chiroque
# e-mail: [email protected]
# IMDEA Networks Institute
#
library(zoo) # na.locf()
library(Rcpp) # sourceCpp()
sourceCpp("memoization_optimal.cpp")
min.err <- function(env, pos, i, m) {
#print(paste0("pos=", pos, " i=", i, " m=", m))
if ( pos > env$N )
return(0)
if ( is.na(env$e[pos,i,m]) ) {
env$e[pos,i,m] <- env$distFun(env$metric[pos], env$metric[i]) + min.err(env, pos+1, i, m)
if( m < env$M ) {
alt <- min.err(env, pos+1, pos, m+1)
if ( env$e[pos,i,m] > alt ) {
env$e[pos,i,m] <- alt
env$obs[pos,i,m] <- TRUE
}
}
}
return(env$e[pos,i,m])
}
opt_obs <- function(env, pos, i, m) {
if ( pos > env$N )
return()
if ( env$obs[pos,i,m] ) {
env$opt.points[pos] <- TRUE
opt_obs(env, pos+1, pos, m+1)
} else {
opt_obs(env, pos+1, i, m)
}
}
getOptimalObsPoints.env <- function(obs.ratio, metric) {
env <- new.env(hash = FALSE)
env$metric <- metric
env$distFun <- function(a, b) abs(a-b)
env$N <- length(metric)
env$M <- floor(N * obs.ratio)
edim <- c(env$N, env$N - 1, env$M)
env$e <- rep(NA, prod(edim))
dim(env$e) <- edim
env$obs <- rep(FALSE, prod(edim))
dim(env$obs) <- edim
opt.err <- min.err(env, 2, 1, 1)
env$opt.points <- rep(FALSE, env$N)
env$opt.points[1] <- TRUE
opt_obs(env, 2, 1, 1)
which(env$opt.points) # RETURN
}
getOptimalObsPoints <- function(obs.ratio, metric) {
min.err <- function(pos, i, m, distFun = function(a, b) abs(a-b)) {
#print(paste0("pos=", pos, " i=", i, " m=", m))
if ( pos > N )
return(0)
cur <- e[pos,i,m]
if ( is.na(cur) ) {
cur <- distFun(metric[pos], metric[i]) + min.err(pos+1, i, m, distFun)
if( m < M ) {
alt <- min.err(pos+1, pos, m+1, distFun)
if ( cur > alt ) {
cur <- alt
obs[pos,i,m] <<- TRUE
}
}
e[pos,i,m] <<- cur
}
return(cur)
}
opt_obs <- function(pos, i, m) {
if ( pos > N )
return()
if ( obs[pos,i,m] ) {
opt.points[pos] <<- TRUE
opt_obs(pos+1, pos, m+1)
} else {
opt_obs(pos+1, i, m)
}
}
N <- length(metric)
M <- floor(N * obs.ratio)
edim <- c(N, N - 1, M)
if (prod(edim) > 1e8)
return(1)
e <- rep(NA, prod(edim))
dim(e) <- edim
obs <- rep(FALSE, prod(edim))
dim(obs) <- edim
opt.err <- min.err(2, 1, 1) #OPTIONAL: distFun()
opt.points <- rep(FALSE, N)
opt.points[1] <- TRUE
opt_obs(2, 1, 1)
which(opt.points) # RETURN
}
getOptimalObsPoints.rcpp <- function(obs.ratio, metric) {
N <- length(metric)
M <- floor(N * obs.ratio)
opt.points <- computeOptimalObservations(metric, N, M, 2, 1, 1)
which(opt.points) # RETURN
}
getOptimalObsMetric <- function(obs.ratio, metric, plot=FALSE) {
N <- length(metric)
## obtain optimal points
#x.seq <- getOptimalObsPoints(obs.ratio, metric)
x.seq <- getOptimalObsPoints.rcpp(obs.ratio, metric)
## obtain optimal observed metric
metric.obs <- rep(NA, N)
metric.obs[x.seq] <- metric[x.seq]
metric.obs <- na.locf(metric.obs)
if ( plot ) {
plot(metric, type="l")
lines(metric.obs, type="s", lty=2)
points(x.seq, metric[x.seq], pch=2)
}
metric.obs # RETURN
}
getEquidObsMetric <- function(obs.ratio, metric, plot=FALSE) {
N <- length(metric)
M <- round(N * obs.ratio)
## obtain equid points
x.seq <- c(1, unique(round(1:(M-1) * 1/obs.ratio)) + 1)
## obtain optimal observed metric
metric.obs <- rep(NA, N)
metric.obs[x.seq] <- metric[x.seq]
metric.obs <- na.locf(metric.obs)
if ( plot ) {
plot(metric, type="l")
lines(metric.obs, type="s", lty=2)
points(x.seq, metric[x.seq], pch=2)
}
metric.obs # RETURN
}
getRandObsMetric <- function(obs.ratio, metric, plot=FALSE) {
N <- length(metric)
M <- round(N * obs.ratio)
## obtain equid points
x.seq <- c(1, sort(sample(2:N, M-1)))
## obtain optimal observed metric
metric.obs <- rep(NA, N)
metric.obs[x.seq] <- metric[x.seq]
metric.obs <- na.locf(metric.obs)
if ( plot ) {
plot(metric, type="l")
lines(metric.obs, type="s", lty=2)
points(x.seq, metric[x.seq], pch=2)
}
metric.obs # RETURN
}
## SMAPE: error formula
#sum(abs(metric - metric.obs)) / sum(metric + metric.obs)
errFun <- function(metric, metric.obs) {
sum(abs(metric - metric.obs)) / sum(metric + metric.obs)
}