GitHub - SleipnirGroup/Sleipnir: Reverse mode autodiff library, interior-point method, and NLP solver DSL for C++23 and Python

C++ Python PyPI Downloads Website C++ API Python API Discord

Sleipnir is a reverse mode autodiff library, interior-point method, and NLP solver DSL for C++23 and Python. The DSL automatically chooses the best solver based on the problem structure.

#include <print>

#include <sleipnir/optimization/problem.hpp>

int main() {
  // Find the x, y pair with the largest product for which x + 3y = 36
  slp::Problem<double> problem;

  auto x = problem.decision_variable();
  auto y = problem.decision_variable();

  problem.maximize(x * y);
  problem.subject_to(x + 3 * y == 36);
  problem.solve();

  // x = 18.0, y = 6.0
  std::println("x = {}, y = {}", x.value(), y.value());
}
#!/usr/bin/env python3

from sleipnir.optimization import Problem


def main():
    # Find the x, y pair with the largest product for which x + 3y = 36
    problem = Problem()

    x, y = problem.decision_variable(2)

    problem.maximize(x * y)
    problem.subject_to(x + 3 * y == 36)
    problem.solve()

    # x = 18.0, y = 6.0
    print(f"x = {x.value()}, y = {y.value()}")


if __name__ == "__main__":
    main()

Sleipnir supports easy cross-compilation via CMake toolchain files, provides good performance by default, and contains clean reference implementations of various solvers with links to explanatory material.

The C++ API also supports arbitrary scalar types, so users can specify higher precision floating-point types at the cost of speed.

Install

The following platforms are supported:

  • Windows
  • Linux
    • OS: Ubuntu 24.04
    • Runtime: GCC 14 libstdc++ (run sudo apt install g++-14)
  • macOS
    • OS: macOS 14.5
    • Runtime: Apple Clang 16.0.0 libc++ from Xcode 16.2 (run xcode-select --install)

To use Sleipnir within a CMake project, add the following to CMakeLists.txt:

include(FetchContent)

FetchContent_Declare(
    Sleipnir
    GIT_REPOSITORY https://github.com/SleipnirGroup/Sleipnir.git
    GIT_TAG main
    EXCLUDE_FROM_ALL
    SYSTEM
)
FetchContent_MakeAvailable(Sleipnir)

target_link_libraries(MyApp PUBLIC Sleipnir::Sleipnir)

To use Sleipnir in Python, install sleipnirgroup-jormungandr from PyPI:

pip install sleipnirgroup-jormungandr

If necessary, follow these instructions to build from source.

Docs

See the C++ API docs, Python API docs, and usage docs.

Examples

See the examples folder, C++ optimization unit tests, and Python optimization unit tests.

Benchmarks

See the benchmarks folder.