Crates.io | lbfgsbrs |
lib.rs | lbfgsbrs |
version | 0.1.2 |
created_at | 2025-06-07 17:59:16.336925+00 |
updated_at | 2025-06-25 08:05:16.447889+00 |
description | Rust port of L-BFGS-B-C |
homepage | |
repository | https://gitlab.com/accpnt/lbfgsb.rs |
max_upload_size | |
id | 1704265 |
size | 232,533 |
Initially started from a c2rust port of L-BFGS-B-C, this project aims to provide a pure Rust implementation of the L-BFGS-B algorithm. Existing Rust crates are, for now, mostly wrappers around Fortran and C implementations. The purpose of this exercise is primarily educational but aims towards a modern take on this algorithm by removing Fortran or C specific idioms (such as goto and 1-based indexing).
This algorithm is widely used for solving bound-constrained optimization problems with memory efficiency in mind, notably by approximating the inverse Hessian matrix for second order evaluation of the objective function's curvature. Starting with an initial estimate of the optimal value that minimizes the objective function, this algorithm proceeds iteratively to refine it through differential calculus, while using limited computer memory.
Roughly, this algorithm minimizes a function f(x) where x is is bounded with a lower and upper boundary.
The compile_commands.json
file has been automatically created using bear. Original C sources can be transpiled using the following command:
c2rust transpile --emit-build-files --translate-const-macros --overwrite-existing --output-dir rust compile_commands.json
The unsafe code resulting from the previous command is available in the unsafe
folder. A few modifications have been made in order to have all three drivers to run.
Refactoring process started following these steps:
safe.py
script in the scripts
folder)libc
reference functions in the unsafe
code (fabs
, fmax
, printf
, etc...) with their Rust counterpartsExtensive use of LLM has been made to speed up the refactoring process.
Output logs of the three drivers are available for C, unsafe and safe Rust implementations for benchmarking and non regression testing. They have been saved in the logs
folder.
Three drivers are originally provided in the Fortran and C sources, keeping the non-convex Rosenbrock function for performance testing of the algorithm. Starting from their output, the refactoring has been checked step by step by comparing the reference values. Unit tests have then been implemented for all three drivers and modules. The drivers can be run using the following command (change driver name accordingly):
RUST_LOG=debug cargo run --example driver3
A pure Matlab implementation of L-BFGS-B
Modern Fortran Refactoring of L-BFGS-B Nonlinear Optimization Code