metaheuristic_designer.checkpointer module#

Module for checkpointing and resuming optimizations runs.

class Checkpointer[source]#

Bases: ABC

Methods

checkpoint()

Evaluate whether a checkpoint should be saved, and perform the save if necessary.

load([file_name, reporter, parallel])

Restore a previously saved algorithm from a checkpoint file.

restart()

Reset the internal timer so that time-based checkpoints are measured from this moment onward.

save(algorithm)

Serialize the algorithm to disk using cloudpickle.

abstract restart()[source]#

Reset the internal timer so that time-based checkpoints are measured from this moment onward.

abstract checkpoint()[source]#

Evaluate whether a checkpoint should be saved, and perform the save if necessary.

Parameters:
algorithmAlgorithm

The running algorithm instance.

Parameters:

algorithm (Algorithm)

abstract save(algorithm)[source]#

Serialize the algorithm to disk using cloudpickle.

A temporary file is written first and atomically moved to the final location, preventing corruption if the process crashes mid-write. The reporter, parallel flag, and the checkpointer itself are temporarily removed before pickling to avoid serialization issues, and then restored.

Parameters:
algorithmAlgorithm

The algorithm to save.

Parameters:

algorithm (Algorithm)

abstract load(file_name=None, reporter='silent', parallel=False)[source]#

Restore a previously saved algorithm from a checkpoint file.

Return type:

Algorithm

Parameters:
file_namestr, optional

Path to the checkpoint file. If not provided, the path given at construction is used.

reporterReporter or str, optional

Reporter to attach to the restored algorithm (a Reporter instance or a string like "tqdm", "silent"). Default is "silent".

parallelbool, optional

Whether parallel evaluation should be enabled after restoration. Default is False.

Returns:
Algorithm

The deserialized algorithm, ready to continue from where it was saved. Ensure you run the algorithm with .resume() so data is not lost.

Parameters:
  • file_name (str | None)

  • reporter (Reporter | str)

  • parallel (bool)

class PickleCheckpointer(checkpoint_file, iteration_frequency=None, time_frequency=None)[source]#

Bases: Checkpointer

Periodically save and restore the state of an optimizations run.

The checkpointer can be triggered by iteration count, elapsed wall-clock time, or both. It writes the entire Algorithm object to disk using cloudpickle, so that an interrupted run can be resumed later without losing progress.

Parameters:
checkpoint_filestr

Path to the file where the checkpoint will be saved (e.g., "run.pkl").

iteration_frequencyint, optional

Save a checkpoint every n iterations.

time_frequencyfloat, optional

Save a checkpoint when at least this many seconds have elapsed since the last save.

Parameters:
  • checkpoint_file (str)

  • iteration_frequency (Optional[int])

  • time_frequency (Optional[float])

Methods

checkpoint(algorithm)

Evaluate whether a checkpoint should be saved, and perform the save if necessary.

load([file_name, reporter])

Restore a previously saved algorithm from a checkpoint file.

restart()

Reset the internal timer so that time-based checkpoints are measured from this moment onward.

save(algorithm)

Serialize the algorithm to disk using cloudpickle.

Notes

At least one of iteration_frequency or time_frequency must be provided; otherwise the checkpointer does nothing and logs a warning.

restart()[source]#

Reset the internal timer so that time-based checkpoints are measured from this moment onward.

checkpoint(algorithm)[source]#

Evaluate whether a checkpoint should be saved, and perform the save if necessary.

Parameters:
algorithmAlgorithm

The running algorithm instance.

Parameters:

algorithm (Algorithm)

save(algorithm)[source]#

Serialize the algorithm to disk using cloudpickle.

A temporary file is written first and atomically moved to the final location, preventing corruption if the process crashes mid-write. The reporter, parallel flag, and the checkpointer itself are temporarily removed before pickling to avoid serialization issues, and then restored.

Parameters:
algorithmAlgorithm

The algorithm to save.

Parameters:

algorithm (Algorithm)

load(file_name=None, reporter='silent')[source]#

Restore a previously saved algorithm from a checkpoint file.

Return type:

Algorithm

Parameters:
file_namestr, optional

Path to the checkpoint file. If not provided, the path given at construction is used.

reporterReporter or str, optional

Reporter to attach to the restored algorithm (a Reporter instance or a string like "tqdm", "silent"). Default is "silent".

parallelbool, optional

Whether parallel evaluation should be enabled after restoration. Default is False.

Returns:
Algorithm

The deserialized algorithm, ready to continue from where it was saved. Ensure you run the algorithm with .resume() so data is not lost.

Parameters:
  • file_name (str | None)

  • reporter (Reporter | str)