【问题标题】:'nloptr' package in 'R' produces different results?“R”中的“nloptr”包产生不同的结果?
【发布时间】:2017-10-01 01:06:38
【问题描述】:

定义数据

   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

但是,两个代码使用相同的算法,但产生不同的结果。谁能解释一下区别? 非常感谢。

【问题讨论】:

    标签: r algorithm optimization


    【解决方案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*
    

    但是,使用“nloptr”包算法成功收敛并提供最佳结果。

      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
    

    因此,“nloptr”包提供了所需的结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-10
      • 2018-05-23
      • 2017-09-11
      • 2020-03-14
      • 1970-01-01
      • 2014-01-28
      • 1970-01-01
      相关资源
      最近更新 更多