From 2eb412222e391c6223d3b6786134b1cfa1a507b9 Mon Sep 17 00:00:00 2001 From: Jules Kruijswijk Date: Wed, 29 Jul 2020 14:03:06 +0200 Subject: [PATCH] Minor reproducibility update --- 1_offline_ranking.R | 17 ---------------- bandit_continuum_offon.R | 8 ++++++-- bandit_continuum_offon_kern.R | 37 ++++++++++++++++++----------------- 3 files changed, 25 insertions(+), 37 deletions(-) diff --git a/1_offline_ranking.R b/1_offline_ranking.R index 4b50464..0451484 100644 --- a/1_offline_ranking.R +++ b/1_offline_ranking.R @@ -130,21 +130,4 @@ for(i in 1:length(deltas)){ p <- plots[[1]] + plots[[2]] + plots[[3]] + plots[[4]] + plots[[5]] + plots[[6]] + plot_layout(guides = "collect") & theme(legend.position = "bottom") ggsave(paste0(f[[1]],"_offline.pdf"), p, device="pdf", width=10, height=7) -#pdf(paste0("offline_",f[[1]],".pdf")) - -#layout(matrix(c(1,2,3,4,5,6,7,7,7), ncol=3, byrow=TRUE), heights=c(3, 3, 1)) -#par(oma=c(4,4,2,2), las=1) -##par(cex=1.05) -#for(hist in histories){ -# plot(hist, type="cumulative", no_par = TRUE, disp="ci", legend=FALSE, use_colors=TRUE, trunc_per_agent = FALSE, xlab = "", ylab = "") -#} -#par(cex=1.05) -#mtext("Time step", 1, 3, outer=TRUE, las = 0) -#mtext("Cumulative regret", 2, 3, outer=TRUE, las = 0) -#par(mar=c(1,1,1,1)) -#plot.new() -#legend(x="center", ncol=4,legend=c("UR","E-First","LiF", "TBL"), fill=c("#F8766D", "#7CAE00", "#00BFC4", "#C77CFF"), title="Legend") -# -#dev.off() } - diff --git a/bandit_continuum_offon.R b/bandit_continuum_offon.R index 0dcbc32..6045ed9 100644 --- a/bandit_continuum_offon.R +++ b/bandit_continuum_offon.R @@ -21,8 +21,12 @@ OnlineOfflineContinuumBandit <- R6::R6Class( post_initialization = function() { self$choice <- runif(self$horizon, min=0, max=1) temp_data <- self$arm_function(self$choice) - self$S <- data.frame(self$choice, temp_data$data) - self$maxval <- temp_data$max + 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') }, diff --git a/bandit_continuum_offon_kern.R b/bandit_continuum_offon_kern.R index 50d2c09..0cfd662 100644 --- a/bandit_continuum_offon_kern.R +++ b/bandit_continuum_offon_kern.R @@ -2,33 +2,37 @@ OnlineOfflineContinuumBanditKernel <- R6::R6Class( inherit = Bandit, class = FALSE, - private = list( - S = NULL, - n = NULL - ), public = list( class_name = "OnlineOfflineContinuumBanditKernel", - delta = NULL, - c1 = NULL, - c2 = NULL, arm_function = NULL, choice = NULL, h = NULL, kernel = NULL, horizon = NULL, - initialize = function(FUN, horizon) { + 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) - private$S <- data.frame(self$choice, self$arm_function(self$choice)) - private$S <- private$S[sample(nrow(private$S)),] - colnames(private$S) <- c('choice', 'reward') - private$n <- 0 + 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() @@ -36,15 +40,12 @@ OnlineOfflineContinuumBanditKernel <- R6::R6Class( context }, get_reward = function(index, context, action) { - reward_at_index <- as.double(private$S$reward[[index]]) - #kern_value <- self$kernel(action_true = private$S$choice[[index]], action_choice = action$choice, bandwith = self$h) - temp_u <- (action$choice - private$S$choice[[index]]) / self$h + 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) - #inc(private$n) <- 1 - #print(paste0("Kernel value: ", kern_value, "action choice: ", action$choice, "true action: ", private$S$choice[[index]], "divy: ", temp_u)) reward <- list( reward = (kern_value * reward_at_index), - optimal_reward = self$c2 + optimal_reward = ifelse(self$max_bool, self$maxval, NA) ) } )