Skip to main content

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

Project description

Sleipnir

Build 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.

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.

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.

Examples

See the examples and optimization unit tests.

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 gcc
    • On macOS, install the Xcode command-line build tools via xcode-select --install. Xcode 13 or later 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.

If CasADi is installed locally, the benchmark executables will be built.

Build instructions

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

C++ library

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

# Build
cmake --build build

# Test
cd build
ctest
cd ..

# 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

# 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.

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 Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

sleipnirgroup_jormungandr-0.0.1.dev125-cp312-cp312-win_amd64.whl (429.7 kB view details)

Uploaded CPython 3.12 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev125-cp312-cp312-manylinux_2_35_x86_64.whl (496.9 kB view details)

Uploaded CPython 3.12 manylinux: glibc 2.35+ x86-64

sleipnirgroup_jormungandr-0.0.1.dev125-cp312-cp312-macosx_10_9_universal2.whl (500.1 kB view details)

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

sleipnirgroup_jormungandr-0.0.1.dev125-cp311-cp311-win_amd64.whl (429.2 kB view details)

Uploaded CPython 3.11 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev125-cp311-cp311-manylinux_2_35_x86_64.whl (495.9 kB view details)

Uploaded CPython 3.11 manylinux: glibc 2.35+ x86-64

sleipnirgroup_jormungandr-0.0.1.dev125-cp311-cp311-macosx_10_9_universal2.whl (495.7 kB view details)

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

sleipnirgroup_jormungandr-0.0.1.dev125-cp310-cp310-win_amd64.whl (428.4 kB view details)

Uploaded CPython 3.10 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev125-cp310-cp310-manylinux_2_35_x86_64.whl (494.8 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.35+ x86-64

sleipnirgroup_jormungandr-0.0.1.dev125-cp310-cp310-macosx_11_7_x86_64.whl (494.4 kB view details)

Uploaded CPython 3.10 macOS 11.7+ x86-64

sleipnirgroup_jormungandr-0.0.1.dev125-cp310-cp310-macosx_10_9_universal2.whl (432.8 kB view details)

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

sleipnirgroup_jormungandr-0.0.1.dev125-cp39-cp39-win_amd64.whl (413.9 kB view details)

Uploaded CPython 3.9 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev125-cp39-cp39-manylinux_2_35_x86_64.whl (495.1 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.35+ x86-64

sleipnirgroup_jormungandr-0.0.1.dev125-cp39-cp39-macosx_11_7_x86_64.whl (494.5 kB view details)

Uploaded CPython 3.9 macOS 11.7+ x86-64

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev125-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 5f03dd10f99d56589e5f62d7be00138d1f32a2c4b2ce07b18bca1e6b74efea57
MD5 e01674ff9ecf04e2e270b0b34fc3c06b
BLAKE2b-256 ff3c2f4c441766df269d08502c376cb0aa13535cbaa7d7da0f3a362f13ad197a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev125-cp312-cp312-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 d3fb47f59575021569a5710cae953d7d0b46fa1273b323fe2916ed41441fb2e8
MD5 7292f97940d549cec21ae29f21419510
BLAKE2b-256 d79b9cc2b0d2ac8db53019eb8dce4eea8b53ec44b228fe350d279be47c70f7b8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev125-cp312-cp312-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 2bbcee01987f6073b56f5b26b17f8d008e09fc6bbe96d4ed09c5ed986d221038
MD5 470f6c42f8278c341d6af84484bf51c6
BLAKE2b-256 9434bf29b71addbe4e7b63179b572b02d4ff5d0c6baa8b6784fca895b5737a34

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev125-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 aa98d55fcd426146b92d9ef3e827ff59b1de0f29dd4ce5db0d4000a85b0951a5
MD5 d0e8f3fb7b2ae951f81f6915fc7e2293
BLAKE2b-256 ceac69490700a715b547619fd42060fa17baa57f234fc339ac2ef8111bd12f66

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev125-cp311-cp311-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 111b528d518a8bad1e81246f6d81c56d23d546b40efca6dda1a4fad8fabf2c54
MD5 83fcb9b7ceac7d6ffbd8bd239c35542e
BLAKE2b-256 55cbdad99cca624f369732f6a5c7d8458b7a5895d8fef493c5c0843ab0e6ca96

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev125-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 336dbfa5818dcb8c133cdf1ae62d8f6327a3fb41f686abf636bcd235692bb77a
MD5 a8890a0fffe2f96b87c2968e38b5b84e
BLAKE2b-256 d265a073c54984d9f63aac629e1955e78eac2e4d84576c33e88fbff5a814849b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev125-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 db8e470117b934cd044ff8a737835283fbf2e9af65655c6098a2fbd653feb660
MD5 abffc9a92724bb775d61b18e02c941f2
BLAKE2b-256 8dc72f0b07dbc6810d96295be470c419d2e23a7d6c668d5b47b2cef0d903c665

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev125-cp310-cp310-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 21bf8c2e2a88a6b0b2eb59d3ddf8053ef79bcbb7f4040fc185006238142fb624
MD5 ea8765fb3003772ef0a22dfa12185571
BLAKE2b-256 910c921344ea5063aaa0f2c58d63b194a71ad81007a5ce6c89d10d2494cb2d08

See more details on using hashes here.

File details

Details for the file sleipnirgroup_jormungandr-0.0.1.dev125-cp310-cp310-macosx_11_7_x86_64.whl.

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev125-cp310-cp310-macosx_11_7_x86_64.whl
Algorithm Hash digest
SHA256 8d85fdecf5e4249f06073b3e2722f4367aec9d02fdeb64d9b5441bbd63e15b49
MD5 821f2fce5310b0c6d4dd1d8a2882846b
BLAKE2b-256 eb7c5b3767d342450c1a2489ebefd17c202d24e0924ad190743da8915c297ea9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev125-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 61bf23f323b0b5328c429a6a4e7cd05a6604675fd003c742ab785a6061295bf2
MD5 74ddfbc706e2099b6ab2d0cb1b455394
BLAKE2b-256 f0c8ecfe6d9e3d4d2ddf7cabb45502673485513dd8bd92a6f7f7b4dfe49acaa1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev125-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 0470c066e47f67b9ce794592c054493e5a799d30ff237850926618efc9bf018a
MD5 286d0960eaac9430276e75aa22cf9de8
BLAKE2b-256 0679eda43224cc7ddef47507ed58450b22734075f03c0773296002827f6b55f0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev125-cp39-cp39-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 9a87c6463e43dbb82e2f04ed42309813cdf2a113e703148c4afd98f6cff25e76
MD5 88210255f1b5750a01576840d0bf78c6
BLAKE2b-256 e8e5aac9eca05d4c49a58f4790cd2e60da844cd5a4cf4b59bbc063c0142a4098

See more details on using hashes here.

File details

Details for the file sleipnirgroup_jormungandr-0.0.1.dev125-cp39-cp39-macosx_11_7_x86_64.whl.

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev125-cp39-cp39-macosx_11_7_x86_64.whl
Algorithm Hash digest
SHA256 7d0a2218cb5a7844244ba0ea987198cc22a58fc73d435512a50b42ee0599171d
MD5 0be943a99265c69978d099d110d49935
BLAKE2b-256 c316794e936cd8879c752570bb4b106671b5609326a5905b8d4e5fba696b5545

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