State Persistence

class Checkpoint(file_name=None, dir_path=None, autoload=True, make_dir=True, raise_errors=False)

This class can be used to save and load evolution progress to and from files. It’s implemented as a lightweight wrapper around the builtin open() function. Objects are (de-)serialized using the dill library. Only those objects which have been set as attributes of the checkpoint object are persisted to disk. The target save file is assigned on object instantiation. Checkpoint objects automatically persist also the RNG states of the random and numpy.random modules.

Parameters
  • file_name (Optional[str]) – The name of the checkpoint file. By default, a random UUID + .dcpf extension is used.

  • dir_path (Optional[Path]) – The path to the checkpoint directory. By default, the current working directory + /deap-er is used.

  • autoload (Optional[bool]) – If True and the checkpoint file exists, loads the file during initialization, optional. The default value is True.

  • make_dir (Optional[bool]) – If True, the target directory is recursively created on save, if it does not exist. The default value is True.

  • raise_errors (Optional[bool]) – If True, errors are propagated, optional. By default, errors are not propagated and False is returned instead.

load()

Loads objects from the checkpoint file and sets them as attributes of self.

Raises
  • IOError – If the operation failed and self.raise_errors is True.

  • dill.PickleError – If the operation failed and self.raise_errors is True.

Returns

True if the operation completed successfully, False otherwise.

Return type

bool

save()

Saves the attributes of self into the checkpoint file. If the file already exists, it will be overwritten. If the target directory does not exist, it will be created recursively.

Raises
  • IOError – If the operation failed and self.raise_errors is True.

  • dill.PickleError – If the operation failed and self.raise_errors is True.

Returns

True if the operation completed successfully, False otherwise.

Return type

bool

range(generations)

A special generator method that behaves almost like the builtin range() function, but it accepts only a single argument of the number of generations to compute. It is intended to be used in a for loop to iterate over the main evolutionary process. The checkpoint is saved to disk every self.save_freq seconds and also at the end of the loop, if saving is enabled. The saving frequency can be changed while the for loop is running. The values of the range counter are automatically determined from the current (loaded) state of the checkpoint object.

Parameters

generations (int) – The amount of generations to compute.

Returns

A generator that yields the integer values of the internal counter.

Return type

range

property save_freq: float

The time in seconds after which to periodically save the checkpoint to a file, when the range() function is being executed in a for loop. Setting the value to -1 disables saving. Accepts int and float types as values. Float-types allow sub-second timing precision.

Returns

The current saving frequency period, in seconds. The default value is 60 seconds.

property last_op: str
Returns the status of the last operation performed on the checkpoint object.
Possible string-type return values are:
  • none

  • load_success

  • load_error

  • save_success

  • save_error

is_loaded()

Shorthand for checkpoint.last_op == 'load_success'.

Returns

True if the checkpoint was successfully loaded from file.

Return type

bool

is_saved()

Shorthand for checkpoint.last_op == 'save_success'.

Returns

True if the checkpoint was successfully saved to file.

Return type

bool