1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
//! Agility tracking for adaptive restarts
//!
//! Agility measures how often variable assignments flip between conflicts.
//! This metric is used in modern SAT solvers like Glucose to make adaptive
//! restart decisions. High agility indicates diverse exploration, while low
//! agility suggests the solver might be stuck in a local search area.
//!
//! References:
//! - "Refining Restarts Strategies for SAT and UNSAT" (Glucose)
//! - "Improving Glucose for Incremental SAT Solving"
#[allow(unused_imports)]
use crate::prelude::*;
/// Statistics for agility tracking
#[derive(Debug, Clone, Default)]
pub struct AgilityStats {
/// Current agility value (0.0 to 1.0)
pub current_agility: f64,
/// Number of flips detected
pub total_flips: u64,
/// Number of assignments tracked
pub total_assignments: u64,
/// Minimum agility observed
pub min_agility: f64,
/// Maximum agility observed
pub max_agility: f64,
}
impl AgilityStats {
/// Display statistics
pub fn display(&self) {
println!("Agility Statistics:");
println!(" Current agility: {:.4}", self.current_agility);
println!(" Total flips: {}", self.total_flips);
println!(" Total assignments: {}", self.total_assignments);
println!(" Min agility: {:.4}", self.min_agility);
println!(" Max agility: {:.4}", self.max_agility);
if self.total_assignments > 0 {
let flip_rate = self.total_flips as f64 / self.total_assignments as f64;
println!(" Overall flip rate: {:.4}", flip_rate);
}
}
}
/// Agility tracker
///
/// Tracks assignment flip rate using exponential moving average.
/// Agility close to 1.0 indicates high diversity (many flips),
/// while agility close to 0.0 indicates stability (few flips).
#[derive(Debug)]
pub struct AgilityTracker {
/// Exponential moving average of flip rate
agility: f64,
/// Decay factor for exponential moving average (0.0 to 1.0)
/// Higher values make agility more responsive to recent behavior
decay: f64,
/// Last assignment for each variable (to detect flips)
last_assignment: Vec<Option<bool>>,
/// Statistics
stats: AgilityStats,
}
impl Default for AgilityTracker {
fn default() -> Self {
Self::new()
}
}
impl AgilityTracker {
/// Create a new agility tracker with default decay
///
/// Default decay is 0.9999, which gives a smoothly changing agility metric
#[must_use]
pub fn new() -> Self {
Self {
agility: 0.0,
decay: 0.9999,
last_assignment: Vec::new(),
stats: AgilityStats {
current_agility: 0.0,
total_flips: 0,
total_assignments: 0,
min_agility: 1.0,
max_agility: 0.0,
},
}
}
/// Create with custom decay factor
///
/// Decay should be in range (0.0, 1.0):
/// - Higher values (e.g., 0.9999) make agility change slowly
/// - Lower values (e.g., 0.99) make agility more responsive
#[must_use]
pub fn with_decay(decay: f64) -> Self {
Self {
agility: 0.0,
decay: decay.clamp(0.0, 1.0),
last_assignment: Vec::new(),
stats: AgilityStats {
current_agility: 0.0,
total_flips: 0,
total_assignments: 0,
min_agility: 1.0,
max_agility: 0.0,
},
}
}
/// Resize for new number of variables
pub fn resize(&mut self, num_vars: usize) {
self.last_assignment.resize(num_vars, None);
}
/// Record a variable assignment
///
/// Updates agility based on whether this assignment differs from the last
pub fn record_assignment(&mut self, var: usize, value: bool) {
// Ensure we have space
if var >= self.last_assignment.len() {
self.last_assignment.resize(var + 1, None);
}
self.stats.total_assignments += 1;
// Check if this is a flip from last assignment
let is_flip = match self.last_assignment[var] {
Some(last_value) => last_value != value,
None => false, // First assignment, not a flip
};
// Update exponential moving average
// agility = decay * agility + (1 - decay) * flip_indicator
// where flip_indicator is 1.0 for flip, 0.0 for no flip
let flip_value = if is_flip { 1.0 } else { 0.0 };
self.agility = self.decay * self.agility + (1.0 - self.decay) * flip_value;
if is_flip {
self.stats.total_flips += 1;
}
// Store current assignment
self.last_assignment[var] = Some(value);
// Update stats
self.stats.current_agility = self.agility;
self.stats.min_agility = self.stats.min_agility.min(self.agility);
self.stats.max_agility = self.stats.max_agility.max(self.agility);
}
/// Get current agility value (0.0 to 1.0)
///
/// Values closer to 1.0 indicate high flip rate (diverse exploration)
/// Values closer to 0.0 indicate low flip rate (focused exploration)
#[must_use]
pub fn agility(&self) -> f64 {
self.agility
}
/// Check if agility is high (above threshold)
///
/// Typical threshold is 0.2-0.3 for restart decisions
#[must_use]
pub fn is_high(&self, threshold: f64) -> bool {
self.agility > threshold
}
/// Check if agility is low (below threshold)
#[must_use]
pub fn is_low(&self, threshold: f64) -> bool {
self.agility < threshold
}
/// Get statistics
#[must_use]
pub fn stats(&self) -> &AgilityStats {
&self.stats
}
/// Reset agility to initial state
pub fn reset(&mut self) {
self.agility = 0.0;
self.last_assignment.clear();
self.stats = AgilityStats {
current_agility: 0.0,
total_flips: 0,
total_assignments: 0,
min_agility: 1.0,
max_agility: 0.0,
};
}
/// Clear assignment history (keeps agility value)
pub fn clear_assignments(&mut self) {
for assignment in &mut self.last_assignment {
*assignment = None;
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_agility_tracker_creation() {
let tracker = AgilityTracker::new();
assert_eq!(tracker.agility(), 0.0);
assert_eq!(tracker.decay, 0.9999);
}
#[test]
fn test_custom_decay() {
let tracker = AgilityTracker::with_decay(0.95);
assert_eq!(tracker.decay, 0.95);
}
#[test]
fn test_no_flips() {
let mut tracker = AgilityTracker::with_decay(0.9);
tracker.resize(2);
// Assign same values repeatedly
for _ in 0..10 {
tracker.record_assignment(0, true);
tracker.record_assignment(1, false);
}
// Agility should be very low (no flips after first assignments)
assert!(tracker.agility() < 0.2);
assert_eq!(tracker.stats().total_flips, 0);
}
#[test]
fn test_all_flips() {
let mut tracker = AgilityTracker::with_decay(0.5);
tracker.resize(1);
// First assignment (not a flip)
tracker.record_assignment(0, true);
assert_eq!(tracker.stats().total_flips, 0);
// All subsequent assignments flip
for i in 0..10 {
let value = i % 2 == 0;
tracker.record_assignment(0, value);
}
// Should have high agility due to flips
assert!(tracker.agility() > 0.3);
// First assignment is not a flip, so 10 assignments = 9 flips
assert_eq!(tracker.stats().total_flips, 9);
}
#[test]
fn test_mixed_flips() {
let mut tracker = AgilityTracker::with_decay(0.8);
tracker.resize(2);
// var 0: flips frequently
tracker.record_assignment(0, true);
tracker.record_assignment(0, false);
tracker.record_assignment(0, true);
tracker.record_assignment(0, false);
// var 1: stable
tracker.record_assignment(1, true);
tracker.record_assignment(1, true);
tracker.record_assignment(1, true);
// Should have moderate agility
let agility = tracker.agility();
assert!(agility > 0.0);
assert!(agility < 1.0);
}
#[test]
fn test_is_high_is_low() {
let mut tracker = AgilityTracker::with_decay(0.9);
tracker.resize(1);
// Start with stable assignments
tracker.record_assignment(0, true);
for _ in 0..5 {
tracker.record_assignment(0, true);
}
assert!(tracker.is_low(0.1));
assert!(!tracker.is_high(0.1));
}
#[test]
fn test_reset() {
let mut tracker = AgilityTracker::new();
tracker.resize(2);
tracker.record_assignment(0, true);
tracker.record_assignment(1, false);
tracker.reset();
assert_eq!(tracker.agility(), 0.0);
assert_eq!(tracker.stats().total_assignments, 0);
assert_eq!(tracker.last_assignment.len(), 0);
}
#[test]
fn test_clear_assignments() {
let mut tracker = AgilityTracker::new();
tracker.resize(2);
tracker.record_assignment(0, true);
let agility_before = tracker.agility();
tracker.clear_assignments();
// Agility value preserved
assert_eq!(tracker.agility(), agility_before);
// But assignments cleared
assert!(tracker.last_assignment.iter().all(|a| a.is_none()));
}
#[test]
fn test_stats_display() {
let mut tracker = AgilityTracker::new();
tracker.resize(1);
tracker.record_assignment(0, true);
tracker.record_assignment(0, false);
let stats = tracker.stats();
assert_eq!(stats.total_assignments, 2);
assert_eq!(stats.total_flips, 1);
}
}