R solnp (package Rsolnp) fails to respect upper bounds

Viewed 311

An optimization with solnp() does not respect the upper bound specified by the parameter UB = 1. This may be a floating point issue, but I wonder how I can work around this if my objective fun involves an inequality check.

Problem: I have a complex objective function that, by its nature, contains an error check. I optimize with solbp. I cannot delete the error check parm > bound. What could I do to make the optimization run?

Edit: The below example is simplified to showcase the error. I can not delete the inequality check that will always be part of the objective function (here minimizeit()). I could, however, add a tolerance to the function's error check.

Working example

library(Rsolnp)
set.seed(123)

x <- 2 * 1:10 + runif(10) / 10 # criterion

# Objective function
minimizeit <- function(parm, x) {
  if (parm > 1) { # checks if the parameter is <= 1
    stop("Argument parm is ", parm)
  }
  return(mean( (parm * 1:10 - x)^2 ))
}

# Run the optimization
solnp(0.5, minimizeit, LB = 0, UB = 1, x = x)
# the UB=1 means an upper bound of 1 on the parameter space

Error Message:

# Error in fun(pars, ...) : Argument parm is 1.00000004472127
# Clearly larger than 1 ...

The manual of Rsolnp states that the rho parameter in control control = list(rho = ) controls the degree of boundary respect. But setting rho = 10 still results in the same error.

The usual solution for numeric issues, using all.equal(), does not work with inequalities.

Session Info

sessionInfo()
# R version 3.5.0 (2018-04-23)
# Platform: x86_64-w64-mingw32/x64 (64-bit)
# Running under: Windows 10 x64 (build 16299)
1 Answers

all.equal will use a tolerance when it tests for equality. Roughly, x and y are equal if abs(x - y) < tolerance. So you could just test parm <= 1 + tolerance, with tolerance a small number such as 1e-6.

Related