r - R中的非线性优化

标签 r mathematical-optimization nlopt

我正在尝试使用包 nloptr 解决优化问题在 R 中。我不确定以下代码有什么问题,因为我不断收到此错误:
Error: nlopt_add_equality_mconstraint returned NLOPT_INVALID_ARGS.
这是问题(注意 (A+)^T 是矩阵 A 的 Moore-Penrose 逆的转置)

enter image description here

和代码:

library( MASS ) ## for the Moore-Penrose inverse ginv()
library( zoo )
library( nloptr )

A = matrix(runif(27, -0.5, 0.5), nc = 3)
sigma = diag(runif(3,0.001,0.002))
M = ncol(A)
b = 1 / M
n = nrow(A)
init_y = rep(1/M, M)
c = -ncol(A)*log(ncol(A))



#### Objective function
eval_f <- function(y) 
{
return( sqrt( as.numeric( t(y) %*% sigma %*% y ) ) )
}

#### Gradient of objective function
eval_grad_f <- function(y) 
{
return( ( 2* sigma %*% y) / as.numeric(sqrt( t(y) %*% sigma %*% y )) )
}

#### Equality Constraint
eval_g_eq <- function(y)
{
return( ( t(ginv(A)) %*% y ) - 1 )## ginv(a) is the Moore-Penrose inverse of A
}


#### Inequality constraint
eval_g_ineq <- function(y)
{
return( c - sum( log(y) * b ) )
}

#### Jacobian of equality constraint
eval_jac_g_eq <- function(y)
{
return( ginv(A) )
} 

#### Jacobian of inequality constraint
eval_jac_g_ineq <- function(y)
{
return( (-1/y) * b )
}


 opts <- list( "algorithm" = "NLOPT_LD_SLSQP",
          "xtol_rel"  = 1.0e-14)

 res0 <- nloptr( x0=init_y,
 eval_f=eval_f,
 eval_grad_f=eval_grad_f,
 lb = rep(0,ncol(A)),
 ub = rep(Inf,ncol(A)),
 eval_g_eq = eval_g_eq,
 eval_jac_g_eq = eval_jac_g_eq,
 eval_g_ineq = eval_g_ineq,
 eval_jac_g_ineq = eval_jac_g_ineq,
 opts = opts
 )

最佳答案

I've seen this happen当人们声明一个只有 1 个参数而不是两个参数的 lambda 值时。问题可能与您如何设置约束有关。您可以查看 this document on the topic .

在这种情况下,问题似乎与您用于 eval_g_eq 的函数有关。 .如果我将此参数设置为 NULL 或 the documentation 第 4 页上给出的示例函数,那么就没有错误了。

关于r - R中的非线性优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24409304/

相关文章:

r - 有没有办法在同一个 ggplot 上使用 2 个色标?

mathematical-optimization - 在 MATLAB 中使用 Opti Toolbox 进行混合整数二次规划

java - k-最短(替代)路径算法,java实现

r - 准备具有外部依赖项的 CRAN R 包 (nlopt)

optimization - 具有单变量优化的 NLopt

c++ - Rcpp 数值 vector 输出只返回一个值

python - 在 pandas groupby 之后只过滤少数组元素

regex - 删除字符串中句点前的所有字符

complexity-theory - 具有特殊指标的旅行商

c++ - 如何正确地将std::vector <std::vector <double>>强制转换为void *并重新解释回去?