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.
go get github.com/Terminally-Online/go-lbfgsbThat's it. Works on macOS (arm64/amd64) and Linux (amd64).
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)
}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.
If you need to rebuild the precompiled binaries (maintainers only):
# Requires gfortran
brew install gcc # macOS
# or
sudo apt-get install gfortran # Linux
makeBSD 2-Clause License. See LICENSE.txt.