定义数据
N_h <- c(39552, 38347, 43969, 36942, 41760)
s_1 <- c(4.6, 3.4, 3.3, 2.8, 3.7)
s_2 <- c(11.7, 9.8, 7.0, 6.5, 9.8)
s_3 <- c(332, 357, 246, 173, 279)
s_cap <- c(0.5, 0.5, 0.5, 0.5, 0.5)
s_h1 <- s_1+s_cap
s_h2 <- s_2+s_cap
s_h3 <- s_3+s_cap
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
cost <- c(3,4,5,6,7)
c_cap <- c(1.5, 1.5, 1.5, 1.5, 1.5)
c<- cost
c
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
N <- sum(N_h)
d_h <- c(N_h/N)
d1 <-(d_h)^2*s_h1
d2 <-(d_h)^2*s_h2
d3 <-(d_h)^2*s_h3
v<- c(0.0037, 0.00868, 0.25)
i<-1; j<-2; k<-3
Gamma<-5
调用非线性优化库
library('nloptr')
目标
f2<-function(x, d1, d2, d3, c, Gamma){
return(x[1])
}
约束
g2<- function(x, d1, d2, d3, c, Gamma){
return(c((c[1]*x[2]+c[2]*x[3]+c[3]*x[4]+c[4]*x[5]+c[5]*x[6]+x[7]*Gamma+sum (x[8:12])-x[1]), #1
(-x[7]-x[8]+c_cap[1]*x[2]),#2
(-x[7]-x[9]+c_cap[2]*x[3]),#3
(-x[7]-x[10]+c_cap[3]*x[4]),#4
(-x[7]-x[11]+c_cap[4]*x[5]),#5
(-x[7]-x[12]+c_cap[5]*x[6]),#6
(d1[1]/x[2]+d1[2]/x[3]+d1[3]/x[4]+d1[4]/x[5]+d1[5]/x[6]-v[i]),#7
(d2[1]/x[2]+d2[2]/x[3]+d2[3]/x[4]+d2[4]/x[5]+d2[5]/x[6]-v[j]),
(d3[1]/x[2]+d3[2]/x[3]+d3[3]/x[4]+d3[4]/x[5]+d3[5]/x[6]-v[k]),
(x[2]+x[3]+x[4]+x[5]+x[6]-1081)
))
}
lowerb<- c(-Inf, 2,2,2,2,2, 0, 0,0,0,0, 0)
upperb<- c(Inf, 1000,1000,1000,1000,1000, Inf, Inf, Inf,Inf,Inf,Inf)
初始化
x0<-c(100, 10,10,10,10,10, 2,2, 2,2,2,2)
使用线性近似的约束优化
Robust <- nloptr(x0=x0,
eval_f = f2,
lb=lowerb,
ub=upperb,
eval_g_ineq=g2,
opts=list("algorithm"="NLOPT_LN_COBYLA",
maxeval=100000,
"xtol_rel"=1.0e-8,
"print_level" = 0))
结果如下:
Call:
nloptr(x0 = x0, eval_f = f2, lb = lowerb, ub = upperb, eval_g_ineq = g2,
opts = list(algorithm = "NLOPT_LN_COBYLA", maxeval = 1e+05, xtol_rel = 1e-08, print_level = 0))
Minimization using NLopt version 2.4.2
NLopt solver status: 4 ( NLOPT_XTOL_REACHED: Optimization stopped because xtol_rel or xtol_abs (above) was reached. )
Number of Iterations....: 1219
Termination conditions: maxeval: 1e+05 xtol_rel: 1e-08
Number of inequality constraints: 10
Number of equality constraints: 0
Optimal value of objective function: 6742.76053944518
Optimal value of controls: 6742.761 234.7352 235.9822 224.6824 158.3741 227.2298 290.1293 70.93367 63.84037 51.29771 30.13394 53.6419
使用相同的算法解决不同的代码
Robust1 <- cobyla(x0, f2, hin = g2,lower = lowerb, upper = upperb,
nl.info = TRUE,control = list(xtol_rel = 1e-8, maxeval = 100000))
结果如下:
Call:
nloptr(x0 = x0, eval_f = fn, lb = lower, ub = upper, eval_g_ineq = hin, opts = opts)
Minimization using NLopt version 2.4.2
NLopt solver status: 5 ( NLOPT_MAXEVAL_REACHED: Optimization stopped because maxeval (above) was reached. )
Number of Iterations....: 100000
Termination conditions: stopval: -Inf xtol_rel: 1e-08 maxeval: 1e+05 ftol_rel: 0 ftol_abs: 0
Number of inequality constraints: 10
Number of equality constraints: 0
Current value of objective function: -19989642.9275736
Current value of controls: -19989643 220.9959 214.9653 215.0129 215.0129 215.0129 2 2 2 2 2 2
然而,两个代码使用相同的算法但产生不同的结果。有人可以解释一下这个区别吗? 非常感谢你。
答案 0 :(得分:1)
可以看出,当直接使用COBYLA算法时,它根本不会收敛,它提供了一些当前值而不是最优值。
*Current value of objective function: -19989642.9275736
Current value of controls: -19989643 220.9959 214.9653 215.0129 215.0129 215.0129 2 2 2 2 2 2*
然而,使用&nbspoptr&#39;包该算法成功收敛并提供最佳结果。
Optimal value of objective function: 6742.76053944518
Optimal value of controls: 6742.761 234.7352 235.9822 224.6824 158.3741 227.2298 290.1293 70.93367 63.84037 51.29771 30.13394 53.6419
因此,&#39; nloptr&#39;包提供了所需的结果。