本文介绍了R中预算分配的优化(以前是Excel Solver)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Excel中遇到的问题翻译为R.我想以"Gesamt"(由函数返回)最大化的形式分配固定的预算.

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)
}

我有一个总预算(即5000),可以分配不同的预算以最大化"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

不能使用强行强制"或网格搜索",因为它将执行15-20次,并且该算法将应用于R-Shiny应用程序.

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.

推荐答案

一个选项是nloptr package:

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)

结果:

> 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

您可以使用res$solutionres$objective等访问解决方案,解决方案的目标.

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

这篇关于R中预算分配的优化(以前是Excel Solver)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 08:01