Skip to main content

solverforge_solver/heuristic/move/
list_multi_swap.rs

1//! Public static facade for canonical multi-list-swap mechanics.
2
3use std::fmt::Debug;
4
5use smallvec::SmallVec;
6use solverforge_core::domain::PlanningSolution;
7use solverforge_scoring::Director;
8
9use super::list_kernel::{
10    multi_swap_do_move, multi_swap_entity_indices, multi_swap_is_doable, multi_swap_tabu_signature,
11    multi_swap_undo_move, MultiSwapCoordinates, StaticListSwapAccess,
12};
13use super::{Move, MoveTabuSignature};
14
15/// Multiple independent intra-list swaps, one per entity.
16///
17/// The public move keeps its historic constructor and score-improvement flag;
18/// shared list-kernel mechanics own doability, notifications, mutation, undo,
19/// and tabu identity for static and runtime carriers alike.
20pub struct ListMultiSwapMove<S, V> {
21    swaps: MultiSwapCoordinates,
22    access: StaticListSwapAccess<S, V>,
23    indices: SmallVec<[usize; 4]>,
24    require_score_improvement: bool,
25}
26
27impl<S, V: Debug> Debug for ListMultiSwapMove<S, V> {
28    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
29        formatter
30            .debug_struct("ListMultiSwapMove")
31            .field("swaps", &self.swaps)
32            .field("variable_name", &self.access.variable_name)
33            .field("require_score_improvement", &self.require_score_improvement)
34            .finish()
35    }
36}
37
38impl<S, V> ListMultiSwapMove<S, V> {
39    pub fn new(
40        swaps: &[(usize, usize, usize)],
41        list_len: fn(&S, usize) -> usize,
42        list_get: fn(&S, usize, usize) -> Option<V>,
43        list_set: fn(&mut S, usize, usize, V),
44        variable_name: &'static str,
45        descriptor_index: usize,
46    ) -> Self {
47        Self {
48            swaps: swaps.iter().copied().collect(),
49            access: StaticListSwapAccess {
50                list_len,
51                list_get,
52                list_set,
53                variable_name,
54                descriptor_index,
55            },
56            indices: multi_swap_entity_indices(swaps),
57            require_score_improvement: false,
58        }
59    }
60
61    pub fn with_require_score_improvement(mut self, require_score_improvement: bool) -> Self {
62        self.require_score_improvement = require_score_improvement;
63        self
64    }
65
66    pub fn swaps(&self) -> &[(usize, usize, usize)] {
67        &self.swaps
68    }
69}
70
71impl<S, V> Move<S> for ListMultiSwapMove<S, V>
72where
73    S: PlanningSolution,
74    V: Clone + PartialEq + Send + Sync + Debug + 'static,
75{
76    type Undo = ();
77
78    fn is_doable<D: Director<S>>(&self, score_director: &D) -> bool {
79        multi_swap_is_doable(&self.access, &self.swaps, score_director)
80    }
81
82    fn do_move<D: Director<S>>(&self, score_director: &mut D) -> Self::Undo {
83        multi_swap_do_move(&self.access, &self.swaps, &self.indices, score_director)
84    }
85
86    fn undo_move<D: Director<S>>(&self, score_director: &mut D, (): Self::Undo) {
87        multi_swap_undo_move(&self.access, &self.swaps, &self.indices, score_director);
88    }
89
90    fn descriptor_index(&self) -> usize {
91        self.access.descriptor_index
92    }
93
94    fn entity_indices(&self) -> &[usize] {
95        &self.indices
96    }
97
98    fn variable_name(&self) -> &str {
99        self.access.variable_name
100    }
101
102    fn telemetry_label(&self) -> &'static str {
103        "list_multi_swap"
104    }
105
106    fn requires_score_improvement(&self) -> bool {
107        self.require_score_improvement
108    }
109
110    fn tabu_signature<D: Director<S>>(&self, score_director: &D) -> MoveTabuSignature {
111        multi_swap_tabu_signature(&self.access, &self.swaps, &self.indices, score_director)
112    }
113}