如何在R中利用非线性约束优化非线性目标函数?

时间:2017-01-13 10:20:24

标签: r optimization

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

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中优化目标函数?尝试使用&#39; Rsolnp&#39; R中可用的包,但不确定如何构造约束和将赋予函数&#39; solnp&#39;的目标函数。在包中。

任何人都可以帮我吗?

1 个答案:

答案 0 :(得分:1)

试试这个(您可能希望使用其他算法,例如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