-1

我有以下功能,我需要将其最大化而不是最小化。

adbudgReturn = function(Spend,a,b,c,d){
adbudgReturn = sum(b+(a-b)*((Spend^c)/(d+(Spend^c))))
return(adbudgReturn)
}

FP_param <- c(95000,0,1.15,700000)
FB_param <- c(23111.55,0,1.15,20000)
GA_param <- c(115004,1409,1.457,2000000)

y = c(0.333333,0.333333,0.333333)

TotalSpend <- function(Budget,y){
                   FP_clicks = adbudgReturn(Budget * y[1], FP_param[1], FP_param[2],  FP_param[3], FP_param[4])
                   FB_clicks = adbudgReturn(Budget * y[2], FB_param[1], FB_param[2],  FB_param[3], FB_param[4])
                   GA_clicks = adbudgReturn(Budget * y[3], GA_param[1], GA_param[2],  GA_param[3], GA_param[4])
                   return(total = FP_clicks + FB_clicks + GA_clicks)
}




startValVec = c(0.33333,0.333333,0.3333333)
minValVec = c(0,0.2,0)
maxValVec = c(0.8,1,08)


MaxClicks_optim.parms <- nlminb(objective = TotalSpend,start = startValVec,
                     lower = minValVec,
                     upper = maxValVec,
                     control = list(iter.max=100000,eval.max=20000),
                     Budget = 10000)

我尝试在 nlminb 函数前面添加减号,即:

-nlminb(..)

但没有任何成功。任何帮助将不胜感激。

我也想添加约束,所以 maxValVec = 1

4

2 回答 2

3

R中的其他优化功能,例如optim()具有内置fnscale控制参数,您可以使用它来从最小化切换到最大化(即optim(..., control=list(fnscale=-1)),但nlminb似乎没有。所以您要么需要翻转原始目标函数中的符号,要么(可能更透明地)制作一个反转符号的包装函数,例如

max_obj <- function(...) -1*TotalSpend(...)
MaxClicks_optim.parms <- nlminb(objective = max_obj, 
           [ .... everything else as before ... ] )

请注意,...定义中的max_obj()是字面的。上面解决方案中唯一需要填写的部分是该[.... everything else as a before ...]部分。绝对明确:

max_obj <- function(...) -1*TotalSpend(...)
MaxClicks_optim.parms <- nlminb(objective = max_obj,
                 start = startValVec,
                 lower = minValVec,
                 upper = maxValVec,
                 control = list(iter.max=100000,eval.max=20000),
                 Budget = 1e4)

如果您使用的是用户指定的gradient参数,您也必须将其包装起来。

这个 CV 问题指出,您可以通过最小化函数的负数来最大化,但不会深入到具体细节。

基于optim()- 的解决方案看起来像:

optim(fn = TotalSpend,
      par = startValVec,
      lower = minValVec,
      upper = maxValVec,
      method = "L-BFGS-B",
      control = list(maxit=100000, fnscale=-1),
      Budget = 1e4)
  • L-BFGS-B 是唯一内置于进行optim()框约束优化的方法
  • optim()没有针对最大迭代次数和最大函数评估的单独控件
于 2020-04-26T19:54:54.633 回答
0

这是一个带有简单抛物线函数的示例,它与nlminband的工作原理相同optim

## ==== Some preliminaries ========================
par(mfrow=c(1,2))
a <- b <- seq(-10, 10, 0.1)

## ==== Search for a minimum ======================

# function has minimum
f1 <- function(a, b) {
  (a - 1)^2 + (b - 2)^2
}
## show function, blue color is low
image(a, b, outer(a,  b, f1), col=topo.colors(16))

## wrapper: combine parameters
g1 <- function(p) f1(p["a"], p["b"])

## minimization
(ret <- nlminb(c(a=0, b=0), g1))
## show minimum
points(t(ret$par), pch="+", cex=2)


## ==== Search for a maximum =======================

## function has a maximum
f2 <- function(a, b) {
  - (a - 1)^2 - (b + 2)^2
}
## brown color is high
image(a, b, outer(a,  b, f2), col=topo.colors(16))

## wrapper:  combine parameters, invert sign
g2 <- function(p) -f2(p["a"], p["b"])

## minimization of negative objective = maximization
(ret <- nlminb(c(a=0, b=0), g2))
## show maximum
points(t(ret$par), pch="+", cex=2)

优化景观

于 2020-04-26T22:09:36.130 回答