【问题标题】:Optimization of budget allocation in R (formerly Excel Solver)R 中预算分配的优化(以前的 Excel Solver)
【发布时间】:2018-10-17 08:19:46
【问题描述】:

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

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”。例子:

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

暴力破解或网格搜索不是一个选项,因为这将执行 15-20 次,并且该算法将应用于 R-Shiny 应用程序。

【问题讨论】:

  • 问题是什么?

标签: r optimization solver


【解决方案1】:

尝试optim 与 L-BFGS-U 方法(允许边界)和 0 的下限。然后将输入分量投影到总和为 5000 的向量上,并将其传递给 NrwGesfscale = -1 说最大化而不是最小化。最终分配将是proj(res$par),如底部所示。没有使用任何包。

proj <- function(x) 5000 * x / sum(x)
st <- proj(rep(1, 5))
f <- function(x) NrwGes(proj(x))
res <- optim(st, f, lower = 0 * st, method = "L-BFGS-B", control = list(fnscale = -1))

给予:

> res
$`par`
[1] 2107.8438  482.5702  468.9409  268.0808  142.4305

$value
[1] 86.64285

$counts
function gradient 
      14       14 

$convergence
[1] 0

$message
[1] "CONVERGENCE: REL_REDUCTION_OF_F <= FACTR*EPSMCH"

> proj(res$par)  # final allocation
[1] 3037.3561  695.3729  675.7334  386.2984  205.2391

【讨论】:

  • 传递一个总加起来为 5000 的投影向量是一个非常酷的技巧。我会建议运行 optim 并对目标函数进行惩罚(等于初始总预算与最大化预算之间的绝对差,时间很大),但您的答案要聪明得多。
  • 你真是个天才!!非常感谢!
  • 对不起,还有一个问题:当我使用结果作为输入时 (NrwGes(res$par),我得到 82.53,但 $value 更大,为 86.64,看起来接近我使用 Excel 收到的值. 我做错了吗?
  • 需要使用NrwGes(proj(res$par))f(res$par)proj(res$par) 是分配向量,而不是 res$par
【解决方案2】:

一个选项是nloptr 包:

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 等访问解决方案、资源目标。

【讨论】:

  • 谢谢...有趣的是,这种分配的结果比 G.Grothdieck 的解决方案要好得多
  • @Markus LnGa。不是这样。两种解决方案都给出了 86.64285 到小数点后五位。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-11
  • 1970-01-01
  • 1970-01-01
  • 2020-09-15
  • 2013-11-28
  • 2023-03-06
  • 2021-11-30
相关资源
最近更新 更多