我有以下优化问题:
Maximisise combined benefit functions
F(x) = 2*10^9 + 160*x - 7*x*ln(x)
F(y) = 3*10^9 + 170*y - 7.5*y*ln(y)
subject to
x + y <= 3*10^9
x,y >= 0
我建立了一个拉格朗日
L(x,y,λ) = F(x) + F(y) + λ(3*10^9-(x+y))
然后,在用图形计算器求解之前,我对它进行了代数简化
y = 3*10^9 - x
e^(19/15)*x^(14/15) + x - 3*10^9 = 0
y^(15/14) + e^(19/14)*y - e^(19/14)*3*10^9 = 0
x = 1.61 * 10^9
y = 1.39 * 10^9
现在,我希望能够在R中实现这个解决方案,因为实际的函数参数可能会改变.我试着改编了我在网上找到的几种不同的解决方案,但似乎都没有奏效.
如果我理解正确的话,我需要一个非线性解算器.因此,我在Rsolnp
中设置了这个问题(灵感来自this answer):
library(Rsolnp)
opt_func_log <- function(x) {
a <- x[1]
b <- x[2]
ben_func <- 2e9 + 160*a - 7*a*log(a) + 3e9 + 170*b - 7.5*b*log(b)
-ben_func #invert to find minimum
}
equal_const <- function(x) {
a <- x[1]
b <- x[2]
a + b # budget constraint formula
}
eps <- .Machine$double.eps*10^2 # low number, but not zero due to logs
x0 <- c(0.1, 0.1) # starting values
budget <- 3e9 # overall budget constraint value
opt_solution_log <- solnp(pars = x0,
fun = opt_func_log,
eqfun = equal_const,
eqB = budget,
LB = c(eps,eps))
不幸的是,我没有得到一个可行的解决方案
Iter: 1 fn: -5032442923.2173 Pars: 213333.43335 213333.43335
solnp-->Redundant constraints were found. Poor
solnp-->intermediate results may result.Suggest that you
solnp-->remove redundant constraints and re-OPTIMIZE
Iter: 2 fn: -5032442923.2173 Pars: 213333.43335 213333.43335
solnp--> Solution not reliable....Problem Inverting Hessian.
我做错了什么?在这个问题中,什么约束是多余的?是我错误地定义了这个问题,还是它就是不能以这种方式解决?