如何在R中优化具有非线性约束的非线性目标函数?

氯17

说,有一个非线性目标函数:

Z= a1 + b1 * ln(x1) + a2 + b2 *ln(x2)  with the objective of maximizing Z

受到以下限制─

x1 + x2 + x3 >=R1
x1 + x2 + x3 <=R2
a1 + b1 * ln(x1) >=R3

如何在R中优化目标函数?使用R中提供的“ Rsolnp”包进行了尝试,但是不确定如何构造约束和将赋予包中功能“ solnp”的目标功能。

谁能帮我这个?

Sandipan Dey

尝试以下操作(您可能希望使用其他算法,例如NLOPT_LD_MMA与指定的jacobian一起使用):

library(nloptr)
a1 <- b1 <- 1
a2 <- b2 <- 1
R1 <- R2 <- 1
R3 <- 25

eval_f1 <- function( x, a1, b1, a2, b2, R1, R2, R3){ 
  return(-a1 - b1 * log(x[1]) - a2 - b2 *log(x[2])) # maximize
}


eval_g1 <- function( x, a1, b1, a2, b2, R1, R2, R3) {
  return(rbind(x[1] + x[2] + x[3] - R1,
               -x[1] - x[2] - x[3] + R2,
               R3 - a1 - b1*log(x[1])))
}

nloptr(x0=c(1,1,1), 
        eval_f=eval_f1, 
        lb = c(1,1,1), 
        ub = c(5,5,5), 
        eval_g_ineq = eval_g1, 
        opts = list("algorithm"="NLOPT_LN_COBYLA"),
        a1 = a1,
        b1 = b1,
        a2 = a2,
        b2 = b2,
        R1 = R1,
        R2 = R2,
        R3 = R3)

#Call:
#nloptr(x0 = c(1, 1, 1), eval_f = eval_f1, lb = c(1, 1, 1), ub = c(5, 
#    5, 5), eval_g_ineq = eval_g1, opts = list(algorithm = "NLOPT_LN_COBYLA"),     
#a1 = a1, b1 = b1, a2 = a2, b2 = b2, R1 = R1, R2 = R2, R3 = R3)


#Minimization using NLopt version 2.4.0 

#NLopt solver status: 5 ( NLOPT_MAXEVAL_REACHED: Optimization stopped because #maxeval (above) was reached. )

#Number of Iterations....: 100 
#Termination conditions:  relative x-tolerance = 1e-04 (DEFAULT) 
#Number of inequality constraints:  3 
#Number of equality constraints:    0 
#Current value of objective function:  -5.08783644210816 
#Current value of controls: 5 4.385916 2.550764

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章