55//! that may need to be interrupted and resumed.
66
77use fugue_evo:: prelude:: * ;
8- use rand:: SeedableRng ;
98use rand:: rngs:: StdRng ;
9+ use rand:: SeedableRng ;
1010use std:: path:: PathBuf ;
1111
1212fn main ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
@@ -52,8 +52,8 @@ fn run_with_checkpoints(checkpoint_dir: &PathBuf) -> Result<(), Box<dyn std::err
5252
5353 // Create checkpoint manager
5454 let mut manager = CheckpointManager :: new ( checkpoint_dir, "evolution" )
55- . every ( 50 ) // Save every 50 generations
56- . keep ( 3 ) ; // Keep last 3 checkpoints
55+ . every ( 50 ) // Save every 50 generations
56+ . keep ( 3 ) ; // Keep last 3 checkpoints
5757
5858 let max_generations = 200 ;
5959
@@ -72,10 +72,17 @@ fn run_with_checkpoints(checkpoint_dir: &PathBuf) -> Result<(), Box<dyn std::err
7272 let p2_idx = selection. select ( & selection_pool, & mut rng) ;
7373
7474 let ( mut c1, mut c2) = crossover
75- . crossover ( & selection_pool[ p1_idx] . 0 , & selection_pool[ p2_idx] . 0 , & mut rng)
75+ . crossover (
76+ & selection_pool[ p1_idx] . 0 ,
77+ & selection_pool[ p2_idx] . 0 ,
78+ & mut rng,
79+ )
7680 . genome ( )
7781 . unwrap_or_else ( || {
78- ( selection_pool[ p1_idx] . 0 . clone ( ) , selection_pool[ p2_idx] . 0 . clone ( ) )
82+ (
83+ selection_pool[ p1_idx] . 0 . clone ( ) ,
84+ selection_pool[ p2_idx] . 0 . clone ( ) ,
85+ )
7986 } ) ;
8087
8188 mutation. mutate ( & mut c1, & mut rng) ;
@@ -102,8 +109,8 @@ fn run_with_checkpoints(checkpoint_dir: &PathBuf) -> Result<(), Box<dyn std::err
102109
103110 // Create checkpoint with current population
104111 let individuals: Vec < Individual < RealVector > > = population. iter ( ) . cloned ( ) . collect ( ) ;
105- let checkpoint = Checkpoint :: new ( gen + 1 , individuals )
106- . with_evaluations ( ( gen + 1 ) * 100 ) ;
112+ let checkpoint =
113+ Checkpoint :: new ( gen + 1 , individuals ) . with_evaluations ( ( gen + 1 ) * 100 ) ;
107114
108115 manager. save ( & checkpoint) ?;
109116 }
@@ -121,7 +128,7 @@ fn resume_from_checkpoint(checkpoint_dir: &PathBuf) -> Result<(), Box<dyn std::e
121128 // Find the latest checkpoint file
122129 let entries: Vec < _ > = std:: fs:: read_dir ( checkpoint_dir) ?
123130 . filter_map ( |e| e. ok ( ) )
124- . filter ( |e| e. path ( ) . extension ( ) . map_or ( false , |ext| ext == "ckpt" ) )
131+ . filter ( |e| e. path ( ) . extension ( ) . is_some_and ( |ext| ext == "ckpt" ) )
125132 . collect ( ) ;
126133
127134 if entries. is_empty ( ) {
@@ -145,7 +152,9 @@ fn resume_from_checkpoint(checkpoint_dir: &PathBuf) -> Result<(), Box<dyn std::e
145152 println ! ( " Evaluations: {}" , checkpoint. evaluations) ;
146153
147154 // Find best in loaded population
148- let best_individual = checkpoint. population . iter ( )
155+ let best_individual = checkpoint
156+ . population
157+ . iter ( )
149158 . filter_map ( |ind| ind. fitness . as_ref ( ) . map ( |f| ( ind, f. to_f64 ( ) ) ) )
150159 . max_by ( |( _, f1) , ( _, f2) | f1. partial_cmp ( f2) . unwrap ( ) ) ;
151160
@@ -154,11 +163,12 @@ fn resume_from_checkpoint(checkpoint_dir: &PathBuf) -> Result<(), Box<dyn std::e
154163 }
155164
156165 // Continue evolution...
157- let mut rng = StdRng :: seed_from_u64 ( 12345 ) ; // Different seed for continuation
166+ let mut rng = StdRng :: seed_from_u64 ( 12345 ) ; // Different seed for continuation
158167 let fitness = Sphere :: new ( 10 ) ;
159168
160169 // Reconstruct population from checkpoint
161- let mut population: Population < RealVector , f64 > = Population :: with_capacity ( checkpoint. population . len ( ) ) ;
170+ let mut population: Population < RealVector , f64 > =
171+ Population :: with_capacity ( checkpoint. population . len ( ) ) ;
162172 for ind in checkpoint. population {
163173 population. push ( ind) ;
164174 }
@@ -183,10 +193,17 @@ fn resume_from_checkpoint(checkpoint_dir: &PathBuf) -> Result<(), Box<dyn std::e
183193 let p2_idx = selection. select ( & selection_pool, & mut rng) ;
184194
185195 let ( mut c1, mut c2) = crossover
186- . crossover ( & selection_pool[ p1_idx] . 0 , & selection_pool[ p2_idx] . 0 , & mut rng)
196+ . crossover (
197+ & selection_pool[ p1_idx] . 0 ,
198+ & selection_pool[ p2_idx] . 0 ,
199+ & mut rng,
200+ )
187201 . genome ( )
188202 . unwrap_or_else ( || {
189- ( selection_pool[ p1_idx] . 0 . clone ( ) , selection_pool[ p2_idx] . 0 . clone ( ) )
203+ (
204+ selection_pool[ p1_idx] . 0 . clone ( ) ,
205+ selection_pool[ p2_idx] . 0 . clone ( ) ,
206+ )
190207 } ) ;
191208
192209 mutation. mutate ( & mut c1, & mut rng) ;
0 commit comments