1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//! Checkpoint — serialization support for resuming GA runs (serde feature).
//!
//! This module is only available when the `serde` feature is enabled.
//! It provides [`Checkpoint`], a serializable snapshot of the GA state
//! at a given generation, along with [`save_checkpoint`] and
//! [`load_checkpoint`] helpers that write/read JSON to disk.
//!
//! The GA run loop automatically saves checkpoints at the interval
//! configured in [`SaveProgressConfiguration`](crate::configuration::SaveProgressConfiguration).
//!
//! # Key items
//!
//! | Item | Description |
//! |------|-------------|
//! | [`Checkpoint`] | Serializable snapshot of GA state (generation, population, config) |
//! | [`save_checkpoint`] | Serialize checkpoint to a JSON file |
//! | [`load_checkpoint`] | Deserialize checkpoint from a JSON file |
//!
//! # When to use
//! Enable the `serde` feature and configure checkpoint frequency via
//! the engine builder. Useful for long-running optimization jobs where
//! you want to resume from the last saved generation after interruption.
use crateGaConfiguration;
use crateGaError;
use cratePopulation;
use crateGenerationStats;
use crateChromosomeT;
use ;
use Path;
/// A serializable snapshot of the GA state at a given generation.
///
/// Contains the population, configuration, generation index, and
/// accumulated per-generation statistics. The fitness function and
/// initialization function are **not** included because they are not
/// serializable — the caller must re-attach them after loading.
///
/// # Examples
///
/// ```rust,no_run
/// use genetic_algorithms::checkpoint::Checkpoint;
/// use genetic_algorithms::chromosomes::Binary;
/// use genetic_algorithms::population::Population;
/// use genetic_algorithms::configuration::GaConfiguration;
///
/// let cp = Checkpoint::<Binary> {
/// population: Population::new(vec![]),
/// configuration: GaConfiguration::default(),
/// generation: 10,
/// stats: vec![],
/// };
/// assert_eq!(cp.generation, 10);
/// ```
/// Saves a [`Checkpoint`] to disk as pretty-printed JSON.
///
/// Creates any missing parent directories before writing.
///
/// # Errors
///
/// Returns [`GaError::CheckpointError`] if directory creation, file writing,
/// or JSON serialization fails.
///
/// # Examples
///
/// ```rust,no_run
/// use std::path::Path;
/// use genetic_algorithms::checkpoint::{Checkpoint, save_checkpoint};
/// use genetic_algorithms::chromosomes::Binary;
/// use genetic_algorithms::population::Population;
/// use genetic_algorithms::configuration::GaConfiguration;
///
/// let cp = Checkpoint::<Binary> {
/// population: Population::new(vec![]),
/// configuration: GaConfiguration::default(),
/// generation: 5,
/// stats: vec![],
/// };
/// save_checkpoint(&cp, Path::new("/tmp/checkpoint.json")).unwrap();
/// ```
/// Loads a [`Checkpoint`] from a JSON file on disk.
///
/// # Errors
///
/// Returns [`GaError::CheckpointError`] if the file cannot be read or
/// the JSON cannot be deserialized into a `Checkpoint<U>`.
///
/// # Examples
///
/// ```rust,no_run
/// use std::path::Path;
/// use genetic_algorithms::checkpoint::load_checkpoint;
/// use genetic_algorithms::chromosomes::Binary;
///
/// let cp = load_checkpoint::<Binary>(Path::new("/tmp/checkpoint.json")).unwrap();
/// assert!(cp.generation > 0);
/// ```