Skip to content

Terminally-Online/go-lbfgsb

Repository files navigation

Go L-BFGS-B

A Go package for L-BFGS-B optimization with zero dependencies.

This is a fork of idavydov/go-lbfgsb (itself a fork of afbarnard/go-lbfgsb) with precompiled Fortran binaries. No GCC, no gfortran, no CGO_LDFLAGS setup required.

Installation

go get github.com/Terminally-Online/go-lbfgsb

That's it. Works on macOS (arm64/amd64) and Linux (amd64).

Usage

package main

import (
    "fmt"
    lbfgsb "github.com/Terminally-Online/go-lbfgsb"
)

// Implement the FunctionWithGradient interface
type Rosenbrock struct{}

func (r Rosenbrock) EvaluateFunction(x []float64) float64 {
    return (1-x[0])*(1-x[0]) + 100*(x[1]-x[0]*x[0])*(x[1]-x[0]*x[0])
}

func (r Rosenbrock) EvaluateGradient(x []float64) []float64 {
    return []float64{
        -2*(1-x[0]) - 400*x[0]*(x[1]-x[0]*x[0]),
        200 * (x[1] - x[0]*x[0]),
    }
}

func main() {
    opt := lbfgsb.NewLbfgsb(2).
        SetFTolerance(1e-10).
        SetGTolerance(1e-10)

    result, status := opt.Minimize(Rosenbrock{}, []float64{-1.2, 1.0})

    fmt.Printf("Status: %v\n", status.Code)
    fmt.Printf("Minimum at: (%.6f, %.6f)\n", result.X[0], result.X[1])
    fmt.Printf("Value: %.6e\n", result.F)
}

What is L-BFGS-B?

L-BFGS-B is a limited-memory quasi-Newton optimization algorithm for bound-constrained problems. It's efficient for large-scale optimization where you can compute gradients but the Hessian is too expensive to store.

Building from Source

If you need to rebuild the precompiled binaries (maintainers only):

# Requires gfortran
brew install gcc  # macOS
# or
sudo apt-get install gfortran  # Linux

make

License

BSD 2-Clause License. See LICENSE.txt.

About

🔬 go-lbfgsb is a Go package exposing a L-BFGS-B optimization interface with minimal setup.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •