solverforge-solver 0.18.0

Solver engine for SolverForge
Documentation
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/* Foragers for local search move selection

Foragers collect accepted move indices during a step and select the
best one to apply. Uses index-based API for zero-clone operation.
*/

use std::fmt::Debug;
use std::marker::PhantomData;

use crate::heuristic::r#move::Move;
use crate::heuristic::selector::move_selector::CandidateId;
use solverforge_core::domain::PlanningSolution;

/// Trait for collecting and selecting moves in local search.
///
/// Foragers are responsible for:
/// - Collecting accepted move indices during move evaluation
/// - Deciding when to quit evaluating early
/// - Selecting the best move index to apply
///
/// # Type Parameters
/// * `S` - The planning solution type
/// * `M` - The move type (for trait bounds only, moves are never stored)
pub trait LocalSearchForager<S, M>: Send + Debug
where
    S: PlanningSolution,
    M: Move<S>,
{
    /* Called at the start of each step to reset state.

    `best_score` is the best solution score ever seen.
    `last_step_score` is the score at the end of the previous step.
    Foragers that implement pick-early-on-improvement use these to decide
    when to stop evaluating moves.
    */
    fn step_started(&mut self, best_score: S::Score, last_step_score: S::Score, step_seed: u64);

    /* Adds an accepted move index to the forager.

    The ID refers to a live candidate in the current move cursor.
    */
    fn add_move_index(&mut self, index: CandidateId, score: S::Score) -> ForagerDecision;

    // Returns true if the forager has collected enough moves and
    // wants to stop evaluating more.
    fn is_quit_early(&self) -> bool;

    fn accepted_count_limit(&self) -> Option<usize> {
        None
    }

    /* Picks the best move index from those collected.

    Returns None if no moves were accepted.
    */
    fn pick_move_index(&mut self) -> Option<(CandidateId, S::Score)>;
}

/// Tells the phase which candidate payload remains owned after online foraging.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ForagerDecision {
    /// Retain the newly accepted candidate.
    Keep,
    /// Release the newly accepted candidate; the current winner remains retained.
    Release,
    /// Retain the new candidate and release the replaced candidate immediately.
    Replace(CandidateId),
}

pub(super) struct BestCandidate<S>
where
    S: PlanningSolution,
{
    selected: Option<(CandidateId, S::Score)>,
    equal_count: u64,
    step_seed: u64,
    random_ties: bool,
}

impl<S> BestCandidate<S>
where
    S: PlanningSolution,
{
    fn new(random_ties: bool) -> Self {
        Self {
            selected: None,
            equal_count: 0,
            step_seed: 0,
            random_ties,
        }
    }

    fn reset(&mut self, step_seed: u64) {
        self.selected = None;
        self.equal_count = 0;
        self.step_seed = step_seed;
    }

    fn consider(&mut self, index: CandidateId, score: S::Score) -> ForagerDecision {
        let Some((selected, best_score)) = self.selected else {
            self.selected = Some((index, score));
            self.equal_count = 1;
            return ForagerDecision::Keep;
        };

        match score.cmp(&best_score) {
            std::cmp::Ordering::Less => ForagerDecision::Release,
            std::cmp::Ordering::Greater => self.replace(index, score),
            std::cmp::Ordering::Equal => {
                self.equal_count += 1;
                if self.random_ties && reservoir_pick(self.step_seed, self.equal_count) {
                    self.selected = Some((index, score));
                    ForagerDecision::Replace(selected)
                } else {
                    ForagerDecision::Release
                }
            }
        }
    }

    pub(super) fn replace(&mut self, index: CandidateId, score: S::Score) -> ForagerDecision {
        self.equal_count = 1;
        match self.selected.replace((index, score)) {
            Some((replaced, _)) => ForagerDecision::Replace(replaced),
            None => ForagerDecision::Keep,
        }
    }

    pub(super) fn take(&mut self) -> Option<(CandidateId, S::Score)> {
        self.equal_count = 0;
        self.selected.take()
    }

    pub(super) fn has_selection(&self) -> bool {
        self.selected.is_some()
    }

    fn random_ties(&self) -> bool {
        self.random_ties
    }
}

fn reservoir_pick(step_seed: u64, equal_count: u64) -> bool {
    let mixed = splitmix64(
        step_seed ^ equal_count.wrapping_mul(0x9E37_79B9_7F4A_7C15) ^ 0xF04A_63E2_39B7_4D11,
    );
    mixed.is_multiple_of(equal_count)
}

fn splitmix64(mut value: u64) -> u64 {
    value = value.wrapping_add(0x9E37_79B9_7F4A_7C15);
    value = (value ^ (value >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
    value = (value ^ (value >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
    value ^ (value >> 31)
}

mod improving;

pub use improving::{FirstBestScoreImprovingForager, FirstLastStepScoreImprovingForager};

/// A forager that stops after `N` accepted moves and picks the best of them.
///
/// The limit is the step evaluation horizon, not a storage cap for a full
/// neighborhood scan. `AcceptedCountForager(1)` therefore behaves like first
/// accepted selection, while larger limits select the best candidate among the
/// first `N` accepted moves.
pub struct AcceptedCountForager<S>
where
    S: PlanningSolution,
{
    // Number of accepted moves to collect before ending the step.
    accepted_count_limit: usize,
    accepted_count: usize,
    best_move: BestCandidate<S>,
    _phantom: PhantomData<fn() -> S>,
}

impl<S> AcceptedCountForager<S>
where
    S: PlanningSolution,
{
    /// Creates a new forager with the given limit.
    ///
    /// # Panics
    /// Panics if `accepted_count_limit` is 0 — a zero limit would quit before
    /// evaluating any move, silently skipping every step.
    ///
    /// # Arguments
    /// * `accepted_count_limit` - Collect this many accepted moves before quitting early
    pub fn new(accepted_count_limit: usize, random_ties: bool) -> Self {
        assert!(
            accepted_count_limit > 0,
            "AcceptedCountForager: accepted_count_limit must be > 0, got 0"
        );
        Self {
            accepted_count_limit,
            accepted_count: 0,
            best_move: BestCandidate::new(random_ties),
            _phantom: PhantomData,
        }
    }
}

impl<S> Clone for AcceptedCountForager<S>
where
    S: PlanningSolution,
{
    fn clone(&self) -> Self {
        Self {
            accepted_count_limit: self.accepted_count_limit,
            accepted_count: 0,
            best_move: BestCandidate::new(self.best_move.random_ties()),
            _phantom: PhantomData,
        }
    }
}

impl<S> Debug for AcceptedCountForager<S>
where
    S: PlanningSolution,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("AcceptedCountForager")
            .field("accepted_count_limit", &self.accepted_count_limit)
            .field("accepted_count", &self.accepted_count)
            .finish()
    }
}

impl<S, M> LocalSearchForager<S, M> for AcceptedCountForager<S>
where
    S: PlanningSolution,
    M: Move<S>,
{
    fn step_started(&mut self, _best_score: S::Score, _last_step_score: S::Score, step_seed: u64) {
        self.accepted_count = 0;
        self.best_move.reset(step_seed);
    }

    fn add_move_index(&mut self, index: CandidateId, score: S::Score) -> ForagerDecision {
        if self.accepted_count >= self.accepted_count_limit {
            return ForagerDecision::Release;
        }
        self.accepted_count += 1;
        self.best_move.consider(index, score)
    }

    fn is_quit_early(&self) -> bool {
        self.accepted_count >= self.accepted_count_limit
    }

    fn accepted_count_limit(&self) -> Option<usize> {
        Some(self.accepted_count_limit)
    }

    fn pick_move_index(&mut self) -> Option<(CandidateId, S::Score)> {
        self.best_move.take()
    }
}

/// A forager that picks the first accepted move.
///
/// This is the simplest forager - it quits after the first accepted move.
pub struct FirstAcceptedForager<S>
where
    S: PlanningSolution,
{
    // The first accepted move index.
    accepted_move: Option<(CandidateId, S::Score)>,
    _phantom: PhantomData<fn() -> S>,
}

impl<S> Debug for FirstAcceptedForager<S>
where
    S: PlanningSolution,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("FirstAcceptedForager")
            .field("has_move", &self.accepted_move.is_some())
            .finish()
    }
}

