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

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

Uploaded Source

Built Distributions

sleipnirgroup_jormungandr-0.0.1.dev198-cp312-cp312-win_amd64.whl (537.5 kB view details)

Uploaded CPython 3.12 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev198-cp312-cp312-manylinux_2_35_x86_64.whl (603.5 kB view details)

Uploaded CPython 3.12 manylinux: glibc 2.35+ x86-64

sleipnirgroup_jormungandr-0.0.1.dev198-cp312-cp312-macosx_10_9_universal2.whl (1.1 MB view details)

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

sleipnirgroup_jormungandr-0.0.1.dev198-cp311-cp311-win_amd64.whl (537.3 kB view details)

Uploaded CPython 3.11 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev198-cp311-cp311-manylinux_2_35_x86_64.whl (600.8 kB view details)

Uploaded CPython 3.11 manylinux: glibc 2.35+ x86-64

sleipnirgroup_jormungandr-0.0.1.dev198-cp311-cp311-macosx_10_9_universal2.whl (1.0 MB view details)

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

sleipnirgroup_jormungandr-0.0.1.dev198-cp310-cp310-win_amd64.whl (536.8 kB view details)

Uploaded CPython 3.10 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev198-cp310-cp310-manylinux_2_35_x86_64.whl (600.4 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.35+ x86-64

sleipnirgroup_jormungandr-0.0.1.dev198-cp310-cp310-macosx_10_9_universal2.whl (1.0 MB view details)

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

sleipnirgroup_jormungandr-0.0.1.dev198-cp39-cp39-win_amd64.whl (521.7 kB view details)

Uploaded CPython 3.9 Windows x86-64

sleipnirgroup_jormungandr-0.0.1.dev198-cp39-cp39-manylinux_2_35_x86_64.whl (600.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.dev198.tar.gz.

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev198.tar.gz
Algorithm Hash digest
SHA256 1f3ed326dd8bf70e8df03769cfe84ef619bd98b64593534bb621a90cdccc107f
MD5 70ccfccb134a24f49043b2ad990241fb
BLAKE2b-256 c7c40e38dda7d1bde71fa7c65785c38d07397c963c580b0467dc0e1a984ccce2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev198-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 c431acc843203cd7b50e3bdc4a52683b0bff0743f0b240e2dcc41aeff80bfdee
MD5 9767bf685e50b9b0dbf99bcfba7ae53e
BLAKE2b-256 c8c94ad846d7720b3590f28d005460dfc0aded4b2d08df5687a517a6ddde3ffc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev198-cp312-cp312-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 121c19b6270ba31f1bb5694ccb7098069abb05032b0c3c82f3bed7caff56bcab
MD5 be87afbde8c2ff7d19cdcf5cf5f0da9f
BLAKE2b-256 00be3572ba464b013264e392331ced679f7e4c073fe85c07fcd4fa5ed8a06e5f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev198-cp312-cp312-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 cd10aad075f552b33bf3e5b7323ff1b076dc67e8814bb6624d188f6da4265e4e
MD5 7a01039d30e76a87535bcd3651376259
BLAKE2b-256 425c4a0f888850678eee50869b46cff6455e15dee4de79e9ccce60cc5404983e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev198-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 2227a2beab74651684eefcedf840fe488037ef744dd6868d1e38f809a29efbeb
MD5 2e4ccbee7c65106dc9d25057d817201f
BLAKE2b-256 8fa2b758e5943d64774838270fb513a995f566d50d5cc92cf928317914e17d7f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev198-cp311-cp311-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 ad439d9dcfec75c9d370209d6ffbd3746c08699ec971899f645a43a52d204115
MD5 a1916e8c2055c31e210d07458b521060
BLAKE2b-256 bedd026686ea2fde709fbdada1bc337e9aede0a1e0b97aa89bf0090a885738d9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev198-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 df70fc712dd13783758fd457ce9f6196da0c134931db160b4a0e91a987c045cb
MD5 e76106fa8ca7fabac1edca6fe13ffb17
BLAKE2b-256 7b97e2bdf024891f13211b1c48d6648c1b5538bbd096bc46045f6f77afff1fad

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev198-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 6b3298f943e857bd058fec2b739c3c772144ad5cf1eda3fcb8cb55e5858f6491
MD5 a8f29111d9adbab274c1ecaa614fe040
BLAKE2b-256 a22f66e6110db5af4e704aa8f235b5be4219479aadef0aab1111aa6ee59bd070

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev198-cp310-cp310-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 86115fda04882289e536f45409afe441151939fddd7405691ab3d89a8fd65cc3
MD5 83a12fbf3a76c15f7155e669858537e8
BLAKE2b-256 13f045018db520cb50884a5b3c25c771fbdcf788e30203d1f9b7cbf9f4af2c52

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev198-cp310-cp310-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 18832ca4de65c1c4e365f19ca3e007ca2714f7992f6d9e50d6ad0f1f9330e979
MD5 675136e7c780136772878c195f7b4af0
BLAKE2b-256 5b891350a42514b553a893b972f9c5fedef29c73f950c548f8354bcf6a29ee20

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev198-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 1a425b32fecfd5a8163ed8866f630b33c4ac103985888b1e5b7a0286a0c5f311
MD5 1e76ed84008f3c9a18223079149843f4
BLAKE2b-256 60b1a38b58f959a7c29b7dd4f703d5cf17ae41f6b38284c0d36dd1cd26171521

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev198-cp39-cp39-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 59c1ccb82f9e6dbf2bbe85c4fe42f9f5ce42f94088f69f4ef19bdfe27813f64b
MD5 1575b413a6b005153cfe6f8b834ebbea
BLAKE2b-256 f86c9ef2d850d7e20a10711345834f7ab92aee392c0b85be60b49fa9377d30b4

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