constrOptim 始终以 optima 的形式返回起始值,计数 = 0,迭代次数 = 1

constrOptim always returns starting values as optima, counts = 0, iterations = 1

提问人:Hack-R 提问时间:2/21/2017 最后编辑:Erwin KalvelagenHack-R 更新时间:2/23/2017 访问量:382

问:

我的约束优化发生了一些有趣的事情,但我不确定问题出在哪里。“优化”值始终与我的起始值完全相同。它说它是收敛的,但计数 = 0,迭代次数 = 1。我不知道为什么。

options(scipens=15)

# Argument Vector ---------------------------------------------------------
# Starting values
args <- c(3553.13, 727.48)

# Revenue Formula ---------------------------------------------------------
intercept <- -4.5315
p_elast   <- -.065
o_elast   <-  .066
leads     <- 20000

rev_fn <- function(args){
  CR       <- exp(intercept + (p_elast*log(args[1])) + (o_elast*log(args[2])))
  price    <- args[1]-args[2]   # charges - offer amount
  quantity <- leads*CR          # leads * CR
  revenue  <- price*quantity

  return(revenue)
}

rev_fn(args)

# Gradient Function -------------------------------------------------------  
gr <- function(args){
        c(leads * (exp(intercept + (p_elast * log(args[1])) + (o_elast *
          log(args[2]))) * (p_elast * (1/args[1]))),
          leads * (exp(intercept + (p_elast * log(args[1])) + (o_elast *
            log(args[2]))) * (o_elast * (1/args[2])))
        )
}

# Constrained Optimization ------------------------------------------------
# Constraints
# 1. Gross price > 0
# 2. Offer > 0
# 3. Gross price > offer

constraint_matrix     <- matrix(c(1,0,1,0,1,-1),ncol=2)
constraint_vector     <- c(0,0,0)

# Note: feasible region =  ui %*% theta - ci >= 0
constrOptim(theta   = c(args[1], # gross price
                        args[2]  # offer amount
                        ),
            f       = rev_fn,
            grad    = gr,
            ui      = constraint_matrix, # constraint matrix (k x p)
            ci      = constraint_vector, # constraint vector of length k
            control = list(fnscale = -1) # maximization
            )
$par
[1] 3553.13  727.48

$value
[1] 552374.2

$counts
[1] 0

$convergence
[1] 0

$message
NULL

$outer.iterations
[1] 1

$barrier.value
[1] 4.919046
R 数学 优化

评论

1赞 n0p 2/22/2017
我建议你用更多的词来描述问题,你也可以添加一个伪代码/算法或你尝试评估的数学表达式列表,以便每个人都可以阅读代码并可能找到错误,无论一个人是否知道 R。
1赞 Erwin Kalvelagen 2/23/2017
(1) 你能检查一下梯度吗?似乎这个符号是错误的(如果乘以 -1,事情正在移动)。(2)即便如此,我相信模型也不会收敛。我怀疑该模型是无界的。

答: 暂无答案