problem
¤
Models that ORACLE can explore.
ORACLE only talks to a model through four questions (the "oracle" calls):
- What is the cost optimum?
(
solve_optimum) - Which near-optimal design is closest to this trial point?
(
nearest) - What does it cost, at least, to build exactly this design?
(
cost_at) - How far can the near-optimal space go in this direction?
(
extreme)
PyomoProblem answers them for any linear Pyomo model (e.g. the EnergyScope
pyomo_ch model, license-free with HiGHS) and AmplProblem for any linear
AMPL model (e.g. the EnergyScope core model).
Classes:
-
AmplProblem–A linear AMPL model, e.g. the EnergyScope
coreorinfrastructure_ch_2050model. -
CostAtPoint–Answer to "what does it cost, at least, to build exactly
point?". -
NearOptimalProblem–Interface between ORACLE and an optimisation model.
-
NearestPoint–Answer to "which near-optimal design is closest to
target?". -
PyomoProblem–A linear Pyomo model, solved with HiGHS (no license needed).
AmplProblem
¤
AmplProblem(
ampl: AMPL,
dimensions: Sequence[str],
design: str = "F",
cost: str = "TotalCost",
lower: Optional[Sequence[float]] = None,
upper: Optional[Sequence[float]] = None,
cost_coefficients: Optional[Sequence[float]] = None,
parser: Optional[Callable] = None,
relax_integers: bool = True,
)
Bases: NearOptimalProblem
A linear AMPL model, e.g. the EnergyScope core or infrastructure_ch_2050 model.
ORACLE declares a few extra AMPL entities, all prefixed with oracle_, and re-solves the model in the same
AMPL session with the solver selected in it (option solver). After each of its solves it drops its
constraints and objectives again and re-selects the session's objective, so the session (e.g. the one of an
Energyscope object) still solves the original model afterwards; its variables hold the last ORACLE solution.
Badly scaled models such as infrastructure_ch_2050 (coefficients from 1e-7 to 1e7) can make an LP solver stop
at a design that is not optimal and still report it as optimal, because the error is below its default tolerance.
ORACLE would then get wrong distances and invalid cuts. To prevent this, ORACLE's solves:
- put the options in
solver_defaults(for Gurobi: deterministic dual simplex with the tightest optimality tolerance) before the session's<solver>_options, which therefore take precedence; the session's options are restored afterwards; - minimise
distance_weighttimes the distance in the nearest-design problem, so that its dual values (which give ORACLE's cuts) are not tolerance-sized; - re-solve once when the solver fails (
solve_result_num500 to 599), e.g. on "unrecoverable numerical issues".
For infrastructure_ch_2050, use Gurobi: CPLEX and HiGHS (through AMPL) do not solve its ORACLE problems
reliably (CPLEX reports numerical issues, HiGHS may not finish). A warning of ORACLE that a cut "would remove
known near-optimal designs" means that the solver's answers are inaccurate.
Parameters:
-
(ampl¤AMPL) –An
amplpy.AMPLobject with the model and data already loaded. -
(dimensions¤Sequence[str]) –Indices of
designto explore, e.g. technology names. -
(design¤str, default:'F') –Name of the indexed design variable (default
'F', the installed capacities). -
(cost¤str, default:'TotalCost') –Name of the total cost variable (default
'TotalCost'). -
(lower¤Optional[Sequence[float]], default:None) –Lower bounds of the explored variables (default: unbounded). They are imposed in ORACLE's solves, so bounds tighter than the model's restrict the explored space to this box.
-
(upper¤Optional[Sequence[float]], default:None) –Upper bounds of the explored variables (default: unbounded), see
lower. -
(cost_coefficients¤Optional[Sequence[float]], default:None) –Under-estimator of the cost, see
cost_coefficients. -
(parser¤Optional[Callable], default:None) –Function turning the solved AMPL object into a result object, used by
design_at. -
(relax_integers¤bool, default:True) –ORACLE needs a convex model. If the model has integer or binary variables, relax them to continuous ones in ORACLE's solves (default, AMPL option
relax_integrality) or raise an error (False).
Methods:
-
bounds– -
cost_at– -
cost_coefficients– -
design_at–Cheapest full system with the design
point: aResultfor EnergyScope -
extreme– -
from_energyscope–Explore the installed capacities of some technologies of an EnergyScope AMPL model.
-
nearest– -
set_budget– -
solve_optimum–
Attributes:
-
ampl– -
dimensions– -
distance_weight(float) –The nearest-design problem minimises
distance_weight * distance; distances and duals are scaled back. -
n_dims(int) – -
parser– -
solver_defaults(dict[str, dict[str, str]]) –Solver options used in ORACLE's solves, per solver (matched in the
solveroption).
Source code in src/energyscope/oracle/problem.py
408 409 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 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 | |
distance_weight
class-attribute
instance-attribute
¤
distance_weight: float = 1000.0
The nearest-design problem minimises distance_weight * distance; distances and duals are scaled back.
solver_defaults
class-attribute
instance-attribute
¤
solver_defaults: dict[str, dict[str, str]] = {
solver: dict(options)
for solver, options in type(
self
).solver_defaults.items()
}
Solver options used in ORACLE's solves, per solver (matched in the solver option).
Default: {'gurobi': {'method': '1', 'opttol': '1e-9'}} (Gurobi's dual simplex with the tightest optimality
tolerance). They are put before the session's <solver>_options, so the session's own options take precedence.
Each instance has its own copy: set it to {} on an instance to use the session's options only.
bounds
¤
bounds() -> tuple[ndarray, ndarray]
Source code in src/energyscope/oracle/problem.py
609 610 | |
cost_at
¤
cost_at(point: ndarray) -> CostAtPoint
Source code in src/energyscope/oracle/problem.py
631 632 633 634 635 | |
cost_coefficients
¤
cost_coefficients() -> Optional[ndarray]
Source code in src/energyscope/oracle/problem.py
612 613 | |
design_at
¤
design_at(point: ndarray) -> Any
Cheapest full system with the design point: a Result for EnergyScope
models (via parser), otherwise the AMPL object holding the solution.
Source code in src/energyscope/oracle/problem.py
642 643 644 645 646 647 648 | |
extreme
¤
extreme(direction: ndarray) -> ndarray
Source code in src/energyscope/oracle/problem.py
637 638 639 640 | |
from_energyscope
classmethod
¤
from_energyscope(
es: Energyscope,
technologies: Sequence[str],
ds: Optional[Dataset] = None,
design: Optional[str] = None,
) -> AmplProblem
Explore the installed capacities of some technologies of an EnergyScope AMPL model.
Parameters:
-
(es¤Energyscope) –An
Energyscopeobject, e.g.Energyscope(model=infrastructure_ch_2050). Its model files are loaded if they are not yet. -
(technologies¤Sequence[str]) –Technologies whose capacity spans the explored space. Prefer technologies with a finite
f_max; infinite bounds are replaced by the extent of the near-optimal space (one extra solve each). -
(ds¤Optional[Dataset], default:None) –Optional
Datasetpassed to the model when it is loaded. -
(design¤Optional[str], default:None) –Name of the capacity variable (default:
Fin the typical-day models such ascore,F_Multin the monthly models).
Returns:
-
AmplProblem–The problem, with the technology bounds
f_min/f_maxand the annualised investment coststau * c_invas cost under-estimator. Integer variables of the model are relaxed.
Source code in src/energyscope/oracle/problem.py
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 494 495 496 497 498 499 500 | |
nearest
¤
nearest(target: ndarray, scale: ndarray) -> NearestPoint
Source code in src/energyscope/oracle/problem.py
622 623 624 625 626 627 628 629 | |
set_budget
¤
set_budget(budget: Optional[float]) -> None
Source code in src/energyscope/oracle/problem.py
604 605 606 607 | |
solve_optimum
¤
solve_optimum() -> tuple[float, ndarray]
Source code in src/energyscope/oracle/problem.py
618 619 620 | |
CostAtPoint
dataclass
¤
CostAtPoint(cost: float, gradient: ndarray)
Answer to "what does it cost, at least, to build exactly point?".
Attributes:
NearOptimalProblem
¤
NearOptimalProblem(dimensions: Sequence[Any])
Bases: ABC
Interface between ORACLE and an optimisation model.
The model must be linear (more generally, convex): ORACLE's approximations are only valid if the near-optimal space is convex. Implement the abstract methods to plug in another modelling framework.
Parameters:
-
(dimensions¤Sequence[Any]) –Names of the design variables that span the explored space (e.g. technology names).
Methods:
-
bounds–Lower and upper bounds of the design variables (
-inf/infwhen unknown). -
cost_at–Minimise the total cost with the design fixed to
point(ignoring the budget). -
cost_coefficients–Coefficients
csuch thatc @ znever exceeds the total cost (e.g. annualised investment costs). -
design_at–Full model solution of the cheapest system that has the design
point. -
extreme–Near-optimal design minimising
direction @ z. -
nearest–Find the near-optimal design closest to
targetin the normmax_i |z_i - target_i| / scale_i. -
set_budget–Set the cost budget
(1 + eps) * optimal costthat defines the near-optimal space. -
solve_optimum–Minimise the total cost (ignoring the budget). Returns the optimal cost and design.
Attributes:
-
dimensions– -
n_dims(int) –
Source code in src/energyscope/oracle/problem.py
76 77 78 79 80 | |
bounds
¤
bounds() -> tuple[ndarray, ndarray]
Lower and upper bounds of the design variables (-inf/inf when unknown).
Source code in src/energyscope/oracle/problem.py
106 107 108 | |
cost_at
abstractmethod
¤
cost_at(point: ndarray) -> CostAtPoint
Minimise the total cost with the design fixed to point (ignoring the budget).
Source code in src/energyscope/oracle/problem.py
98 99 100 | |
cost_coefficients
¤
cost_coefficients() -> Optional[ndarray]
Coefficients c such that c @ z never exceeds the total cost (e.g. annualised investment costs).
They give ORACLE's initial "cost cut" c @ z <= budget. Return None if no such under-estimator is known.
Source code in src/energyscope/oracle/problem.py
110 111 112 113 114 115 | |
design_at
¤
design_at(point: ndarray) -> Any
Full model solution of the cheapest system that has the design point.
Source code in src/energyscope/oracle/problem.py
117 118 119 | |
extreme
abstractmethod
¤
extreme(direction: ndarray) -> ndarray
Near-optimal design minimising direction @ z.
Source code in src/energyscope/oracle/problem.py
102 103 104 | |
nearest
abstractmethod
¤
nearest(target: ndarray, scale: ndarray) -> NearestPoint
Find the near-optimal design closest to target in the norm max_i |z_i - target_i| / scale_i.
Source code in src/energyscope/oracle/problem.py
94 95 96 | |
set_budget
¤
set_budget(budget: Optional[float]) -> None
Set the cost budget (1 + eps) * optimal cost that defines the near-optimal space.
Source code in src/energyscope/oracle/problem.py
86 87 88 | |
solve_optimum
abstractmethod
¤
solve_optimum() -> tuple[float, ndarray]
Minimise the total cost (ignoring the budget). Returns the optimal cost and design.
Source code in src/energyscope/oracle/problem.py
90 91 92 | |
NearestPoint
dataclass
¤
NearestPoint(
point: ndarray,
distance: float,
normal: ndarray,
cost: float,
)
Answer to "which near-optimal design is closest to target?".
Attributes:
-
point(ndarray) –The closest near-optimal design
z_f(in the units of the design variables). -
distance(float) –Its (scaled) L-infinity distance to the target. Zero means the target itself is near-optimal.
-
normal(ndarray) –Sensitivity of
distanceto the target (the dual variables of the problem). It is the normal of a hyperplane that separates the target from the whole near-optimal space. -
cost(float) –Total cost of the design found.
PyomoProblem
¤
PyomoProblem(
model: ConcreteModel,
design: Union[str, Any],
cost: Union[str, Any],
dimensions: Optional[Sequence[Any]] = None,
lower: Optional[Sequence[float]] = None,
upper: Optional[Sequence[float]] = None,
cost_coefficients: Optional[Sequence[float]] = None,
parser: Optional[Callable] = None,
relax_integers: bool = True,
solver_options: Optional[dict] = None,
)
Bases: NearOptimalProblem
A linear Pyomo model, solved with HiGHS (no license needed).
ORACLE adds a block model.oracle to the model with the extra variables, constraints and objectives it needs,
deactivates the model's own objectives, and re-solves the model with a persistent HiGHS instance, so each query
only takes a fraction of a full solve. The model is therefore dedicated to ORACLE (from_energyscope builds a
fresh one).
Parameters:
-
(model¤ConcreteModel) –The Pyomo
ConcreteModel. Its objectives are deactivated; the cost to minimise is given bycost. -
(design¤Union[str, Any]) –The indexed design variable (or its name), e.g.
'F_Mult'. -
(cost¤Union[str, Any]) –The total cost variable or expression (or its name), e.g.
'TotalCost'. -
(dimensions¤Optional[Sequence[Any]], default:None) –Indices of
designto explore (default: all of them). -
(lower¤Optional[Sequence[float]], default:None) –Lower bounds of the explored variables (default: the variable bounds). Given bounds are added to the model as constraints, so bounds tighter than the model's restrict the explored space to this box.
-
(upper¤Optional[Sequence[float]], default:None) –Upper bounds of the explored variables (default: the variable bounds), see
lower. -
(cost_coefficients¤Optional[Sequence[float]], default:None) –Under-estimator of the cost, see
cost_coefficients. -
(parser¤Optional[Callable], default:None) –Function turning the solved model into a result object, used by
design_at. -
(relax_integers¤bool, default:True) –ORACLE needs a convex model. If the model has integer or binary variables, relax them to continuous ones (default) or raise an error (
False). -
(solver_options¤Optional[dict], default:None) –Extra HiGHS options.
Methods:
-
bounds– -
cost_at– -
cost_coefficients– -
design_at–Cheapest full system with the design
point: aResultfor EnergyScope -
extreme– -
from_energyscope–Explore the installed capacities of some technologies of an EnergyScope Pyomo model.
-
nearest– -
set_budget– -
solve_optimum–
Attributes:
-
dimensions– -
model– -
n_dims(int) – -
parser–
Source code in src/energyscope/oracle/problem.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 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 226 227 228 | |
bounds
¤
bounds() -> tuple[ndarray, ndarray]
Source code in src/energyscope/oracle/problem.py
311 312 | |
cost_at
¤
cost_at(point: ndarray) -> CostAtPoint
Source code in src/energyscope/oracle/problem.py
334 335 336 337 338 339 340 341 | |
cost_coefficients
¤
cost_coefficients() -> Optional[ndarray]
Source code in src/energyscope/oracle/problem.py
314 315 | |
design_at
¤
design_at(point: ndarray) -> Any
Cheapest full system with the design point: a Result for EnergyScope
models (via parser), otherwise the solved Pyomo model.
Source code in src/energyscope/oracle/problem.py
351 352 353 354 355 | |
extreme
¤
extreme(direction: ndarray) -> ndarray
Source code in src/energyscope/oracle/problem.py
343 344 345 346 347 348 349 | |
from_energyscope
classmethod
¤
from_energyscope(
technologies: Sequence[str],
model: Optional[PyomoModel] = None,
solver_options: Optional[dict] = None,
) -> PyomoProblem
Explore the installed capacities of some technologies of an EnergyScope Pyomo model.
Parameters:
-
(technologies¤Sequence[str]) –Technologies whose capacity (
F_Mult) spans the explored space, e.g.['PV', 'WIND', 'NUCLEAR', 'CCGT']. -
(model¤Optional[PyomoModel], default:None) –An EnergyScope
PyomoModel(default:energyscope.models.pyomo_ch). -
(solver_options¤Optional[dict], default:None) –Extra HiGHS options.
Returns:
-
PyomoProblem–The problem, with the technology bounds
f_min/f_maxand the annualised investment coststau * c_invas cost under-estimator.
Source code in src/energyscope/oracle/problem.py
230 231 232 233 234 235 236 237 238 239 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 | |
nearest
¤
nearest(target: ndarray, scale: ndarray) -> NearestPoint
Source code in src/energyscope/oracle/problem.py
323 324 325 326 327 328 329 330 331 332 | |
set_budget
¤
set_budget(budget: Optional[float]) -> None
Source code in src/energyscope/oracle/problem.py
306 307 308 309 | |
solve_optimum
¤
solve_optimum() -> tuple[float, ndarray]
Source code in src/energyscope/oracle/problem.py
317 318 319 320 321 | |