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

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

Uploaded Source

Built Distributions

sleipnirgroup_jormungandr-0.0.1.dev229-cp312-cp312-win_amd64.whl (467.5 kB view details)

Uploaded CPython 3.12 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev229-cp312-cp312-manylinux_2_35_x86_64.whl (408.9 kB view details)

Uploaded CPython 3.12 manylinux: glibc 2.35+ x86-64

sleipnirgroup_jormungandr-0.0.1.dev229-cp312-cp312-macosx_10_9_universal2.whl (682.2 kB view details)

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

sleipnirgroup_jormungandr-0.0.1.dev229-cp311-cp311-win_amd64.whl (467.4 kB view details)

Uploaded CPython 3.11 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev229-cp311-cp311-manylinux_2_35_x86_64.whl (409.2 kB view details)

Uploaded CPython 3.11 manylinux: glibc 2.35+ x86-64

sleipnirgroup_jormungandr-0.0.1.dev229-cp311-cp311-macosx_10_9_universal2.whl (684.0 kB view details)

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

sleipnirgroup_jormungandr-0.0.1.dev229-cp310-cp310-win_amd64.whl (467.3 kB view details)

Uploaded CPython 3.10 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev229-cp310-cp310-manylinux_2_35_x86_64.whl (409.5 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.35+ x86-64

sleipnirgroup_jormungandr-0.0.1.dev229-cp310-cp310-macosx_10_9_universal2.whl (684.4 kB view details)

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

sleipnirgroup_jormungandr-0.0.1.dev229-cp39-cp39-win_amd64.whl (467.7 kB view details)

Uploaded CPython 3.9 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev229-cp39-cp39-manylinux_2_35_x86_64.whl (409.7 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.35+ x86-64

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev229.tar.gz
Algorithm Hash digest
SHA256 d35a6256bd01882b255a1e87cca4c2d6440d90863a31b63f391395c9827a91fa
MD5 4a16fe1790e3421c7085fb7e87016241
BLAKE2b-256 97dac9fb387017b666778a6d1041bb6719a432e411465a54a116be8e54b8d0e0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev229-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 cec787647eb0a6d8ec7e6e726849bcc356591c611d904ccbbe0426d023d1b7f2
MD5 26b11ca05631fd2821f8caca5581cdb0
BLAKE2b-256 195270dc8f715d9ab53bbc05248af442baa3719db1bd75903edb10ab4f6ab665

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev229-cp312-cp312-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 0f5e0846b01aea9bf0b66dcbba6215edc5f89cccbcfd6e429c23d8d15418ed88
MD5 7bf086453468931e74f31f11686353f7
BLAKE2b-256 aa5552319eb603b1f8c7603de9d21c47077bf25ea2805cceb9ae48c41c45be0d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev229-cp312-cp312-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 d4a07aee4ac4709fc2ec21e457e005b6bd6ac210282b0acd79a6d5eea04b9869
MD5 824b42de764aba5bdc46e5f1d8e08f2e
BLAKE2b-256 83131a604d8267f17308d123f61b67f38cf8c6e756b6bf293e670e04b80fdb17

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev229-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 78dadb1838bee98df872c9b5519906c8b582b8152e176237f6ae855a11578957
MD5 41fc98cec163ac85e7041f1e8c3186ee
BLAKE2b-256 7fbe92535ec9bafd25245d2d9bb2f00806aed57dafe7136d584ef68587968a1d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev229-cp311-cp311-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 8e297c17557235753b470841dfd099c63d1ea96ddcb1c092a9a15f210168ec99
MD5 539e914ad9bb200344f15f9fa8b9b2cc
BLAKE2b-256 719a26746fee1d5380d3f2686eff0f6af8a07af7f77d7838cddc68b523adaf99

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev229-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 9c1626a3c1d2436e77e473da29fb24b9f43c887fcbacb6d719fe2ff63f06ae87
MD5 64713d4c794f0bec0ebfeea4104bdd67
BLAKE2b-256 3662a74c7649949a5709729d7ef414231b64265259eed581310c6cc9b12685ed

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev229-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 cd4f51691fe5b1de3de0a3e450b94924a08c5e004b68339e4bbceec15c72b7d8
MD5 bf544522f0d354b576a9cf87980666c9
BLAKE2b-256 c81dd93062d265579145716c0eae364eafaf0cfe4761d7935f3df4ac3d54ddaa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev229-cp310-cp310-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 414e57e7992d12841a13fa446993b7a7d4fa34292fbc6ebdc0ea29b361633415
MD5 50cc7658a9b8b3ff4f5b034d1403dcc4
BLAKE2b-256 366f27380f3d9c03038d79d87d784cdf637013ff1b3763a3cba927dd458281bf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev229-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 522197b511cabcf1a41e53eac47a5f57aa91c713e4a44d27f0db744f92ce0e48
MD5 c64215715504c44357defcd84f2487cf
BLAKE2b-256 1fa178fef8a13fee6c86fa48546562e1b37b0970a50c130548288e0efecaa319

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev229-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 17b00f14cd7193e5525e98811fae29ccccd107295e5e0b6ff5775157f50f1ef9
MD5 b4fcfc82187b0921a9adc1b5045a14b4
BLAKE2b-256 62d178f59012ad9a26c5349c3015c98a9e92b6cfbfeb450eada481dff270f52f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev229-cp39-cp39-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 364872e283273f060c30da04829a2cef74919a1169f57d3eecbe88340fd3ca1b
MD5 15a6e3ab9cc52d7f3b9241346b77c2cd
BLAKE2b-256 19036704a6a843fc6bb4975a67d9138f7cf11f69053f2c341c3f399e671d9a87

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