Skip to content

Context Class

ropt.context

The ropt.context module provides the context class used by optimization workflows.

EnOptContext

Bases: BaseModel

The primary context object for a single optimization run.

EnOptContext holds all information needed to run an ensemble-based optimization: variables, objectives, constraints, realizations, gradient settings, samplers, filters, and the optimizer/backend. It is constructed from plain Python dicts or config objects and validated on creation.

Index-based sharing

All tuple-based plugin fields (realization_filters, function_estimators, samplers, variable_transforms, objective_transforms, and nonlinear_constraint_transforms) are referenced by index from other config fields. For example, the samplers field of VariablesConfig is an integer array whose values index into the samplers tuple — use all zeros when a single sampler is shared across all variables, or distinct indices when different samplers are needed per variable. The same pattern applies to transform indices in VariablesConfig, ObjectiveFunctionsConfig, and NonlinearConstraintsConfig.

Optional names

The names attribute maps axis types (see AxisName) to ordered sequences of labels for variables, objectives, and constraints. It is not required for the optimization itself, but when present it is used to produce labelled multi-index results in exported data frames.

Plugin instances

The backend field and all tuple-based plugin fields (realization_filters, function_estimators, samplers, variable_transforms, objective_transforms, and nonlinear_constraint_transforms) store plugin instances. Instead of constructing instances manually, these fields can be initialized with a configuration object or a plain dict of settings — Pydantic will resolve and instantiate the appropriate plugin automatically. Each config class has a method field that selects the plugin implementation. The configuration classes are defined in the ropt.config sub-package.

Broadcasting

Many nested config classes represent per-variable or per-objective properties (e.g., bounds, perturbation magnitudes) as numpy arrays. A size-1 array is broadcast to all elements; otherwise the array length must match the count of the corresponding entities.

Warning

EnOptContext objects are immutable after construction. Do not attempt to serialize and round-trip them (e.g., to/from JSON): numpy arrays and plugin instances cannot survive a round-trip faithfully. Persist the raw input dicts instead.

Attributes:

Name Type Description
variables VariablesConfig

Variable settings.

objectives ObjectiveFunctionsConfig

Objective function settings.

linear_constraints LinearConstraintsConfig | None

Optional linear constraint settings.

nonlinear_constraints NonlinearConstraintsConfig | None

Optional nonlinear constraint settings.

realizations RealizationsConfig

Ensemble realization settings.

optimizer OptimizerConfig

Optimizer settings.

backend BackendInstance

Backend plugin instance used for function evaluations.

gradient GradientConfig

Gradient estimation settings.

realization_filters tuple[RealizationFilterInstance, ...]

Tuple of realization filter plugin instances.

function_estimators tuple[FunctionEstimatorInstance, ...]

Tuple of function estimator plugin instances.

samplers tuple[SamplerInstance, ...]

Tuple of sampler plugin instances.

variable_transforms tuple[VariableTransformInstance, ...]

Tuple of variable transform plugin instances.

objective_transforms tuple[ObjectiveTransformInstance, ...]

Tuple of objective transform plugin instances.

nonlinear_constraint_transforms tuple[NonlinearConstraintTransformInstance, ...]

Tuple of nonlinear constraint transform plugin instances.

names dict[str, tuple[str | int, ...]]

Optional mapping of axis names to label sequences.

lock

lock() -> None

Lock the object to prevent sharing and re-use.

Raises:

Type Description
RuntimeError

If the object is already locked.