Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Equality constraint with COBYLA #135

Closed
fangzhou-xie opened this issue Mar 14, 2023 · 5 comments
Closed

Equality constraint with COBYLA #135

fangzhou-xie opened this issue Mar 14, 2023 · 5 comments

Comments

@fangzhou-xie
Copy link

fangzhou-xie commented Mar 14, 2023

Hi, the NLopt documentation mentions that "Only some of the NLopt algorithms (AUGLAG, SLSQP, COBYLA, and ISRES) currently support nonlinear equality constraints".

However, the following example will run into error:

f <- function(x) {
  return(sum(x^2))
}

eq <- function(x) {
  return(sum(x)-1)
}


sol <- nloptr::nloptr(
  x0 = c(.1, .4),
  eval_f = f,
  lb = c(0, -Inf),
  ub = c(Inf, Inf),
  eval_g_eq = eq,
  opts = list("algorithm" = "NLOPT_LN_COBYLA")
)
#> Warning in nloptr.add.default.options(opts.user = opts, x0 = x0,
#> num_constraints_ineq = num_constraints_ineq, : No termination criterium
#> specified, using default (relative x-tolerance = 1e-04)
#> Error in is.nloptr(ret): If you want to use equality constraints, then you should use one of these algorithms 
# NLOPT_LD_AUGLAG, NLOPT_LN_AUGLAG, NLOPT_LD_AUGLAG_EQ, NLOPT_LN_AUGLAG_EQ, NLOPT_GN_ISRES, NLOPT_LD_SLSQP

Created on 2023-03-14 with reprex v2.0.2

I am not sure why this is the case and I greatly appreciate any help. Thank you very much!

@fangzhou-xie
Copy link
Author

For my naive example, I could pack the COBYLA algorithm as the local algo for AUGLAG to make it work. But I am still curious why directly using COBYLA wouldn't work.

f <- function(x) {
  return(sum(x^2))
}

eq <- function(x) {
  return(sum(x)-1)
}


sol <- nloptr::nloptr(
  x0 = c(.1, .4),
  eval_f = f,
  lb = c(0, -Inf),
  ub = c(Inf, Inf),
  eval_g_eq = eq,
  opts = list(
    "algorithm" = "NLOPT_LN_AUGLAG_EQ",
    "xtol_rel" = 1.0e-8,
    "maxeval"    = 1000,
    "local_opts" = list(
      "algorithm" = "NLOPT_LN_COBYLA",
      "maxeval"    = 1000,
      "xtol_rel" = 1.0e-8
    )
  )
)

sol$solution
#> [1] 0.4999605 0.4999605

Created on 2023-03-14 with reprex v2.0.2

@aadler
Copy link
Contributor

aadler commented Apr 25, 2023

It's kicked out by lines 193–203 in is.nlopt. While it isn't in the help there, it is in the help for cobyla. Specifically "COBYLA supports equality constraints by transforming them into two inequality constraints. As this does not give full satisfaction with the implementation in NLOPT, it has not been made available here." I presume @jyypma and @hwborchers agreed not to implement it using nlopt as well.

@astamm
Copy link
Owner

astamm commented Jun 2, 2023

This is worth looking into. I'll take a look soon. If it is possible to use nonlinear equality constraints in COBYLA from nlopt, I do not see why this would not be supported by nloptr.

@jyypma
Copy link
Collaborator

jyypma commented Jun 3, 2023 via email

@astamm
Copy link
Owner

astamm commented Oct 4, 2023

Thanks @jyypma and @aadler. I will make the fix in is.nloptr.

@astamm astamm closed this as completed in 1264f8c Oct 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants