algorithm
¤
The ORACLE algorithm (Turan, Moret & Bardow, 2026).
ORACLE sandwiches the unknown near-optimal space Z between two polytopes:
- the inner approximation
I: the convex hull of near-optimal designs found so far.Iis insideZbecauseZis convex, so every point ofIis a guaranteed near-optimal design; - the outer approximation
O: the intersection of halfspaces ("cuts") that are known to containZ.
and repeats until the two meet:
- Initialise
Iwith the cost optimum andOwith the bounds of the design variables and a cost cut. - Measure: find the point of
Ofurthest fromI. Its distance (the ORACLE metric) bounds the error of both approximations; stop when it is below the tolerance. - Explore: find the near-optimal design closest to that point (one solve of the full model).
- Refine: add that design to
Iand, if the point was not near-optimal, cut it offOwith a hyperplane that separates it fromZ(built from the dual variables of step 3).
Classes:
-
Oracle–Explore the whole near-optimal space of a model with ORACLE.
-
OracleResult–Snapshot of ORACLE's approximations of the near-optimal space.
Oracle
¤
Oracle(
problem: NearOptimalProblem,
eps: float = 0.05,
tol: Optional[float] = None,
scale: Union[None, str, Sequence[float]] = None,
metric: str = "auto",
cost_cut: bool = True,
verbose: bool = True,
mip_rel_gap: float = 0.01,
mip_time_limit: Optional[float] = None,
)
Explore the whole near-optimal space of a model with ORACLE.
Example
from energyscope.oracle import Oracle, PyomoProblem
problem = PyomoProblem.from_energyscope(technologies=['PV', 'WIND', 'NUCLEAR', 'CCGT'])
oracle = Oracle(problem, eps=0.03)
result = oracle.run(max_iter=50)
result.ranges()
Use run for the full algorithm, or
initialise followed by
step (or by furthest_point, nearest_point and refine) to
follow it step by step.
Parameters:
-
(problem¤NearOptimalProblem) –The model to explore, e.g. a
PyomoProblem. -
(eps¤float, default:0.05) –Allowed relative cost increase over the optimum: designs costing at most
(1 + eps)times the optimal cost are near-optimal (more preciselyoptimal cost + eps * |optimal cost|, also valid for negative costs). -
(tol¤Optional[float], default:None) –Stop when the ORACLE metric (the largest distance between the two approximations) is below
tol. Default: 1 % of the largest (scaled) bound range. -
(scale¤Union[None, str, Sequence[float]], default:None) –How distances are measured.
None(default, as in the paper) uses the units of the design variables. When units or magnitudes differ (e.g. GW and GWh), use'extent', which divides each dimension by its extent in the near-optimal space (two extra solves per dimension, which also tighten the initial bounds), or'range', which divides it by its bound range. Or give one positive number per dimension. -
(metric¤str, default:'auto') –How the ORACLE metric is computed:
'vertex'(exact, up to about 6 dimensions),'milp'(the bilevel problem of the paper, solved as a MILP) or'auto'(default), seefurthest_point. -
(cost_cut¤bool, default:True) –Also add the cost cut of the paper (one extra solve per iteration, usually fewer iterations).
-
(verbose¤bool, default:True) –Print one line per iteration.
-
(mip_rel_gap¤float, default:0.01) –Relative optimality gap of the metric MILP (the paper uses 10 %). Convergence is always judged on the MILP's proven upper bound, so a gap only makes some trial points less than the furthest.
-
(mip_time_limit¤Optional[float], default:None) –Time limit in seconds for each metric MILP.
Methods:
-
design–Cheapest full system with the design
point, e.g. an EnergyScope -
furthest_point–Step 2: the ORACLE metric - the point of the outer approximation furthest from the inner one.
-
initialise–Step 1: solve the cost optimum and build the first inner and outer approximations.
-
nearest_point–Step 3: the near-optimal design closest to
target(one solve of the full model). -
refine–Step 4: add the design found to the inner approximation and cut
targetoff the outer one. -
run–Run ORACLE until the metric is below the tolerance or
max_iteriterations are done. -
step–One ORACLE iteration (steps 2 to 4). Returns the iteration record (also appended to the history).
Attributes:
-
A(ndarray) –Normals of the outer approximation
{z : A z <= b}. -
b(ndarray) –Right-hand sides of the outer approximation
{z : A z <= b}. -
budget(Optional[float]) – -
cost_cut– -
dimensions– -
eps– -
inner_points(ndarray) –Near-optimal designs found so far, one per row (they span the inner approximation).
-
lower(Optional[ndarray]) – -
metric– -
mip_rel_gap– -
mip_time_limit– -
n_dims(int) – -
optimal_cost(Optional[float]) – -
optimum(Optional[ndarray]) – -
problem– -
result(OracleResult) –The current approximations, as an
OracleResult. -
scale(Optional[ndarray]) – -
tol(Optional[float]) – -
upper(Optional[ndarray]) – -
verbose–
Source code in src/energyscope/oracle/algorithm.py
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | |
inner_points
property
¤
inner_points: ndarray
Near-optimal designs found so far, one per row (they span the inner approximation).
design
¤
design(point: Union[Sequence[float], dict, Series])
Cheapest full system with the design point, e.g. an EnergyScope
Result.
Its cost is within the budget if point is in the near-optimal space (always true inside the inner
approximation).
Source code in src/energyscope/oracle/algorithm.py
511 512 513 514 515 516 517 518 | |
furthest_point
¤
furthest_point() -> FurthestPoint
Step 2: the ORACLE metric - the point of the outer approximation furthest from the inner one.
Returns:
-
FurthestPoint–The point (in the units of the design variables), its scaled distance to the inner approximation, and a
-
FurthestPoint–certified upper bound on the metric (equal to the distance, unless the MILP stopped at its gap or time
-
FurthestPoint–limit).
Source code in src/energyscope/oracle/algorithm.py
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 | |
initialise
¤
initialise() -> Oracle
Step 1: solve the cost optimum and build the first inner and outer approximations.
- inner approximation: the cost-optimal design;
- outer approximation: the bounds of the design variables, plus the cost cut
c @ z <= budgetwhen the problem knows an under-estimatorcof the cost. Infinite bounds (and all bounds withscale='extent') are replaced by the extent of the near-optimal space in that dimension (one extra solve each); the designs reaching these extents are added to the inner approximation.
Source code in src/energyscope/oracle/algorithm.py
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 | |
nearest_point
¤
nearest_point(target: ndarray) -> NearestPoint
Step 3: the near-optimal design closest to target (one solve of the full model).
The dual variables give the normal of a hyperplane that separates target from the near-optimal space;
they are checked against the distance found and re-oriented if the solver uses the opposite sign convention.
Source code in src/energyscope/oracle/algorithm.py
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 | |
refine
¤
refine(target: ndarray, nearest: NearestPoint) -> dict
Step 4: add the design found to the inner approximation and cut target off the outer one.
Two cuts can be added, both valid for the whole near-optimal space:
- the separating cut
mu @ z <= mu @ z_f(always, whentargetis not near-optimal), wheremuare the dual variables of step 3: since the distance to the near-optimal space is a convex function of the target andmuits slope, no near-optimal design can be on the far side of this hyperplane; - the cost cut
cost(z_f) + lam @ (z - z_f) <= budget(ifcost_cut), wherelamis the slope of the minimum cost as a function of the design: it linearises the cost from below, so it is also valid.
Returns:
-
dict–Which cuts were added and whether
targetwas cut off.
Source code in src/energyscope/oracle/algorithm.py
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 | |
run
¤
run(max_iter: int = 100) -> OracleResult
Run ORACLE until the metric is below the tolerance or max_iter iterations are done.
Can be called again to continue a previous run.
Source code in src/energyscope/oracle/algorithm.py
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 | |
step
¤
step() -> dict
One ORACLE iteration (steps 2 to 4). Returns the iteration record (also appended to the history).
Source code in src/energyscope/oracle/algorithm.py
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 | |
OracleResult
dataclass
¤
OracleResult(
dimensions: list,
eps: float,
optimal_cost: float,
budget: float,
optimum: Series,
inner_points: DataFrame,
outer: DataFrame,
history: DataFrame,
scale: Series,
tol: float,
distance: float,
converged: bool,
)
Snapshot of ORACLE's approximations of the near-optimal space.
Attributes:
-
dimensions(list) –Names of the explored design variables.
-
eps(float) –Allowed relative cost increase over the optimum (e.g.
0.05for 5 %). -
optimal_cost(float) –Cost of the optimal design.
-
budget(float) –Cost budget
optimal_cost + eps * |optimal_cost|defining the near-optimal space. -
optimum(Series) –The cost-optimal design.
-
inner_points(DataFrame) –Near-optimal designs found (one row each); their convex hull is the inner approximation.
-
outer(DataFrame) –Halfspaces
sum(row[d] * z[d]) <= rhsof the outer approximation, with their origin inkind. -
history(DataFrame) –One row per iteration: ORACLE metric, trial point, nearest near-optimal design, cuts added, timings.
-
scale(Series) –Scale of each dimension used to measure distances (distances are in units of
z / scale). -
tol(float) –Convergence tolerance on the ORACLE metric.
-
distance(float) –Certified upper bound on the ORACLE metric, i.e. on the largest distance between the two approximations (and so on the error made by using either of them).
-
converged(bool) –Whether the metric reached the tolerance.
Methods:
-
contains–Whether
pointlies in the inner (guaranteed near-optimal) or outer (possibly near-optimal) region. -
distance_to_inner–Scaled L-infinity distance from
pointto the inner approximation (0 if it is inside). -
flat_dimensions–Dimensions that do not vary in the outer approximation (and so in the near-optimal space).
-
plot_convergence–Plot the ORACLE metric against the iterations, see
-
plot_projection–Plot both approximations projected on two dimensions, see
-
plot_ranges–Plot the near-optimal range of each dimension, see
plot_ranges. -
ranges–Smallest and largest value of each dimension over both approximations.
-
sample–Random designs from the inner (guaranteed near-optimal) or outer approximation.
-
vertices–Vertices of the inner or outer approximation (exact, practical up to about 6 dimensions).
outer_A
property
¤
outer_A: ndarray
Normals of the outer approximation's halfspaces, one row per halfspace.
contains
¤
contains(
point: Union[Sequence[float], dict, Series],
region: str = "inner",
tol: float = 1e-06,
) -> bool
Whether point lies in the inner (guaranteed near-optimal) or outer (possibly near-optimal) region.
Source code in src/energyscope/oracle/algorithm.py
87 88 89 90 91 92 93 94 95 | |
distance_to_inner
¤
distance_to_inner(
point: Union[Sequence[float], dict, Series],
) -> float
Scaled L-infinity distance from point to the inner approximation (0 if it is inside).
Source code in src/energyscope/oracle/algorithm.py
97 98 99 100 101 | |
flat_dimensions
¤
flat_dimensions() -> list
Dimensions that do not vary in the outer approximation (and so in the near-optimal space).
Such a dimension is fixed by the model, e.g. by an equality constraint. It makes the approximations flat, so
vertices and
sample cannot be used on the outer approximation:
explore without it.
Source code in src/energyscope/oracle/algorithm.py
103 104 105 106 107 108 109 110 111 112 113 | |
plot_convergence
¤
plot_convergence(**kwargs)
Plot the ORACLE metric against the iterations, see
plot_convergence.
Source code in src/energyscope/oracle/algorithm.py
148 149 150 151 152 | |
plot_projection
¤
plot_projection(x: str, y: str, **kwargs)
Plot both approximations projected on two dimensions, see
plot_projection.
Source code in src/energyscope/oracle/algorithm.py
154 155 156 157 158 | |
plot_ranges
¤
plot_ranges(**kwargs)
Plot the near-optimal range of each dimension, see plot_ranges.
Source code in src/energyscope/oracle/algorithm.py
160 161 162 163 | |
ranges
¤
ranges() -> DataFrame
Smallest and largest value of each dimension over both approximations.
The true near-optimal range of each dimension lies between the inner and the outer one.
Source code in src/energyscope/oracle/algorithm.py
74 75 76 77 78 79 80 81 82 83 84 85 | |
sample
¤
sample(
n: int = 100,
region: str = "inner",
seed: Optional[int] = None,
) -> DataFrame
Random designs from the inner (guaranteed near-optimal) or outer approximation.
Samples are approximately uniform (hit-and-run random walk). If the inner approximation is flat (fewer
than n_dims + 1 independent points), random convex combinations of its points are returned instead.
Source code in src/energyscope/oracle/algorithm.py
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | |
vertices
¤
vertices(region: str = 'outer') -> DataFrame
Vertices of the inner or outer approximation (exact, practical up to about 6 dimensions).
Source code in src/energyscope/oracle/algorithm.py
115 116 117 118 119 120 121 122 123 124 125 | |