Skip to content

Context Class

EnOptContext is the validated, frozen container that holds every setting needed to execute a single optimization run. It is typically built from a plain dict (EnOptContext.model_validate(CONFIG)).

For a narrative overview of all fields — including broadcasting rules, sharing plugin instances by key, defaults, and worked examples — see the Configuration user-manual page.

The scales applied to the objectives and the nonlinear constraints of a run are read with get_objective_scales and get_constraint_scales.

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.

See the Configuration guide for an in-depth description of broadcasting rules, index-based sharing of plugin instances, the names attribute, and how dicts are resolved into plugin instances.

Warning

EnOptContext objects are immutable after construction. Do not attempt to serialize and round-trip them (for example to and 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 RealizationFilterInstances

Realization filter plugin instances, by key. A sequence is keyed by position.

function_estimators FunctionEstimatorInstances

Function estimator plugin instances, by key. A sequence is keyed by position.

samplers SamplerInstances

Sampler plugin instances, by key. A sequence is keyed by position.

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

Optional mapping of axis names to label sequences.

get_objective_scales

get_objective_scales() -> NDArray[np.float64]

Return the scale applied to each objective.

Objectives are divided by their scale before they reach the optimizer, and multiplied by it again before they are reported. The scales are the configured scales, multiplied by an estimated factor if auto-scaling is enabled and has run.

Returns:

Type Description
NDArray[float64]

The objective scales.

get_constraint_scales

get_constraint_scales() -> NDArray[np.float64] | None

Return the scale applied to each nonlinear constraint.

Returns:

Type Description
NDArray[float64] | None

The constraint scales, or None if there are no constraints.

get_objective_offsets

get_objective_offsets() -> NDArray[np.float64]

Return the offset applied to each objective.

Objectives have their offset subtracted before they are divided by their scale, and it is added back before they are reported.

Returns:

Type Description
NDArray[float64]

The objective offsets.

get_nonlinear_constraint_bounds

get_nonlinear_constraint_bounds() -> (
    tuple[NDArray[np.float64], NDArray[np.float64]] | None
)

Return the scaled nonlinear constraint bounds.

The bounds are transformed together with the constraint values, so that the configured constraint is the constraint that is solved. Scales are positive, so the bounds keep their order.

Returns:

Type Description
tuple[NDArray[float64], NDArray[float64]] | None

The lower and upper bounds, or None if there are no constraints.

lock

lock() -> None

Lock the object to prevent sharing and re-use.

Raises:

Type Description
WorkflowError

If the object is already locked.