Skip to main content

A linearity-exploiting sparse nonlinear constrained optimization problem solver that uses the interior-point method.

Project description

Sleipnir

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

Sparsity and Linearity-Exploiting Interior-Point solver - Now Internally Readable

Named after Odin's eight-legged horse from Norse mythology, Sleipnir is a linearity-exploiting sparse nonlinear constrained optimization problem solver that uses the interior-point method.

#include <print>

#include <sleipnir/optimization/OptimizationProblem.hpp>

int main() {
  // Find the x, y pair with the largest product for which x + 3y = 36
  sleipnir::OptimizationProblem problem;

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

  problem.Maximize(x * y);
  problem.SubjectTo(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 jormungandr.optimization import OptimizationProblem


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

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

    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's internals are intended to be readable by those who aren't domain experts with links to explanatory material for its algorithms.

Benchmarks

flywheel-scalability-results cart-pole-scalability-results
flywheel-scalability-results-casadi.csv
flywheel-scalability-results-sleipnir.csv
cart-pole-scalability-results-casadi.csv
cart-pole-scalability-results-sleipnir.csv

Generated by tools/generate-scalability-results.sh from benchmarks/scalability source.

  • CPU: AMD Ryzen 7 7840U
  • RAM: 64 GB, 5600 MHz DDR5
  • Compiler version: g++ (GCC) 14.1.1 20240507

The following thirdparty software was used in the benchmarks:

  • CasADi 3.6.5 (autodiff and NLP solver frontend)
  • Ipopt 3.14.16 (NLP solver backend)
  • MUMPS 5.6.2 (linear solver)

Ipopt uses MUMPS by default because it has free licensing. Commercial linear solvers may be much faster.

See benchmark details for more.

Install

C++ library

See the build instructions.

Python library

pip install sleipnirgroup-jormungandr

API docs

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

Examples

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

Build

Dependencies

  • C++23 compiler
    • On Windows, install Visual Studio Community 2022 and select the C++ programming language during installation
    • On Linux, install GCC 14 or greater via sudo apt install g++
    • On macOS 14 or greater, install the Xcode command-line build tools via xcode-select --install. Xcode 15.3 or greater is required.
  • CMake 3.21 or greater
    • On Windows, install from the link above
    • On Linux, install via sudo apt install cmake
    • On macOS, install via brew install cmake
  • Python 3.9 or greater
    • On Windows, install from the link above
    • On Linux, install via sudo apt install python
    • On macOS, install via brew install python
  • Eigen
  • nanobind (build only)
  • Catch2 (tests only)

Library dependencies which aren't installed locally will be automatically downloaded and built by CMake.

The benchmark executables require CasADi to be installed locally.

C++ library

On Windows, open a Developer PowerShell. On Linux or macOS, open a Bash shell.

# Clone the repository
git clone git@github.com:SleipnirGroup/Sleipnir
cd Sleipnir

# Configure; automatically downloads library dependencies
cmake -B build -S .

# Build
cmake --build build

# Test
ctest --test-dir build --output-on-failure

# Install
cmake --install build --prefix pkgdir

The following build types can be specified via -DCMAKE_BUILD_TYPE during CMake configure:

  • Debug
    • Optimizations off
    • Debug symbols on
  • Release
    • Optimizations on
    • Debug symbols off
  • RelWithDebInfo (default)
    • Release build type, but with debug info
  • MinSizeRel
    • Minimum size release build
  • Asan
    • Enables address sanitizer
  • Tsan
    • Enables thread sanitizer
  • Ubsan
    • Enables undefined behavior sanitizer
  • Perf
    • RelWithDebInfo build type, but with frame pointer so perf utility can use it

Python library

On Windows, open a Developer PowerShell. On Linux or macOS, open a Bash shell.

# Clone the repository
git clone git@github.com:SleipnirGroup/Sleipnir
cd Sleipnir

# Setup
pip install --user build

# Build
python -m build --wheel

# Install
pip install --user dist/sleipnirgroup_jormungandr-*.whl

# Test
pytest

Test diagnostics

Passing the --enable-diagnostics flag to the test executable enables solver diagnostic prints.

Some test problems generate CSV files containing their solutions. These can be plotted with tools/plot_test_problem_solutions.py.

Benchmark details

Running the benchmarks

Benchmark projects are in the benchmarks folder. To compile and run them, run the following in the repository root:

# Install CasADi and [matplotlib, numpy, scipy] pip packages first
cmake -B build -S .
cmake --build build
./tools/generate-scalability-results.sh

See the contents of ./tools/generate-scalability-results.sh for how to run specific benchmarks.

How we improved performance

Make more decisions at compile time

During problem setup, equality and inequality constraints are encoded as different types, so the appropriate setup behavior can be selected at compile time via operator overloads.

Reuse autodiff computation results that are still valid (aka caching)

The autodiff library automatically records the linearity of every node in the computational graph. Linear functions have constant first derivatives, and quadratic functions have constant second derivatives. The constant derivatives are computed in the initialization phase and reused for all solver iterations. Only nonlinear parts of the computational graph are recomputed during each solver iteration.

For quadratic problems, we compute the Lagrangian Hessian and constraint Jacobians once with no problem structure hints from the user.

Use a performant linear algebra library with fast sparse solvers

Eigen provides these. It also has no required dependencies, which makes cross compilation much easier.

Use a pool allocator for autodiff expression nodes

This promotes fast allocation/deallocation and good memory locality.

We could mitigate the solver's high last-level-cache miss rate (~42% on the machine above) further by breaking apart the expression nodes into fields that are commonly iterated together. We used to use a tape, which gave computational graph updates linear access patterns, but tapes are monotonic buffers with no way to reclaim storage.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sleipnirgroup_jormungandr-0.0.1.dev225.tar.gz (119.9 kB view details)

Uploaded Source

Built Distributions

sleipnirgroup_jormungandr-0.0.1.dev225-cp312-cp312-win_amd64.whl (467.2 kB view details)

Uploaded CPython 3.12 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev225-cp312-cp312-manylinux_2_35_x86_64.whl (408.6 kB view details)

Uploaded CPython 3.12 manylinux: glibc 2.35+ x86-64

sleipnirgroup_jormungandr-0.0.1.dev225-cp312-cp312-macosx_10_9_universal2.whl (681.9 kB view details)

Uploaded CPython 3.12 macOS 10.9+ universal2 (ARM64, x86-64)

sleipnirgroup_jormungandr-0.0.1.dev225-cp311-cp311-win_amd64.whl (467.1 kB view details)

Uploaded CPython 3.11 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev225-cp311-cp311-manylinux_2_35_x86_64.whl (408.9 kB view details)

Uploaded CPython 3.11 manylinux: glibc 2.35+ x86-64

sleipnirgroup_jormungandr-0.0.1.dev225-cp311-cp311-macosx_10_9_universal2.whl (683.7 kB view details)

Uploaded CPython 3.11 macOS 10.9+ universal2 (ARM64, x86-64)

sleipnirgroup_jormungandr-0.0.1.dev225-cp310-cp310-win_amd64.whl (467.1 kB view details)

Uploaded CPython 3.10 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev225-cp310-cp310-manylinux_2_35_x86_64.whl (409.1 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.35+ x86-64

sleipnirgroup_jormungandr-0.0.1.dev225-cp310-cp310-macosx_10_9_universal2.whl (684.1 kB view details)

Uploaded CPython 3.10 macOS 10.9+ universal2 (ARM64, x86-64)

sleipnirgroup_jormungandr-0.0.1.dev225-cp39-cp39-win_amd64.whl (467.5 kB view details)

Uploaded CPython 3.9 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev225-cp39-cp39-manylinux_2_35_x86_64.whl (409.4 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.35+ x86-64

File details

Details for the file sleipnirgroup_jormungandr-0.0.1.dev225.tar.gz.

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev225.tar.gz
Algorithm Hash digest
SHA256 9e53ccec58700bd9b4dc8665acd1b602e616947a346d0d6f713f42a92bc3648c
MD5 14c4169ce90dc7b770eac7faf214a7ab
BLAKE2b-256 e0822a89dfece7e62e4e20a4f6a408e656ba60f426472f0cf9ca636076863523

See more details on using hashes here.

File details

Details for the file sleipnirgroup_jormungandr-0.0.1.dev225-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev225-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 e87a0de24b3a1790c30ad291713da7b98662a1d3e92788ba955cdff628712250
MD5 3b607069b6c89aa3c696937fd648ce97
BLAKE2b-256 ef0c3442c6eee77c97db1b36ed4b2ad5fb0ad6c1d93367774b5b1cc30c9531a6

See more details on using hashes here.

File details

Details for the file sleipnirgroup_jormungandr-0.0.1.dev225-cp312-cp312-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev225-cp312-cp312-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 950bba0a6f9ea219a8d466b4699d366054ec86c3ebf311dfaf84ae04ce832352
MD5 7b06a6f5ba1abf612ae21c6ad54fd542
BLAKE2b-256 2561f5ec7fec6eb5b981b1faa3f852718a1bb3416cdcd41818cd522ef5d46977

See more details on using hashes here.

File details

Details for the file sleipnirgroup_jormungandr-0.0.1.dev225-cp312-cp312-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev225-cp312-cp312-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 fcbafb79a5a7d5084036be6dba0395fe70a2af57aef06fc727d6cedf9c277899
MD5 dfe370f148f44b734f7c981ac131faa2
BLAKE2b-256 8907f8141ee2f8379e25947915ffbcf528c5761b67bde9ce4eb3e23839abc7b8

See more details on using hashes here.

File details

Details for the file sleipnirgroup_jormungandr-0.0.1.dev225-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev225-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 a0b5b3648ac910e394dddce6f7240a97cc737717d1b67609525f27657306ed2a
MD5 89774f584bc43eaaa6bb9202bee06679
BLAKE2b-256 6f1287b2247938b1bbc46be80a5f608e888480cd883023afa9d9ac319a4af56e

See more details on using hashes here.

File details

Details for the file sleipnirgroup_jormungandr-0.0.1.dev225-cp311-cp311-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev225-cp311-cp311-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 c789be1297f9bfde009676e3de3e7caf7afe8321546df05065b8599953983106
MD5 75fcad0abed4e45ea62654dbb7e6e595
BLAKE2b-256 8efffa21941bf9c4ee89cb8ddc58b2f24d2951f668a19c0ad002a8f77c063872

See more details on using hashes here.

File details

Details for the file sleipnirgroup_jormungandr-0.0.1.dev225-cp311-cp311-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev225-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 b5087824358d99fc534964399fcf09d218d7e716768a82ca669cda4afd0d1c10
MD5 e31b0c8010847723e9492fe4ae313e2c
BLAKE2b-256 e9dec71134915d4a2fb5b9cdbc1dc1e58c59b4b92cb9623891ad29f853160ed1

See more details on using hashes here.

File details

Details for the file sleipnirgroup_jormungandr-0.0.1.dev225-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev225-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 f6a79e844e1c6ca92626517fe842f14a331dfe1ed3c6cf0c88efa790736e5bcc
MD5 5b3ba42363d0f27e4eef9443d9e6ac8c
BLAKE2b-256 722d001feb918f8a8d4906872617366c923c822e9c88ee12af4d68e36d3c446e

See more details on using hashes here.

File details

Details for the file sleipnirgroup_jormungandr-0.0.1.dev225-cp310-cp310-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev225-cp310-cp310-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 8c8541f2677ae87191b93422135faf74074ec9cd72b9fb70433d9dbf774ae263
MD5 5ddc9e7576a21b00642980c64c1cc360
BLAKE2b-256 c14134a9946b55364655722c7d942c49bfcc0f8351e5731772005e4537db155a

See more details on using hashes here.

File details

Details for the file sleipnirgroup_jormungandr-0.0.1.dev225-cp310-cp310-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev225-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 7d2291e7d98ac56cf57a120e501e541263fa9ac35ba62b052f49db81a0df602e
MD5 5644a6a04ebe0648958444ef74d0765c
BLAKE2b-256 f63329c80078018b928c62525945e9baaee02c5c2d68f522afd9f2645bf7c832

See more details on using hashes here.

File details

Details for the file sleipnirgroup_jormungandr-0.0.1.dev225-cp39-cp39-win_amd64.whl.

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev225-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 9f32a1db34820f418b0cb5d9426d8ea8009bf48a27a4e32e307709cc5bcdf0ff
MD5 8a68bc259ed694935b40cd31fd3f28bb
BLAKE2b-256 a7ca610d5780725dbe061872cef2a1d92f139a972256036f4ac7b997c5ca439a

See more details on using hashes here.

File details

Details for the file sleipnirgroup_jormungandr-0.0.1.dev225-cp39-cp39-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev225-cp39-cp39-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 6641ce215c6c18598e17db7a1becd6b29e069be594550151ff6f7b25400518e8
MD5 84779aa3d1a4dea88f14c8aa867ef3ba
BLAKE2b-256 10e37b4099af24ad454ddfad9b051257c98a85e9fba861bd282bf358c0568e58

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page