R/optimizer_for_max.R

Defines functions optimizer_for_max

Documented in optimizer_for_max

#' Optimizer for maximizing an outcome assuming constant elasticity and constant random sampling space limit across the trading period
#' 
#' NOTE: This function works only for small sample set. It will not produce an output for large input data and may crach your R session.
#' 
#' @title Optimizer for maximizing an outcome assuming constant elasticity and constant random sampling space limit across the trading period.
#' @inheritParams compute_delta_revenue
#' @inheritParams permutations
#' @inheritParams select_elements_from_vector_as_given_by_integer_vector
#' @param df A data frame where first column (df[,1]) contains the volume (PAX) as cumulative, while second column (df[,2]) contains the revenue (price).
#' @param limit A numeric of the delta space limit.
#' @param granularity A numeric of delta space granularity (i.e. the lenght of "jumps" within the space).
#' @param elas A numeric (constant) elasticity.
#' @export 


optimizer_for_max = function(df, limit=0.1,granularity=0.05, elasticity=-2){
  # NOTE:   Revenue <-> Price
  #         PAX     <-> Volume
  
  ## Step 1 : creating universal delta PAX set
  universal_delta = create_universal_delta_set(limit=limit,granularity = granularity)
  
  ## Step 2 : computing the corresponding delta REV for each member of delta PAX
  delta_rev = sapply(X = universal_delta, FUN=compute_delta_revenue, elas=elasticity)
  
  ## Step 3 : creating the revenue matrix where columns are weeks to departure (time points), and rows are the range of revenues to select from  
  rev = as.numeric(df[,2])
  rev_mx_after_applying_deltas_space = sapply(X = rev, 
                                              FUN=apply_vector_of_deltas_on_absolute_value_and_create_search_space,
                                              deltas=delta_rev)

  ## Step 4 : calculating PAX matrix with non-cumulative  
  PAX_cumulative = as.numeric(df[,1])
  PAX_nonCumulative = cumulative_to_nonCumulative(PAX_cumulative)

  PAX_mx_after_applying_deltas_space = sapply(X=PAX_nonCumulative,
                                              FUN=apply_vector_of_deltas_on_absolute_value_and_create_search_space,
                                              deltas = universal_delta)
  # return(rev_mx_after_applying_deltas_space)
  ## Step 5 : calculating TOTAL revenue for each revenue (price) at each time-point
  Total_revenue_per_each_revenue_per_timePoint = PAX_mx_after_applying_deltas_space*rev_mx_after_applying_deltas_space
  #return(list(PAX_mx_after_applying_deltas_space,
  #            rev_mx_after_applying_deltas_space,
  #            Total_revenue_per_each_revenue_per_timePoint))
  ## Step 6: computing all  possiable permutations of the TOTAL revenues
  # [Side note: we're assuming that revenues are dependent on each other; i.e. are considered in totality not independently]
  print("Computing permutations...")
  perm = permutations(n=nrow(Total_revenue_per_each_revenue_per_timePoint),
                      r=ncol(Total_revenue_per_each_revenue_per_timePoint),
                      repeats.allowed = T)
  
  ## Step 7: computing the TOTAL of TOTAL revenues per each permutations (which were found 
  total_of_total_rev_permuted = apply(X = perm, 
                       MARGIN = 1,
                       FUN = select_combinatorial_rows_from_table,
                       table=Total_revenue_per_each_revenue_per_timePoint)

  total_of_total_rev_permuted = t(total_of_total_rev_permuted)
  rev_sums_per_permutation = rowSums(total_of_total_rev_permuted)

  ## Step 8: identifying the maximum revenue and fetching the combination that generated it
  maxBoolean = rev_sums_per_permutation==max(rev_sums_per_permutation)
  permutation_yielding_max = perm[maxBoolean,]
  
  ## Step 9: fetching the delta revenues from which revenues yilding max where generated
  optimal_delta_rev = select_elements_from_vector_as_given_by_integer_vector(vector_to_select_from = delta_rev,
                                                                             vector_specifying_the_selection= permutation_yielding_max)
  
  ## Step 10: computing the optimal delta demand
  optimal_delta_demand = sapply(X=optimal_delta_rev,FUN=compute_delta_demand, elas = elasticity) 
  # return(optimal_delta_demand)
  ## Step 11: applying delta demand upon inputted data points to be optimized
  optimized_col1_o = apply_vector_of_deltas_on_absolute_value(deltas = optimal_delta_demand,dataPoint = df[,1])
  optimized_col2_o = apply_vector_of_deltas_on_absolute_value(deltas = optimal_delta_rev, dataPoint = df[,2])
  
  sum_of_col2_before_optimization = sum(rev)
  sum_of_col2_after_optimization = sum(optimized_col2_o)
  
  return(list(
    results=list(optimized_col1=optimized_col1_o,
              optimized_col2=optimized_col2_o),
    parameters=list(df=df,
                    limit=limit,
                    granularity=granularity,
                    elasticity=elasticity)))
}
msxakk89/dat documentation built on Aug. 3, 2020, 6:39 p.m.