@@ -57,6 +57,8 @@ pub struct LinearProgramState<P, F> {
57
57
pub max_iters : u64 ,
58
58
/// Evaluation counts
59
59
pub counts : HashMap < String , u64 > ,
60
+ /// Update evaluation counts?
61
+ pub counting_enabled : bool ,
60
62
/// Time required so far
61
63
pub time : Option < instant:: Duration > ,
62
64
/// Status of optimization execution
@@ -151,6 +153,20 @@ impl<P, F> LinearProgramState<P, F> {
151
153
self . cost = cost;
152
154
self
153
155
}
156
+
157
+ /// Overrides state of counting function executions (default: false)
158
+ /// ```
159
+ /// # use argmin::core::{State, LinearProgramState};
160
+ /// # let mut state: LinearProgramState<Vec<f64>, f64> = LinearProgramState::new();
161
+ /// # assert!(!state.counting_enabled);
162
+ /// let state = state.counting(true);
163
+ /// # assert!(state.counting_enabled);
164
+ /// ```
165
+ #[ must_use]
166
+ pub fn counting ( mut self , mode : bool ) -> Self {
167
+ self . counting_enabled = mode;
168
+ self
169
+ }
154
170
}
155
171
156
172
impl < P , F > State for LinearProgramState < P , F >
@@ -206,6 +222,7 @@ where
206
222
last_best_iter : 0 ,
207
223
max_iters : std:: u64:: MAX ,
208
224
counts : HashMap :: new ( ) ,
225
+ counting_enabled : false ,
209
226
time : Some ( instant:: Duration :: new ( 0 , 0 ) ) ,
210
227
termination_status : TerminationStatus :: NotTerminated ,
211
228
}
@@ -504,7 +521,7 @@ where
504
521
/// ```
505
522
/// # use std::collections::HashMap;
506
523
/// # use argmin::core::{Problem, LinearProgramState, State, ArgminFloat};
507
- /// # let mut state: LinearProgramState<Vec<f64>, f64> = LinearProgramState::new();
524
+ /// # let mut state: LinearProgramState<Vec<f64>, f64> = LinearProgramState::new().counting(true) ;
508
525
/// # assert_eq!(state.counts, HashMap::new());
509
526
/// # state.counts.insert("test2".to_string(), 10u64);
510
527
/// #
@@ -521,9 +538,11 @@ where
521
538
/// # assert_eq!(state.counts, hm);
522
539
/// ```
523
540
fn func_counts < O > ( & mut self , problem : & Problem < O > ) {
524
- for ( k, & v) in problem. counts . iter ( ) {
525
- let count = self . counts . entry ( k. to_string ( ) ) . or_insert ( 0 ) ;
526
- * count = v
541
+ if self . counting_enabled {
542
+ for ( k, & v) in problem. counts . iter ( ) {
543
+ let count = self . counts . entry ( k. to_string ( ) ) . or_insert ( 0 ) ;
544
+ * count = v
545
+ }
527
546
}
528
547
}
529
548
0 commit comments