Adaptive sessions¶
Ask for configurations with AdaptiveSession.ask(n), evaluate them in your own
processes, and return results with tell(trial_id, scores). Use a journal path
for asking and telling across processes. Reopening retains the trial population
but restarts the sampler random stream; batching and reopening do not promise
the identical proposal sequence of an uninterrupted sequential run.
Report per-replicate values to retain Monte Carlo standard errors and counts. Confidence constraints require at least two finite replicates, including for non-objective constraint scores. A rejected tell leaves its trial pending so it can be corrected. Result score columns retain weighted objective means; raw means and uncertainty remain in metadata, and feasibility checks use raw units.
Inspect session.trials() or filter with trials("pending"),
trials("complete"), and trials("failed"). Snapshots include configuration,
trial id, state, and metadata. Record evaluation failures explicitly:
session.fail(trial_id, "worker timed out")
retry_id, config = session.retry(trial_id, max_retries=2)
# Evaluate config again and report against retry_id, not trial_id.
session.tell(retry_id, scores)
Retries create new trial ids with the same parameters and retain the failed
attempt. The bound applies across a chain: to retry a failed retry, pass its id.
No retry happens automatically. Calling retry again on the same failed id
returns its existing child, even after reopening or completion. Serialize retry
requests for a given id; simultaneous writers may create duplicate children.
Duplicate tell or fail reports and transitions from terminal states are
rejected. Retrying an external evaluation may repeat its side effects; the
caller must make those operations safe to repeat.
Use session.enqueue(config) to evaluate a known complete configuration before
sampling new ones. Every call queues a distinct evaluation. Queued configurations
are visible through trials("waiting"); ask() supplies their evaluation ids.
Continuous bounds, log factors and categorical/discrete levels are validated.
Queued, retried and imported trials join the sampler population when completed;
constraints are processed through the same tell lifecycle as sampled trials.
The adaptive extra requires Optuna 4.5 or newer for public generation assignment.
To import completed observations, give both sessions the same explicit
revision="simulator-v2-scorer-v1-data-v3-fidelity-high" and matching factor,
objective (including weights/directions) and constraint definitions:
new_session.warm_start(previous_session)
# Saved tables from previous_session.results() also retain the import schema:
new_session.warm_start(load_results("previous-results"))
Imports preserve raw means, standard errors, replicate counts and evaluation provenance. They create completed trials without calling a simulator. Repeating an import skips known evaluation identities, including after reopening and through intermediate sessions; conflicting results for an existing identity are refused. All rows are validated before any are imported. Interrupted imports resume their pending completion on the next identical import without adding another trial. Serialize imports into a destination session. A bare grid table lacks a verifiable session schema and cannot be imported automatically. The revision is the caller's assertion of matching simulator, scorer, data, randomness and fidelity, not evidence inferred from the scores.
Journals now store a versioned schema and refuse incompatible definitions on reopen. Nonempty legacy journals without that identity are also refused: use a new journal or study name. Legacy observations require reconstruction and validation against their original definitions; they are not silently adopted.
trade_study.AdaptiveSession(factors, observables, *, constraints=None, seed=42, path=None, study_name=_STUDY_NAME, revision=None)
¶
Persistent ask/tell multi-objective search over design factors.
Create or reopen a session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
factors
|
list[Factor]
|
Design factors to search. |
required |
observables
|
list[Observable]
|
Objectives, with directions and weights. |
required |
constraints
|
list[Constraint] | None
|
Feasibility constraints on reported scores; the
|
None
|
seed
|
int
|
Sampler seed. A reopened session restarts the sampler's random stream; the population already in storage is kept. |
42
|
path
|
str | Path | None
|
Journal file for a persistent session; in-memory when
|
None
|
study_name
|
str
|
Name of the study inside the journal. |
_STUDY_NAME
|
revision
|
str | None
|
Caller-managed simulator/scorer/data/fidelity revision. Required for importing completed observations between searches. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the revision is empty or a journal's stored schema is incompatible or absent in an existing nonempty study. |
Source code in src/trade_study/session.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | |
ask(n=1)
¶
Propose n configs.
Returns:
| Type | Description |
|---|---|
list[tuple[int, dict[str, Any]]]
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/trade_study/session.py
tell(trial_id, scores)
¶
Record a trial's scores.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trial_id
|
int
|
Id returned by :meth: |
required |
scores
|
Mapping[str, float | Sequence[float]]
|
Per observable, one value or the per-replicate values. Objectives are optimized on their (weighted) means; every reported score is available to constraints. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the trial id is unknown, already told, or an objective or constraint score is missing, or a constraint has no finite mean/required standard error. Rejected tells leave the trial pending and may be corrected. |
Source code in src/trade_study/session.py
trials(state=None)
¶
Inspect trials in creation order without exposing storage internals.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
str | None
|
Optional filter: |
None
|
Returns:
| Type | Description |
|---|---|
list[SessionTrial]
|
Independent snapshots, including raw scores, failure reasons, |
list[SessionTrial]
|
and retry lineage in metadata where available. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the state filter is unknown. |
Source code in src/trade_study/session.py
fail(trial_id, reason)
¶
Mark a pending trial failed, preserving its configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trial_id
|
int
|
Id returned by :meth: |
required |
reason
|
str
|
Nonempty description of the evaluation failure. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the id is unknown, the trial is not pending, or the reason is empty. Duplicate failure reports are rejected. |
Source code in src/trade_study/session.py
retry(trial_id, *, max_retries=1)
¶
Create a bounded retry of a failed trial with identical parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trial_id
|
int
|
Failed trial to retry. To retry another failed attempt, pass that attempt's id. |
required |
max_retries
|
int
|
Maximum additional attempts across the retry chain. |
1
|
Returns:
| Type | Description |
|---|---|
int
|
New trial id and the original configuration. Repeating a request |
dict[str, Any]
|
for the same failed id returns its existing child, including |
tuple[int, dict[str, Any]]
|
after reopening; it never creates a second child. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the id is unknown, not failed, or its retry limit has been reached, or the limit is less than one. |
Notes
Serialize retry requests for a given id. Idempotency applies to repeated requests, not simultaneous requests from multiple writers.
Source code in src/trade_study/session.py
enqueue(config)
¶
Queue a known configuration before sampling new configurations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
dict[str, Any]
|
Complete factor configuration inside the declared domain. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the configuration's factor names or values are invalid. |
Notes
Each call queues a distinct evaluation. Completed observations
should instead be imported with :meth:warm_start.
Source code in src/trade_study/session.py
warm_start(source)
¶
Import compatible completed evaluations, without re-evaluating them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
AdaptiveSession | ResultsTable
|
Session or saved/loaded table produced by session.results(). Factor/objective/constraint definitions and explicit revision must match. Bare grid tables have no verifiable session schema. |
required |
Returns:
| Type | Description |
|---|---|
list[int]
|
Newly imported trial ids. Repeated imports skip evaluations already |
list[int]
|
present, including after reopening and through intermediate imports. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If revision/schema/provenance/observations are invalid, or an existing evaluation id has conflicting results. |
Notes
Serialize imports into a destination session. The revision is the caller's assertion of matching model, scorer, data and fidelity; the library cannot establish that assertion from scores alone.
Source code in src/trade_study/session.py
results()
¶
Return completed trials.
Returns:
| Type | Description |
|---|---|
ResultsTable
|
|
ResultsTable
|
weighted objective values (as |
ResultsTable
|
metadata holding the trial id, all reported means, their standard |
ResultsTable
|
errors and replicate counts, and the constraint values (feasible |
ResultsTable
|
when every value is at most zero). |
Source code in src/trade_study/session.py
trade_study.SessionTrial(trial_id, config, state, metadata)
dataclass
¶
Snapshot of an adaptive trial, including failure and retry metadata.