All Projects → yixuan → Lbfgspp

yixuan / Lbfgspp

Licence: mit
A header-only C++ library for L-BFGS and L-BFGS-B algorithms

Projects that are alternatives of or similar to Lbfgspp

Meshoptimizer
Mesh optimization library that makes meshes smaller and faster to render
Stars: ✭ 2,930 (+1095.92%)
Mutual labels:  optimization
Prepack
A JavaScript bundle optimizer.
Stars: ✭ 14,369 (+5764.9%)
Mutual labels:  optimization
Unitysizeexplorer
Visualize how much space each asset in your Unity game takes and quickly optimize your game's file size
Stars: ✭ 242 (-1.22%)
Mutual labels:  optimization
Gulp Image
Optimize PNG, JPEG, GIF, SVG images with gulp task.
Stars: ✭ 213 (-13.06%)
Mutual labels:  optimization
Awesome Go Perf
A curated list of Awesome Go performance libraries and tools
Stars: ✭ 223 (-8.98%)
Mutual labels:  optimization
Superstring.py
A fast and memory-optimized string library for heavy-text manipulation in Python
Stars: ✭ 231 (-5.71%)
Mutual labels:  optimization
Quantum Neural Networks
This repository contains the source code used to produce the results presented in the paper "Continuous-variable quantum neural networks". Due to subsequent interface upgrades, these scripts will work only with Strawberry Fields version <= 0.10.0.
Stars: ✭ 207 (-15.51%)
Mutual labels:  optimization
Minisam
A general and flexible factor graph non-linear least square optimization framework
Stars: ✭ 246 (+0.41%)
Mutual labels:  optimization
Fewshotlearning
Pytorch implementation of the paper "Optimization as a Model for Few-Shot Learning"
Stars: ✭ 223 (-8.98%)
Mutual labels:  optimization
Learningx
Deep & Classical Reinforcement Learning + Machine Learning Examples in Python
Stars: ✭ 241 (-1.63%)
Mutual labels:  optimization
Dietpi
Lightweight justice for your single-board computer!
Stars: ✭ 2,871 (+1071.84%)
Mutual labels:  optimization
Optimal Roadtrip Usa
Contains maps for the article, "Computing the optimal road trip across the U.S." and similar articles
Stars: ✭ 221 (-9.8%)
Mutual labels:  optimization
Argmin
Mathematical optimization in pure Rust
Stars: ✭ 234 (-4.49%)
Mutual labels:  optimization
Datauri
Generate Data-URI scheme via terminal or node.js
Stars: ✭ 212 (-13.47%)
Mutual labels:  optimization
Openmdao
OpenMDAO repository.
Stars: ✭ 243 (-0.82%)
Mutual labels:  optimization
Androidgodeye
An app performance monitor(APM) , like "Android Studio profiler", you can easily monitor the performance of your app real time in browser
Stars: ✭ 2,430 (+891.84%)
Mutual labels:  optimization
Functional intro to python
[tutorial]A functional, Data Science focused introduction to Python
Stars: ✭ 228 (-6.94%)
Mutual labels:  optimization
Bayesian Optimization
Python code for bayesian optimization using Gaussian processes
Stars: ✭ 245 (+0%)
Mutual labels:  optimization
Aqo
Adaptive query optimization for PostgreSQL
Stars: ✭ 243 (-0.82%)
Mutual labels:  optimization
Jmetalpy
A framework for single/multi-objective optimization with metaheuristics
Stars: ✭ 236 (-3.67%)
Mutual labels:  optimization

LBFGS++ LBFGS++

UPDATE on 2020-03-06: LBFGS++ now includes a new L-BFGS-B solver for box-constrained optimization problems. Check the example below for its usage.

LBFGS++ is a header-only C++ library that implements the Limited-memory BFGS algorithm (L-BFGS) for unconstrained minimization problems, and a modified version of the L-BFGS-B algorithm for box-constrained ones.

The code for the L-BFGS solver is derived and modified from the libLBFGS library developed by Naoaki Okazaki.

LBFGS++ is implemented as a header-only C++ library, whose only dependency, Eigen, is also header-only.

A Quick Example

To use LBFGS++, one needs to first define a functor to represent the multivariate function to be minimized. It should return the objective function value on a vector x and overwrite the vector grad with the gradient evaluated on x. For example we could define the Rosenbrock function in the following way:

#include <Eigen/Core>
#include <iostream>
#include <LBFGS.h>

using Eigen::VectorXd;
using namespace LBFGSpp;

