@@ -11,7 +11,7 @@ use crate::mode::Mode;
1111use crate :: state:: State ;
1212use crate :: { ObjectiveFunction , ParallelObjectiveFunction } ;
1313
14- pub trait Constraints : Sync + std:: fmt:: Debug {
14+ pub trait Constraints : Sync + std:: fmt:: Debug {
1515 fn meets_constraints ( & self , x : & DVector < f64 > ) -> bool ;
1616 fn clone_box ( & self ) -> Box < dyn Constraints > ;
1717}
@@ -58,7 +58,14 @@ pub struct Sampler<F> {
5858}
5959
6060impl < F > Sampler < F > {
61- pub fn new ( dim : usize , constraints : Option < Box < dyn Constraints > > , max_resamples : Option < usize > , population_size : usize , objective_function : F , rng_seed : u64 ) -> Self {
61+ pub fn new (
62+ dim : usize ,
63+ constraints : Option < Box < dyn Constraints > > ,
64+ max_resamples : Option < usize > ,
65+ population_size : usize ,
66+ objective_function : F ,
67+ rng_seed : u64 ,
68+ ) -> Self {
6269 Self {
6370 dim,
6471 constraints,
@@ -102,11 +109,17 @@ impl<F> Sampler<F> {
102109 } ;
103110
104111 if parallel_update {
105- z. into_par_iter ( ) . map ( transform) . filter ( ok_constraints) . collect ( )
112+ z. into_par_iter ( )
113+ . map ( transform)
114+ . filter ( ok_constraints)
115+ . collect ( )
106116 } else {
107- z. into_iter ( ) . map ( transform) . filter ( ok_constraints) . collect ( )
117+ z. into_iter ( )
118+ . map ( transform)
119+ . filter ( ok_constraints)
120+ . collect ( )
108121 }
109- } ,
122+ }
110123 None => {
111124 if parallel_update {
112125 z. into_par_iter ( ) . map ( transform) . collect ( )
@@ -139,7 +152,6 @@ impl<F> Sampler<F> {
139152 i += 1 ;
140153 }
141154
142-
143155 let mut points = evaluate_points ( y, & mut self . objective_function ) ?;
144156
145157 self . function_evals += points. len ( ) ;
@@ -285,7 +297,14 @@ mod tests {
285297 fn test_sample ( ) {
286298 let dim = 10 ;
287299 let population_size = 12 ;
288- let mut sampler = Sampler :: new ( dim, None , None , population_size, Box :: new ( |_: & DVector < f64 > | 0.0 ) , 1 ) ;
300+ let mut sampler = Sampler :: new (
301+ dim,
302+ None ,
303+ None ,
304+ population_size,
305+ Box :: new ( |_: & DVector < f64 > | 0.0 ) ,
306+ 1 ,
307+ ) ;
289308 let state = State :: new ( vec ! [ 0.0 ; dim] . into ( ) , 2.0 ) ;
290309
291310 let n = 5 ;
@@ -323,17 +342,33 @@ mod tests {
323342
324343 // No resampling: Value should be out-of-bounds
325344 {
326- let mut sampler = Sampler :: new ( dim, Some ( Box :: new ( bounds. clone ( ) ) ) , Some ( 0 ) , population_size, objective_function, 1 ) ;
345+ let mut sampler = Sampler :: new (
346+ dim,
347+ Some ( Box :: new ( bounds. clone ( ) ) ) ,
348+ Some ( 0 ) ,
349+ population_size,
350+ objective_function,
351+ 1 ,
352+ ) ;
327353 let state = State :: new ( vec ! [ 0.0 ; dim] . into ( ) , 2.0 ) ;
328354 let individuals = sampler. sample ( & state, Mode :: Minimize , false ) . unwrap ( ) ;
329355
330- assert ! ( individuals[ 0 ] . point[ 0 ] < bounds. lower[ 0 ]
331- || individuals[ 0 ] . point[ 0 ] > bounds. upper[ 0 ] ) ;
356+ assert ! (
357+ individuals[ 0 ] . point[ 0 ] < bounds. lower[ 0 ]
358+ || individuals[ 0 ] . point[ 0 ] > bounds. upper[ 0 ]
359+ ) ;
332360 }
333361
334362 // With limited resampling: Value should be in bounds
335363 {
336- let mut sampler = Sampler :: new ( dim, Some ( Box :: new ( bounds. clone ( ) ) ) , Some ( 10 ) , population_size, objective_function, 1 ) ;
364+ let mut sampler = Sampler :: new (
365+ dim,
366+ Some ( Box :: new ( bounds. clone ( ) ) ) ,
367+ Some ( 10 ) ,
368+ population_size,
369+ objective_function,
370+ 1 ,
371+ ) ;
337372 let state = State :: new ( vec ! [ 0.0 ; dim] . into ( ) , 2.0 ) ;
338373 let individuals = sampler. sample ( & state, Mode :: Minimize , false ) . unwrap ( ) ;
339374
@@ -343,7 +378,14 @@ mod tests {
343378
344379 // With unlimited resampling: Value should be in bounds
345380 {
346- let mut sampler = Sampler :: new ( dim, Some ( Box :: new ( bounds. clone ( ) ) ) , None , population_size, objective_function, 1 ) ;
381+ let mut sampler = Sampler :: new (
382+ dim,
383+ Some ( Box :: new ( bounds. clone ( ) ) ) ,
384+ None ,
385+ population_size,
386+ objective_function,
387+ 1 ,
388+ ) ;
347389 let state = State :: new ( vec ! [ 0.0 ; dim] . into ( ) , 2.0 ) ;
348390 let individuals = sampler. sample ( & state, Mode :: Minimize , false ) . unwrap ( ) ;
349391
0 commit comments