-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbandit_continuum_offon.R
50 lines (50 loc) · 1.44 KB
/
bandit_continuum_offon.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
#' @export
OnlineOfflineContinuumBandit <- R6::R6Class(
inherit = Bandit,
class = FALSE,
public = list(
class_name = "OnlineOfflineContinuumBandit",
delta = NULL,
horizon = NULL,
choice = NULL,
arm_function = NULL,
max_bool = FALSE,
maxval = NULL,
S = NULL,
initialize = function(FUN, max_bool, delta, horizon) {
self$arm_function <- FUN
self$horizon <- horizon
self$delta <- delta
self$k <- 1
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')
},
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]])
if (abs(self$S$choice[[index]] - action$choice) < self$delta) {
reward <- list(
reward = reward_at_index,
optimal_reward = ifelse(self$max_bool, self$maxval, NA)
)
} else {
NULL
}
}
)
)