class Rosenbrock
{
private:
    int n;
public:
    Rosenbrock(int n_) : n(n_) {}
    double operator()(const VectorXd& x, VectorXd& grad)
    {
        double fx = 0.0;
        for(int i = 0; i < n; i += 2)
        {
            double t1 = 1.0 - x[i];
            double t2 = 10 * (x[i + 1] - x[i] * x[i]);
            grad[i + 1] = 20 * t2;
            grad[i]     = -2.0 * (x[i] * grad[i + 1] + t1);
            fx += t1 * t1 + t2 * t2;
        }
        return fx;
    }
};

Then we just need to set up parameters, create solver object, provide initial guess, and then run the minimization function.

int main()
{
    const int n = 10;
    // Set up parameters
    LBFGSParam<double> param;
    param.epsilon = 1e-6;
    param.max_iterations = 100;

    // Create solver and function object
    LBFGSSolver<double> solver(param);
    Rosenbrock fun(n);

    // Initial guess
    VectorXd x = VectorXd::Zero(n);
    // x will be overwritten to be the best point found
    double fx;
    int niter = solver.minimize(fun, x, fx);

    std::cout << niter << " iterations" << std::endl;
    std::cout << "x = \n" << x.transpose() << std::endl;
    std::cout << "f(x) = " << fx << std::endl;

    return 0;
}

The example can then be compiled and run.

$ g++ -I/path/to/eigen -I/path/to/lbfgspp/include -O2 example.cpp
$ ./a.out
23 iterations
x =
1 1 1 1 1 1 1 1 1 1
f(x) = 1.87948e-19

You can also use a different line search algorithm by providing a second template parameter to LBFGSSolver. For example, the code below illustrates the bracketing line search algorithm (contributed by @DirkToewe).

int main()
{
    const int n = 10;
    // Set up parameters
    LBFGSParam<double> param;
    param.epsilon = 1e-6;
    param.max_iterations = 100;

    // Create solver and function object
    LBFGSSolver<double, LineSearchBracketing> solver(param);
    Rosenbrock fun(n);

    // Initial guess
    VectorXd x = VectorXd::Zero(n);
    // x will be overwritten to be the best point found
    double fx;
    int niter = solver.minimize(fun, x, fx);

    std::cout << niter << " iterations" << std::endl;
    std::cout << "x = \n" << x.transpose() << std::endl;
    std::cout << "f(x) = " << fx << std::endl;

    return 0;
}

Box-constrained Problem

If the parameters to be optimized have simple bounds, then the L-BFGS-B solver class LBFGSBSolver can be used. The code is very similar to that of LBFGSSolver. Below is the same Rosenbrock example, but we require that all variables should be between 2 and 4.

#include <Eigen/Core>
#include <iostream>
#include <LBFGSB.h>  // Note the different header file

using Eigen::VectorXd;
using namespace LBFGSpp;

class Rosenbrock
{
private:
    int n;
public:
    Rosenbrock(int n_) : n(n_) {}
    double operator()(const VectorXd& x, VectorXd& grad)
    {
        double fx = 0.0;
        for(int i = 0; i < n; i += 2)
        {
            double t1 = 1.0 - x[i];
            double t2 = 10 * (x[i + 1] - x[i] * x[i]);
            grad[i + 1] = 20 * t2;
            grad[i]     = -2.0 * (x[i] * grad[i + 1] + t1);
            fx += t1 * t1 + t2 * t2;
        }
        return fx;
    }
};

int main()
{
    const int n = 10;
    // Set up parameters
    LBFGSBParam<double> param;  // New parameter class
    param.epsilon = 1e-6;
    param.max_iterations = 100;

    // Create solver and function object
    LBFGSBSolver<double> solver(param);  // New solver class
    Rosenbrock fun(n);

    // Bounds
    VectorXd lb = VectorXd::Constant(n, 2.0);
    VectorXd ub = VectorXd::Constant(n, 4.0);

    // Initial guess
    VectorXd x = VectorXd::Constant(n, 3.0);

    // x will be overwritten to be the best point found
    double fx;
    int niter = solver.minimize(fun, x, fx, lb, ub);

    std::cout << niter << " iterations" << std::endl;
    std::cout << "x = \n" << x.transpose() << std::endl;
    std::cout << "f(x) = " << fx << std::endl;

    return 0;
}

Note that we also allow infinite values for the lower and upper bounds. In such cases one can define ub[i] = std::numeric_limits<double>::infinity(), for example.

Documentation

The API reference page contains the documentation of LBFGS++ generated by Doxygen.

License

LBFGS++ is an open source project under the MIT license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].