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/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::print("x = {}, y = {}\n", 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.4 (autodiff and NLP solver frontend)
  • Ipopt 3.14.13 (NLP solver backend)
  • MUMPS 5.5.1 (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 the OS package manager
    • On macOS, install Xcode command-line build tools 13 or greater via xcode-select --install
  • CMake 3.21 or greater
    • On Windows, install Visual Studio Community 2022 and select the C++ programming language during installation
    • On Linux, install via the OS package manager
    • On macOS, install via brew install cmake
  • Eigen
  • fmtlib (internal only)
  • pybind11 (build only)
  • googletest (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

Python library

# Setup
pip install --user build

# Build
python -m build --wheel

# Install
pip install --user dist/sleipnirgroup_jormungandr-*.whl

# Test
pytest

Supported build types

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

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 Distribution

sleipnirgroup_jormungandr-0.0.1.dev3-cp310-cp310-manylinux_2_35_x86_64.whl (459.0 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.35+ x86-64

File details

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

File metadata

File hashes

Hashes for sleipnirgroup_jormungandr-0.0.1.dev3-cp310-cp310-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 11d13701b7fd10de9a0883c7a6d5ed2ea84c6b87cb0c97fb70a0a4b9a008d200
MD5 e8a02fa9d901e3b3a1af1fc8048b5fe1
BLAKE2b-256 cc52752b26dcb1a541ec165e21d430135d6953db9167554dfe1dc88bb9414c75

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