checkpoint_manager (faber_neuroevolution v1.2.4)

View Source

Network Checkpoint Manager.

This module provides network checkpointing capabilities for saving and loading evolved networks at key milestones during training.

Checkpoint Triggers

Networks are saved at configurable milestones: fitness_record - When a new best fitness is achieved generation_interval - Every N generations evaluation_interval - Every N evaluations

Storage Format

Each checkpoint is stored as an Erlang term file containing: Network binary (from network_evaluator:to_binary/1) Metadata (fitness, generation, timestamp, etc.) Configuration used during training

Summary

Functions

Generate a checkpoint filename from metadata. Format: REASON-genN-fitF-TIMESTAMP.checkpoint Uses dashes as separators since reason names may contain underscores.

Delete a specific checkpoint.

Get the current checkpoint directory.

Initialize the checkpoint manager with a configuration. Creates the checkpoint directory if it doesn't exist.

List all checkpoints in the default directory.

List all checkpoints in the specified directory. Returns a list of maps with metadata and filename for each checkpoint.

Load the checkpoint with the best fitness.

Load the checkpoint with the best fitness from a specific directory.

Load a checkpoint from a file.

Load the most recent checkpoint.

Load the most recent checkpoint from a specific directory.

Parse checkpoint information from a filename. Extracts reason, generation, fitness, and timestamp from the filename. Format: REASON-genN-fitF-TIMESTAMP.checkpoint

Prune old checkpoints, keeping only the most recent N per reason. Options: max_per_reason - Maximum checkpoints to keep per reason (default: 20) keep_best - Always keep the best fitness checkpoint (default: true)

Save a checkpoint with the given individual and metadata.

Save a checkpoint with additional options. Options: checkpoint_dir - Override the default directory compress - true (default) or false

Set the checkpoint directory.

Types

checkpoint/0

-type checkpoint() :: #{individual := individual(), metadata := checkpoint_metadata()}.

checkpoint_metadata/0

-type checkpoint_metadata() ::
          #{reason := checkpoint_reason(),
            fitness := float(),
            generation := non_neg_integer(),
            total_evaluations := non_neg_integer(),
            individual_id := term(),
            timestamp := non_neg_integer(),
            config => map()}.

checkpoint_reason/0

-type checkpoint_reason() ::
          fitness_record | generation_interval | evaluation_interval | manual | pre_mutation |
          training_complete.

fitness/0

-type fitness() :: float() | undefined.

generation/0

-type generation() :: non_neg_integer().

genome/0

-type genome() ::
          #genome{connection_genes ::
                      [#connection_gene{innovation :: pos_integer() | undefined,
                                        from_id :: term(),
                                        to_id :: term(),
                                        weight :: float(),
                                        enabled :: boolean()}],
                  input_count :: non_neg_integer(),
                  hidden_count :: non_neg_integer(),
                  output_count :: non_neg_integer()}.

individual/0

-type individual() ::
          #individual{id :: individual_id(),
                      network :: network(),
                      genome :: genome() | undefined,
                      parent1_id :: individual_id() | undefined,
                      parent2_id :: individual_id() | undefined,
                      fitness :: fitness(),
                      metrics :: metrics(),
                      generation_born :: generation(),
                      birth_evaluation :: non_neg_integer(),
                      max_age :: pos_integer(),
                      is_survivor :: boolean(),
                      is_offspring :: boolean()}.

individual_id/0

-type individual_id() :: term().

metrics/0

-type metrics() :: map().

network/0

-type network() :: term().

Functions

checkpoint_filename(Metadata, Dir)

-spec checkpoint_filename(checkpoint_metadata(), file:filename()) -> file:filename().

Generate a checkpoint filename from metadata. Format: REASON-genN-fitF-TIMESTAMP.checkpoint Uses dashes as separators since reason names may contain underscores.

delete_checkpoint(Filename)

-spec delete_checkpoint(file:filename()) -> ok | {error, term()}.

Delete a specific checkpoint.

get_checkpoint_dir()

-spec get_checkpoint_dir() -> file:filename().

Get the current checkpoint directory.

init(Config)

-spec init(map()) -> ok | {error, term()}.

Initialize the checkpoint manager with a configuration. Creates the checkpoint directory if it doesn't exist.

list_checkpoints()

-spec list_checkpoints() -> [map()].

List all checkpoints in the default directory.

list_checkpoints(Options)

-spec list_checkpoints(map()) -> [map()].

List all checkpoints in the specified directory. Returns a list of maps with metadata and filename for each checkpoint.

load_best_fitness()

-spec load_best_fitness() ->
                           {ok, individual(), checkpoint_metadata()} | {error, no_checkpoints | term()}.

Load the checkpoint with the best fitness.

load_best_fitness(Options)

-spec load_best_fitness(map()) ->
                           {ok, individual(), checkpoint_metadata()} | {error, no_checkpoints | term()}.

Load the checkpoint with the best fitness from a specific directory.

load_checkpoint(Filename)

-spec load_checkpoint(file:filename()) -> {ok, individual(), checkpoint_metadata()} | {error, term()}.

Load a checkpoint from a file.

load_latest()

-spec load_latest() -> {ok, individual(), checkpoint_metadata()} | {error, no_checkpoints | term()}.

Load the most recent checkpoint.

load_latest(Options)

-spec load_latest(map()) -> {ok, individual(), checkpoint_metadata()} | {error, no_checkpoints | term()}.

Load the most recent checkpoint from a specific directory.

parse_checkpoint_filename(Filename)

-spec parse_checkpoint_filename(file:filename()) -> {ok, map()} | {error, invalid_format}.

Parse checkpoint information from a filename. Extracts reason, generation, fitness, and timestamp from the filename. Format: REASON-genN-fitF-TIMESTAMP.checkpoint

prune_checkpoints(Options)

-spec prune_checkpoints(map()) -> {ok, non_neg_integer()} | {error, term()}.

Prune old checkpoints, keeping only the most recent N per reason. Options: max_per_reason - Maximum checkpoints to keep per reason (default: 20) keep_best - Always keep the best fitness checkpoint (default: true)

save_checkpoint(Individual, Metadata)

-spec save_checkpoint(individual(), checkpoint_metadata()) -> ok | {error, term()}.

Save a checkpoint with the given individual and metadata.

save_checkpoint(Individual, Metadata, Options)

-spec save_checkpoint(individual(), checkpoint_metadata(), map()) -> ok | {error, term()}.

Save a checkpoint with additional options. Options: checkpoint_dir - Override the default directory compress - true (default) or false

set_checkpoint_dir(Dir)

-spec set_checkpoint_dir(file:filename()) -> ok | {error, term()}.

Set the checkpoint directory.