Learn R Programming

nloptr

nloptr is an R interface to NLopt, a free/open-source library for nonlinear optimization started by Steven G. Johnson, providing a common interface for a number of different free optimization routines available online as well as original implementations of various other algorithms. It can be used to solve general nonlinear programming problems with nonlinear constraints and lower and upper bounds for the controls, such as

$$ \min_{x \in \mathbb{R}^n} \quad f(x), $$

s.t. $g(x) \le 0$, $h(x) = 0$ and $\ell \le x \le u$.

The NLopt library is available under the GNU Lesser General Public License (LGPL), and the copyrights are owned by a variety of authors. See the website for information on how to cite NLopt and the algorithms you use.

Installation

Windows

On Windows, for old versions of R (R <= 4.1.x), the nlopt v2.7.1 from rwinlib is used. For newer versions of R (R >= 4.2.0), the nlopt version from the corresponding RTools toolchain is used.

Linux and macOS

On Unix-like platforms, we use pkg-config to find a suitable system build of NLopt (i.e. with version >= 2.7.0).

  • If it is found it is used.
  • Otherwise, NLopt 2.7.1 is built from included sources using CMake. In this case, a binary of CMake stored in environment variable CMAKE_BIN is searched on the PATH and, alternatively, on a macOS-specific location. If that variable cannot be set, install will abort suggesting ways of installing CMake. The minimal version requirement on cmake is >= 3.2.0.

Installing CMake (macOS and Linux only)

Minimal version requirement for cmake is 3.2.0.

You can install CMake by following CMake installation instructions. The important thing is that you add the CMake binary to your PATH:

  • On macOS, you can install CMake and then run it. In the menu bar, there is an item How to Install For Command Line Use which you can click on to have proper instructions on how to update your PATH. Note that the location of the CMake binary is always /Applications/CMake.app/Contents/bin/cmake. Hence, nloptr knows where to find it even if you do not update your PATH.
  • On Linux, it will be automatically added unless you specifically change the default installation directory before building CMake.

Alternatively, you can set an environment variable CMAKE_BIN pointing to a CMake binary of your liking on your computer for nloptr to use.

Installing nloptr

You can install nloptr from CRAN using:

install.packages("nloptr")

Alternatively, you can install the development version from GitHub:

# install.packages("remotes")
remotes::install_github("astamm/nloptr")

Acknowledgments

I would like to express my sincere gratitude to Avraham Adler, Dirk Eddelbuettel, Mikael Jagan, Tomas Kalibera, Jeroen Ooms and Jelmer Ypma for their contributions and the very instructive discussions about the pros and cons of various build strategies in R packages.

Reference

Steven G. Johnson, The NLopt nonlinear-optimization package, https://nlopt.readthedocs.io/en/latest/

Copy Link

Version

Install

install.packages('nloptr')

Monthly Downloads

366,529

Version

2.2.0

License

LGPL (>= 3)

Issues

Pull Requests

Stars

Forks

Maintainer

Aymeric Stamm

Last Published

March 12th, 2025

Functions in nloptr (2.2.0)

mlsl

Multi-level Single-linkage
nl.opts

Setting NL Options
nloptr.print.options

Print description of nloptr options
neldermead

Nelder-Mead Simplex
nl.grad

Numerical Gradients and Jacobians
mma

Method of Moving Asymptotes
nloptr

R interface to NLopt
nloptr.get.default.options

Return a data.frame with all the options that can be supplied to nloptr.
nloptr-package

R interface to NLopt
newuoa

New Unconstrained Optimization with quadratic Approximation
stogo

Stochastic Global Optimization
slsqp

Sequential Quadratic Programming (SQP)
print.nloptr

Print results after running nloptr
sbplx

Subplex Algorithm
tnewton

Preconditioned Truncated Newton
varmetric

Shifted Limited-memory Variable-metric
auglag

Augmented Lagrangian Algorithm
lbfgs

Low-storage BFGS
is.nloptr

R interface to NLopt
direct

DIviding RECTangles Algorithm for Global Optimization
crs2lm

Controlled Random Search
isres

Improved Stochastic Ranking Evolution Strategy
cobyla

Constrained Optimization by Linear Approximations
ccsaq

Conservative Convex Separable Approximation with Affine Approximation plus Quadratic Penalty
bobyqa

Bound Optimization by Quadratic Approximation
check.derivatives

Check analytic gradients of a function using finite difference approximations