impl<S> FirstAcceptedForager<S>
where
    S: PlanningSolution,
{
    pub fn new() -> Self {
        Self {
            accepted_move: None,
            _phantom: PhantomData,
        }
    }
}

impl<S> Clone for FirstAcceptedForager<S>
where
    S: PlanningSolution,
{
    fn clone(&self) -> Self {
        Self {
            accepted_move: None, // Fresh state for clone
            _phantom: PhantomData,
        }
    }
}

impl<S> Default for FirstAcceptedForager<S>
where
    S: PlanningSolution,
{
    fn default() -> Self {
        Self::new()
    }
}

impl<S, M> LocalSearchForager<S, M> for FirstAcceptedForager<S>
where
    S: PlanningSolution,
    M: Move<S>,
{
    fn step_started(&mut self, _best_score: S::Score, _last_step_score: S::Score, _step_seed: u64) {
        self.accepted_move = None;
    }

    fn add_move_index(&mut self, index: CandidateId, score: S::Score) -> ForagerDecision {
        if self.accepted_move.is_none() {
            self.accepted_move = Some((index, score));
            ForagerDecision::Keep
        } else {
            ForagerDecision::Release
        }
    }

    fn is_quit_early(&self) -> bool {
        self.accepted_move.is_some()
    }

    fn accepted_count_limit(&self) -> Option<usize> {
        Some(1)
    }

    fn pick_move_index(&mut self) -> Option<(CandidateId, S::Score)> {
        self.accepted_move.take()
    }
}

/// A forager that evaluates all accepted moves and picks the best.
///
/// Unlike `AcceptedCountForager(N)`, this forager never quits early - it
/// always evaluates the full move space before selecting the best score.
pub struct BestScoreForager<S>
where
    S: PlanningSolution,
{
    // Best accepted move index and score seen in the current step.
    best_move: BestCandidate<S>,
    _phantom: PhantomData<fn() -> S>,
}

impl<S> BestScoreForager<S>
where
    S: PlanningSolution,
{
    pub fn new(random_ties: bool) -> Self {
        Self {
            best_move: BestCandidate::new(random_ties),
            _phantom: PhantomData,
        }
    }
}

impl<S> Default for BestScoreForager<S>
where
    S: PlanningSolution,
{
    fn default() -> Self {
        Self::new(true)
    }
}

impl<S> Debug for BestScoreForager<S>
where
    S: PlanningSolution,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("BestScoreForager")
            .field("has_move", &self.best_move.has_selection())
            .finish()
    }
}

impl<S> Clone for BestScoreForager<S>
where
    S: PlanningSolution,
{
    fn clone(&self) -> Self {
        Self {
            best_move: BestCandidate::new(self.best_move.random_ties()),
            _phantom: PhantomData,
        }
    }
}

impl<S, M> LocalSearchForager<S, M> for BestScoreForager<S>
where
    S: PlanningSolution,
    M: Move<S>,
{
    fn step_started(&mut self, _best_score: S::Score, _last_step_score: S::Score, step_seed: u64) {
        self.best_move.reset(step_seed);
    }

    fn add_move_index(&mut self, index: CandidateId, score: S::Score) -> ForagerDecision {
        self.best_move.consider(index, score)
    }

    fn is_quit_early(&self) -> bool {
        false // Never quit early — always evaluate the full move space
    }

    fn pick_move_index(&mut self) -> Option<(CandidateId, S::Score)> {
        self.best_move.take()
    }
}

#[cfg(test)]
mod any_tests;
#[cfg(test)]
mod tests;