@@ -11,8 +11,15 @@ use crate::mode::Mode;
1111use crate :: state:: State ;
1212use crate :: { ObjectiveFunction , ParallelObjectiveFunction } ;
1313
14- pub trait Constraints : Sync {
14+ pub trait Constraints : Sync + std :: fmt :: Debug {
1515 fn meets_constraints ( & self , x : & DVector < f64 > ) -> bool ;
16+ fn clone_box ( & self ) -> Box < dyn Constraints > ;
17+ }
18+
19+ impl Clone for Box < dyn Constraints > {
20+ fn clone ( & self ) -> Box < dyn Constraints > {
21+ self . clone_box ( )
22+ }
1623}
1724
1825#[ derive( Debug , Clone ) ]
@@ -25,16 +32,20 @@ impl Constraints for Bounds {
2532 fn meets_constraints ( & self , x : & DVector < f64 > ) -> bool {
2633 ( 0 ..x. len ( ) ) . all ( |i| x[ i] >= self . lower [ i] && x[ i] <= self . upper [ i] )
2734 }
35+
36+ fn clone_box ( & self ) -> Box < dyn Constraints > {
37+ Box :: new ( self . clone ( ) )
38+ }
2839}
2940
3041/// A type for sampling and evaluating points from the distribution for each generation
3142pub struct Sampler < F > {
3243 /// Number of dimensions to sample from
3344 dim : usize ,
34- /// If set, resamples until all points are within bounds
45+ /// If set, resamples until all points satisfy the constraints
3546 constraints : Option < Box < dyn Constraints > > ,
3647 /// The maximum number of resamples.
37- /// If this limit is hit, uses points even if they are outside the bounds
48+ /// If this limit is hit, uses points even if they violate the constraints
3849 max_resamples : Option < usize > ,
3950 /// Number of points to sample each generation
4051 population_size : usize ,
@@ -47,10 +58,10 @@ pub struct Sampler<F> {
4758}
4859
4960impl < F > Sampler < F > {
50- pub fn new ( dim : usize , bounds : Option < Box < dyn Constraints > > , max_resamples : Option < usize > , population_size : usize , objective_function : F , rng_seed : u64 ) -> Self {
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 {
5162 Self {
5263 dim,
53- constraints : bounds ,
64+ constraints,
5465 max_resamples,
5566 population_size,
5667 rng : ChaCha12Rng :: seed_from_u64 ( rng_seed) ,
@@ -85,15 +96,15 @@ impl<F> Sampler<F> {
8596
8697 match constraints {
8798 Some ( constraints) => {
88- let in_bounds = |yk : & DVector < f64 > | {
99+ let ok_constraints = |yk : & DVector < f64 > | {
89100 let point = to_point ( & yk, state. mean ( ) , state. sigma ( ) ) ;
90101 constraints. meets_constraints ( & point)
91102 } ;
92103
93104 if parallel_update {
94- z. into_par_iter ( ) . map ( transform) . filter ( in_bounds ) . collect ( )
105+ z. into_par_iter ( ) . map ( transform) . filter ( ok_constraints ) . collect ( )
95106 } else {
96- z. into_iter ( ) . map ( transform) . filter ( in_bounds ) . collect ( )
107+ z. into_iter ( ) . map ( transform) . filter ( ok_constraints ) . collect ( )
97108 }
98109 } ,
99110 None => {
@@ -115,14 +126,14 @@ impl<F> Sampler<F> {
115126 break ;
116127 }
117128
118- let mut bounds = self . constraints . as_ref ( ) . map ( |x| x. as_ref ( ) ) ;
129+ let mut constraints = self . constraints . as_ref ( ) . map ( |x| x. as_ref ( ) ) ;
119130 if let Some ( max) = self . max_resamples {
120131 if i >= max {
121- bounds = None ;
132+ constraints = None ;
122133 }
123134 }
124135
125- let mut new_samps: Vec < _ > = sample ( remain, bounds ) ;
136+ let mut new_samps: Vec < _ > = sample ( remain, constraints ) ;
126137 y. append ( & mut new_samps) ;
127138
128139 i += 1 ;
0 commit comments