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.dev129-cp312-cp312-win_amd64.whl (434.6 kB view details)

Uploaded CPython 3.12 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev129-cp312-cp312-manylinux_2_35_x86_64.whl (510.1 kB view details)

Uploaded CPython 3.12 manylinux: glibc 2.35+ x86-64

sleipnirgroup_jormungandr-0.0.1.dev129-cp312-cp312-macosx_10_9_universal2.whl (510.1 kB view details)

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

sleipnirgroup_jormungandr-0.0.1.dev129-cp311-cp311-win_amd64.whl (434.4 kB view details)

Uploaded CPython 3.11 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev129-cp311-cp311-manylinux_2_35_x86_64.whl (510.0 kB view details)

Uploaded CPython 3.11 manylinux: glibc 2.35+ x86-64

sleipnirgroup_jormungandr-0.0.1.dev129-cp311-cp311-macosx_10_9_universal2.whl (504.9 kB view details)

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

sleipnirgroup_jormungandr-0.0.1.dev129-cp310-cp310-win_amd64.whl (433.7 kB view details)

Uploaded CPython 3.10 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev129-cp310-cp310-manylinux_2_35_x86_64.whl (507.6 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.35+ x86-64

sleipnirgroup_jormungandr-0.0.1.dev129-cp310-cp310-macosx_11_7_x86_64.whl (503.6 kB view details)

Uploaded CPython 3.10 macOS 11.7+ x86-64

sleipnirgroup_jormungandr-0.0.1.dev129-cp310-cp310-macosx_10_9_universal2.whl (440.0 kB view details)

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

sleipnirgroup_jormungandr-0.0.1.dev129-cp39-cp39-win_amd64.whl (419.3 kB view details)

Uploaded CPython 3.9 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev129-cp39-cp39-manylinux_2_35_x86_64.whl (509.2 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.35+ x86-64

sleipnirgroup_jormungandr-0.0.1.dev129-cp39-cp39-macosx_11_7_x86_64.whl (503.6 kB view details)

Uploaded CPython 3.9 macOS 11.7+ x86-64

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev129-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 0061e766e96b0a24c87defc04ecb8dd9c9f600a6ffd07a7d6c1463cd3e81da48
MD5 4d59ad5c77bb91126ca1725521f673be
BLAKE2b-256 cfd75fb8402202ad1ad81544a413b1d777dceae680eee33963dc94681426e671

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev129-cp312-cp312-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 486ca16300c913aeba743273b5073444d1cf21084aef32ead7e82843bfed06b2
MD5 61c302402c7042b542aef50617f81e80
BLAKE2b-256 89a45dd6d0bd02d0485645a771bbc659a0953e24099d93f2e2c30a7a0b2fc454

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev129-cp312-cp312-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 248b41120c22824c27db8ef98a0599671260307540c4167829f8f12f167ac904
MD5 6760b9055e92b1c30a12ea5d8212d1ba
BLAKE2b-256 d3754e2895ec18949a91d2bc8d2a69a2703390acb5f7af272ef1cbb5dad6b939

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev129-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 56d7bb6d96fb96188c8a6597c0d7ba2cb13c792e3f72914514a24c8b2e50d749
MD5 170137edcfa9ceba731fed61eae03368
BLAKE2b-256 f89d8f3f320f09a2784a2490b052f00a78cd5de53cb4f840fcdb6f929111ed15

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev129-cp311-cp311-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 e1fd0e319063ec19e545cd9044346e2952856f029a2180052059b9796f169578
MD5 bfc6703e06c1f149ed6ed9e3628c9b9a
BLAKE2b-256 9ad86507a4f5a16252c4729de8ef48f3a8c833758d76e8d20ba336737d4e2efa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev129-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 dfb089fa968bb77795c60d446c14a4649bc437989663cad9e0c50df242362843
MD5 6622cb9ace6b1d9cd3993b50b8b32d09
BLAKE2b-256 fc0272e20be6098f06cf1af2e4d5a10393223b33baf5231cd313454d1f4e9523

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev129-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 2e35dee81714b713800157a977884a699047eff26db12a71e17f4867715b36cd
MD5 ef75c53455db047705ca46a8b27896a8
BLAKE2b-256 f3b840f35e5a96f5c678d427cac66857b1c1b512358217b06661cefb79b356ef

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev129-cp310-cp310-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 930f194aacaf1a6d5aeb7dbcc8ce4700a0bf998ab3577edd5d5902e813759cf8
MD5 5c9209c6a061bcb432594979e425d281
BLAKE2b-256 d6fdd6a3f005bae25abb2b2ba948d6dab326eb236c4a070da0622bbe73ed1b34

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev129-cp310-cp310-macosx_11_7_x86_64.whl
Algorithm Hash digest
SHA256 c2f80aac24e6575f9eec94aadc6466295f07efa90bbb8c5d53e06fced648bc54
MD5 d49f7b6d0fa3b6babc70dfad5e958ab1
BLAKE2b-256 c63fbc1b5563f4afc6191520294c017b20d1de6cbad9ef9eceaffb8e355de795

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev129-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 b1cb2d9402543e43dfb69b528e7c532f55526d1bb136d090ab1a98ec94b18f13
MD5 1d7fb83501bd51143e305583114b7f96
BLAKE2b-256 14ab294e6de5804bcbbd34e26b8644ae5bb6be1bbee40eebd8001285ab2d9927

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev129-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 d2bc0e722e13f8852ba333c6e78ea05f96b6e4959d40c7a478e134b21542676a
MD5 eb80403b7e691d6147c71be1a88036e1
BLAKE2b-256 7260ba161331b02e43d9c7e9a94557fe49b86a45a60c9a547825661e7d14a85a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev129-cp39-cp39-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 fb16e3d8a302e7afc3abb30ca03b45dc07bcbd1e6997d9842999b8f71010107e
MD5 c91e8c4a6d7a6bb26eb458a972c1e076
BLAKE2b-256 dddd923c87b50671bda607798f46702dd888ebed125649acca7d427ca2855959

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev129-cp39-cp39-macosx_11_7_x86_64.whl
Algorithm Hash digest
SHA256 db21095c0c7725a4d88a25ed6909f6077d1b1184107ed64ad6655de8e3339f73
MD5 bf2d53d4ab53180fec3b874ddbd12984
BLAKE2b-256 e544d88408a8102685474205ba5ba9ebd6bbcae1611031992dfc07e0e010c769

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