我有以下优化问题:
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
中设置了这个问题(受到这个答案的启发):
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.
我做错了什么?这个问题中什么约束是多余的?我是否错误地定义了问题,或者只是通过这种方式无法解决?
我不知道你正在使用的软件包,但我尝试了基本R
foo <- function(x) {
big_number <- 10^12
-(
2*10^9 + 160*x[1] - 7*x[1]*log(x[1]) +
3*10^9 + 170*x[2] - 7.5*x[2]*log(x[2])
) +
if (sum(x) > 3*10^9) big_number else 0
}
init <- c(1 * 10^9, 2 * 10^9)
optim(par = init, fn = foo)
# $par
# [1] 1610058872 1389941050
#
# $value
# [1] -40508596398
# ...