【问题标题】:"multiple inequality constraints" - Minimization with R nloptr package“多重不等式约束” - 使用 R nloptr 包最小化
【发布时间】:2016-10-23 10:22:55
【问题描述】:

有没有办法在 R 的 nloptr 包中定义多个“不等式约束”?

不等式函数需要有五个不等式约束;矩阵的 colsum(从整数向量堆叠)

这是我实现它的方式:

 constraint.func <- function(my.data.var)
{
  column = 2
  constr <- c("numeric",ncol(my.data.matrix.inj) ) 

  for(index in 1:ncol(my.data.matrix.inj)) #1 to 5
  {
    constr[index] <- sum(my.data.var[column], my.data.var[column+6],  my.data.var[column+12], my.data.var[column+18])-1 
    column = column+1
  }
   constr.1 <- c(constr[1],constr[2],constr[3],constr[4],constr[5])

 return(constr.1)
}

my.data.var 是堆叠为矩阵的数字向量。

my.data.var <- c(10,0.25,0.25,0.25,0.25,0.25,
             10,0.25,0.25,0.25,0.25,0.25,
             10,0.25,0.25,0.25,0.25,0.25,
             10,0.25,0.25,0.25,0.25,0.25)

我的.data.var

NLOPTR 定义如下,但当我运行它时,它显示“不等式约束数 =0”。

  opts = list("algorithm"="NLOPT_LN_COBYLA",
            "xtol_rel"=1.0e-5, "maxeval"=500)

result <- nloptr(my.data.var,eval_f = Error.func,lb=c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
                 ub = (Inf,1,1,1,1,1,Inf,1,1,1,1,1,Inf,1,1,1,1,1,Inf,1,1,1,1,1),
           eval_g_ineq=constraint.func,opts = opts)

print(result)

【问题讨论】:

  • 我参考了这篇帖子stackoverflow.com/questions/31431575/…,但无济于事。
  • 这里有一些feedback on urgent begging
  • @halfer:当然。谢谢
  • Error.func 未定义,并且在添加从 pkg::nloptr 引入 nloptr 的代码后尝试运行代码时会显示缺少 c( 调用。
  • @42- 我刚刚修改了代码并使其工作。已编辑。

标签: r nonlinear-optimization


【解决方案1】:

我知道这已经晚了好几年,但我最近也遇到了这个问题,似乎eval_g_ineq 可以返回一个约束值向量:


library(nloptr)

# objective function
eval_f0 <- function( x, a, b ){return( sqrt(x[2]) )}

# constraint functions
eval_g0 <- function( x, a, b ) {
  
  g1 <- (a*x[1] + b)^3 - x[2] ^2
  g2 <- (a*x[1] + 2 * b)^3 - x[2]
  
  return( c(g1, g2) )
}

a <- c(2,-1)
b <- c(0, 1)
x0 <- c(1.234,5.678)

# Solve using NLOPT_LN_COBYLA without gradient information
res1 <- nloptr( x0=x0 ,
                eval_f=eval_f0,
                lb = c(-Inf,0),
                ub = c(Inf,Inf),
                eval_g_ineq = eval_g0,
                opts = list("algorithm" = "NLOPT_LN_COBYLA",
                            "xtol_rel" = 1e-8,
                            "maxeval" = 1e4,
                            "print_level" = 2),
                a = a, 
                b = b )
print( res1 )

【讨论】:

【解决方案2】:

更新了 Constraint.func,现在 nloptr 选择了不等式约束。

constraint.func <- function(my.data.var)
{
  column = 2
  constr <- vector("numeric",length = 5)

 for(index in 1:ncol(my.data.matrix.inj))
  {
    constr[index] <- sum(my.data.var[column], my.data.var[column+6], my.data.var[column+12], my.data.var[column+18])-1
    column = column+1
  }
 return(constr) }

【讨论】:

    猜你喜欢
    • 2015-10-04
    • 2017-07-07
    • 1970-01-01
    • 2019-06-13
    • 1970-01-01
    • 2020-07-27
    • 1970-01-01
    • 2019-11-17
    • 2015-08-10
    相关资源
    最近更新 更多