-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbandit_continuum_offon_kern.R
52 lines (52 loc) · 1.62 KB
/
bandit_continuum_offon_kern.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
#' @export
OnlineOfflineContinuumBanditKernel <- R6::R6Class(
inherit = Bandit,
class = FALSE,
public = list(
class_name = "OnlineOfflineContinuumBanditKernel",
arm_function = NULL,
choice = NULL,
h = NULL,
kernel = NULL,
horizon = NULL,
max_bool = FALSE,
maxval = NULL,
S = NULL,
n = NULL,
initialize = function(FUN, max_bool, horizon) {
self$arm_function <- FUN
self$k <- 1
self$horizon <- horizon
self$h <- horizon^(-1/5)
self$kernel <- function(action_true, action_choice, bandwith){ 1/sqrt(2*pi)*exp(((action_choice - action_true) / bandwith)^2/2) }
self$max_bool <- max_bool
},
post_initialization = function() {
self$choice <- runif(self$horizon, min=0, max=1)
temp_data <- self$arm_function(self$choice)
if(self$max_bool == TRUE){
self$S <- data.frame(self$choice, temp_data$data)
self$maxval <- temp_data$max
} else {
self$S <- data.frame(self$choice, temp_data)
}
self$S <- self$S[sample(nrow(self$S)),]
colnames(self$S) <- c('choice', 'reward')
self$n <- 0
},
get_context = function(index) {
context <- list()
context$k <- self$k
context
},
get_reward = function(index, context, action) {
reward_at_index <- as.double(self$S$reward[[index]])
temp_u <- (action$choice - self$S$choice[[index]]) / self$h
kern_value <- 1/sqrt(2*pi) * exp(-temp_u^2 / 2)
reward <- list(
reward = (kern_value * reward_at_index),
optimal_reward = ifelse(self$max_bool, self$maxval, NA)
)
}
)
)