contextual_execution to adapt an acquired sequence to the current observation. The mechanism learns offsets for the base’s command coordinates; your application combines those offsets with the preserved base before execution.
Start with the selected execution from Structural stages and the shared Python helper.
During frozen use,
evaluate computes offsets from the current observation using unchanged learned state.
Keep the base and context contract together
This example continues with a two-coordinate command interface. Both coordinates use normalized values in[-1, 1], and each command lasts 0.5 seconds. The selected base contains between one and eight commands.
H is the selected base length. Apply corrections to those H commands, then append neutral padding in the executor. Keep this rule identical during comparisons and retained use.
Here target_dx = goal_x - initial_x and target_dy = goal_y - initial_y. Relative position lets the correction distinguish starts at different locations. This encoding suits the example’s planar relative-position task; include absolute position, obstacles, or other measured state if your environment makes them relevant.
Keep the context coordinate order, normalization, base sequence, command decoder, command duration and padding rule together when saving the controller. The refinement Domain stores corrections, and your application preserves the base sequence. Changing the base or its command count changes what the offsets mean.
Configure the refinement Domain
UsePREFIX, trajectory and create_stage from the configuration guide, structural_domains from Structural stages, and the application’s reset_and_measure adapter from Sequence acquisition.
Before running this guide, implement the following application hooks:
Register separate
refinement-support, refinement-training and evaluation partitions in your scenario registry. Use support vectors to configure the context representation and training scenarios for candidate comparisons. Reserve evaluation scenarios until retained evaluation. The code below constructs support vectors from real resets using the same coordinate scales as acquisition.
If you need the optional precision settings, choose the configuration and executor settings before running create_stage.
POST /domains/{domain_id}/trajectory/configure with the JSON body {"config": config} after creating a fresh Domain and checking its trajectory state.
The
contexts vectors supplied at configuration establish the numeric context representation. In the comparison loop, your adapter maps each context_id to repeatable reset conditions and measures the resulting context for evaluate.
Precision across operating conditions
When an acquired sequence needs different corrections across a broad range of observations, consider the following optional configuration. Merge this fragment intoconfig before the first create_stage call, with a client that supports context-bound response guidance.
Optional precision configuration fragment
Normalization and response guidance are optional and default off. Response-guided proposals still require execution on discovery and confirmation contexts before an update is retained.
Context binding: response guidance requires
context_observations on propose, binding every training context ID to its measured numeric observation. Implement this field using the proposal schema supported by your deployment, and verify that your client forwards it with the configuration options above. The basic comparison example below supports the default search. Extend its proposal body with the required context bindings before enabling response guidance.
Pair the search radius with the decoder
The executor composes corrections asclip(base + offset_scale * offset, -1, 1). The example uses a scale of 1.0. Choose the search radius and residual scale together so candidates can make useful changes within the action limits. Set the scale in your executor and radius in the trajectory configuration. Set both before training and preserve them for candidate and retained evaluation.
Changing the scale changes the reachable corrections within the decoded control bounds of [-1, 1]. Record the proposed offsets and decoded controls to check the effect of the search radius, including how often clipping collapses distinct proposals to the same command. Keep the base, scale, and clipping rule with the saved refinement state.
Use a measured precision objective
When an application measures continuous error, a smooth quality score can distinguish partial improvements. Keep the actual completion test separate. For example:scale fixed in the application’s scoring contract. This example assigns zero quality to an executed attempt without a valid error measurement; record the reason separately. With objective_names: ["quality"], have execute_actions return a measured quality value alongside the task’s actual completion result. The comparison loop reads the configured objective names, producing one quality value per outcome row for this configuration.
Use representative operating conditions together in each comparison, with separate discovery and confirmation subsets. Budget physical executions as returned candidates × contexts per update. Choose stopping from development checks; keep final evaluation contexts unused until learning stops.
Propose a comparison
Prepare a batch of repeatable training contexts. Use a discovery subset and a separate confirmation subset within the batch. The example assigns alternating IDs to discovery; the remaining IDs are the confirmation contexts. Keep this batch within the training partition.state and goal are required by the shared proposal envelope. For this contextual comparison workflow, the client supplies one representative context as state and [0.0] as goal. Send each execution’s actual context to evaluate and its measured objectives to observe.
Use a fresh request_id for each new comparison. Keep the returned decision_id with the batch until its measured outcomes are submitted. Candidate indices are zero-based positions in this proposal’s candidates array; do not reuse an index with another proposal’s decision ID.
Evaluate and execute every candidate on every context
Use thereset_context function defined above and your execute_actions adapter. In this example, measured completion is 0 or 1, and measured progress lies in [0, 1]; larger values mean better outcomes.
In this example, completion uses the same rule as the preceding guides: after the eight command slots, or an earlier task termination, the measured planar distance to the goal is at most 0.05 metres and the measured speed is at most 0.1 metres/second. A safety stop sets completion to false. Progress measures the fraction of the initial target distance removed, clipped to [0, 1]. If the initial distance is exactly zero, progress is 1 only when the final distance is also zero; otherwise it is 0.
The application adapter computes these values from the actual initial and final measurements of each execution. The example’s position and velocity coordinates are divided by one metre and one metre/second respectively, so the numeric thresholds below use those units directly.
execute_actions return this result using its measured trace and actual goal. This example adds progress as an objective for the contextual-refinement stage.
Reset separately for every candidate/context pair. Candidates must face the same reset conditions and scoring rules.
evaluate request requires context. For a pending candidate, provide both candidate_index and decision_id. Read offsets from the response and add them to the saved base using the same action limits used in training.
This example composes scaled offsets with the base and clips the resulting controls. If your adapter applies a coordinate transform, validate its inputs and preserve the transform and composition order through training and retained use.
Submit one complete measured comparison
Feedback needs one row for every returned candidate and every requested context, including the confirmation contexts. Do not send only the winner or the discovery rows.candidate_index, context_id and objectives; the top-level body uses decision_id and outcomes. Keep objective values in the configured order. Record measured task failures as outcomes. Finish all scheduled executions before submitting the batch, or reconcile an interrupted decision through retained use and recovery.
The request must satisfy both the schema’s 32768-row maximum and the route’s 1 MiB body limit. Size the candidate/context batch before running it. Do not divide one complete comparison into separate partial observe calls unless your deployed service explicitly supports that operation.
Read a recorded refinement update
The following excerpts come from a recorded API interaction. They retain selected response fields, with the decision ID replaced by one consistent label. This comparison used eight candidates across 64 contexts, split equally between discovery and confirmation.confirmation_gain contains one value because this comparison used one objective, quality. The fingerprint changed when the update was retained.
The candidate index identifies a position within this proposal. Keep the same decision ID on candidate evaluations and the complete observation batch.
Separate completed evaluations have compared an acquired base with its learned contextual correction on the same reserved conditions while learning was stopped. The corrections improved execution on unseen conditions and restored successful behavior after changes to the environment or actuator response. Follow-up runs reproduced the acquired behavior after a client reconnect, with unchanged learner fingerprints. These evaluations connect an accepted update to its effect on later execution.
Decide whether the correction helps
Use development comparisons to choose when to stop practice. Keep final evaluation contexts reserved until the policy is frozen. Compare the acquired base and contextual policy on the same cases, recording both repaired failures and lost successes. When context changes include different operating regimes, add the relevant measured variables and retain representative original conditions in practice when preservation matters. Create a new refinement lineage when the context encoding changes, because existing corrections depend on the original encoding.Use the retained correction
After learning, omit both pending-candidate fields fromevaluate to obtain the retained correction for a newly measured context.
propose or observe call. Save results in the application’s evaluation log. An unchanged refinement fingerprint verifies the exposed refinement state. Check the corresponding state for any other learning mechanisms used during evaluation.
See Retained use and operational checks for reconnecting, preserving the base and adapter, and handling interrupted requests.