r - R : Error in is.nloptr(ret) : inequality constraints in x0 returns NA

标签 r optimization runtime-error nonlinear-optimization

我正在尝试使用R,CRAN page中的nloptr包解决非线性优化问题。

library('nloptr')

#parameters 
N_h <- c(39552, 38347, 43969, 36942, 41760)

s_h1 <- c(4.6, 3.4, 3.3, 2.8, 3.7)

s_h2 <- c(11.7, 9.8, 7.0, 6.5, 9.8)

s_h3 <- c(332, 357, 246, 173, 279)

Cost <- c(3,4,5,6,7)

C_cap <- c(1.5, 1.5, 1.5, 1.5, 1.5)

c<- Cost+C_cap
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



#Objective
f0<-function(x, d1, d2, d3, c, Gamma){
 return(x[1])
}


#Constraints
g0<- 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]),

       (-x[7]-x[8]+c[1]*x[2]),
       (-x[7]-x[9]+c[2]*x[3]),
       (-x[7]-x[10]+c[3]*x[4]),
       (-x[7]-x[11]+c[4]*x[5]),
       (-x[7]-x[12]+c[5]*x[6]),
       (d1[1]/x[2]+d1[2]/x[3]+d1[3]/x[4]+d1[4]/x[5]+d1[5]/x[6]-0.5037),
       (d2[1]/x[2]+d2[2]/x[3]+d2[3]/x[4]+d2[4]/x[5]+d2[5]/x[6]-0.50868),
       (d3[1]/x[2]+d3[2]/x[3]+d3[3]/x[4]+d3[4]/x[5]+d3[5]/x[6]-0.55),
       (x[2]+x[3]+x[4]+x[5]+x[6]-1081)
   ))
    }

  #Initialization and algo
x0<-c(100, 10,10,10,10,10,  2,  2,2,2,2)



Rob2 <- nloptr(x0=x0,
                eval_f = f0,
                lb=c(-Inf, 2,2,2,2,2,  0,  0,0,0,0),
                ub=c(Inf, 1000,1000,1000,1000,1000,  Inf,  Inf,Inf,Inf,Inf),
                eval_g_ineq=g0,
                opts=list("algorithm"="NLOPT_GN_MLSL",
                          maxeval=100000,
                          "xtol_rel"=1.0e-8,
                          "print_level" = 2),
                d1=d1, d2=d2,d3=d3, c=c, Gamma=Gamma)

运行此算法时,错误如下:
   Error in is.nloptr(ret) : inequality constraints in x0 returns NA

任何建议或帮助将是不胜感激的!!

#编辑

原始问题如下
Min x[1]
subject to
-x[7]-x[8]+c[1]*x[2]<=0
-x[7]-x[9]+c[2]*x[3]<=0
..
..
x[2]+x[3]+x[4]+x[5]+x[6]-1081<=0

最佳答案

我想这里的问题是您在初始x0中包含常量值,需要将其单独放置为d1 <-2
d2 <-2

关于r - R : Error in is.nloptr(ret) : inequality constraints in x0 returns NA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43345525/

相关文章:

r - 错误 ggplotly : VECTOR_ELT() can only be applied to a 'list' , 不是 'NULL'

r - 如何在 R 中创建维度为 M x N 的数据框

c - 编译器优化应用于哪个级别?

java - ObjectInputStream 和 getInputStream

c - if 语句涉及变量位移 - 错误与否(更新)

r - 在 Glmnet 和 dotCall64 中使用 R 中的长向量

r - ntile 函数在最新版本的 R 中不起作用

c - 是否可以使用 GCC 获得经过优化的中间 C 文件?

java - 字节数组上的 Karatsuba 乘法优化

codeigniter CSRF 错误 : "The action you have requested is not allowed."