Metadata-Version: 2.4
Name: lunopt
Version: 0.14.1
Summary: LUNOPT, the Lightweight Unified Nonlinear OPTimiser: a sparse SQP for optimal control and trajectory optimisation
Keywords: optimisation,nonlinear programming,SQP,optimal control,trajectory optimisation,pygmo
Author-Email: "Luna Space Ltd." <info@lunaspace.eu>
License-Expression: MPL-2.0
License-File: LICENSE
License-File: THIRD_PARTY_NOTICES.md
License-File: licenses/COLAMD-notice.txt
License-File: licenses/Eigen-Apache-2.0.txt
License-File: licenses/Eigen-BSD-3-Clause-Intel.txt
License-File: licenses/Eigen-MPL-2.0.txt
License-File: licenses/GCC-Runtime-Library-Exception-3.1.txt
License-File: licenses/SuperLU-notice.txt
License-File: licenses/nanobind-BSD-3-Clause.txt
License-File: licenses/robin-map-MIT.txt
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: C++
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Scientific/Engineering :: Mathematics
Project-URL: Homepage, https://github.com/luna-space/lunopt
Project-URL: Source, https://github.com/luna-space/lunopt
Project-URL: Changelog, https://github.com/luna-space/lunopt/blob/main/CHANGELOG.md
Project-URL: Documentation, https://github.com/luna-space/lunopt/blob/main/docs/algorithm.md
Requires-Python: >=3.12
Requires-Dist: numpy>=1.26
Provides-Extra: pygmo
Requires-Dist: pygmo>=2.19; extra == "pygmo"
Description-Content-Type: text/markdown

# LUNOPT

The Lightweight Unified Nonlinear OPTimiser: a sparse SQP for smooth nonlinear programmes, by Luna Space. It is made
for optimal control and trajectory optimisation (collocation, single and multiple shooting), tuned and measured on
problems of up to about 10^4 unknowns; larger ones solve too, without that tuning.

- Smooth objectives, equality constraints, inequality constraints and ranges (by slack unknowns: docs/algorithm.md
  section 1), and bounds.
- A sparse exact-Hessian SQP. One sparse LDLᵀ factorisation of the KKT system serves the
  Newton step, the inertia correction, the interior point on the bounds, the second-order
  corrections and the projection back to feasibility. Its globalisation joins a filter line
  search to a trust region by regularisation; where it does not converge, a feasibility
  fallback keeps the best feasible point (or finds one) and improves it.
- The Hessian by differences of the gradient, for problems with first derivatives alone: the
  unknowns grouped by the Hessian's sparsity (Powell and Toint's symmetric grouping), so
  that a Hessian costs one gradient and Jacobian a group (`lunopt/differences.hpp`).
- A quasi-Newton Hessian for problems whose evaluations are dear (propagations: single and
  multiple shooting): each function's Hessian kept by symmetric rank-one updates of its own
  gradient's changes, at no evaluation beyond the iterations' (`lunopt/quasi_newton.hpp`).
- An optimum's parametric sensitivity: the derivatives of the answer, its multipliers and its
  objective (to second order) with respect to the problem's parameters, by the KKT system on
  the active set, with the range over which the active set holds and one-sided derivatives at
  weakly active bounds (`lunopt/sensitivity.hpp`).
- A diagnosis of a solve: the violated constraints, the prices, the bounds held, the
  dependent constraints and the unknowns nothing depends on.
- A dense SQP with a damped BFGS update and an active-set QP, for small problems.
- Header-only C++23 on Eigen 5; dual numbers for problems by automatic differentiation.
- Python bindings (`lunopt.minimise`) and a pygmo algorithm (`pygmo.algorithm( lunopt.sqp() )`).

## Use

```cpp
#include <lunopt/ad.hpp>
#include <lunopt/sparse_sqp.hpp>

// min x0^2 + x1^2  subject to  x0 x1 = 1,  x0 >= 0.
const auto problem = lunopt::sparse_by_ad(
    { lunopt::Variable{ .name = "x0", .lower = 0.0 }, lunopt::Variable{ .name = "x1" } },
    { lunopt::Constraint{ .name = "product", .target = 1.0, .tolerance = 1e-10 } },
    []( const auto& x ) { return std::vector{ x[0] * x[1] }; },
    []( const auto& x ) { return x[0] * x[0] + x[1] * x[1]; } );
const auto r = lunopt::minimise_sparse( problem, Eigen::VectorXd{ { 2.0, 0.3 } } );
// r->z = ( 1, 1 ), r->converged; on failure, lunopt::diagnose( problem, *r ) says why.
```

(`examples/readme.cpp`, run by the tests.) A large problem gives its sparse Jacobian and the Lagrangian's Hessian itself
(`lunopt::SparseProblem`). With CMake:

```cmake
add_subdirectory(lunopt)  # or FetchContent; Eigen 5.0.1 fetched unless Eigen3::Eigen exists
target_link_libraries(your_target PRIVATE lunopt::lunopt)
```

## Python

