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

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++23 compiler
    • On Windows, install Visual Studio Community 2022 and select the C++ programming language during installation
    • On Linux, install GCC 14 or greater via sudo apt install g++
    • On macOS 14 or greater, install the Xcode command-line build tools via xcode-select --install. Xcode 15.3 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
  • 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.dev147.tar.gz (94.5 kB view details)

Uploaded Source

Built Distributions

sleipnirgroup_jormungandr-0.0.1.dev147-cp312-cp312-win_amd64.whl (480.3 kB view details)

Uploaded CPython 3.12 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev147-cp312-cp312-manylinux_2_35_x86_64.whl (527.0 kB view details)

Uploaded CPython 3.12 manylinux: glibc 2.35+ x86-64

sleipnirgroup_jormungandr-0.0.1.dev147-cp312-cp312-macosx_10_9_universal2.whl (862.1 kB view details)

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

sleipnirgroup_jormungandr-0.0.1.dev147-cp311-cp311-win_amd64.whl (480.0 kB view details)

Uploaded CPython 3.11 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev147-cp311-cp311-manylinux_2_35_x86_64.whl (525.9 kB view details)

Uploaded CPython 3.11 manylinux: glibc 2.35+ x86-64

sleipnirgroup_jormungandr-0.0.1.dev147-cp311-cp311-macosx_10_9_universal2.whl (854.0 kB view details)

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

sleipnirgroup_jormungandr-0.0.1.dev147-cp310-cp310-win_amd64.whl (479.3 kB view details)

Uploaded CPython 3.10 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev147-cp310-cp310-manylinux_2_35_x86_64.whl (525.2 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.35+ x86-64

sleipnirgroup_jormungandr-0.0.1.dev147-cp310-cp310-macosx_10_9_universal2.whl (851.4 kB view details)

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

sleipnirgroup_jormungandr-0.0.1.dev147-cp39-cp39-win_amd64.whl (464.6 kB view details)

Uploaded CPython 3.9 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev147-cp39-cp39-manylinux_2_35_x86_64.whl (525.0 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.35+ x86-64

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev147.tar.gz
Algorithm Hash digest
SHA256 0bae255ba985904638a7cc546cb6f85cf6642edd92eef8a54e722dec1790f260
MD5 68acc9b1975624bef89d414ccb8bb72e
BLAKE2b-256 7ab05abc7de180554d871ad9088d011dbdd80eee147d2b26c000d438f8a8b6dd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev147-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 4f80e20d8135b15b29c2471ffa4546210887ff528ec2d7ecac95e28d409f1efc
MD5 2a6305a4a765f800f6efbf76fbadaa83
BLAKE2b-256 d164ff2a721aad62612fec9f3400b2d5265f0826ad89283ff970185d3adc898b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev147-cp312-cp312-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 086b6bf9faabbc068bc8c3980cd1325b387b91c78f61ebb6179224019654d812
MD5 ea5f31e9f11adef8ecdf2a64ff82fdd8
BLAKE2b-256 9cd9f6b79c51c5f65eda34211f089339edacdea86edf50c5a6ff2c356b501aef

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev147-cp312-cp312-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 ad78272269fd48cf97dd457cf16dc7ba49c2d6333237889054fb4116836b4b7c
MD5 2cd73af59e78a1135e74a4384256aede
BLAKE2b-256 b0d7bd7e036ea00b481e9d904dc4f47c00d82d5a24ff079dea70f8cd4a0ffcc4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev147-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 f4fcbccbcc7c41616b6938d531b5a6bf856b1cd41d75ea2ab4390b07ddef68b8
MD5 15b54ec082fad50dcd85929a8158b1b9
BLAKE2b-256 ad48ba659a4f992d879fe77b9bdda755f1c2b7f457283e1ea3195118094ca826

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev147-cp311-cp311-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 69d0afe5cc8215a99ba1b02122f9dc7ad90e720fda7015bbca0aa65fc9c2e23a
MD5 b981ed6912712fa5f2d840bdc8004c30
BLAKE2b-256 e29ecd82777bd22f8a381955526dd9a441364bcf4792285f2f8c09e7a6c79d09

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev147-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 ba3c1f275bc2dfd5e119966cb7baa4e3dc157cbad49fc297470a1ee5b7ab2bbf
MD5 7e3c845714933d2b8d753d2ae65f39c9
BLAKE2b-256 49a608cbeacc26ac5a07ce74ad91c6e83cd72bcb5b4c0a05152f4b42ef6996d5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev147-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 447b8c29f032426930f7ba2a6f3ecf8b101dd565dd83b741e74caffcc74cb009
MD5 e6ee01e05ad9837c32d38360eef37db4
BLAKE2b-256 1f79483fb980d58db770452d6802a2909fad7486529e8599c9630e8fddb9d296

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev147-cp310-cp310-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 70be82f06df0a8675b8400dc6f94094c15fbeebcd96370e5e7adeed91eaff624
MD5 ea4eba625f0b89244c00377e66c9b701
BLAKE2b-256 5872a703005e9c57d6e0396c9e0bb73502fc1a16a67ca655204c5ef87f6da817

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev147-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 039b091e6a846283e97577924ee371b0d1bd7aaed6540297e91966a815848b93
MD5 c2d8874f7bb4bcf280ac7d6d339455fb
BLAKE2b-256 ef267677bca75c03846bed36b8b778f1fd4eaaab87f290f58881ffe7add81a30

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev147-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 b7341817466a01ea2e7a30e885fbd702c3a8da15eb2ff05337b561d394c28436
MD5 cfea2271c0b89e4eb7a2ba2b8503b05c
BLAKE2b-256 dd205bba93380bd4b8a4901abd6edc0b1e4d72bbb47e31982c538a90bcad9fd2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev147-cp39-cp39-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 5cb93bc8dffdd0e7f87ab57e857f115b3e3064ff9b88fee2e4ec3ed0ca15187e
MD5 aaf86620097f9b4fea531f279f922760
BLAKE2b-256 660442b7f4b5afc1120d3796ddf2a3c2ab9724321cb4079a0580c340077555e0

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