1

Is there a way to define multiple "inequality constraints" in nloptr package in R?

The inequality function needs to have five inequality constraints; colsum of a matrix (stacked from a integer vector) <=1 . (5 out of 6 columns)

This is how I implemented to achieve it:

 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 is numeric vector that is stacked as a matrix.

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)

my.data.var

NLOPTR is defined as below but when I run it, it says "number of inequality constraints =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)
Dharman
  • 30,962
  • 25
  • 85
  • 135
Modi
  • 143
  • 1
  • 13
  • I took reference from this post http://stackoverflow.com/questions/31431575/minimization-with-r-nloptr-package-multiple-equality-constraints but to no avail. – Modi Jun 21 '16 at 18:22
  • Here is some [feedback on urgent begging](http://meta.stackoverflow.com/q/326569/472495). – halfer Jun 21 '16 at 19:19
  • @halfer: sure. thnx – Modi Jun 21 '16 at 19:28
  • `Error.func` is undefined and there is a missing `c(` call that shows up when trying to run the code after adding code that brings in nloptr from pkg::nloptr. – IRTFM Jun 21 '16 at 23:28
  • @42- I modified the code just now and made it work. Edited. – Modi Jun 22 '16 at 14:23

2 Answers2

3

I updated the the Constraint.func as and now the nloptr picks the inequality constraints.

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) }
Modi
  • 143
  • 1
  • 13
2

I know is this is years late, but I recently ran into this problem as well, and seems like eval_g_ineq can return a vector of constraint values:


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 )
cmilando
  • 46
  • 4
  • Great answer! Can you please take a look at this question? https://stackoverflow.com/questions/68280857/r-x-probs-outside-0-1 ? Thanks! – stats_noob Jul 07 '21 at 19:56