

revenue_multi <- function(r, u, p_set,p_a, c){
  if (length(p_set)==1){
    return(r[p_set[1]] * u[p_set[1]] - (1-u[p_set[1]]) * p_a * c)
  }else{
    current <- p_set[1]
    p_set <- p_set[-1]
    return(r[current] * u[current] - (1-u[current])*(p_a * c - (1-p_a) * revenue_multi(r,u,p_set,p_a,c)))
  }
}

optimal_sequence <- function(r, u, q, c){
  p_a <- 1 - q
  value <- (r*u-c*p_a*(1-u))/(1-q*(1-u))
  incre_index <- sort(value,index.return=TRUE, decreasing=TRUE)$ix
  optimal <- c()
  for (i in (1:length(incre_index))){
    if (value[incre_index[i]]>0){
      optimal <- c(optimal, incre_index[i])
    }
  }
  return(optimal)
}

simu_choice <- function(r,u, sq, p_a){
  for (i in (1:length(sq))){
    if (runif(1)<u[sq[i]]){
      return(c(i))
    }else{
      if (runif(1)<p_a){
        return(c(-1,i))
      }
    }
  }
  return(c(0))
}

read_data <- function(){
  history = read.csv(file="./data/history_1665.csv", header=TRUE, sep=",")
  df = history
  # Ensure df is a data.frame
  df <- as.data.frame(df)
  
  # Ensure numeric types
  df$clk <- as.numeric(df$clk)
  df$nonclk <- as.numeric(df$nonclk)
  df$price <- as.numeric(df$price)
  
 
  # Step 1: Aggregate CTR and price by campaign_id
  aggregated_df <- df %>%
    group_by(campaign_id) %>%
    summarise(
      total_clk = sum(clk),
      CTR = total_clk / (total_clk + sum(nonclk)),
      price = first(price)
    )
  
  # Step 2: Take top 100 campaigns by total clicks
  top_product <- aggregated_df %>%
    arrange(desc(total_clk)) %>%
    slice_head(n = 100) %>%
    dplyr::select(campaign_id, CTR, price)
}

read_data_topNclick <- function(){
  top_product = read.csv(file="./data/topN.csv", header=TRUE, sep=",")
  top_product = as.data.frame(top_product)
}

read_data_topNrevenue <- function(){
  top_product = read.csv(file="./data/topN.csv", header=TRUE, sep=",")
  top_product = as.data.frame(top_product)
}

initialize_env <- function(){
  n_product <<- 100
  c_cost <<- 0.85 #abandonment cost
  p_a <<- 0.0064 #abandonment probability
  q_a <<- 1-p_a
  data_plot <<- c()
  current_curve <<-0
  average_regret <<- 0
  T_total <<- 50000
  epoch_num <<- c(1:T)
  data_plot <<- c()
  average_estimation <<- 0
  data_plot <<- c()
}

set.seed(0)
initialize_env()

if (args[3] == "1665"){
  top_product = read_data()
  u <- top_product$CTR
  r <- rep(1,n_product)
}

if (args[3] == "topNclick"){
  top_product = read_data_topNclick()
  u <- top_product$clk_rate
  r <- rep(1,n_product)
}

if (args[3] == "topNrevenue"){
  top_product = read_data_topNrevenue()
  u <- top_product$clk_rate
  r <- top_product$price
}


while (current_curve<curve_num){
  current_curve <- current_curve + 1
  #print(current_curve)
  counting <- rep(0, n_product)
  exitnum <- 0 
  abandonnum <- 0 
  pick <- rep(0,n_product)
  uhat <- rep(1,n_product)
  qhat <- 1
  regret_total <- 0
  regret <- c()
  t <- 0
  while (t<T_total){
    t <- t + 1
    sq <- optimal_sequence(r,uhat,qhat,c_cost)
    if (is.null(sq)) {
      sq <- sample(1:n_product, 1)
    }
    revenue_sq <- revenue_multi(r, u, sq,p_a, c_cost)
    optimal_sq <- optimal_sequence(r,u,q_a,c_cost)
    revenue_op <- revenue_multi(r, u, optimal_sq,p_a, c_cost)
    regret_total <- regret_total + revenue_op - revenue_sq
    regret <- c(regret, regret_total)
    choice <- simu_choice(r,u, sq, p_a)
    if (length(choice)==1){
      if (choice==0){
        counting[sq] <- counting[sq] + 1
        exitnum <- exitnum + length(sq)
      }else{
        pick[sq[choice]] <- pick[sq[choice]] + 1
        for (i in (1:choice)){
          counting[sq[i]] <- counting[sq[i]] + 1
        }
        exitnum <- exitnum + choice - 1
      }
    }else{
      abandonnum <- abandonnum + 1
      level <- choice[2]
      exitnum <- exitnum + level - 1
      for (i in (1:level)){
        counting[sq[i]] <- counting[sq[i]] + 1
      }
    }
    for (i in (1:n_product)){
      uhat[i] <- min(pick[i]/(counting[i]+0.00001) + 0.15*sqrt(log(t+1)/(counting[i]+2)),1)
    }
    total <- abandonnum + exitnum
    qhat <- min(exitnum/(total+0.00001) +0.15 * sqrt(log(t+1)/(total+2)),1)
  }
  average_regret <- average_regret + regret[T_total]
  average_estimation <- average_estimation + sum(abs(uhat-u))+abs(qhat-q_a)
  epoch_num <- c(1:T_total)
  dd <- cbind(epoch_num,regret)
  epoch_num <- rep(current_curve,T_total)
  dd <- cbind(dd,epoch_num)
  data_plot <- rbind(data_plot,dd)
}

