Skip to main content

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

Project description

Sleipnir

Build PyPI Downloads C++ Documentation 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 <fmt/core.h>
#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
  fmt::println("x = {}, y = {}", x.Value(), y.Value());
}
#!/usr/bin/env python3

import jormungandr as jmg


def main():
    # Find the x, y pair with the largest product for which x + 3y = 36
    problem = jmg.optimization.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 on a AMD Ryzen 7 7840U with 64 GB RAM. 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

Examples

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

Build

Dependencies

  • C++20 compiler
    • On Windows, install Visual Studio Community 2022 and select the C++ programming language during installation
    • On Linux, install GCC 11 or greater via sudo apt install g++
    • On macOS 13 or greater, install the Xcode command-line build tools via xcode-select --install. Xcode 15.0.1 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
  • fmtlib (internal only)
  • pybind11 (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.

Branding

Logo

SVG, PNG (1000px)
Font: Centaur

Color palette

Purple Yellow
6D3D94 FDB813

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

Uploaded Source

Built Distributions

sleipnirgroup_jormungandr-0.0.1.dev145-cp312-cp312-win_amd64.whl (433.0 kB view details)

Uploaded CPython 3.12 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev145-cp312-cp312-manylinux_2_35_x86_64.whl (531.7 kB view details)

Uploaded CPython 3.12 manylinux: glibc 2.35+ x86-64

sleipnirgroup_jormungandr-0.0.1.dev145-cp312-cp312-macosx_10_9_universal2.whl (883.8 kB view details)

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

sleipnirgroup_jormungandr-0.0.1.dev145-cp311-cp311-win_amd64.whl (432.9 kB view details)

Uploaded CPython 3.11 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev145-cp311-cp311-manylinux_2_35_x86_64.whl (531.7 kB view details)

Uploaded CPython 3.11 manylinux: glibc 2.35+ x86-64

sleipnirgroup_jormungandr-0.0.1.dev145-cp311-cp311-macosx_10_9_universal2.whl (878.2 kB view details)

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

sleipnirgroup_jormungandr-0.0.1.dev145-cp310-cp310-win_amd64.whl (432.0 kB view details)

Uploaded CPython 3.10 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev145-cp310-cp310-manylinux_2_35_x86_64.whl (530.1 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.35+ x86-64

sleipnirgroup_jormungandr-0.0.1.dev145-cp310-cp310-macosx_10_9_universal2.whl (875.7 kB view details)

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

sleipnirgroup_jormungandr-0.0.1.dev145-cp39-cp39-win_amd64.whl (418.0 kB view details)

Uploaded CPython 3.9 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev145-cp39-cp39-manylinux_2_35_x86_64.whl (530.1 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.35+ x86-64

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev145.tar.gz
Algorithm Hash digest
SHA256 91e0965e1ee1e146de8a2ca9440f713cbaf83c61d6ad97190cfe9fc1131b6c7e
MD5 6057f97e4ffeb44e38fba88106f2403a
BLAKE2b-256 954d2f3caa14f0f1cabf0bc68aa28b84aaeb55f19981cfb5d405455d66de20da

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev145-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 396a514773d8dbf7e2f94705d34e3a6c91d52592663cf5e1f8bad71dd2a8c53b
MD5 03b74619c980dfb8db706b93f5b8ace5
BLAKE2b-256 93d351246bff8d02144bf8ba2b6709533a0f159591bbeaa4935d90d443d4277c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev145-cp312-cp312-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 1beaf27ecb64cd286c5f2b70fa468a7a988b4db0a5517977091288d21ba03679
MD5 43d3f97a059cb893b16fd058a1dc80e1
BLAKE2b-256 55a8b54f896b374433b1ef4f386e005c7c8e9a1b83730eba02287ec4f63fce19

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev145-cp312-cp312-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 e110e6c0a50454d008527d45de6173e4c2485457b15fa00d1d1129e4b0584916
MD5 20791e9b1d8f5e06c5b0abbaa7a11abc
BLAKE2b-256 77cc718bc756de60291c1c91824b182b190f1927ee8527192dbbb4f5b502f298

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev145-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 2fec51a3e8129ec8ea6f1f393eae6d5bbe387063a9e27a545e4ade85d01d4a0b
MD5 e184789a853224d5daf7fc3e10b5ff67
BLAKE2b-256 f2a8f28dc21e60384cd00604242fb4dec40b79cea09b1b20c1afd1f8967e40ec

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev145-cp311-cp311-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 69238ed1515f224607a124801352269c020b97da26de810d8e731ec906fb3760
MD5 5058d128a4518d40a6ab6fe07f02367c
BLAKE2b-256 2e1fe62be49527da8b37a53e6834e8c5a0231ba298907609781183bdcceea44e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev145-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 373070d4d8a017a89f11198a05e3866fb208eda9e62e63a4aee7f76f62607b65
MD5 ec7b0d3284e3afa61d5264bcddae2a2e
BLAKE2b-256 9dd7ef6bda079b3d3b4bed58420bfa75e6ca5f6d5eacf5e7033ffc5755bd0d8d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev145-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 c90131241a9e9dfb05f8260be5bd52dcd22f46b0148b1ed182cc8587b98993a3
MD5 72c5fb16d62b28d5be44c13f56e59406
BLAKE2b-256 49ce35524dc46498f944f77c2424b16aa8c2613f2587bfdf7d0f0df912cc329e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev145-cp310-cp310-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 347eb37e9c48e0a26c5ebb37f7718888bb00cc9f113fa18348abba87959a67c8
MD5 3f8a685b4721c089ed565d9d082e6fd8
BLAKE2b-256 dfd566d1336666356d926f6386726984219c1d00af66ba33a652dca5f973c1bb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev145-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 293a9267ef3e8c60ef3815d56770c9d554e1229d21f438f086576e875cc15374
MD5 dd076f1a3683db29eb0bb444ed32fa2d
BLAKE2b-256 8acf2e2489042e8d58a0d36879abbc30a617a40cb01a35dbbc9ec64565708198

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev145-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 442fd7e0f3007b9d639d24082f87bee7501046c178c4c7aee23c94dfe56fd5da
MD5 fbde5ec8e0107813094ff69018c5ef53
BLAKE2b-256 834906a70eccb8c75e481d78930ee67c6a0d19d6f72896a2094a9b28e365b86e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev145-cp39-cp39-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 346bc87f888a63bbf4bc8b4881ac8535b7fce443e3f174d28ae3ab8b0ddb7164
MD5 d23645c6f84f069814696dfd847a1d18
BLAKE2b-256 25804225dde600de2af4db74279a42f187066297d8a6a3dffd61df273c99a6c3

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