Skip to content

Compute Steps

A ComputeStep is an executable unit of work among ropt's workflow components. Two implementations ship with ropt: OptimizationStep runs an optimization algorithm, and EvaluationStep runs a single ensemble evaluation.

See Optimization Workflows for usage.

ropt.components.compute_steps.ComputeStep

Bases: ABC, Generic[_ResultT]

Abstract base class for optimization compute steps.

A concrete step performs a specific action, such as running an optimizer or evaluating functions, and implements it in _run. The public run adds the guard that a step instance may only have one active run at a time, raising a WorkflowError if the same instance is already running on another thread.

The type parameter is what run returns.

__init__

__init__() -> None

Initialize the ComputeStep.

add_event_handler

add_event_handler(handler: EventHandler) -> None

Attach an event handler to receive this step's events.

Parameters:

Name Type Description Default
handler EventHandler

The handler to add.

required

Raises:

Type Description
TypeError

If handler is not an event handler.

event_handlers property

event_handlers: list[EventHandler]

The event handlers attached to this compute step.

Returns:

Type Description
list[EventHandler]

A list of handlers.

stop

stop() -> None

Request that this run stop gracefully at the next safe point.

Intended for an event handler that decides, after inspecting an event, that its optimization should stop; the run then ends with ExitCode.USER_ABORT. Setting the request is thread-safe, so a handler running behind an event dispatcher may call it too. A new run clears any earlier request.

stopped property

stopped: bool

Whether a stop has been requested for the current run.

Returns:

Type Description
bool

True if stop has been called since the run started.

run

run(
    context: EnOptContext,
    variables: ArrayLike,
    *,
    metadata: dict[str, Any] | None = None,
) -> _ResultT

Run this compute step.

Parameters:

Name Type Description Default
context EnOptContext

The optimization context.

required
variables ArrayLike

The initial variable values.

required
metadata dict[str, Any] | None

Optional metadata to attach to the results.

None

Returns:

Type Description
_ResultT

The result of the execution, if any.

Raises:

Type Description
WorkflowError

If this instance is already running on another thread.

ropt.components.compute_steps.EvaluationStep

Bases: ComputeStep[None]

The default evaluation step compute step.

Evaluates a batch of variable vectors (a single vector or a 2-D matrix where each row is a variable vector) and yields FunctionResults objects. Emits START_ENSEMBLE_EVALUATOR, START_EVALUATION, FINISHED_EVALUATION, and FINISHED_ENSEMBLE_EVALUATOR events.

See Optimization Workflows for the full event lifecycle description.

__init__

__init__(*, evaluator: Evaluator) -> None

Initialize a default evaluator.

Parameters:

Name Type Description Default
evaluator Evaluator

The evaluator object to run function evaluations.

required

ropt.components.compute_steps.OptimizationStep

Bases: ComputeStep[ExitCode]

The default optimizer compute step.

Executes an optimization algorithm, iteratively performing function and gradient evaluations. Emits START_OPTIMIZER, START_EVALUATION, FINISHED_EVALUATION, and FINISHED_OPTIMIZER events.

See Optimization Workflows for the full event lifecycle description.

__init__

__init__(*, evaluator: Evaluator) -> None

Initialize a default optimizer.

Parameters:

Name Type Description Default
evaluator Evaluator

The evaluator object to run function evaluations.

required