使用NLOPT / Gurobi求解混合约束优化

时间:2017-04-11 02:50:09

标签: r nonlinear-optimization

我目前正在开发一个项目,我想使用R和NLOPT包(或Gurobi)来解决以下优化问题:

查找min || y-y_h || _L ^ 2使x = Ay_h,y> = 0,其中x,y为给定大小为16 * 1的矢量,A = 16 *也给出了24矩阵。

我的尝试:

R代码

nrow=16;
ncol = 24;
lambda = matrix(sample.int(100, size = ncol*nrow, replace = T),nrow,ncol);
lambda = lambda - diag(lambda)*diag(x=1, nrow, ncol);
y = rpois(ncol,lambda) + rtruncnorm(ncol,0,1,mean = 0, sd = 1); 

x = matrix (0, nrow, 1);
x_A1 = y[1]+y[2]+y[3];
x_A2 = y[4]+y[7]+y[3];
x_B1 = y[4]+y[5]+y[6];
x_B2 = y[11]+y[1];
x_C1 = y[7]+y[8]+y[9];
x_C2 = y[2]+y[5]+y[12];
x_D1 = y[10]+y[11]+y[12];
x_D2 = y[3]+y[6]+y[9];
x_E1 = y[13]+y[14]+y[15];
x_E2 = y[18]+y[19]+y[23];
x_F1 = y[20]+y[21]+y[19];
x_F2 = y[22]+y[16]+y[13];
x_G1 = y[23]+y[22]+y[24];
x_G2 = y[14]+y[17]+y[20];
x_H1 = y[16]+y[17]+y[18];
x_H2 = y[15]+y[21]+y[24];

d <- c(x_A1, x_A2,x_B1, x_B2,x_C1, x_C2,x_D1, x_D2,x_E1, 
       x_E2,x_F1, x_F2,x_G1, x_G2,x_H1, x_H2)
x <- matrix(d, nrow, byrow=TRUE)

A = matrix(c(1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, #x_A^1 
             0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0, #x_A^2 
             0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, #x_B^1
             1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0, #x_B^2 
             0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, #x_C^1 
             0,1,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0, #x_C^2 
             0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0, #x_D^1 
             0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, #x_D^2 
             0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0, #x_E^1 
             0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,1,0, #x_E^2
             0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0, #x_F^1 
             0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0, #x_F^2 
             0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0,0, #x_G^2 
             0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1, #x_G^1 
             0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0, #x_H^1 
             0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,1), #x_H^2
           nrow, ncol, byrow= TRUE)

尝试了两个代码来解决问题:min || y - y_h || _L ^ 2其中x = Ay_h,y&gt; = 0其中x,y,A都在上面给出。< / p>

#f(x)= || yhat -y || _L2

eval_f <- function( yhat ) {
  return( list( "objective" = norm((mean(yhat-y))^2, type = "2")))
}

# inequality constraint
eval_g_ineq <- function( yhat ) {
  constr <- c(0 - yhat)
  return( list( "constraints"=constr ))
}

# equalities constraint
eval_g_eq <- function( yhat ) {
  constr <- c( x-A%*%yhat )
  return( list( "constraints"=constr ))
}

x0 <- y

#lower bound of control variable
lb <- c(matrix (0, ncol, 1))

local_opts <- list( "algorithm" = "NLOPT_LD_MMA",
                    "xtol_rel" = 1.0e-7 )
opts <- list( "algorithm" = "NLOPT_LD_AUGLAG",
              "xtol_rel" = 1.0e-7,
              "maxeval" = 1000,
              "local_opts" = local_opts )
res <- nloptr( x0=x0,
               eval_f=eval_f, 
               eval_grad_f = NULL,
               lb=lb,
               eval_g_ineq = eval_g_ineq, 
               eval_g_eq=eval_g_eq,
               opts=opts)
print(res)

Gurobi代码:

**#model <- list()
#model$B <- A
#model$obj <- norm((y-yhat)^2, type = "2")
#model$modelsense <- "min"
#model$rhs <- c(x,0)
#model$sense <- c('=', '>=')
#model$vtype <- 'C'
#result <- gurobi(model, params)
#print('Solution:')
#print(result$objval)
#print(result$yhat)**

我的问题:首先,当我运行上面的R代码时,它一直给我这条消息: is.nloptr(ret)出错:   客观梯度中的元素数量错误 另外:警告信息: 在is.na(f0 $渐变):   is.na()应用于'NULL'类型的非(列表或向量)

我试图避免计算渐变,因为我没有关于y的密度函数的任何信息。有人可以帮我解决上面的错误吗?

对于Gurobi代码,我收到此消息:错误:是(模型$ A,“矩阵”)||是(型号$ A,“sparseMatrix”)||是(型号$ A,....不是TRUE

但我的矩阵A输入正确,那么这个错误是什么意思呢?

1 个答案:

答案 0 :(得分:1)

我几天前才开始使用nloptr。这个问题已经很老了,但我仍然会回答。当你使用'nloptr'和'NLOPT_LD_AUGLAG'算法时,'LD'代表local并使用渐变...所以你需要在中间用'LN'选择其他东西。例如,“NLOPT_LN_COBYLA”在没有渐变的情况下应该可以正常工作。 实际上你可以查看nloptr包手册。