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

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, 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
  • 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.dev221.tar.gz (117.9 kB view details)

Uploaded Source

Built Distributions

sleipnirgroup_jormungandr-0.0.1.dev221-cp312-cp312-win_amd64.whl (465.0 kB view details)

Uploaded CPython 3.12 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev221-cp312-cp312-manylinux_2_35_x86_64.whl (405.4 kB view details)

Uploaded CPython 3.12 manylinux: glibc 2.35+ x86-64

sleipnirgroup_jormungandr-0.0.1.dev221-cp312-cp312-macosx_10_9_universal2.whl (677.7 kB view details)

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

sleipnirgroup_jormungandr-0.0.1.dev221-cp311-cp311-win_amd64.whl (465.0 kB view details)

Uploaded CPython 3.11 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev221-cp311-cp311-manylinux_2_35_x86_64.whl (405.8 kB view details)

Uploaded CPython 3.11 manylinux: glibc 2.35+ x86-64

sleipnirgroup_jormungandr-0.0.1.dev221-cp311-cp311-macosx_10_9_universal2.whl (679.2 kB view details)

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

sleipnirgroup_jormungandr-0.0.1.dev221-cp310-cp310-win_amd64.whl (465.1 kB view details)

Uploaded CPython 3.10 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev221-cp310-cp310-manylinux_2_35_x86_64.whl (406.1 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.35+ x86-64

sleipnirgroup_jormungandr-0.0.1.dev221-cp310-cp310-macosx_10_9_universal2.whl (679.7 kB view details)

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

sleipnirgroup_jormungandr-0.0.1.dev221-cp39-cp39-win_amd64.whl (465.4 kB view details)

Uploaded CPython 3.9 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev221-cp39-cp39-manylinux_2_35_x86_64.whl (406.4 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.35+ x86-64

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev221.tar.gz
Algorithm Hash digest
SHA256 b03e409994a6fcfc2482c4abacb118094895d82110a0b7c9fbcffc0a4603fbb7
MD5 c6855e8226c11dce29c8095ed2eff2cd
BLAKE2b-256 0d06c2daf61a7c0a1bd89adabefaa0c4d2182177710769cdf0f3c93fc27a87fe

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev221-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 b389366d682d52820a80c0e3a918da24260086e1c843a559f98d1586235e1eef
MD5 5c59ecb90dfb0b2c13838a453adaf28f
BLAKE2b-256 682bc9e8f931712eec3d04210f13462c22b3828af65a9d1d3765353548aec948

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev221-cp312-cp312-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 b22f51d51ee17ea5324627d85ba1ff8c8cab7d2178eb4a1807b1461d24f60f9b
MD5 ae2f21d184cae319606ef413b086ea8b
BLAKE2b-256 b256ea4109a65921a3101e1966cabec0cf11202135fa81ffacef4caa99e0128c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev221-cp312-cp312-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 e90bb8518b7dc8120c9750cc46502292668f10804d9c4acda900b1979b340088
MD5 34d1731c7c19bf26ecef5f2b24fb4a1a
BLAKE2b-256 263e49d94e9de9a9cce30e78beec5074b78460a2a672db7a729af747ce859055

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev221-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 1d7446034907e1cfdde63cde416d5d382de8b191ba922bd2cf70af57b7926e26
MD5 02101ca4af1752797f41d51481a2d977
BLAKE2b-256 761101d4284cd25833dd85ca7286e8e33ceb0f94c90999dde1b5f859a50cb75f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev221-cp311-cp311-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 040523983b214019c7e12f26c5d6d6cb05fd0427c28ab9a2ccc341025a9e81be
MD5 524b66a571aa5d41fb9b723819762649
BLAKE2b-256 c35a624155312e69e1033bc74019f93e79acbaf02bbc34317ccba832e77ceaec

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev221-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 54b1197ccda5143e521882cb712b78a7e123b9c53d78197fabb566072d5615d1
MD5 8b0641b8a20e0cd2a17813ceff5b9e0a
BLAKE2b-256 81090f1a7020c77783261fc826bde06224271eea60e394be504471516799e6cc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev221-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 10e7589b0023efc2d0f47509d6bf72fd7bfd32768702fc76705f0a1721f8eace
MD5 0dfa86411fffd9d50de440b5a29e8759
BLAKE2b-256 8a3285a72a37e2eab5ebb9e03ce2ebdb93e87e353b915d261774dd41c863937b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev221-cp310-cp310-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 52482f389d4b045c1003ed5605b5987e3aee3e98ab55d9ceded93d9233313ba5
MD5 540ef5c10dd53f034625f5189f9aca13
BLAKE2b-256 29dd7602bce90334c7cdc241402cb4c1759298f4afc1cb6df879ad9105059407

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev221-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 ae51c9b1c9cf40576df9e372f2b98832aeea9e46ebfd704abb4262621fcdc49c
MD5 2389a9fc999096074a93269b29c98e91
BLAKE2b-256 d00e705f1da0ede9f3f6c7c277566422c0cd37b0a3ce57b40270bb0c97d45206

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev221-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 f2293bf24db6e7134f545e2fb93ef9478ae1828e27f8255a4a110df4632f7d08
MD5 2ffca5e2af853568cd577814bff70119
BLAKE2b-256 054642067cad43244937d324b8f7c96d42f683608c255912bc38de49dd49c82f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev221-cp39-cp39-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 c220304e6fa09bf9399262dd2f67e3de37fe87de3b675934a9d8d47d76cb2ed5
MD5 5fd51bc304f0bdede17fadd3f17d5a6a
BLAKE2b-256 8df5d12ce18817bbe3e6afa22b23972640e04787b996fc61cf1dd43ebd514be0

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