OptimLib is a lightweight C++ library of numerical optimization methods for nonlinear functions.
Features
Author: Keith O'Hara
Available Algorithms:
OptimLib functions have the following generic form:
algorithm_name(<initial and final values>, <objective function>, <objective function data>);
The inputs, in order, are:
For example, the BFGS algorithm is called using
bool bfgs(arma::vec& init_out_vals, std::function<double (const arma::vec& vals_inp, arma::vec* grad_out, void* opt_data)> opt_objfn, void* opt_data);
The library can be installed on Unix-alike systems via the standard ./configure && make
method:
# clone optim git clone -b master --single-branch https://github.com/kthohr/optim ./optim # build and install cd ./optim ./configure -i "/usr/local" -p make make install
The last line will install OptimLib into /usr/local
Configuration options: (see ./configure -h
)
-h
print help-i
install path; default: the build directory
-i "/usr/local"
-m
specify the BLAS and Lapack libraries to link against;
-m "-lopenblas"
or -m "-framework Accelerate"
-o
compiler optimization options;
-O3 -march=native -ffp-contract=fast -flto -DARMA_NO_DEBUG
-p
enable OpenMP parallelization features (recommended)-c
a coverage build (used with Codecov)-d
a 'development' build-g
a debugging build (optimization flags set to -O0 -g
)--header-only-version
generate a header-only version of OptimLib (see below)OptimLib is built on the Armadillo C++ linear algebra library. The configure
script will search for
Armadillo files in the standard locations: /usr/include
, /usr/local/include
,
/opt/include
, /opt/local/include
. If the Armadillo header files are installed elsewhere,
set the following environment variable before running configure
:
export ARMA_INCLUDE_PATH=/path/to/armadillo
Otherwise the build script will proceed to download any required files from the Armadillo GitLab repository.
The library is also available in header-only format (i.e., without compiling a shared library). Simply run configure
with the --header-only-version
option:
./configure --header-only-version
This will create a new directory header_only_version that contains a header-only copy of OptimLib.
To use OptimLib with an R package, first generate a header-only version of the library (see above). Then add the compiler definition USE_RCPP_ARMADILLO
before including the OptimLib files:
#define USE_RCPP_ARMADILLO #include "optim.hpp"
Or you can set this preprocessor directive during compilation:
g++ ... -DUSE_RCPP_ARMADILLO ...