pycapacity-cpp 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pycapacity_cpp-0.1.0/CMakeLists.txt +115 -0
- pycapacity_cpp-0.1.0/PKG-INFO +130 -0
- pycapacity_cpp-0.1.0/README.md +119 -0
- pycapacity_cpp-0.1.0/conda-recipe/meta.yaml +45 -0
- pycapacity_cpp-0.1.0/environment.yml +36 -0
- pycapacity_cpp-0.1.0/include/pycapacity_cpp/combinations.hpp +12 -0
- pycapacity_cpp-0.1.0/include/pycapacity_cpp/polycap.hpp +116 -0
- pycapacity_cpp-0.1.0/pyproject.toml +35 -0
- pycapacity_cpp-0.1.0/python/pycapacity_cpp/__init__.py +249 -0
- pycapacity_cpp-0.1.0/src/bindings.cpp +120 -0
- pycapacity_cpp-0.1.0/src/box_lp.cpp +286 -0
- pycapacity_cpp-0.1.0/src/box_lp.hpp +41 -0
- pycapacity_cpp-0.1.0/src/combinations.cpp +23 -0
- pycapacity_cpp-0.1.0/src/hpsm.cpp +97 -0
- pycapacity_cpp-0.1.0/src/hrep.cpp +151 -0
- pycapacity_cpp-0.1.0/src/hull.cpp +77 -0
- pycapacity_cpp-0.1.0/src/ichm.cpp +393 -0
- pycapacity_cpp-0.1.0/src/vepoli2.cpp +137 -0
- pycapacity_cpp-0.1.0/tests/test_against_python.py +168 -0
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
cmake_minimum_required(VERSION 3.18)
|
|
2
|
+
project(pycapacity_cpp LANGUAGES C CXX)
|
|
3
|
+
|
|
4
|
+
set(CMAKE_CXX_STANDARD 17)
|
|
5
|
+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
|
6
|
+
if(NOT CMAKE_BUILD_TYPE)
|
|
7
|
+
set(CMAKE_BUILD_TYPE Release)
|
|
8
|
+
endif()
|
|
9
|
+
|
|
10
|
+
option(PYCAPACITY_CPP_FETCH_DEPS
|
|
11
|
+
"Download Eigen and Qhull if they are not found on the system" ON)
|
|
12
|
+
option(PYCAPACITY_CPP_USE_HIGHS
|
|
13
|
+
"Use HiGHS (if found) as fallback LP solver of the ICHM" ON)
|
|
14
|
+
|
|
15
|
+
include(FetchContent)
|
|
16
|
+
|
|
17
|
+
# inside a conda environment, use its libraries (eigen, qhull, highs, ...)
|
|
18
|
+
if(DEFINED ENV{CONDA_PREFIX})
|
|
19
|
+
list(APPEND CMAKE_PREFIX_PATH $ENV{CONDA_PREFIX})
|
|
20
|
+
endif()
|
|
21
|
+
|
|
22
|
+
# Eigen (header only; conda-forge: eigen)
|
|
23
|
+
find_package(Eigen3 3.3 QUIET NO_MODULE)
|
|
24
|
+
if(NOT TARGET Eigen3::Eigen)
|
|
25
|
+
if(NOT PYCAPACITY_CPP_FETCH_DEPS)
|
|
26
|
+
message(FATAL_ERROR "Eigen3 not found (conda install -c conda-forge eigen)")
|
|
27
|
+
endif()
|
|
28
|
+
message(STATUS "Eigen3 not found: downloading Eigen 3.4.0")
|
|
29
|
+
FetchContent_Declare(eigen
|
|
30
|
+
URL https://gitlab.com/libeigen/eigen/-/archive/3.4.0/eigen-3.4.0.tar.gz)
|
|
31
|
+
FetchContent_GetProperties(eigen)
|
|
32
|
+
if(NOT eigen_POPULATED)
|
|
33
|
+
FetchContent_Populate(eigen)
|
|
34
|
+
endif()
|
|
35
|
+
add_library(Eigen3::Eigen INTERFACE IMPORTED)
|
|
36
|
+
set_target_properties(Eigen3::Eigen PROPERTIES
|
|
37
|
+
INTERFACE_INCLUDE_DIRECTORIES ${eigen_SOURCE_DIR})
|
|
38
|
+
endif()
|
|
39
|
+
|
|
40
|
+
# Qhull, reentrant C library (conda-forge: qhull)
|
|
41
|
+
find_package(Qhull QUIET)
|
|
42
|
+
if(TARGET Qhull::qhull_r)
|
|
43
|
+
set(QHULL_TARGET Qhull::qhull_r)
|
|
44
|
+
else()
|
|
45
|
+
if(NOT PYCAPACITY_CPP_FETCH_DEPS)
|
|
46
|
+
message(FATAL_ERROR "Qhull not found (conda install -c conda-forge qhull)")
|
|
47
|
+
endif()
|
|
48
|
+
message(STATUS "Qhull not found: downloading Qhull 2020.2")
|
|
49
|
+
FetchContent_Declare(qhull
|
|
50
|
+
URL https://github.com/qhull/qhull/archive/refs/tags/2020.2.tar.gz)
|
|
51
|
+
FetchContent_GetProperties(qhull)
|
|
52
|
+
if(NOT qhull_POPULATED)
|
|
53
|
+
FetchContent_Populate(qhull)
|
|
54
|
+
endif()
|
|
55
|
+
file(GLOB QHULL_R_SOURCES ${qhull_SOURCE_DIR}/src/libqhull_r/*.c)
|
|
56
|
+
add_library(qhull_r_static STATIC ${QHULL_R_SOURCES})
|
|
57
|
+
target_include_directories(qhull_r_static PUBLIC ${qhull_SOURCE_DIR}/src)
|
|
58
|
+
set_target_properties(qhull_r_static PROPERTIES POSITION_INDEPENDENT_CODE ON)
|
|
59
|
+
set(QHULL_TARGET qhull_r_static)
|
|
60
|
+
endif()
|
|
61
|
+
|
|
62
|
+
# HiGHS, optional fallback LP solver (conda-forge: highs); built from source
|
|
63
|
+
# as a static library when not found (pip / wheel builds)
|
|
64
|
+
if(PYCAPACITY_CPP_USE_HIGHS)
|
|
65
|
+
find_package(highs QUIET)
|
|
66
|
+
if(NOT TARGET highs::highs AND PYCAPACITY_CPP_FETCH_DEPS)
|
|
67
|
+
message(STATUS "HiGHS not found: building HiGHS 1.7.2 from source")
|
|
68
|
+
set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)
|
|
69
|
+
set(BUILD_TESTING OFF CACHE BOOL "" FORCE)
|
|
70
|
+
set(FAST_BUILD ON CACHE BOOL "" FORCE)
|
|
71
|
+
set(ZLIB OFF CACHE BOOL "" FORCE)
|
|
72
|
+
set(BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
|
|
73
|
+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
|
74
|
+
FetchContent_Declare(highs
|
|
75
|
+
URL https://github.com/ERGO-Code/HiGHS/archive/refs/tags/v1.7.2.tar.gz)
|
|
76
|
+
FetchContent_MakeAvailable(highs)
|
|
77
|
+
if(NOT TARGET highs::highs AND TARGET highs)
|
|
78
|
+
add_library(highs::highs ALIAS highs)
|
|
79
|
+
endif()
|
|
80
|
+
endif()
|
|
81
|
+
endif()
|
|
82
|
+
|
|
83
|
+
# C++ library (usable on its own from C++)
|
|
84
|
+
add_library(polycap STATIC
|
|
85
|
+
src/combinations.cpp
|
|
86
|
+
src/hpsm.cpp
|
|
87
|
+
src/vepoli2.cpp
|
|
88
|
+
src/hull.cpp
|
|
89
|
+
src/hrep.cpp
|
|
90
|
+
src/box_lp.cpp
|
|
91
|
+
src/ichm.cpp)
|
|
92
|
+
target_include_directories(polycap PUBLIC
|
|
93
|
+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>)
|
|
94
|
+
target_link_libraries(polycap PUBLIC Eigen3::Eigen ${QHULL_TARGET})
|
|
95
|
+
if(TARGET highs::highs)
|
|
96
|
+
message(STATUS "HiGHS found: used as fallback LP solver")
|
|
97
|
+
target_link_libraries(polycap PUBLIC highs::highs)
|
|
98
|
+
target_compile_definitions(polycap PUBLIC PYCAPACITY_CPP_HAVE_HIGHS)
|
|
99
|
+
else()
|
|
100
|
+
message(STATUS "HiGHS not used")
|
|
101
|
+
endif()
|
|
102
|
+
set_target_properties(polycap PROPERTIES POSITION_INDEPENDENT_CODE ON)
|
|
103
|
+
if(MSVC)
|
|
104
|
+
target_compile_options(polycap PRIVATE /W4)
|
|
105
|
+
else()
|
|
106
|
+
target_compile_options(polycap PRIVATE -Wall -Wextra)
|
|
107
|
+
endif()
|
|
108
|
+
|
|
109
|
+
# Python extension module pycapacity_cpp._core
|
|
110
|
+
find_package(Python 3.8 REQUIRED COMPONENTS Interpreter Development.Module)
|
|
111
|
+
find_package(pybind11 CONFIG REQUIRED)
|
|
112
|
+
pybind11_add_module(_core MODULE src/bindings.cpp)
|
|
113
|
+
target_link_libraries(_core PRIVATE polycap)
|
|
114
|
+
# only this component goes into the wheel (not the install rules of HiGHS)
|
|
115
|
+
install(TARGETS _core LIBRARY DESTINATION pycapacity_cpp COMPONENT python)
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
Metadata-Version: 2.2
|
|
2
|
+
Name: pycapacity-cpp
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Compiled (C++) polytope algorithms for pycapacity: HPSM, VEPOLI2 and ICHM
|
|
5
|
+
Author: Antun Skuric
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/auctus-team/pycapacity
|
|
8
|
+
Requires-Python: >=3.10
|
|
9
|
+
Requires-Dist: numpy
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
|
|
12
|
+
# pycapacity-cpp
|
|
13
|
+
|
|
14
|
+
Compiled (C++) implementations of the polytope algorithms of
|
|
15
|
+
[pycapacity](https://github.com/auctus-team/pycapacity):
|
|
16
|
+
|
|
17
|
+
| algorithm | used for | speed-up over the Python code |
|
|
18
|
+
|---|---|---|
|
|
19
|
+
| `hyper_plane_shift_method` (HPSM) | velocity / acceleration / human force polytopes (H-rep) | 20-170x |
|
|
20
|
+
| `vertex_enumeration_vepoli2` | robot force polytopes (V-rep) | 15-50x |
|
|
21
|
+
| `iterative_convex_hull_method` (ICHM) | human polytopes, any problem of the unified form | 10-90x |
|
|
22
|
+
| `halfspace_intersection` (H -> V in 2D/3D) | `pycapacity.algorithms.hspace_to_vertex` | 2-4x |
|
|
23
|
+
|
|
24
|
+
It also provides `project_polytope(proj, ineq, eq=None, tol=None)`, a
|
|
25
|
+
general polytope projection `{E x + f | A x <= b, C x = d}` in any output
|
|
26
|
+
dimension with the same arguments and result as `pypoman.project_polytope`
|
|
27
|
+
(see below).
|
|
28
|
+
|
|
29
|
+
(Measured with `primer/benchmarks/bench_cpp.py`; the ranges depend on the
|
|
30
|
+
problem size, see the benchmark output.) On whole `pycapacity` calls: Franka
|
|
31
|
+
Panda velocity/acceleration/force polytopes 0.2/0.34/0.034 ms (Python 1.5-2
|
|
32
|
+
ms), 50-muscle human force polytope 1.3 ms at 10 N (Python 39 ms); see
|
|
33
|
+
`docs/source/backend.rst`.
|
|
34
|
+
|
|
35
|
+
When `pycapacity-cpp` is installed, `pycapacity` uses it automatically: the
|
|
36
|
+
functions of `pycapacity.algorithms`, and so every `robot.*` and `human.*`
|
|
37
|
+
polytope function, keep their inputs and outputs and run in C++. To select
|
|
38
|
+
the implementation:
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
import pycapacity.algorithms as algos
|
|
42
|
+
algos.get_backend() # 'cpp' or 'python'
|
|
43
|
+
algos.set_backend("python") # or "cpp", "auto" (default)
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
or set the environment variable `PYCAPACITY_BACKEND=python` before importing.
|
|
47
|
+
|
|
48
|
+
## Installation
|
|
49
|
+
|
|
50
|
+
### conda (recommended)
|
|
51
|
+
|
|
52
|
+
From the `pycapacity` folder:
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
conda env create -f cpp/environment.yml
|
|
56
|
+
conda activate pycapacity
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
This creates an environment with pycapacity, the compiled backend and their
|
|
60
|
+
dependencies (all from conda-forge). In an existing conda environment:
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
conda install -c conda-forge cxx-compiler cmake ninja eigen qhull highs pybind11 scikit-build-core
|
|
64
|
+
pip install ./cpp
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### pip
|
|
68
|
+
|
|
69
|
+
Once released on PyPI, prebuilt wheels (Linux x86_64/aarch64, macOS
|
|
70
|
+
arm64/x86_64, Windows x64, Python 3.10-3.15) need no compiler:
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
pip install pycapacity[fast] # or: pip install pycapacity-cpp
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
From the sources, a C++17 compiler is needed (CMake comes from pip). Eigen,
|
|
77
|
+
Qhull and HiGHS are downloaded and built during the build if they are not
|
|
78
|
+
found:
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
pip install ./cpp
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### Releasing
|
|
85
|
+
|
|
86
|
+
The wheels are built and tested by `.github/workflows/cpp-wheels.yml`
|
|
87
|
+
(cibuildwheel; every wheel runs `cpp/tests` against the Python
|
|
88
|
+
implementation). Pull requests touching `cpp/` build them; a tag
|
|
89
|
+
`cpp-vX.Y.Z` matching the version in `cpp/pyproject.toml` also publishes
|
|
90
|
+
them to PyPI (trusted publishing, environment `pypi`).
|
|
91
|
+
|
|
92
|
+
## How the ICHM is made fast
|
|
93
|
+
|
|
94
|
+
Almost all the time of the ICHM goes into the linear programs, one per facet,
|
|
95
|
+
each solved for a new cost on the same constraints
|
|
96
|
+
`y_min <= y <= y_max, E y = e, G y <= g`, with very few rows. A general LP
|
|
97
|
+
solver spends ~150 us per call in setup even for 3 pivots (HiGHS, GLPK), so
|
|
98
|
+
`pycapacity_cpp` uses a small dense bounded-variable primal simplex that keeps
|
|
99
|
+
its basis between calls (the previous optimum stays feasible when only the
|
|
100
|
+
cost changes), flips all wrong-bound variables at once when possible, and
|
|
101
|
+
solves the facets in an order where consecutive normals are close. A typical
|
|
102
|
+
LP then takes 2-20 us. Every solution is checked and HiGHS solves it instead
|
|
103
|
+
if the check fails (`info["n_lp_fallback"]` counts these; it is 0 in all the
|
|
104
|
+
benchmarks and tests). `lp_solver="highs"` forces HiGHS for comparison.
|
|
105
|
+
|
|
106
|
+
## General projection: `project_polytope`
|
|
107
|
+
|
|
108
|
+
```python
|
|
109
|
+
import pycapacity_cpp as pc
|
|
110
|
+
vertices = pc.project_polytope((E, f), (A, b), (C, d), tol=1e-6)
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Inequality rows with a single nonzero become variable bounds (the box of
|
|
114
|
+
the ICHM); the others are general constraint rows. With up to 200 constraint
|
|
115
|
+
rows the LPs use the dense simplex; beyond that they go to HiGHS. The
|
|
116
|
+
polytope must be bounded (a `ValueError` is raised otherwise). The result is
|
|
117
|
+
an inner approximation within `tol` (default `1e-7` of the size; never
|
|
118
|
+
below `1e-8` of the size, the precision of the LP solutions).
|
|
119
|
+
|
|
120
|
+
## Tests
|
|
121
|
+
|
|
122
|
+
```bash
|
|
123
|
+
python -m pytest cpp/tests # C++ against Python, same polytopes
|
|
124
|
+
PYCAPACITY_BACKEND=cpp python -m pytest tests
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## Using the library from C++
|
|
128
|
+
|
|
129
|
+
`include/pycapacity_cpp/polycap.hpp` declares the functions (Eigen inputs and
|
|
130
|
+
outputs); the CMake target `polycap` is a static library.
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
# pycapacity-cpp
|
|
2
|
+
|
|
3
|
+
Compiled (C++) implementations of the polytope algorithms of
|
|
4
|
+
[pycapacity](https://github.com/auctus-team/pycapacity):
|
|
5
|
+
|
|
6
|
+
| algorithm | used for | speed-up over the Python code |
|
|
7
|
+
|---|---|---|
|
|
8
|
+
| `hyper_plane_shift_method` (HPSM) | velocity / acceleration / human force polytopes (H-rep) | 20-170x |
|
|
9
|
+
| `vertex_enumeration_vepoli2` | robot force polytopes (V-rep) | 15-50x |
|
|
10
|
+
| `iterative_convex_hull_method` (ICHM) | human polytopes, any problem of the unified form | 10-90x |
|
|
11
|
+
| `halfspace_intersection` (H -> V in 2D/3D) | `pycapacity.algorithms.hspace_to_vertex` | 2-4x |
|
|
12
|
+
|
|
13
|
+
It also provides `project_polytope(proj, ineq, eq=None, tol=None)`, a
|
|
14
|
+
general polytope projection `{E x + f | A x <= b, C x = d}` in any output
|
|
15
|
+
dimension with the same arguments and result as `pypoman.project_polytope`
|
|
16
|
+
(see below).
|
|
17
|
+
|
|
18
|
+
(Measured with `primer/benchmarks/bench_cpp.py`; the ranges depend on the
|
|
19
|
+
problem size, see the benchmark output.) On whole `pycapacity` calls: Franka
|
|
20
|
+
Panda velocity/acceleration/force polytopes 0.2/0.34/0.034 ms (Python 1.5-2
|
|
21
|
+
ms), 50-muscle human force polytope 1.3 ms at 10 N (Python 39 ms); see
|
|
22
|
+
`docs/source/backend.rst`.
|
|
23
|
+
|
|
24
|
+
When `pycapacity-cpp` is installed, `pycapacity` uses it automatically: the
|
|
25
|
+
functions of `pycapacity.algorithms`, and so every `robot.*` and `human.*`
|
|
26
|
+
polytope function, keep their inputs and outputs and run in C++. To select
|
|
27
|
+
the implementation:
|
|
28
|
+
|
|
29
|
+
```python
|
|
30
|
+
import pycapacity.algorithms as algos
|
|
31
|
+
algos.get_backend() # 'cpp' or 'python'
|
|
32
|
+
algos.set_backend("python") # or "cpp", "auto" (default)
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
or set the environment variable `PYCAPACITY_BACKEND=python` before importing.
|
|
36
|
+
|
|
37
|
+
## Installation
|
|
38
|
+
|
|
39
|
+
### conda (recommended)
|
|
40
|
+
|
|
41
|
+
From the `pycapacity` folder:
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
conda env create -f cpp/environment.yml
|
|
45
|
+
conda activate pycapacity
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
This creates an environment with pycapacity, the compiled backend and their
|
|
49
|
+
dependencies (all from conda-forge). In an existing conda environment:
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
conda install -c conda-forge cxx-compiler cmake ninja eigen qhull highs pybind11 scikit-build-core
|
|
53
|
+
pip install ./cpp
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### pip
|
|
57
|
+
|
|
58
|
+
Once released on PyPI, prebuilt wheels (Linux x86_64/aarch64, macOS
|
|
59
|
+
arm64/x86_64, Windows x64, Python 3.10-3.15) need no compiler:
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
pip install pycapacity[fast] # or: pip install pycapacity-cpp
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
From the sources, a C++17 compiler is needed (CMake comes from pip). Eigen,
|
|
66
|
+
Qhull and HiGHS are downloaded and built during the build if they are not
|
|
67
|
+
found:
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
pip install ./cpp
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Releasing
|
|
74
|
+
|
|
75
|
+
The wheels are built and tested by `.github/workflows/cpp-wheels.yml`
|
|
76
|
+
(cibuildwheel; every wheel runs `cpp/tests` against the Python
|
|
77
|
+
implementation). Pull requests touching `cpp/` build them; a tag
|
|
78
|
+
`cpp-vX.Y.Z` matching the version in `cpp/pyproject.toml` also publishes
|
|
79
|
+
them to PyPI (trusted publishing, environment `pypi`).
|
|
80
|
+
|
|
81
|
+
## How the ICHM is made fast
|
|
82
|
+
|
|
83
|
+
Almost all the time of the ICHM goes into the linear programs, one per facet,
|
|
84
|
+
each solved for a new cost on the same constraints
|
|
85
|
+
`y_min <= y <= y_max, E y = e, G y <= g`, with very few rows. A general LP
|
|
86
|
+
solver spends ~150 us per call in setup even for 3 pivots (HiGHS, GLPK), so
|
|
87
|
+
`pycapacity_cpp` uses a small dense bounded-variable primal simplex that keeps
|
|
88
|
+
its basis between calls (the previous optimum stays feasible when only the
|
|
89
|
+
cost changes), flips all wrong-bound variables at once when possible, and
|
|
90
|
+
solves the facets in an order where consecutive normals are close. A typical
|
|
91
|
+
LP then takes 2-20 us. Every solution is checked and HiGHS solves it instead
|
|
92
|
+
if the check fails (`info["n_lp_fallback"]` counts these; it is 0 in all the
|
|
93
|
+
benchmarks and tests). `lp_solver="highs"` forces HiGHS for comparison.
|
|
94
|
+
|
|
95
|
+
## General projection: `project_polytope`
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
import pycapacity_cpp as pc
|
|
99
|
+
vertices = pc.project_polytope((E, f), (A, b), (C, d), tol=1e-6)
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Inequality rows with a single nonzero become variable bounds (the box of
|
|
103
|
+
the ICHM); the others are general constraint rows. With up to 200 constraint
|
|
104
|
+
rows the LPs use the dense simplex; beyond that they go to HiGHS. The
|
|
105
|
+
polytope must be bounded (a `ValueError` is raised otherwise). The result is
|
|
106
|
+
an inner approximation within `tol` (default `1e-7` of the size; never
|
|
107
|
+
below `1e-8` of the size, the precision of the LP solutions).
|
|
108
|
+
|
|
109
|
+
## Tests
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
python -m pytest cpp/tests # C++ against Python, same polytopes
|
|
113
|
+
PYCAPACITY_BACKEND=cpp python -m pytest tests
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## Using the library from C++
|
|
117
|
+
|
|
118
|
+
`include/pycapacity_cpp/polycap.hpp` declares the functions (Eigen inputs and
|
|
119
|
+
outputs); the CMake target `polycap` is a static library.
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# conda-build recipe for pycapacity-cpp (draft, not yet built with conda-build)
|
|
2
|
+
# conda build -c conda-forge cpp/conda-recipe
|
|
3
|
+
{% set version = "0.1.0" %}
|
|
4
|
+
|
|
5
|
+
package:
|
|
6
|
+
name: pycapacity-cpp
|
|
7
|
+
version: {{ version }}
|
|
8
|
+
|
|
9
|
+
source:
|
|
10
|
+
path: ..
|
|
11
|
+
|
|
12
|
+
build:
|
|
13
|
+
number: 0
|
|
14
|
+
script: {{ PYTHON }} -m pip install . -vv --no-deps --no-build-isolation
|
|
15
|
+
|
|
16
|
+
requirements:
|
|
17
|
+
build:
|
|
18
|
+
- {{ compiler('cxx') }}
|
|
19
|
+
- {{ compiler('c') }}
|
|
20
|
+
- cmake >=3.18
|
|
21
|
+
- ninja
|
|
22
|
+
host:
|
|
23
|
+
- python
|
|
24
|
+
- pip
|
|
25
|
+
- scikit-build-core >=0.8
|
|
26
|
+
- pybind11
|
|
27
|
+
- eigen >=3.3,<4
|
|
28
|
+
- qhull
|
|
29
|
+
- highs
|
|
30
|
+
run:
|
|
31
|
+
- python
|
|
32
|
+
- numpy
|
|
33
|
+
- qhull
|
|
34
|
+
- highs
|
|
35
|
+
|
|
36
|
+
test:
|
|
37
|
+
imports:
|
|
38
|
+
- pycapacity_cpp
|
|
39
|
+
commands:
|
|
40
|
+
- python -c "import numpy as np, pycapacity_cpp as c; H, d = c.hyper_plane_shift_method(np.random.randn(3, 6), -np.ones(6), np.ones(6)); assert H.shape == (30, 3)"
|
|
41
|
+
|
|
42
|
+
about:
|
|
43
|
+
home: https://github.com/auctus-team/pycapacity
|
|
44
|
+
license: MIT
|
|
45
|
+
summary: Compiled (C++) polytope algorithms for pycapacity (HPSM, VEPOLI2, ICHM)
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# conda environment with pycapacity and its compiled backend pycapacity-cpp
|
|
2
|
+
#
|
|
3
|
+
# conda env create -f cpp/environment.yml (from the pycapacity folder)
|
|
4
|
+
# conda activate pycapacity
|
|
5
|
+
#
|
|
6
|
+
# Everything comes from conda-forge; the C++ part is compiled during the
|
|
7
|
+
# install (about a minute), no system compiler or library is needed.
|
|
8
|
+
name: pycapacity
|
|
9
|
+
channels:
|
|
10
|
+
- conda-forge
|
|
11
|
+
dependencies:
|
|
12
|
+
- python>=3.10
|
|
13
|
+
- numpy
|
|
14
|
+
- scipy
|
|
15
|
+
- highspy # LP solver of the Python implementation
|
|
16
|
+
- cvxopt # optional: GLPK, faster for small LPs
|
|
17
|
+
- matplotlib
|
|
18
|
+
# cddlib/gmp for pycddlib (pip section), used by pycapacity.polyhedral
|
|
19
|
+
- cddlib
|
|
20
|
+
- gmp
|
|
21
|
+
# to build pycapacity-cpp
|
|
22
|
+
- cxx-compiler
|
|
23
|
+
- cmake>=3.18
|
|
24
|
+
- ninja
|
|
25
|
+
- eigen=3.4
|
|
26
|
+
- qhull
|
|
27
|
+
- highs
|
|
28
|
+
- pybind11
|
|
29
|
+
- scikit-build-core
|
|
30
|
+
- pytest
|
|
31
|
+
- pip
|
|
32
|
+
- pip:
|
|
33
|
+
- pycddlib>=3 # optional: exact methods in pycapacity.polyhedral
|
|
34
|
+
- pypoman
|
|
35
|
+
- -e .. # pycapacity (editable)
|
|
36
|
+
- . # pycapacity-cpp
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
#include <vector>
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
All combinations of r distinct indices out of {0, ..., n-1}, in
|
|
6
|
+
lexicographic order (same order as Python's itertools.combinations).
|
|
7
|
+
@param combinations - output, one combination per entry
|
|
8
|
+
@param n - number of elements
|
|
9
|
+
@param r - length of each combination
|
|
10
|
+
**/
|
|
11
|
+
void CombinationRepetition(std::vector<std::vector<int>> &combinations, int n,
|
|
12
|
+
int r);
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
// pycapacity_cpp: compiled implementations of the polytope algorithms of
|
|
2
|
+
// pycapacity (HPSM, VEPOLI2, ICHM). Same problem definitions and conventions
|
|
3
|
+
// as pycapacity.algorithms.
|
|
4
|
+
#pragma once
|
|
5
|
+
|
|
6
|
+
#include <Eigen/Dense>
|
|
7
|
+
|
|
8
|
+
namespace pycapacity_cpp {
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
Hyper-plane shifting method (Gouttefarde and Krut, 2010).
|
|
12
|
+
|
|
13
|
+
Half-space representation H x <= d of the zonotope
|
|
14
|
+
P = {x | x = A y, y_min <= y <= y_max}, A of shape (m, n), n >= m >= 2.
|
|
15
|
+
|
|
16
|
+
@param H, d output, one pair of parallel facets per independent set of m-1
|
|
17
|
+
columns of A (rows c and -c are stored consecutively)
|
|
18
|
+
@param tol a group of m-1 columns is used if its rank is m-1 (singular
|
|
19
|
+
values above tol * largest) and its normal is new (up to tol)
|
|
20
|
+
*/
|
|
21
|
+
void hyper_plane_shift_method(Eigen::MatrixXd &H, Eigen::VectorXd &d,
|
|
22
|
+
const Eigen::MatrixXd &A,
|
|
23
|
+
const Eigen::VectorXd &y_min,
|
|
24
|
+
const Eigen::VectorXd &y_max,
|
|
25
|
+
double tol = 1e-12);
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
VEPOLI2 vertex enumeration (Skuric, Padois and Daney, ICRA 2021).
|
|
29
|
+
|
|
30
|
+
Vertices of P = {x | A x = y - y_bias, y_min <= y <= y_max},
|
|
31
|
+
A of shape (n, m), n >= m, full column rank.
|
|
32
|
+
|
|
33
|
+
@param x_vert output (m, k) vertices
|
|
34
|
+
@param y_vert output (n, k) values y - y_bias = A x of each vertex
|
|
35
|
+
@param y_bias bias (n), or an empty vector
|
|
36
|
+
*/
|
|
37
|
+
void vertex_enumeration_vepoli2(Eigen::MatrixXd &x_vert,
|
|
38
|
+
Eigen::MatrixXd &y_vert,
|
|
39
|
+
const Eigen::MatrixXd &A,
|
|
40
|
+
const Eigen::VectorXd &y_min,
|
|
41
|
+
const Eigen::VectorXd &y_max,
|
|
42
|
+
const Eigen::VectorXd &y_bias,
|
|
43
|
+
double tol = 1e-7);
|
|
44
|
+
|
|
45
|
+
/** Result of the iterative convex hull method. */
|
|
46
|
+
struct IchmResult {
|
|
47
|
+
Eigen::MatrixXd x_vert; // (dim, k) vertices of the final hull
|
|
48
|
+
Eigen::MatrixXd y_vert; // (L, k) inputs producing each vertex
|
|
49
|
+
Eigen::MatrixXd H; // (f, dim) facet normals, H x <= d
|
|
50
|
+
Eigen::VectorXd d; // (f)
|
|
51
|
+
Eigen::MatrixXi faces; // (f, dim) vertex indices (into x_vert) per facet
|
|
52
|
+
int n_lp = 0; // number of linear programs solved
|
|
53
|
+
double max_delta = 0; // largest facet-to-polytope distance at the end
|
|
54
|
+
bool converged = false; // max_delta <= tol
|
|
55
|
+
int n_lp_fallback = 0; // LPs that were solved by HiGHS
|
|
56
|
+
double t_lp_ms = 0; // time spent in the LP solver
|
|
57
|
+
double t_hull_ms = 0; // time spent in Qhull
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
Iterative convex hull method (Skuric, Padois, Rezzoug and Daney, RA-L 2022).
|
|
62
|
+
|
|
63
|
+
P_x = {x | x = P z, A z = B y - bias, G_in y <= h_in, G_eq y = h_eq,
|
|
64
|
+
y_min <= y <= y_max}
|
|
65
|
+
|
|
66
|
+
(the projection P, bias and extra constraints are optional: pass empty
|
|
67
|
+
matrices/vectors). Inner approximation whose facets are within tol of the
|
|
68
|
+
true polytope (tol below 1e-8 x the size of the polytope, the precision of
|
|
69
|
+
the LP solutions, is raised to that value). The linear programs (only their cost
|
|
70
|
+
changes) are solved by a small dense bounded-variable simplex warm-started
|
|
71
|
+
from the previous basis, with HiGHS as fallback (lp_solver: 0 auto, 1 dense
|
|
72
|
+
simplex only, 2 HiGHS only). Qhull computes the convex hulls.
|
|
73
|
+
|
|
74
|
+
Bias convention: identical to pycapacity.algorithms.iterative_convex_hull_method.
|
|
75
|
+
*/
|
|
76
|
+
IchmResult iterative_convex_hull_method(
|
|
77
|
+
const Eigen::MatrixXd &A, const Eigen::MatrixXd &B,
|
|
78
|
+
const Eigen::VectorXd &y_min, const Eigen::VectorXd &y_max, double tol,
|
|
79
|
+
const Eigen::MatrixXd &P, const Eigen::VectorXd &bias,
|
|
80
|
+
const Eigen::MatrixXd &G_in, const Eigen::VectorXd &h_in,
|
|
81
|
+
const Eigen::MatrixXd &G_eq, const Eigen::VectorXd &h_eq,
|
|
82
|
+
int max_iter = 1000, int lp_solver = 0);
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
Bounds lo <= x <= hi of every variable over {G x <= g, E x = e,
|
|
86
|
+
lo0 <= x <= hi0}, where the given bounds lo0/hi0 may be infinite (replaced by
|
|
87
|
+
+-big). Returns false if the set is empty or a bound reaches +-big
|
|
88
|
+
(unbounded), or on numerical failure.
|
|
89
|
+
*/
|
|
90
|
+
bool bound_variables(Eigen::VectorXd &lo, Eigen::VectorXd &hi,
|
|
91
|
+
const Eigen::MatrixXd &G, const Eigen::VectorXd &g,
|
|
92
|
+
const Eigen::MatrixXd &E, const Eigen::VectorXd &e,
|
|
93
|
+
const Eigen::VectorXd &lo0, const Eigen::VectorXd &hi0,
|
|
94
|
+
double big);
|
|
95
|
+
|
|
96
|
+
/** Convex hull of the columns of X (dim, N) with Qhull. */
|
|
97
|
+
struct Hull {
|
|
98
|
+
Eigen::MatrixXd normals; // (f, dim), outward, unit
|
|
99
|
+
Eigen::VectorXd offsets; // (f), normal . x + offset = 0 on the facet
|
|
100
|
+
Eigen::MatrixXi simplices; // (f, dim) point indices (columns of X)
|
|
101
|
+
bool ok = false;
|
|
102
|
+
};
|
|
103
|
+
Hull convex_hull(const Eigen::MatrixXd &X, const char *options = "Qt");
|
|
104
|
+
|
|
105
|
+
/** Vertices of {x | H x <= d} given a point c strictly inside (dim >= 2),
|
|
106
|
+
* each vertex once, and the triangulated faces (indices into V's columns).
|
|
107
|
+
* ok = false if c is not strictly inside or the set is unbounded. */
|
|
108
|
+
struct HtoV {
|
|
109
|
+
Eigen::MatrixXd V; // (dim, nv)
|
|
110
|
+
Eigen::MatrixXi faces; // (nfaces, dim)
|
|
111
|
+
bool ok = false;
|
|
112
|
+
};
|
|
113
|
+
HtoV halfspace_intersection(const Eigen::MatrixXd &H, const Eigen::VectorXd &d,
|
|
114
|
+
const Eigen::VectorXd &c);
|
|
115
|
+
|
|
116
|
+
} // namespace pycapacity_cpp
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["scikit-build-core>=0.8", "pybind11>=2.10"]
|
|
3
|
+
build-backend = "scikit_build_core.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "pycapacity-cpp"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Compiled (C++) polytope algorithms for pycapacity: HPSM, VEPOLI2 and ICHM"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [{ name = "Antun Skuric" }]
|
|
13
|
+
dependencies = ["numpy"]
|
|
14
|
+
|
|
15
|
+
[project.urls]
|
|
16
|
+
Homepage = "https://github.com/auctus-team/pycapacity"
|
|
17
|
+
|
|
18
|
+
[tool.scikit-build]
|
|
19
|
+
wheel.packages = ["python/pycapacity_cpp"]
|
|
20
|
+
cmake.build-type = "Release"
|
|
21
|
+
# only the Python module (not the install rules of the dependencies built from source)
|
|
22
|
+
install.components = ["python"]
|
|
23
|
+
install.strip = true
|
|
24
|
+
|
|
25
|
+
# wheels for PyPI: built and tested by .github/workflows/cpp-wheels.yml
|
|
26
|
+
[tool.cibuildwheel]
|
|
27
|
+
# no free-threaded (cp3XXt) builds: the bindings are not declared free-threading safe
|
|
28
|
+
skip = ["*-musllinux_*", "*-win32", "*_i686", "pp*", "cp3??t-*"]
|
|
29
|
+
build-verbosity = 1
|
|
30
|
+
# test every wheel against the Python implementation of the repository
|
|
31
|
+
test-requires = ["pytest", "scipy", "matplotlib", "highspy"]
|
|
32
|
+
test-command = "pip install --no-deps {project} && pytest {package}/tests -q"
|
|
33
|
+
|
|
34
|
+
[tool.cibuildwheel.macos]
|
|
35
|
+
environment = { MACOSX_DEPLOYMENT_TARGET = "11.0" }
|