“多重不等式约束”-用R nloptr包最小化

“多重不等式约束”-用R nloptr包最小化,r,nonlinear-optimization,R,Nonlinear Optimization,有没有一种方法可以在nloptr包中定义多个不等式约束 不等式函数需要有五个不等式约束;从整数向量叠加的矩阵的colsumI更新了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)) {

有没有一种方法可以在nloptr包中定义多个不等式约束

不等式函数需要有五个不等式约束;从整数向量叠加的矩阵的colsumI更新了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) }
我将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) }

我知道这已经晚了几年,但我最近也遇到了这个问题,而且似乎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 )

我知道这已经晚了几年,但我最近也遇到了这个问题,而且似乎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 )

我从这篇文章中找到了参考资料,但没有用。这里有一些。@halfer:当然可以。thnxror.func未定义,在添加从pkg::nloptr引入nloptr的代码后尝试运行代码时,出现了一个缺少的c调用。@42-我刚刚修改了代码并使其正常工作。编辑。我从这篇文章中引用了一些参考资料,但没有用。这里有一些。@halfer:当然。thnxror.func未定义,在添加从pkg::nloptr引入nloptr的代码后尝试运行代码时,出现了一个缺少的c调用。@42-我刚刚修改了代码并使其正常工作。编辑。

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 )