Metadata-Version: 2.2
Name: pycapacity-cpp
Version: 0.1.0
Summary: Compiled (C++) polytope algorithms for pycapacity: HPSM, VEPOLI2 and ICHM
Author: Antun Skuric
License: MIT
Project-URL: Homepage, https://github.com/auctus-team/pycapacity
Requires-Python: >=3.10
Requires-Dist: numpy
Description-Content-Type: text/markdown

# pycapacity-cpp

Compiled (C++) implementations of the polytope algorithms of
[pycapacity](https://github.com/auctus-team/pycapacity):

| algorithm | used for | speed-up over the Python code |
|---|---|---|
| `hyper_plane_shift_method` (HPSM) | velocity / acceleration / human force polytopes (H-rep) | 20-170x |
| `vertex_enumeration_vepoli2` | robot force polytopes (V-rep) | 15-50x |
| `iterative_convex_hull_method` (ICHM) | human polytopes, any problem of the unified form | 10-90x |
| `halfspace_intersection` (H -> V in 2D/3D) | `pycapacity.algorithms.hspace_to_vertex` | 2-4x |

It also provides `project_polytope(proj, ineq, eq=None, tol=None)`, a
general polytope projection `{E x + f | A x <= b, C x = d}` in any output
dimension with the same arguments and result as `pypoman.project_polytope`
(see below).

(Measured with `primer/benchmarks/bench_cpp.py`; the ranges depend on the
problem size, see the benchmark output.) On whole `pycapacity` calls: Franka
Panda velocity/acceleration/force polytopes 0.2/0.34/0.034 ms (Python 1.5-2
ms), 50-muscle human force polytope 1.3 ms at 10 N (Python 39 ms); see
`docs/source/backend.rst`.

When `pycapacity-cpp` is installed, `pycapacity` uses it automatically: the
functions of `pycapacity.algorithms`, and so every `robot.*` and `human.*`
polytope function, keep their inputs and outputs and run in C++. To select
the implementation:

```python
import pycapacity.algorithms as algos
algos.get_backend()          # 'cpp' or 'python'
algos.set_backend("python")  # or "cpp", "auto" (default)
```

or set the environment variable `PYCAPACITY_BACKEND=python` before importing.

## Installation

### conda (recommended)

From the `pycapacity` folder:

```bash
conda env create -f cpp/environment.yml
conda activate pycapacity
```

This creates an environment with pycapacity, the compiled backend and their
dependencies (all from conda-forge). In an existing conda environment:

```bash
conda install -c conda-forge cxx-compiler cmake ninja eigen qhull highs pybind11 scikit-build-core
pip install ./cpp
```

### pip

Once released on PyPI, prebuilt wheels (Linux x86_64/aarch64, macOS
arm64/x86_64, Windows x64, Python 3.10-3.15) need no compiler:

```bash
pip install pycapacity[fast]   # or: pip install pycapacity-cpp
```

From the sources, a C++17 compiler is needed (CMake comes from pip). Eigen,
Qhull and HiGHS are downloaded and built during the build if they are not
found:

```bash
pip install ./cpp
```

### Releasing

The wheels are built and tested by `.github/workflows/cpp-wheels.yml`
(cibuildwheel; every wheel runs `cpp/tests` against the Python
implementation). Pull requests touching `cpp/` build them; a tag
`cpp-vX.Y.Z` matching the version in `cpp/pyproject.toml` also publishes
them to PyPI (trusted publishing, environment `pypi`).

## How the ICHM is made fast

Almost all the time of the ICHM goes into the linear programs, one per facet,
each solved for a new cost on the same constraints
`y_min <= y <= y_max, E y = e, G y <= g`, with very few rows. A general LP
solver spends ~150 us per call in setup even for 3 pivots (HiGHS, GLPK), so
`pycapacity_cpp` uses a small dense bounded-variable primal simplex that keeps
its basis between calls (the previous optimum stays feasible when only the
cost changes), flips all wrong-bound variables at once when possible, and
solves the facets in an order where consecutive normals are close. A typical
LP then takes 2-20 us. Every solution is checked and HiGHS solves it instead
if the check fails (`info["n_lp_fallback"]` counts these; it is 0 in all the
benchmarks and tests). `lp_solver="highs"` forces HiGHS for comparison.

## General projection: `project_polytope`

```python
import pycapacity_cpp as pc
vertices = pc.project_polytope((E, f), (A, b), (C, d), tol=1e-6)
```

Inequality rows with a single nonzero become variable bounds (the box of
the ICHM); the others are general constraint rows. With up to 200 constraint
rows the LPs use the dense simplex; beyond that they go to HiGHS. The
polytope must be bounded (a `ValueError` is raised otherwise). The result is
an inner approximation within `tol` (default `1e-7` of the size; never
below `1e-8` of the size, the precision of the LP solutions).

## Tests

```bash
python -m pytest cpp/tests              # C++ against Python, same polytopes
PYCAPACITY_BACKEND=cpp python -m pytest tests
```

## Using the library from C++

`include/pycapacity_cpp/polycap.hpp` declares the functions (Eigen inputs and
outputs); the CMake target `polycap` is a static library.
