odatse.algorithm._algorithm module#
- class odatse.algorithm._algorithm.AlgorithmBase(info: Info, runner: Runner | None = None, run_mode: str = 'initial')[source]#
Bases:
objectBase class for algorithms, providing common functionality and structure.
Lifecycle#
main()drives the three-phase lifecycle by calling the framework wrappers:main() ├── prepare() runner.prepare → dispatch(init/resume/continue) → _prepare() ├── run() _run() └── post() _post() → runner.post()
Subclasses implement the hooks with underscore prefix:
_prepare()(optional),_run()(required),_post()(required). The plain-named wrappersprepare,run,postare framework internals and must not be overridden in subclasses.Checkpoint#
Each class declares
_checkpoint_attrs(a list of attribute names).__getstate__()walks the MRO and collects them all automatically. Subclasses normally need only declare their own_checkpoint_attrsand override_apply_state()to callsuper()and restore RNG / algorithm-specific state. Override_save_state()/_load_state()only when extra files (e.g. an external policy object) must be written.Initialize the algorithm with the given information and runner.
- param info:
Information object containing algorithm and base parameters.
- type info:
Info
- param runner:
Optional runner object to execute the algorithm.
- type runner:
Runner (optional)
- param run_mode:
Mode in which the algorithm should run.
- type run_mode:
str
- abstractmethod __init__(info: Info, runner: Runner | None = None, run_mode: str = 'initial') None[source]#
Initialize the algorithm with the given information and runner.
- Parameters:
info (Info) – Information object containing algorithm and base parameters.
runner (Runner (optional)) – Optional runner object to execute the algorithm.
run_mode (str) – Mode in which the algorithm should run.
- __init_rng(info: Info) None#
Initialize the random number generator.
- Parameters:
info (Info) – Information object containing algorithm parameters.
- _apply_state(data: dict, mode: str = 'resume', restore_rng: bool = True) None[source]#
Restore the base algorithm state from a checkpoint snapshot.
Validates the MPI configuration, restores the timer, checks that algorithm parameters are consistent, and restores the RNG state. Subclasses should call
super()._apply_state(data, mode=mode, restore_rng=restore_rng)and then handle their own subclass-specific fields (_checkpoint_attrs, continue-mode semantics, etc.). The RNG state saved by__getstate__for every algorithm is restored here (guarded byrestore_rng), so subclasses need not repeat it.- Parameters:
data (dict) – Snapshot previously produced by
__getstate__.mode (str) –
"resume"or"continue". Passed through to subclass overrides so they can implement continue-mode semantics.restore_rng (bool) – When True (default) the RNG state is restored from data.
- _check_parameters(param=None)[source]#
Check the parameters of the algorithm against previous parameters.
- Parameters:
(optional) (param) – Previous parameters to check against.
- abstractmethod _initialize() None[source]#
Set up initial algorithm state for a fresh run (init mode).
Called by
prepare()whenmodestarts with"init". Must not use the runner (evaluation happens later in_run()).
- _load_data(filename='state.pickle') dict[source]#
Load data from a file.
- Parameters:
filename – Name of the file to load the data from.
- Returns:
Dictionary containing the loaded data.
- Return type:
dict
- _load_state(filename, mode='resume', restore_rng=True) None[source]#
Load a checkpoint snapshot from filename and apply it.
Delegates to
_load_data()then_apply_state().Override in subclasses only when extra files must be read (e.g. an external policy object). In that case call
super()._load_state(filename, mode=mode, restore_rng=restore_rng)first.- Parameters:
filename (str) – Path to the checkpoint file.
mode (str) –
"resume"or"continue", forwarded to_apply_state().restore_rng (bool) – Whether to restore the RNG state.
- abstractmethod _prepare() None[source]#
Algorithm-specific preparation, called after dispatch.
Override in subclasses to perform setup that must happen after the checkpoint state is established (e.g. initialising timer entries).
- _reach_consensus(error: Exception | None, ok: ndarray) None[source]#
Collectively agree on whether every algorithm rank succeeded.
Every algorithm rank must call this exactly once per phase, regardless of whether its phase body succeeded or raised.
okis[1]when this rank’s phase succeeded and[0]otherwise;erroris the exception this rank caught (orNone).A single
Allreduceshares the success flags, then:if this rank failed, its own exception is re-raised;
else if any other rank failed,
OtherAlgorithmProcessErroris raised so this rank bails out too.
Because the only collective on the failure path is this one
Allreduce– reached by all ranks whether they succeeded or failed – a per-rank failure can no longer leave the other ranks blocked. (Collectives inside the_prepare/_run/_posthooks remain the responsibility of each algorithm to keep balanced across ranks.)
- abstractmethod _run() None[source]#
Execute the main algorithm loop.
For
initmode, perform the initial evaluation here before entering the main loop. Call_save_state()at the appropriate points inside the loop.
- _save_data(data, filename='state.pickle', ngen=3) None[source]#
Save data to a file with versioning.
- Parameters:
data – Data to be saved.
filename – Name of the file to save the data.
ngen (int, default: 3) – Number of generations for versioning.
- _save_state(filename) None[source]#
Save a checkpoint snapshot to filename.
Uses
__getstate__()to collect all fields declared in_checkpoint_attrsacross the MRO, then delegates to_save_data()for versioned pickle storage.Override in subclasses only when extra files must be written alongside the pickle (e.g. an external policy object). In that case call
super()._save_state(filename)first.
- post() dict[source]#
Framework wrapper for the post phase.
Calls the
_post()hook thenrunner.post().Do not override this method in subclasses. Implement
_post()instead.
- prepare() None[source]#
Framework wrapper for the prepare phase.
Calls
runner.prepare(), dispatches init/resume/continue, then calls the_prepare()hook.Do not override this method in subclasses. Implement
_prepare()instead.
- run() None[source]#
Framework wrapper for the run phase.
Calls the
_run()hook. Runner calls are handled byprepare()andpost(); this wrapper contains no runner invocations.Do not override this method in subclasses. Implement
_run()instead.
- class odatse.algorithm._algorithm.AlgorithmStatus(value)[source]#
Bases:
IntEnumEnumeration for the status of the algorithm.
- odatse.algorithm._algorithm.flatten_dict(d, parent_key='', separator='.')[source]#
Flatten a nested dictionary.
- Parameters:
d – Dictionary to flatten.
parent_key (str, default : "") – Key for the parent dictionary.
separator (str, default : ".") – Separator to use between keys.
- Returns:
Flattened dictionary.
- Return type:
dict