C++ acceleration

pycapacity is written in Python and needs nothing else. The optional package pycapacity-cpp makes the same functions 6 to 50 times faster. It is in the cpp/ folder of the repository and provides C++ implementations of the polytope algorithms of pycapacity:

  • hyper_plane_shift_method (HPSM): velocity and acceleration polytopes (robot and human)

  • vertex_enumeration_vepoli2: robot force polytopes

  • iterative_convex_hull_method (ICHM): human force and velocity polytopes, reachable space, any problem of the unified form

  • hspace_to_vertex in 2D and 3D: every polytope given by its half-spaces

When it 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++.

import pycapacity.algorithms as algos
algos.get_backend()          # 'cpp' or 'python'
algos.set_backend("python")  # or "cpp", "auto" (default: C++ if installed)

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

Installation

pip install pycapacity[fast]

Pre-built wheels are available for Linux, macOS and Windows; see Installation for building it from source or with conda.

Linear programs of the Python implementation

Without pycapacity-cpp, the ICHM and the other functions that solve linear programs use HiGHS (highspy, installed with pycapacity), or GLPK when cvxopt is installed (pip install pycapacity[cvxopt]) and the problem is small (up to 64 variables), where it is faster. The choice can be forced:

import pycapacity.algorithms as algos
algos.set_lp_solver("highs")   # or "glpk", "auto" (default)

or with the environment variable PYCAPACITY_LP_SOLVER.

Timings

On whole pycapacity calls, the C++ implementation is 6 to 50 times faster than the Python one: from 6 to 7 times for the robot velocity and acceleration polytopes (HPSM) to about 50 times for the robot force polytope (VEPOLI2), and 25 to 30 times for the human force and velocity polytopes (ICHM). The measured times of every metric, with both implementations, are in the tables of Polytope evaluation algorithms; the comparison with other methods (Qhull, DDM, Bretl and Lall) is in Comparison with other methods.

Run examples/scripts/backend_comparison.py for a quick check on your machine.

Why the ICHM is 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 with very few rows. A general LP solver spends ~150 µs 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 µs. Every solution is checked and HiGHS solves it instead if the check fails.

General polytope projection

pycapacity_cpp.project_polytope computes the projection of any bounded polytope

\[P = \{E x + f ~|~ A x \leq b,~ C x = d\}\]

in any output dimension, with the same arguments and result as pypoman.project_polytope, using the ICHM: the result is within tol of the exact projection (an inner approximation; exact up to the LP precision for small tol).

import pycapacity_cpp as pc
vertices = pc.project_polytope((E, f), (A, b), (C, d), tol=1e-6)
vertices, H, h, info = pc.project_polytope((E, f), (A, b), (C, d), tol=1e-6, return_info=True)

Inequality rows with a single nonzero are used as variable bounds; with up to 200 other constraint rows the LPs use the dense simplex, beyond that HiGHS. See examples/scripts/general_projection.py.