#!/usr/bin/env Rscript

library(dplyr)
library(MASS)
library(stats)

curve_num <- 50

run_code <- function(args){
  if (args[1] == "noncontextual" & args[2] == "DAC"){
    source("noncontextual_ucb.R")
  }
  if (args[1] == "noncontextual" & args[2] == "ETC"){
    source("noncontextual_benchmark_ETC.R")
  }
  if (args[1] == "noncontextual" & args[2] == "eps_Greedy"){
    source("noncontextual_benchmark_eps.R")
  }
  
  if (args[1] == "contextual" & args[2] == "DAC"){
    source("contextual_ucb.R")
  }
  if (args[1] == "contextual" & args[2] == "ETC"){
    source("contextual_benchmark_ETC.R")
  }
  if (args[1] == "contextual" & args[2] == "eps_Greedy"){
    source("contextual_benchmark_eps.R")
  }
  # save file
  
  folder_path <- "./experiment_result"
  
  # Make sure the folder exists
  if (!dir.exists(folder_path)) {
    dir.create(folder_path, recursive = TRUE) # Create folder if it doesn't exist
  }
  
  words <- args
  file_title <- paste0(words, collapse = "")
  file_path <- file.path(folder_path,file_title)
  write.csv(data_plot, file=file_path, row.names = FALSE) # save the data
}

# The first figure (Comparison of Algorithm DAC-Bandit and two benchmark algorithms)

print("Generating the first figure")
args = c("noncontextual", "DAC", "1665")
run_code(args)
print("noncontextual DAC: done")
args = c("noncontextual", "ETC", "1665")
run_code(args)
print("noncontextual ETC: done")
args = c("noncontextual", "eps_Greedy", "1665")
run_code(args)
print("noncontextual eps_Greedy: done")


args = c("noncontextualDAC1665", "noncontextualETC1665", "noncontextualeps_Greedy1665")
source("plot_multiple.R")
print("First figure generated")

# The second figure (Comparison of Algorithm Contextual DAC-Bandit and two benchmark algorithms)

print("Generating the second figure")
args = c("contextual", "DAC")
run_code(args)
print("contextual DAC: done")
args = c("contextual", "ETC")
run_code(args)
print("contextual ETC: done")
args = c("contextual", "eps_Greedy")
run_code(args)
print("contextual eps_Greedy: done")

args = c("contextualDAC", "contextualETC", "contextualeps_Greedy")
source("plot_multiple.R")
print("Second figure generated")


# The third figure (Regret comparison in the multi-category experiment)
print("Generating the third figure")
args = c("noncontextual", "DAC", "topNclick")
run_code(args)
print("noncontextual DAC: done")
args = c("noncontextual", "ETC", "topNclick")
run_code(args)
print("noncontextual ETC: done")
args = c("noncontextual", "eps_Greedy", "topNclick")
run_code(args)
print("noncontextual eps_Greedy: done")
args = c("noncontextualDACtopNclick", "noncontextualETCtopNclick", "noncontextualeps_GreedytopNclick")
source("plot_multiple.R")
print("third figure generated")


# The fourth figure (Regret comparison with revenue objective)
print("Generating the fourth figure")
args = c("noncontextual", "DAC", "topNrevenue")
run_code(args)
print("noncontextual DAC: done")
args = c("noncontextual", "ETC", "topNrevenue")
run_code(args)
print("noncontextual ETC: done")
args = c("noncontextual", "eps_Greedy", "topNrevenue")
run_code(args)
print("noncontextual eps_Greedy: done")
args = c("noncontextualDACtopNrevenue", "noncontextualETCtopNrevenue", "noncontextualeps_GreedytopNrevenue")
source("plot_multiple.R")
print("fourth figure generated")
