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.2.1 20240805

The following thirdparty software was used in the benchmarks:

  • CasADi 3.6.7 (autodiff and NLP solver frontend)
  • Ipopt 3.14.16 (NLP solver backend)
  • MUMPS 5.7.0 (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

Minimum system requirements

Sleipnir requires somewhat newer operating systems and C++ runtimes for std::print().

  • Windows
  • Linux
    • OS: Ubuntu 24.04
    • Runtime: GCC 14 libstdc++ (run sudo apt install g++-14)
  • macOS
    • OS: macOS 14
    • Runtime: Apple Clang 15.0.0 libc++ from Xcode 15.3 (run xcode-select --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 10 or greater, install Visual Studio Community 2022 and select the C++ programming language during installation
    • On Ubuntu 24.04 or greater, install GCC 14 via sudo apt install g++-14
    • On macOS 14 or greater, install the Xcode 15.3 command-line build tools via xcode-select --install
  • 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.dev247.tar.gz (120.6 kB view details)

Uploaded Source

Built Distributions

sleipnirgroup_jormungandr-0.0.1.dev247-cp313-cp313-win_amd64.whl (469.8 kB view details)

Uploaded CPython 3.13 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev247-cp313-cp313-manylinux_2_35_x86_64.whl (409.7 kB view details)

Uploaded CPython 3.13 manylinux: glibc 2.35+ x86-64

sleipnirgroup_jormungandr-0.0.1.dev247-cp313-cp313-macosx_10_13_universal2.whl (683.4 kB view details)

Uploaded CPython 3.13 macOS 10.13+ universal2 (ARM64, x86-64)

sleipnirgroup_jormungandr-0.0.1.dev247-cp312-cp312-win_amd64.whl (469.8 kB view details)

Uploaded CPython 3.12 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev247-cp312-cp312-manylinux_2_35_x86_64.whl (409.7 kB view details)

Uploaded CPython 3.12 manylinux: glibc 2.35+ x86-64

sleipnirgroup_jormungandr-0.0.1.dev247-cp312-cp312-macosx_10_13_universal2.whl (683.5 kB view details)

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

sleipnirgroup_jormungandr-0.0.1.dev247-cp311-cp311-win_amd64.whl (469.4 kB view details)

Uploaded CPython 3.11 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev247-cp311-cp311-manylinux_2_35_x86_64.whl (409.9 kB view details)

Uploaded CPython 3.11 manylinux: glibc 2.35+ x86-64

sleipnirgroup_jormungandr-0.0.1.dev247-cp311-cp311-macosx_10_9_universal2.whl (684.2 kB view details)

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

sleipnirgroup_jormungandr-0.0.1.dev247-cp310-cp310-win_amd64.whl (469.7 kB view details)

Uploaded CPython 3.10 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev247-cp310-cp310-manylinux_2_35_x86_64.whl (410.1 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.35+ x86-64

sleipnirgroup_jormungandr-0.0.1.dev247-cp310-cp310-macosx_10_9_universal2.whl (684.6 kB view details)

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

sleipnirgroup_jormungandr-0.0.1.dev247-cp39-cp39-win_amd64.whl (470.2 kB view details)

Uploaded CPython 3.9 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev247-cp39-cp39-manylinux_2_35_x86_64.whl (410.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.dev247.tar.gz.

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev247.tar.gz
Algorithm Hash digest
SHA256 13c9037a87e551615c8a1439468a3b2d4638ca2999c5f36d2b236c70e2812ba4
MD5 dc6665ce72baf8f9fb466abe2d5036fe
BLAKE2b-256 04f534ecc953f44faccea94aa94fbea1859006d1b66ae80e3b64aae9ab2cf131

See more details on using hashes here.

File details

Details for the file sleipnirgroup_jormungandr-0.0.1.dev247-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev247-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 fa58a332ca7c731ddfc7d496263379bbc4c39774cd269451abc9ae5bc8db8130
MD5 65d4a6dba0ff8931a5bb1257c45f41a9
BLAKE2b-256 895478ed6ba7d5cea3ffcab6caf6f70ccc7f82bd154cc9c6876edfa372201ef4

See more details on using hashes here.

File details

Details for the file sleipnirgroup_jormungandr-0.0.1.dev247-cp313-cp313-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev247-cp313-cp313-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 6e3bcf30dfd486df224945b1bd444ee4c91cd5ff518c0448e41398e8b7312ff4
MD5 f65cd7f806b8eaf5f1a89f53149c7e51
BLAKE2b-256 637bf0c211d4dfb79c598d0cefa58dc2cb984da78141213e2ae1404b53c499f0

See more details on using hashes here.

File details

Details for the file sleipnirgroup_jormungandr-0.0.1.dev247-cp313-cp313-macosx_10_13_universal2.whl.

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev247-cp313-cp313-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 072a403c77e9981c5cfc4763d04d0be33a397c6949c2cf24dfef0fe8669f023a
MD5 7df487f526e42c48cfbd377dd026f55f
BLAKE2b-256 7f055a51ecaa91bbeb19214d9b716e307a71df96ca9608f5e423189cc493fcf7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev247-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 837190959d0b38679d79008a32228e2c741c4c688180ab9c34477dbf94629bd1
MD5 63e25916af57a587aea9b837b4f983d6
BLAKE2b-256 fb1b6d86795982697fe9e9c716bed75bdd4937eebf61cd3e70502d15dc4810cd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev247-cp312-cp312-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 8a9669eeb4e4cd3e6332caa3b4938f18d014bf8783ebfb7a96fdf09e1fdfb18d
MD5 d31493872990f225ba2639088fc286d8
BLAKE2b-256 52b41793d935002cc78f1201fe9bde67403d67ea8391144ecc313898c15121fb

See more details on using hashes here.

File details

Details for the file sleipnirgroup_jormungandr-0.0.1.dev247-cp312-cp312-macosx_10_13_universal2.whl.

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev247-cp312-cp312-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 081505772a8e95314fadd1a50a0a4160a9b8f4d1cfd2d793f4005694a24851f6
MD5 2ed9ce409b4753b033c8bb39153fc256
BLAKE2b-256 3b20d54c54d52360b13f4ea6a5b6d6817db3f2125132e32d7790570e638ff667

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev247-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 e0195ae365594525949558cfe0e1dcd10250d264b857221161ec103017aec24e
MD5 b6ad190014189074508c5ff0473ead0b
BLAKE2b-256 505227ae06068fab80c55f03abad0c15eec9176d8a691cf2ee3ebec4d0740a48

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev247-cp311-cp311-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 06b6b1b5c7c738a3eb2338557f8f9aa4f8f18dbd7f39c700925b4722df43b11f
MD5 3ed3df60fd4531e8b8e6e9f2672be82e
BLAKE2b-256 d3fdd8b1f55cdf10e771cacc8ca9fd5409cd301795946c242fc947d98a130c27

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev247-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 10c4245fdb0b61edb5a721fef4c719169d1e3028aa9a7cd9b3987bc51c9ce43e
MD5 2add6f2832e103d40314dced40d910f3
BLAKE2b-256 3673ce7166ba913fe7f23a74966b37f535b684b75652905b1fe9cdbc7c9473c8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev247-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 7130305889e9ba5b9f80ac72f1577d1e9e33081bcebed31e4ffdf3f32236e63c
MD5 31b2f7a6a20885e2f5de1b76ef2db6f2
BLAKE2b-256 f3f31de058def256b995f3de507ed6433ede830cb5e4fa148050bc48f5d0922a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev247-cp310-cp310-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 dbe1c2dd853a674d34a1601c995dfce19678555bb6369bbf2f0ce8dc51bcfe26
MD5 43cebafbf3f2216b589d14e29170c73f
BLAKE2b-256 4972a178da3bb3166774602a7d405f55a1154b9b77ecba838686be007f2e1bae

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev247-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 1c97d72c76fa7e9ade2a5989aa227e2b8f2062bc78c3b1775ff14dcb305226cf
MD5 9cb4f478225e288dc0b31213249206b3
BLAKE2b-256 e16fdb67d77e5f6ff58d6e1f3524d4c0f7344ea9d1bc066923001b0dd086afd0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev247-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 c68323ffde7b613be33bc7edabcdcc7968994719300be8ddd65ac8db923795ea
MD5 893c589034585af050b48b7995974a0a
BLAKE2b-256 f9ab5ccefb0787d9ba93ec0fefa5351cd210e3be4d8e3dd72b800f2b5718b43f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev247-cp39-cp39-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 b3e53493344938b68be14266ceea86662eb0475c9af504b334a2ab313e89baa3
MD5 65693f840baf6c20876e4105a442c221
BLAKE2b-256 dadcdb0e56d99bc1bc90dfc356564c3330ae41404e4a61d723db1213c209cb0f

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