Search code examples
roptimizationsolver

Optimization of budget allocation in R (formerly Excel Solver)


i translated a Problem I had in Excel into R. I want to allocate a fixed Budget in a form that "Gesamt" (which is returned by the function) is maximized.

NrwGes <- function(Budget, Speed, maxnrw, cpcrp) {
    BudgetA <- Budget[1]
    BudgetB <- Budget[2]
    BudgetC <- Budget[3]
    BudgetD <- Budget[4]
    BudgetE <- Budget[5]

    MaxNRW <- c(90, 40, 40, 25, 15)
    Speed <- c(0.9, 0.9, 0.9, 0.9, 0.9)
    cpcrp <- c(6564, 4494, 3962, 4525, 4900)

    TV <- BudgetA*1000/cpcrp[1]
    Catchup <- BudgetB*1000/cpcrp[2]
    YT <- BudgetC*1000/cpcrp[3]
    FB <- BudgetD*1000/cpcrp[4]
    Display <- BudgetE*1000/cpcrp[5] 

    a <- TV^Speed[1]/(1+abs((TV)^Speed[1]-1)/(MaxNRW[1]*0.98))
    b <- Catchup^Speed[2]/(1+abs((Catchup)^Speed[2]-1)/(MaxNRW[2]*0.98))
    c <- YT^Speed[3]/(1+abs((YT)^Speed[3] -1)/(MaxNRW[3]*0.98))
    d <- FB^Speed[4]/(1+abs((FB)^Speed[4]-1)/(MaxNRW[4]*0.98))
    e <- Display^Speed[5]/(1+abs((Display)^Speed[5]-1)/(MaxNRW[5]*0.93))

    Gesamt <- a+(100-a)/100*b+((100-a)/100*(100-b)/100*c)+((100-a)/100*(100-b)/100*(100-c)/100*d)+((100-a)/100*(100-b)/100*(100-c)/100*(100-d)/100*e)
    return(Gesamt)
}

I have a total Budget (i.e 5000), which can be allocated differently to maximize "Gesamt". Examples:

NrwGes(c(5000, 0, 0, 0, 0)) # 72.16038
NrwGes(c(2000, 1500, 1000, 500, 0)) # 84.23121

Brute Forcing or grid search is not an option since this will be done 15-20 times and the algorithm will be applied to an R-Shiny App.


Solution

  • An option is nloptr package :

    library(nloptr)
    
    # we use NLOPT_LN_COBYLA algorithm because it doesn't need gradient functions
    opts <- list(algorithm="NLOPT_LN_COBYLA",
                 xtol_rel=1.0e-8,
                 maxeval=10000)
    # objective function (negative because nloptr always minimize)
    objFun <- function(x){ -NrwGes(x) }
    
    # sum of budget <= 5000 (in the form g(x) <= 0)
    g <- function(x){ sum(x) - 5000 }
    
    
    res <- nloptr(x0=rep.int(0,5), # initial solution (all zeros)
                  eval_f=objFun, 
                  lb=rep.int(0,5), # lowerbounds = 0
                  ub=rep.int(5000,5), # upperbounds = 5000
                  eval_g_ineq=g,
                  opts=opts)
    

    Result :

    > res
    Call:
    nloptr(x0 = rep.int(0, 5), eval_f = objFun, lb = rep.int(0, 5), 
        ub = rep.int(5000, 5), eval_g_ineq = g, opts = opts)
    
    
    Minimization using NLopt version 2.4.2 
    
    NLopt solver status: 4 ( NLOPT_XTOL_REACHED: Optimization stopped because xtol_rel 
    or xtol_abs (above) was reached. )
    
    Number of Iterations....: 261 
    Termination conditions:  xtol_rel: 1e-08    maxeval: 10000 
    Number of inequality constraints:  1 
    Number of equality constraints:    0 
    Optimal value of objective function:  -86.6428477187536 
    Optimal value of controls: 3037.382 695.3725 675.7232 386.2929 205.2291
    

    N.B. you can access to solution, objective of res using res$solution, res$objective etc.