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
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.
add_event_handler
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 |
event_handlers
property
The event handlers attached to this compute step.
Returns:
| Type | Description |
|---|---|
list[EventHandler]
|
A list of handlers. |
stop
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
Whether a stop has been requested for the current run.
Returns:
| Type | Description |
|---|---|
bool
|
|
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__
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__
Initialize a default optimizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
evaluator
|
Evaluator
|
The evaluator object to run function evaluations. |
required |