geometry
¤
Polytope geometry used by ORACLE.
Everything in this module is model-independent: it works on
- an outer approximation given in halfspace form
{z : A z <= b}, and - an inner approximation given as the convex hull of a set of points (one point per row of
points).
Distances are measured with the (weighted) L-infinity norm max_i |z_i - y_i|, as in the ORACLE paper. Pass
coordinates that are already scaled if the dimensions have different units.
Classes:
-
FurthestPoint–Solution of the ORACLE metric: the point of the outer approximation that is furthest from the inner one.
Functions:
-
bounding_box–Smallest axis-aligned box containing
{z : A z <= b}(two LPs per dimension). -
chebyshev_center–Centre and radius of the largest ball inside
{z : A z <= b}. -
distance_to_hull–L-infinity distance from
zto the convex hull ofpoints(one LP). -
furthest_point–The ORACLE metric:
max_{z in O} min_{y in I} ||z - y||_inf. -
halfspaces_of_hull–Halfspace form
(A, b)of the convex hull ofpoints, orNoneif the hull is flat. -
hit_and_run–Approximately uniform samples from
{z : A z <= b}with the hit-and-run random walk. -
hull_vertex_indices–Indices of the points that are vertices of their convex hull, also when the hull is flat.
-
polytope_vertices–All vertices of the bounded polytope
{z : A z <= b}. -
support_point–Point of
{z : A z <= b}that goes furthest indirection(maximisesdirection @ z).
Attributes:
FurthestPoint
dataclass
¤
FurthestPoint(
point: ndarray,
distance: float,
upper_bound: float,
method: str,
)
Solution of the ORACLE metric: the point of the outer approximation that is furthest from the inner one.
Attributes:
-
point(ndarray) –The furthest point of the outer approximation (the next "trial point" to explore).
-
distance(float) –Its L-infinity distance to the inner approximation (the ORACLE metric).
-
upper_bound(float) –A certified upper bound on the metric. Equals
distancefor the exact vertex method; for the MILP it is the solver's dual bound, which is larger when the MILP stops at its optimality gap or time limit (distanceis then only a lower bound on the metric). -
method(str) –'vertex'or'milp'.
bounding_box
¤
bounding_box(
A: ndarray, b: ndarray
) -> tuple[ndarray, ndarray]
Smallest axis-aligned box containing {z : A z <= b} (two LPs per dimension).
Source code in src/energyscope/oracle/geometry.py
73 74 75 76 77 78 79 80 81 82 | |
chebyshev_center
¤
Centre and radius of the largest ball inside {z : A z <= b}.
Parameters:
-
(A¤ndarray) –Halfspace normals, shape
(n_halfspaces, n_dims). -
(b¤ndarray) –Right-hand sides, shape
(n_halfspaces,).
Returns:
-
tuple[ndarray, float]–The centre and the radius of the ball. A radius of (almost) zero means the polytope has no interior.
Source code in src/energyscope/oracle/geometry.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
distance_to_hull
¤
L-infinity distance from z to the convex hull of points (one LP).
Parameters:
-
(z¤ndarray) –The point, shape
(n_dims,). -
(points¤ndarray) –The points spanning the hull, shape
(n_points, n_dims).
Returns:
-
tuple[float, ndarray]–The distance and the weights of the closest convex combination of
points.
Source code in src/energyscope/oracle/geometry.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | |
furthest_point
¤
furthest_point(
A: ndarray,
b: ndarray,
points: ndarray,
method: str = "auto",
upper_bound: Optional[float] = None,
time_limit: Optional[float] = None,
mip_rel_gap: float = 1e-06,
mip_abs_gap: float = 0.0,
) -> FurthestPoint
The ORACLE metric: max_{z in O} min_{y in I} ||z - y||_inf.
O = {z : A z <= b} is the outer approximation and I = conv(points) the inner approximation. The value is
the largest error made by using either approximation in place of the (unknown) near-optimal space, and the point
reaching it is where the approximations disagree the most - which is where ORACLE explores next.
Parameters:
-
(A¤ndarray) –Halfspace normals of the outer approximation, shape
(n_halfspaces, n_dims). Must describe a bounded set. -
(b¤ndarray) –Right-hand sides, shape
(n_halfspaces,). -
(points¤ndarray) –Points spanning the inner approximation, shape
(n_points, n_dims). -
(method¤str, default:'auto') –'vertex'enumerates the vertices ofOand evaluates the distance at each of them. This is exact because the distance to a convex set is a convex function, so its maximum over a polytope is reached at a vertex.'milp'solves the bilevel problem of the paper as a single-level MILP (KKT conditions with big-M constraints), which scales better with the number of dimensions.'auto'(default) picks'vertex'up toVERTEX_METHOD_MAX_DIMENSIONS(6) dimensions. -
(upper_bound¤Optional[float], default:None) –Known upper bound on the metric (e.g. its previous value), used to speed up the MILP.
-
(time_limit¤Optional[float], default:None) –Time limit in seconds for the MILP. When it is reached, the best point found is returned with the solver's upper bound on the metric.
-
(mip_rel_gap¤float, default:1e-06) –Relative optimality gap for the MILP.
-
(mip_abs_gap¤float, default:0.0) –Absolute optimality gap for the MILP.
Returns:
-
FurthestPoint–The furthest point and its distance.
Source code in src/energyscope/oracle/geometry.py
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | |
halfspaces_of_hull
¤
halfspaces_of_hull(
points: ndarray,
) -> Optional[tuple[ndarray, ndarray]]
Halfspace form (A, b) of the convex hull of points, or None if the hull is flat.
Source code in src/energyscope/oracle/geometry.py
395 396 397 398 399 400 401 402 403 404 405 406 407 | |
hit_and_run
¤
hit_and_run(
A: ndarray,
b: ndarray,
n_samples: int,
start: Optional[ndarray] = None,
thinning: Optional[int] = None,
rng: Optional[Generator] = None,
) -> ndarray
Approximately uniform samples from {z : A z <= b} with the hit-and-run random walk.
Parameters:
-
(A¤ndarray) –Halfspace normals.
-
(b¤ndarray) –Right-hand sides.
-
(n_samples¤int) –Number of samples to return.
-
(start¤Optional[ndarray], default:None) –Interior starting point (default: the Chebyshev centre).
-
(thinning¤Optional[int], default:None) –Number of walk steps between two returned samples (default:
10 * n_dims). -
(rng¤Optional[Generator], default:None) –Random generator, for reproducibility.
Returns:
-
ndarray–Array of shape
(n_samples, n_dims).
Source code in src/energyscope/oracle/geometry.py
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 | |
hull_vertex_indices
¤
hull_vertex_indices(points: ndarray) -> ndarray
Indices of the points that are vertices of their convex hull, also when the hull is flat.
A flat hull (e.g. collinear points in 2-D) is handled in the affine subspace spanned by the points.
Source code in src/energyscope/oracle/geometry.py
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 | |
polytope_vertices
¤
polytope_vertices(A: ndarray, b: ndarray) -> ndarray
All vertices of the bounded polytope {z : A z <= b}.
Uses Qhull's halfspace intersection, so it is exact but its cost grows quickly with the number of dimensions.
Raises:
-
ValueError–If the polytope has no interior (it is flat or empty), or if Qhull can only enumerate its vertices approximately (nearly degenerate input).
Source code in src/energyscope/oracle/geometry.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | |
support_point
¤
support_point(
A: ndarray, b: ndarray, direction: ndarray
) -> ndarray
Point of {z : A z <= b} that goes furthest in direction (maximises direction @ z).
Source code in src/energyscope/oracle/geometry.py
85 86 87 88 | |