```python
import numpy as np
import lunopt

# min x0^2 + x1^2  subject to  x0 x1 = 1,  x0 >= 0
problem = lunopt.Problem(
    objective=lambda x: x @ x,
    gradient=lambda x: 2 * x,
    constraints=lambda x: [x[0] * x[1] - 1.0],
    jacobian=lambda x: [[x[1], x[0]]],                       # or a scipy.sparse matrix, or ( rows, columns, entries )
    hessian=lambda x, s, y: [[2 * s, 0.0], [y[0], 2 * s]],   # of s f + y^T c; its lower triangle read
    lower=[0.0, -np.inf],
)
r = lunopt.minimise(problem, [2.0, 0.3], progress=True)     # r.x = ( 1, 1 ), r.multipliers = ( -2 ), r.converged
```

Constraints have ranges (`constraint_lower`, `constraint_upper`; equal bounds make an equation); without a Hessian
the solver takes it by differences of the gradient (`method="differences"`, on the problem's `hessian_pattern` or the
one its derivatives' structure gives), by quasi-Newton updates where an evaluation is dear (`method="quasi-newton"`), or,
for small problems, a damped BFGS matrix (`method="bfgs"`); a function may raise
`lunopt.PointRefused` where it cannot evaluate a point, and the solver takes a shorter step; `lunopt.diagnose( problem,
r )` says why a solve ended where it did. An optimum's sensitivity to the problem's parameters q:

```python
def problem_at(q):                      # the problem made at q (here x0 x1 = q[0]): anything of it may depend on q
    return lunopt.Problem(
        objective=lambda x: x @ x,
        gradient=lambda x: 2 * x,
        constraints=lambda x: [x[0] * x[1] - q[0]],
        jacobian=lambda x: [[x[1], x[0]]],
        hessian=lambda x, s, y: [[2 * s, 0.0], [y[0], 2 * s]],
        lower=[0.0, -np.inf],
    )

q = np.array([1.0])
r = lunopt.minimise(problem_at(q), [2.0, 0.3])
s = lunopt.sensitivity(problem_at, q, r, second_order=True)
# s.x: dx*/dq = ( 0.5, 0.5 ); s.objective, s.second: df*/dq = 2, d2f*/dq2 = 0 (f* = 2 q); s.range_ahead,
# s.range_behind: how far along each parameter the active set holds; s.predict( r.x, dq ), s.predict_objective( r.f,
# dq ): the optimum at q + dq
```

In pygmo:

```python
import pygmo as pg
pop = pg.algorithm(lunopt.sqp()).evolve(pg.population(pg.problem(pg.hock_schittkowski_71()), 5))
```

The module is built by CMake (`-DLUNOPT_PYTHON=ON`, a Python 3.12+ with nanobind 3.1; the `gate` and `clang` presets
use the repository's `.venv-python`) into `build/<preset>/python/lunopt`.

## Install

- **C++**: header-only. With CMake, `add_subdirectory` or `FetchContent` of this repository (Eigen 5.0.1 is fetched
  where the project has no `Eigen3::Eigen` target), or `cmake --install` it and `find_package( lunopt )` (an installed
  Eigen 5 then); link `lunopt::lunopt`. A C++23 compiler: GCC 15 or 16, Clang 22.
- **Python**: `pip install lunopt` (Linux wheels for CPython 3.12 to 3.14, from the first public release; numpy, and
  pygmo for `lunopt.sqp`), or from source with `pip install .` (scikit-build-core, CMake, a C++23 compiler).

## Build and test

```bash
bash tools/check.sh            # the gate: GCC, Clang, the sanitizers, clang-format, clang-tidy
bash tools/check.sh gate       # one build and its tests (GCC)
```

The presets are in `CMakePresets.json`; the Python tests use Python 3.12 in `.venv-python` with nanobind 3.1.0 and
pygmo. The benchmarks against IPOPT on CUTEst's problems are in [docs/benchmarks.md](docs/benchmarks.md), with how to
run them (`bench/`).

## Documentation

- [docs/algorithm.md](docs/algorithm.md): the algorithm, with the equations the code's comments cite.
- [docs/benchmarks.md](docs/benchmarks.md): the measurements, on CUTEst's problems against IPOPT.
- [CHANGELOG.md](CHANGELOG.md): every version's changes.
- The headers' comments document the C++ API; the Python functions' docstrings document theirs.

## Status and roadmap

In development: the API may still change before 1.0. LUNOPT was extracted from Luna Space's flight dynamics engine,
where it designs many-revolution low-thrust transfers. Next:

- Linux wheels on PyPI, then conda-forge;
- a stronger handling of inequality constraints, linear and nonlinear (the benchmarks' weakest part);
- an API reference and a user's guide, with examples in C++ and Python;
- LUNOPT as a pagmo algorithm in C++, beside the Python one for pygmo.

## Licence

LUNOPT is free software under the [Mozilla Public License 2.0](LICENSE): use it in any program, open or proprietary;
changes to LUNOPT's own files are shared under the same licence when you distribute them. Copyright © 2026 Luna Space
Ltd. Third-party software in the binary packages: [THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md). Contributing:
[CONTRIBUTING.md](CONTRIBUTING.md); conduct: [CODE_OF_CONDUCT.md](CODE_OF_CONDUCT.md); security reports:
[SECURITY.md](SECURITY.md).
