Bayesian-optimization layer
BayesianOptimizer runs one of these GEMSEO libraries. BaseBOLibrary owns everything that must not depend on the backend; a backend only implements _setup, _ask and _tell.
| Algorithm | Library | Backend |
|---|---|---|
Ax_Bayesian (default) |
AxOptimizationLibrary |
Ax: Sobol for n_init trials, then BoTorch. |
BO_RandomSearch |
RandomSearchLibrary |
Seeded uniform sampling (log-uniform for scaling="log"), the reference backend of the tests. |
Driver
mdo_framework.optimization.bo_library
This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
BaseBOLibrary
Bases: BaseOptimizationLibrary
GEMSEO driver running an ask-evaluate-tell loop for any backend.
The driver owns everything that must not depend on the backend: the budget, the time limit, the guarded start point, the classification of failures, the bindings between user names and GEMSEO functions, and the feasibility of a trial. A backend only proposes candidates and learns their outcome. It never sees GEMSEO names or NaN, never decides to stop and never evaluates x0.
Attributes:
| Name | Type | Description |
|---|---|---|
result |
BORunResult | None
|
Outcome of the last run, set even when the run is aborted. |
Source code in src/mdo_framework/optimization/bo_library.py
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 229 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 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 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 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 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 | |
MAX_STALLED_GENERATIONS = 5
class-attribute
instance-attribute
Generations adding no new evaluation that end the run.
BaseBOSettings
Bases: BaseOptimizerSettings
Settings shared by the backends of the Bayesian optimization driver.
The budget is set by the phases, never by max_iter: the run makes
n_init initial trials and n_steps model-driven trials, after the
start point x0 when it is evaluated. max_iter is derived from them and
only accepted if it agrees. The GEMSEO tolerances ftol_*, xtol_*
and scaling_threshold are ignored: only the budget and max_time
stop a run, so a plateau never ends it early.
Attributes:
| Name | Type | Description |
|---|---|---|
design_variables |
tuple[DesignVariable, ...]
|
Design variables, in the order of the design space. |
objectives |
tuple[ObjectiveSpec, ...]
|
Objectives, in the order of the objective function. |
constraints |
tuple[ConstraintSpec, ...]
|
Inequality constraints, in the order they were added. |
parameter_constraints |
tuple[str, ...]
|
Linear constraints between design variables. |
n_init |
PositiveInt
|
Number of initial trials. |
n_steps |
PositiveInt
|
Number of model-driven trials. |
evaluate_x0 |
bool | None
|
Whether to evaluate x0 first. |
seed |
int | None
|
Seed of the backend, |
batch_size |
PositiveInt
|
Maximum number of candidates asked at once. |
max_consecutive_failures |
PositiveInt
|
Failed trials in a row that end the run. |
Source code in src/mdo_framework/optimization/bo_library.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | |
x0_enabled
property
Whether x0 is evaluated.
add_constraints(scenario, constraints)
Add the constraints of a study to a GEMSEO scenario.
The tolerance is folded into the bound, so that GEMSEO, which runs with a zero tolerance, agrees with the feasibility of the library.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scenario
|
Any
|
GEMSEO scenario to add the constraints to. |
required |
constraints
|
Sequence[ConstraintSpec]
|
Constraint declarations. |
required |
Source code in src/mdo_framework/optimization/bo_library.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | |
build_metric_bindings(problem, objectives, constraints)
Bind every user output to the GEMSEO function that computes it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
problem
|
OptimizationProblem
|
GEMSEO problem built from the study. |
required |
objectives
|
Sequence[ObjectiveSpec]
|
Objective declarations, in the order of the objective. |
required |
constraints
|
Sequence[ConstraintSpec]
|
Constraint declarations, in the order they were added. |
required |
Returns:
| Type | Description |
|---|---|
tuple[MetricBinding, ...]
|
The objective bindings followed by the constraint bindings. |
Raises:
| Type | Description |
|---|---|
OptimizationConfigurationError
|
If the problem does not hold exactly these objectives and constraints. |
Source code in src/mdo_framework/optimization/bo_library.py
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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | |
Types
mdo_framework.optimization.bo_types
This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
MAX_ENUMERATED_POINTS = 100000
module-attribute
Largest discrete space whose points are checked against the constraints.
BORunResult
dataclass
Outcome of a Bayesian optimization run.
Attributes:
| Name | Type | Description |
|---|---|---|
records |
Sequence[TrialRecord]
|
Every trial, in run order. |
stop_reason |
StopReason
|
Why the run stopped. |
bindings |
Sequence[MetricBinding]
|
Objective and constraint bindings of the run. |
Source code in src/mdo_framework/optimization/bo_types.py
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 381 382 383 384 385 386 387 | |
best
property
The record to report, see select_best.
evaluations
property
Evaluations per phase, which exclude abandoned trials, and failures.
feasible
property
Whether the reported record respects every constraint.
pareto_front
property
Feasible records not dominated by another, empty for one objective.
BOSpace
dataclass
Design space of a run: variables and linear constraints between them.
Attributes:
| Name | Type | Description |
|---|---|---|
design_variables |
tuple[DesignVariable, ...]
|
Range and choice variables, in schema order. |
linear_constraints |
tuple[LinearConstraint, ...]
|
The parameter constraints, parsed. |
parameter_constraints |
tuple[str, ...]
|
The same constraints as expressions, for backends that take them verbatim. |
Source code in src/mdo_framework/optimization/bo_types.py
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 | |
names
property
Design variable names, in schema order.
box_centre()
Return the centre of the variable bounds, ignoring constraints.
Source code in src/mdo_framework/optimization/bo_types.py
567 568 569 | |
cardinality()
Return the number of distinct points of a finite space.
Returns:
| Type | Description |
|---|---|
int | None
|
The number of points satisfying the linear constraints when every |
int | None
|
variable is discrete, |
int | None
|
the constraints apply to more than |
int | None
|
candidate points. |
Source code in src/mdo_framework/optimization/bo_types.py
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 | |
chebyshev_centre()
Return the centre of the largest ball inside the linear constraints.
The ball is measured in unit-box coordinates over the range variables; integers are rounded and choices sit at the box centre.
Raises:
| Type | Description |
|---|---|
OptimizationConfigurationError
|
If the constraints leave no point in the design space, or rounding integers leaves it. |
Source code in src/mdo_framework/optimization/bo_types.py
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 | |
contains(parameters)
Return whether a point is in the design space.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parameters
|
Mapping[str, Scalar]
|
User value of every design variable. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
bool
|
variables and a declared choice, and the linear constraints hold. |
Source code in src/mdo_framework/optimization/bo_types.py
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 | |
from_vector(x)
Decode a GEMSEO design vector into user values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Sequence[float] | ndarray
|
Design vector, in schema order. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Scalar]
|
User values: floats, ints, and the declared choice values. |
Source code in src/mdo_framework/optimization/bo_types.py
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 | |
start_point()
Return the point the run starts from.
Declared initial values are used, the box centre elsewhere. If that
violates a linear constraint and no variable declares an initial value,
the Chebyshev centre is used instead.
Raises:
| Type | Description |
|---|---|
OptimizationConfigurationError
|
If declared initial values violate a linear constraint, or no Chebyshev centre exists. |
Source code in src/mdo_framework/optimization/bo_types.py
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 | |
to_vector(parameters)
Encode user values as a GEMSEO design vector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parameters
|
Mapping[str, Scalar]
|
User value of every design variable. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
The vector, in schema order, with choices as indices. |
Source code in src/mdo_framework/optimization/bo_types.py
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 | |
Candidate
dataclass
A point proposed by a backend.
Attributes:
| Name | Type | Description |
|---|---|---|
parameters |
Mapping[str, Scalar]
|
User values of the design variables, in schema order. |
key |
Hashable | None
|
Backend handle used to report the outcome, e.g. an Ax trial index. |
Source code in src/mdo_framework/optimization/bo_types.py
44 45 46 47 48 49 50 51 52 53 54 | |
MetricBinding
dataclass
Link between a user output and the GEMSEO function that computes it.
The user value is sign * gemseo_value[index] + offset.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
User output name of the objective or constraint. |
role |
Literal['objective', 'constraint']
|
|
gemseo_name |
str
|
Name of the GEMSEO function holding the value. |
index |
int
|
Position of the value in that function's output. |
sign |
float
|
Factor undoing the GEMSEO transformation. |
offset |
float
|
Term undoing the GEMSEO transformation, in user units. |
minimize |
bool
|
Direction of an objective. |
threshold |
float | None
|
Reference point of an objective, in user units. |
op |
Literal['<=', '>='] | None
|
|
bound |
float | None
|
Bound of a constraint, in user units. |
tolerance |
float
|
Violation of the bound still accepted, in user units. |
scale |
float
|
Typical magnitude of a constraint, to compare violations. |
Source code in src/mdo_framework/optimization/bo_types.py
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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 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 | |
margin(value)
Return the distance to the bound, positive when inside it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
float
|
Raw user value of the constraint output. |
required |
Returns:
| Type | Description |
|---|---|
float
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the binding is not a constraint. |
Source code in src/mdo_framework/optimization/bo_types.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | |
satisfied(value)
Return whether a value respects the bound within the tolerance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
float
|
Raw user value of the output. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
bool
|
|
bool
|
GEMSEO enforces so both agree at the edge; always |
bool
|
objective. |
Source code in src/mdo_framework/optimization/bo_types.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | |
user_value(output_data)
Return the raw user value from the output data of GEMSEO.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_data
|
Mapping[str, Any]
|
GEMSEO function values, by GEMSEO function name. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The value of the user output, in user units. |
Source code in src/mdo_framework/optimization/bo_types.py
133 134 135 136 137 138 139 140 141 142 143 | |
violation(value)
Return how far a value lies past the bound, in units of scale.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
float
|
Raw user value of the output. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Zero when |
float
|
folded bound GEMSEO enforces divided by |
float
|
objective. |
Source code in src/mdo_framework/optimization/bo_types.py
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | |
TrialOutcome
dataclass
What evaluating a candidate produced.
Attributes:
| Name | Type | Description |
|---|---|---|
status |
TrialStatus
|
|
metrics |
Mapping[str, float]
|
Raw user value of each objective and constraint output. |
reason |
str | None
|
Why the trial failed or was abandoned, |
Source code in src/mdo_framework/optimization/bo_types.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | |
TrialRecord
dataclass
A candidate together with its outcome.
Attributes:
| Name | Type | Description |
|---|---|---|
index |
int
|
Position of the trial in the run. |
phase |
Phase
|
|
parameters |
Mapping[str, Scalar]
|
User values of the design variables. |
outcome |
TrialOutcome
|
What the evaluation produced. |
Source code in src/mdo_framework/optimization/bo_types.py
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 229 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 267 268 269 | |
feasible(bindings)
Return whether the trial completed within every constraint.
Source code in src/mdo_framework/optimization/bo_types.py
217 218 219 220 221 222 223 224 | |
to_history_entry(bindings)
Return the record as a history entry keyed by user names only.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bindings
|
Sequence[MetricBinding]
|
Objective and constraint bindings of the run. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The entry. |
dict[str, Any]
|
their margins; both are empty unless the trial completed. |
Source code in src/mdo_framework/optimization/bo_types.py
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 267 268 269 | |
violation(bindings)
Return the total violation, net of tolerance, in constraint scales.
Source code in src/mdo_framework/optimization/bo_types.py
226 227 228 229 230 231 | |
gemseo_bound(spec)
Return the bound GEMSEO enforces for a constraint.
The tolerance is folded into the bound, so GEMSEO runs with a zero
tolerance and its feasibility matches MetricBinding.satisfied.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
ConstraintSpec
|
Constraint declaration. |
required |
Returns:
| Type | Description |
|---|---|
float
|
|
Source code in src/mdo_framework/optimization/bo_types.py
74 75 76 77 78 79 80 81 82 83 84 85 86 | |
select_best(records, bindings)
Pick the record to report as the result of a run.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
records
|
Sequence[TrialRecord]
|
Trial records, in run order. |
required |
bindings
|
Sequence[MetricBinding]
|
Objective and constraint bindings of the run. |
required |
Returns:
| Type | Description |
|---|---|
TrialRecord | None
|
With one objective, the feasible completed record that is best in the |
TrialRecord | None
|
user direction; with several, the Pareto-front point closest to the |
TrialRecord | None
|
ideal point. Without a feasible record, the completed record with the |
TrialRecord | None
|
least total violation. Earlier records win ties. |
TrialRecord | None
|
completed. |
Source code in src/mdo_framework/optimization/bo_types.py
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 | |
Random search
mdo_framework.optimization.random_search
This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
RandomSearchLibrary
Bases: BaseBOLibrary
Seeded random search, the reference backend of the driver.
It draws every design variable uniformly (log-uniformly on a log scale, among the choices for a choice variable), keeps the draws that satisfy the parameter constraints, and never proposes a point twice. It learns nothing from the outcomes, so it needs no objective model.
Source code in src/mdo_framework/optimization/random_search.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 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 | |
MAX_DRAWS = 1000
class-attribute
instance-attribute
Draws made to find one new point satisfying the parameter constraints.
RandomSearchSettings
Bases: BaseBOSettings
Settings of the random-search backend, those of the driver.
Source code in src/mdo_framework/optimization/random_search.py
25 26 | |
Ax backend
mdo_framework.optimization.ax_algo_lib
This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
AxOptimizationLibrary
Bases: BaseBOLibrary
Bayesian optimization backend running on the Ax client API.
It only maps the three hooks of the driver onto an Ax experiment: the search space, the metrics and the generation strategy of the experiment, the trials Ax proposes, and the outcome of each. Everything else belongs to the driver.
Source code in src/mdo_framework/optimization/ax_algo_lib.py
155 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 229 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 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | |
__init__(algo_name='Ax_Bayesian', client_factory=None)
Create the library.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
algo_name
|
str
|
Name of the algorithm. |
'Ax_Bayesian'
|
client_factory
|
Callable[..., Client] | None
|
Callable building the Ax client from the keyword
|
None
|
Source code in src/mdo_framework/optimization/ax_algo_lib.py
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | |
AxSettings
Bases: BaseBOSettings
Settings of the Ax backend.
Attributes:
| Name | Type | Description |
|---|---|---|
use_bonsai |
bool
|
Whether to use the experimental BONSAI acquisition of Ax instead of the default one. |
Source code in src/mdo_framework/optimization/ax_algo_lib.py
144 145 146 147 148 149 150 151 152 | |
Errors
mdo_framework.optimization.errors
This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
OptimizationConfigurationError
Bases: ValueError
Raised when the optimization request is invalid for the current backend.
Source code in src/mdo_framework/optimization/errors.py
11 12 | |
OptimizationExecutionError
Bases: RuntimeError
Raised when optimization cannot produce a valid result.
Attributes:
| Name | Type | Description |
|---|---|---|
partial_result |
What the run produced before it was aborted, for
example its trial history; |
Source code in src/mdo_framework/optimization/errors.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | |
__init__(message, *, partial_result=None)
Build the error.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Why the optimization failed. |
required |
partial_result
|
Mapping[str, Any] | None
|
What the run produced before it was aborted. |
None
|
Source code in src/mdo_framework/optimization/errors.py
23 24 25 26 27 28 29 30 31 32 33 | |