Configuration Classes
ropt.config
Configuration classes for ensemble-based optimization.
Pydantic models that together define a complete optimization setup. Each
corresponds to a top-level section of the configuration dictionary used to build
an EnOptContext, the in-memory configuration of a
single run. See
Configuration Sections for the
fields, their defaults and worked examples.
VariablesConfig
Configuration class for optimization variables.
VariablesConfig defines optimization variable settings for an
EnOptContext object: bounds, types, mask, and
perturbation settings.
See Configuration Sections for detailed descriptions and usage examples.
The perturbation defaults are
DEFAULT_PERTURBATION_MAGNITUDE,
DEFAULT_PERTURBATION_TYPE
(one of PerturbationType) and
DEFAULT_PERTURBATION_BOUNDARY_TYPE
(one of BoundaryType).
Variables reach the optimizer as \(y = (x - o)/s\) and are reported back as
\(x = s\,y + o\), using scales and offsets. A scale is a change of units,
so every entry must be positive. There is no auto_scale here:
scales_and_offsets_from_bounds
derives scales from the bounds when that is what is wanted.
Attributes:
| Name | Type | Description |
|---|---|---|
variable_count |
int
|
Number of variables. |
lower_bounds |
Array1D
|
Lower bounds for the variables (default: \(-\infty\)). |
upper_bounds |
Array1D
|
Upper bounds for the variables (default: \(+\infty\)). |
types |
ArrayEnum
|
Optional variable types. |
mask |
Array1DBool
|
Optional boolean mask indicating free variables. |
scales |
Array1D
|
Scale factors for the variables (default: 1.0). |
offsets |
Array1D
|
Offsets for the variables (default: 0.0). |
perturbation_magnitudes |
Array1D
|
Magnitude of the perturbation of each variable. |
perturbation_types |
ArrayEnum
|
Type of perturbation for each variable. |
boundary_types |
ArrayEnum
|
Handling of perturbations that violate a bound. |
samplers |
Keys
|
Sampler for each variable, by key (default: |
seed |
ItemOrTuple[int]
|
Seed for the random number generator used by the samplers. |
ObjectiveFunctionsConfig
Configuration class for objective functions.
ObjectiveFunctionsConfig defines objective function settings for an
EnOptContext object.
See Configuration Sections for detailed descriptions and usage examples.
The reported target_objective is divided by the estimated auto_scale
factors, so runs that differ in that setting compare only through
functions.objectives or their variables.
Attributes:
| Name | Type | Description |
|---|---|---|
weights |
Array1D
|
Weights for the objective functions (default: 1.0). |
scales |
Array1D
|
Scale factors for the objective functions (default: 1.0). |
offsets |
Array1D
|
Offsets for the objective functions (default: 0.0). |
auto_scale |
bool
|
Estimate additional scales from the first batch. |
maximize |
Array1DBool
|
Which objectives to maximize (default: |
realization_filters |
Keys
|
Realization filter per objective, by key (default: none). |
function_estimators |
Keys
|
Function estimator per objective, by key (default: |
LinearConstraintsConfig
Configuration class for linear constraints.
LinearConstraintsConfig defines linear constraints used as the
linear_constraints field of an
EnOptContext object.
See Configuration Sections for detailed descriptions and usage examples.
Attributes:
| Name | Type | Description |
|---|---|---|
coefficients |
Array2D
|
Matrix of coefficients for the linear constraints. |
lower_bounds |
Array1D
|
Lower bounds for the right-hand side of the constraint equations. |
upper_bounds |
Array1D
|
Upper bounds for the right-hand side of the constraint equations. |
scales |
Array1D
|
Scale factors for the constraint equations (default: 1.0). |
auto_scale |
bool
|
Estimate an additional scale for each equation. |
NonlinearConstraintsConfig
Configuration class for non-linear constraints.
NonlinearConstraintsConfig defines nonlinear constraints used as the
nonlinear_constraints field of an
EnOptContext object.
See Configuration Sections for detailed descriptions and usage examples.
Attributes:
| Name | Type | Description |
|---|---|---|
lower_bounds |
Array1D
|
Lower bounds for the right-hand-side values. |
upper_bounds |
Array1D
|
Upper bounds for the right-hand-side values. |
scales |
Array1D
|
Scale factors for the constraint functions (default: 1.0). |
auto_scale |
Array1DBool
|
Which constraints to auto-scale (default: |
realization_filters |
Keys
|
Realization filter per constraint, by key (default: none). |
function_estimators |
Keys
|
Function estimator per constraint, by key (default: |
RealizationsConfig
Configuration class for realizations.
RealizationsConfig defines realization ensemble settings for an
EnOptContext object.
See Configuration Sections for detailed descriptions and usage examples.
realization_min_success defaults to the number of realizations, so a
single failure ends the run.
Attributes:
| Name | Type | Description |
|---|---|---|
weights |
Array1D
|
Weights for the realizations (default: 1.0). |
realization_min_success |
NonNegativeInt | None
|
Minimum number of successful realizations. |
OptimizerConfig
Configuration class for the optimization algorithm.
OptimizerConfig defines workflow-level settings for an optimization run,
configured as the optimizer field of
EnOptContext.
See Configuration Sections for detailed descriptions and usage examples.
Warning
Capturing output rewires process-global state, so one run at a time can
do it. A second run that sets stdout or stderr while another holds
the capture raises WorkflowError.
Attributes:
| Name | Type | Description |
|---|---|---|
max_batches |
PositiveInt | None
|
Maximum number of batch evaluations (optional). |
max_functions |
PositiveInt | None
|
Maximum number of function evaluations (optional). |
output_dir |
Path | None
|
Output directory for the optimizer (optional). |
stdout |
Path | None
|
File to redirect optimizer standard output (optional). |
stderr |
Path | None
|
File to redirect optimizer standard error (optional). |
BackendConfig
Configuration class for the optimization backend.
BackendConfig defines the configuration for the optimization algorithm
used by an optimization backend plugin.
See Configuration Sections for detailed descriptions and usage examples.
Attributes:
| Name | Type | Description |
|---|---|---|
method |
str
|
Name of the optimization method. |
max_iterations |
PositiveInt | None
|
Maximum number of iterations (optional). |
convergence_tolerance |
NonNegativeFloat | None
|
Convergence tolerance, on scaled quantities (optional). |
parallel |
bool
|
Allow parallelized function evaluations (default: |
verbose |
bool | NonNegativeInt | None
|
How much the optimizer reports (optional). |
options |
dict[str, Any] | list[str] | None
|
Generic options for the optimizer (optional). |
GradientConfig
Configuration class for gradient calculations.
GradientConfig specifies how gradients are estimated in gradient-based
optimizers. It is used as the gradient field of
EnOptContext.
See Configuration Sections for detailed descriptions and usage examples.
number_of_perturbations defaults to
DEFAULT_NUMBER_OF_PERTURBATIONS,
and perturbation_min_success to number_of_perturbations.
Attributes:
| Name | Type | Description |
|---|---|---|
number_of_perturbations |
PositiveInt
|
Number of perturbations. |
perturbation_min_success |
PositiveInt | None
|
Minimum number of successful perturbations. |
merge_realizations |
bool
|
Merge realizations for the final gradient. |
evaluation_policy |
Literal['speculative', 'separate', 'auto']
|
How to evaluate functions and gradients. |
FunctionEstimatorConfig
Configuration class for function estimators.
FunctionEstimatorConfig configures a function estimator plugin, which
controls how objective and constraint function values (and their gradients)
are combined across realizations.
See Configuration Sections for detailed descriptions and usage examples.
Attributes:
| Name | Type | Description |
|---|---|---|
method |
str
|
Name of the function estimator method. |
options |
dict[str, Any]
|
Dictionary of options for the function estimator. |
RealizationFilterConfig
Configuration class for realization filters.
RealizationFilterConfig configures a
RealizationFilter plugin that
adjusts per-realization weights.
See Configuration Sections for detailed descriptions and usage examples.
Attributes:
| Name | Type | Description |
|---|---|---|
method |
str
|
Name of the realization filter method. |
options |
dict[str, Any]
|
Dictionary of options for the realization filter. |
SamplerConfig
Configuration class for samplers.
SamplerConfig configures a Sampler plugin that
generates variable perturbations for gradient estimation.
See Configuration Sections for detailed descriptions and usage examples.
Attributes:
| Name | Type | Description |
|---|---|---|
method |
str
|
Name of the sampler method. |
options |
dict[str, Any]
|
Dictionary of options for the sampler. |
shared |
bool
|
Whether to share perturbation values between realizations (default: |
ropt.config.constants
Default values used by the configuration classes.
See Configuration Sections for detailed explanations of these defaults and their interactions.
DEFAULT_SEED
module-attribute
Default seed for random number generators used by samplers.
DEFAULT_NUMBER_OF_PERTURBATIONS
module-attribute
Default number of perturbations for gradient estimation.
DEFAULT_PERTURBATION_MAGNITUDE
module-attribute
Default scaling factor applied to sampler-generated perturbation values.
DEFAULT_PERTURBATION_BOUNDARY_TYPE
module-attribute
Default boundary handling for perturbations that violate variable bounds.
ropt.config.options
Schema models for describing and validating plugin options.
A plugin declares the options that each of its methods accepts. The schema
turns that declaration into a pydantic model that validates the options field
of a configuration section, and into a Markdown table for the documentation.
OptionsSchemaModel
Bases: BaseModel
The schema for the options of every method of a plugin.
The methods are described by a mapping of method names to
MethodSchemaModel objects, each
describing a method supported by the plugin.
Attributes:
| Name | Type | Description |
|---|---|---|
methods |
dict[str, MethodSchemaModel[Any]]
|
A mapping of method names to their schemas. |
common |
list[MethodSchemaModel[Any]]
|
Optional method schemas defining options shared by all methods. |
Example:
from ropt.config.options import OptionsSchemaModel
schema = OptionsSchemaModel.model_validate(
{
"methods": {
"method": {"options": {"a": float, "b": int | str}},
}
}
)
options = schema.get_options_model("method")
print(options.model_validate({"a": 1.0, "b": 1})) # a=1.0 b=1
get_options_model
Create a pydantic model for validating the options of one method.
The method is looked up by name, case-insensitively, and its options are combined with the common options that it does not exclude.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str
|
The name of the method for which to create the options model. |
required |
Returns:
| Type | Description |
|---|---|
type[BaseModel]
|
A model that validates the options of the given method. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the method is not found in the schema. |
MethodSchemaModel
The schema for the options of a single method.
Attributes:
| Name | Type | Description |
|---|---|---|
options |
dict[str, T]
|
A dictionary of option names and their types. |
url |
HttpUrl | None
|
An optional URL for the plugin. |
exclude |
set[str]
|
A set of common options to exclude for this method. |
title |
str | None
|
An optional title for common method sections. |
doc |
str | None
|
An optional description of the common section. |
gen_options_table
Generate a Markdown table documenting plugin options.
Common options are listed first, followed by a table of method-specific options, one row per method. A method whose schema provides a URL is hyperlinked to it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
dict[str, Any]
|
A dictionary representing the schema of plugin options. |
required |
Returns:
| Type | Description |
|---|---|
str
|
A string containing the documented plugin options. |