Trait argmin::core::checkpointing::Checkpoint
source · pub trait Checkpoint<S, I> {
// Required methods
fn save(&self, solver: &S, state: &I) -> Result<(), Error>;
fn load(&self) -> Result<Option<(S, I)>, Error>;
fn frequency(&self) -> CheckpointingFrequency;
// Provided method
fn save_cond(&self, solver: &S, state: &I, iter: u64) -> Result<(), Error> { ... }
}
Expand description
An interface for checkpointing methods
Handles saving of a checkpoint. The methods save
(saving the
checkpoint), load
(loading a checkpoint) are mandatory to implement.
The method save_cond
determines if the conditions for calling
save
are met, and if yes, calls save
. frequency
returns the
conditions in form of a CheckpointingFrequency
.
§Example
use argmin::core::Error;
use argmin::core::checkpointing::{Checkpoint, CheckpointingFrequency};
use serde::{Serialize, de::DeserializeOwned};
struct MyCheckpoint {
frequency: CheckpointingFrequency,
// ..
}
impl<S, I> Checkpoint<S, I> for MyCheckpoint
where
// Both `solver` (`S`) and `state` (`I`) (probably) need to be (de)serializable
S: Serialize + DeserializeOwned,
I: Serialize + DeserializeOwned,
{
fn save(&self, solver: &S, state: &I) -> Result<(), Error> {
// Save `solver` and `state`
Ok(())
}
fn load(&self) -> Result<Option<(S, I)>, Error> {
// Load `solver` and `state` from checkpoint
// Return `Ok(None)` in case checkpoint is not found.
Ok(Some((solver, state)))
}
fn frequency(&self) -> CheckpointingFrequency {
self.frequency
}
}
Required Methods§
sourcefn save(&self, solver: &S, state: &I) -> Result<(), Error>
fn save(&self, solver: &S, state: &I) -> Result<(), Error>
Save a checkpoint
Gets a reference to the current solver
of type S
and to the current state
of type
I
. Both solver and state can maintain state. Optimization problems itself are not allowed
to have state which changes during an optimization (at least not in the context of
checkpointing).
sourcefn load(&self) -> Result<Option<(S, I)>, Error>
fn load(&self) -> Result<Option<(S, I)>, Error>
Loads a saved checkpoint
Returns the solver of type S
and the state
of type I
.
sourcefn frequency(&self) -> CheckpointingFrequency
fn frequency(&self) -> CheckpointingFrequency
Indicates how often checkpoints should be saved
Returns enum CheckpointingFrequency
.