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.dev249.tar.gz (120.6 kB view details)

Uploaded Source

Built Distributions

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

Uploaded CPython 3.13 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev249-cp313-cp313-manylinux_2_35_x86_64.whl (410.0 kB view details)

Uploaded CPython 3.13 manylinux: glibc 2.35+ x86-64

sleipnirgroup_jormungandr-0.0.1.dev249-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.dev249-cp312-cp312-win_amd64.whl (469.8 kB view details)

Uploaded CPython 3.12 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev249-cp312-cp312-manylinux_2_35_x86_64.whl (410.0 kB view details)

Uploaded CPython 3.12 manylinux: glibc 2.35+ x86-64

sleipnirgroup_jormungandr-0.0.1.dev249-cp312-cp312-macosx_10_13_universal2.whl (683.4 kB view details)

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

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

Uploaded CPython 3.11 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev249-cp311-cp311-manylinux_2_35_x86_64.whl (410.3 kB view details)

Uploaded CPython 3.11 manylinux: glibc 2.35+ x86-64

sleipnirgroup_jormungandr-0.0.1.dev249-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.dev249-cp310-cp310-win_amd64.whl (469.7 kB view details)

Uploaded CPython 3.10 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev249-cp310-cp310-manylinux_2_35_x86_64.whl (410.6 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.35+ x86-64

sleipnirgroup_jormungandr-0.0.1.dev249-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.dev249-cp39-cp39-win_amd64.whl (470.2 kB view details)

Uploaded CPython 3.9 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev249-cp39-cp39-manylinux_2_35_x86_64.whl (410.8 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.35+ x86-64

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev249.tar.gz
Algorithm Hash digest
SHA256 4a356c4974b91dac62d05f9976548f7d0edbf8a1ac798e495433184679d00d7b
MD5 1cc53b5932d0a51d350c4803e5dcc149
BLAKE2b-256 0b67ebcc0794a4b203f45e8abc6191a1412247721a4133c3682903d201ef2921

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev249-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 aa02e6a9060beea52753625670bba3a239aacde6f9f4b1b6cdc5a6864e304f3d
MD5 209e865814275e226b321350aba8c9f1
BLAKE2b-256 68252d14561c5e6dcbdce985d329be8f70e8eeadf9d690b08781326f8274f578

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev249-cp313-cp313-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 c9948efde984322556062ac5e8ffc9deedfdf43be0f1bd121c87eaaf6d59e6c6
MD5 cd388e2d78364be2a907b1598d3c6c41
BLAKE2b-256 cbd68500ad724363d8bad50543eba4a89316f3f409f9a8274382983d0d414c3e

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev249-cp313-cp313-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 de503601bd0b653703cc8a8d6308c95365b034fbf56e10ae0ad3ae404da497ea
MD5 f16a017ccf2355a209c31448770b6d99
BLAKE2b-256 d5123be02f2a629785c18c231fbf762c2a7d6681fb7d8489cd9bdafbd0f3521e

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev249-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 d1b1fea65a32bb52b91ca1f91996047cd836542471097ce76f1bd35fcd4d8896
MD5 d63b13aa3c8bb9e3ec923ded6cc64456
BLAKE2b-256 4a2241950c3a681a7fbbbf355ea6dbe012734f32404eeae2d7a409a1c22918d7

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev249-cp312-cp312-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 74d2c22764f594cd8fe14d32cbbb94a88f6b31c6e0da347bc8b5a73cc7fdc65f
MD5 94e7c2bbe2a89e48b4b99472150e0a25
BLAKE2b-256 e904c80dde132fe1b36e9e56b157d8b8968c469f7e3b5082d9408b8cea6e60c2

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev249-cp312-cp312-macosx_10_13_universal2.whl
Algorithm Hash digest
SHA256 dee63d9818e8ae6edd787e84aadfd6a7b0af4bc4f3032a602e6d320d212321c5
MD5 9705f728be38b702d1ceee835e5c8b05
BLAKE2b-256 0010f65b31bcf71f55c6bc9b3c3a269a542caf6eb3677a00b7bc15c20d4c24cf

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev249-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 a3209121e49ac9ecf7a48d3261641955627f0fb21a3ab2173b471d6d60d1f2c1
MD5 8f4ee5c81e0fd96a70ccf421cba2a172
BLAKE2b-256 738d93cc6f6027e678308b9aa34e5b737628e2ab4a9b71de034352e5e565364f

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev249-cp311-cp311-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 7ac84a994187c4dc91e233f6930d4078f349ba766fae22082f753e6d15616a59
MD5 1b86cf0e01d5160eabbca9f3dcd56ba5
BLAKE2b-256 7402784dcba0eda5be253dd7ad0d33902464906ce75ab22c52881a8ccd589e0b

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev249-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 7f0be4c07515d900b381b0c651b2ec04daf7b9875cf9c10803d1af388c69b855
MD5 6047d2a9dd25b5f98387abfce3d53142
BLAKE2b-256 5b0dc8c5b83410cd435a735a846be2e5918b4b591f230a72345950d9840e7971

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev249-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 2e63e66ccc70b13af23abbd8b5bf184e563212059c85c1522fdd76ee9cf99751
MD5 db7a20dde2413f89a68cfdcbc350073d
BLAKE2b-256 91da79eaf7c626e3f320a130a734a9b0b929749b3e0a179899e6b35fa6d492e4

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev249-cp310-cp310-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 64c69404eb392b1f67f4378b09f6bfab1fcc95ce37fefc64960b84193d64ae91
MD5 842d0ba0dbaa89bf6c3792d11cd0e813
BLAKE2b-256 89aac80b75055ecb147c585c22edc1ca96810c7131e48aee9460fe67809690cd

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev249-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 0a4cf8aa5e6d1eacd529aa48de5db032f3e6925b342c1c69cbb676df057023e4
MD5 f12af10b37a91a3e4f618598b869141b
BLAKE2b-256 7ae46deeb486aa2b3d12cf3ef419b4f3bd6798bc99696eb10495a0df5f8c6e74

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev249-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 817d2abd96648bb06505ae6261aa9a266b58e63e81652a4071c3e87bb1cefccc
MD5 381c1fd6fc193a9583b35badfc194e73
BLAKE2b-256 539dbdb0ebe67367c97e46ae49a0a4dba371be489aa9d1003789aecd04180c45

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev249-cp39-cp39-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 e4c2a02cacfc65a15f9b1cceb3ca5e9df29d4fa93cb7fd7a2784fe1e17a407f6
MD5 0f8a0888363efe427944ae9b40933af3
BLAKE2b-256 fd265af1dc58db56625027971c2161b52e6b5d3fe829928687536fa64e31e178

See more details on using hashes here.

Provenance

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