"multiple inequality constraints" - Minimization with R nloptr package
Asked Answered
U

2

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)
Ushas answered 21/6, 2016 at 18:15 Comment(5)
I took reference from this post #31432075 but to no avail.Ushas
Here is some feedback on urgent begging.Butterandeggs
@halfer: sure. thnxUshas
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.Tenet
@42- I modified the code just now and made it work. Edited.Ushas
U
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) }
Ushas answered 22/6, 2016 at 14:27 Comment(0)
M
3

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 )
Mell answered 3/1, 2021 at 12:48 Comment(1)
Great answer! Can you please take a look at this question? https://mcmap.net/q/585726/-r-x-39-probs-39-outside-0-1 ? Thanks!Confine

© 2022 - 2024 — McMap. All rights reserved.