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