Skip to main content

solverforge_solver/heuristic/move/
list_swap.rs

1/* ListSwapMove - swaps two elements within or between list variables.
2
3This move exchanges two elements at different positions.
4Useful for TSP-style improvements and route optimization.
5
6# Zero-Erasure Design
7
8Uses typed function pointers for list operations. No `dyn Any`, no downcasting.
9*/
10
11use std::fmt::Debug;
12
13use solverforge_core::domain::PlanningSolution;
14use solverforge_scoring::Director;
15
16use super::Move;
17
18/// A move that swaps two elements in list variables.
19///
20/// Supports both intra-list swaps (within same entity) and inter-list swaps
21/// (between different entities). Uses typed function pointers for zero-erasure.
22///
23/// # Type Parameters
24/// * `S` - The planning solution type
25/// * `V` - The list element value type
26///
27/// # Example
28///
29/// ```
30/// use solverforge_solver::heuristic::r#move::ListSwapMove;
31/// use solverforge_core::domain::PlanningSolution;
32/// use solverforge_core::score::SoftScore;
33///
34/// #[derive(Clone, Debug)]
35/// struct Vehicle { id: usize, visits: Vec<i32> }
36///
37/// #[derive(Clone, Debug)]
38/// struct Solution { vehicles: Vec<Vehicle>, score: Option<SoftScore> }
39///
40/// impl PlanningSolution for Solution {
41///     type Score = SoftScore;
42///     fn score(&self) -> Option<Self::Score> { self.score }
43///     fn set_score(&mut self, score: Option<Self::Score>) { self.score = score; }
44/// }
45///
46/// fn list_len(s: &Solution, entity_idx: usize) -> usize {
47///     s.vehicles.get(entity_idx).map_or(0, |v| v.visits.len())
48/// }
49/// fn list_get(s: &Solution, entity_idx: usize, pos: usize) -> Option<i32> {
50///     s.vehicles.get(entity_idx).and_then(|v| v.visits.get(pos).copied())
51/// }
52/// fn list_set(s: &mut Solution, entity_idx: usize, pos: usize, val: i32) {
53///     if let Some(v) = s.vehicles.get_mut(entity_idx) {
54///         if let Some(elem) = v.visits.get_mut(pos) { *elem = val; }
55///     }
56/// }
57///
58/// // Swap elements at positions 1 and 3 in vehicle 0
59/// let m = ListSwapMove::<Solution, i32>::new(
60///     0, 1, 0, 3,
61///     list_len, list_get, list_set,
62///     "visits", 0,
63/// );
64/// ```
65pub struct ListSwapMove<S, V> {
66    // First entity index
67    first_entity_index: usize,
68    // Position in first entity's list
69    first_position: usize,
70    // Second entity index
71    second_entity_index: usize,
72    // Position in second entity's list
73    second_position: usize,
74    list_len: fn(&S, usize) -> usize,
75    list_get: fn(&S, usize, usize) -> Option<V>,
76    // Set element at position
77    list_set: fn(&mut S, usize, usize, V),
78    variable_name: &'static str,
79    descriptor_index: usize,
80    // Store indices for entity_indices()
81    indices: [usize; 2],
82}
83
84impl<S, V> Clone for ListSwapMove<S, V> {
85    fn clone(&self) -> Self {
86        *self
87    }
88}
89
90impl<S, V> Copy for ListSwapMove<S, V> {}
91
92impl<S, V: Debug> Debug for ListSwapMove<S, V> {
93    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
94        f.debug_struct("ListSwapMove")
95            .field("first_entity", &self.first_entity_index)
96            .field("first_position", &self.first_position)
97            .field("second_entity", &self.second_entity_index)
98            .field("second_position", &self.second_position)
99            .field("variable_name", &self.variable_name)
100            .finish()
101    }
102}
103
104impl<S, V> ListSwapMove<S, V> {
105    /* Creates a new list swap move with typed function pointers.
106
107    # Arguments
108    * `first_entity_index` - First entity index
109    * `first_position` - Position in first entity's list
110    * `second_entity_index` - Second entity index
111    * `second_position` - Position in second entity's list
112    * `list_len` - Function to get list length
113    * `list_get` - Function to get element at position
114    * `list_set` - Function to set element at position
115    * `variable_name` - Name of the list variable
116    * `descriptor_index` - Entity descriptor index
117    */
118    #[allow(clippy::too_many_arguments)]
119    pub fn new(
120        first_entity_index: usize,
121        first_position: usize,
122        second_entity_index: usize,
123        second_position: usize,
124        list_len: fn(&S, usize) -> usize,
125        list_get: fn(&S, usize, usize) -> Option<V>,
126        list_set: fn(&mut S, usize, usize, V),
127        variable_name: &'static str,
128        descriptor_index: usize,
129    ) -> Self {
130        Self {
131            first_entity_index,
132            first_position,
133            second_entity_index,
134            second_position,
135            list_len,
136            list_get,
137            list_set,
138            variable_name,
139            descriptor_index,
140            indices: [first_entity_index, second_entity_index],
141        }
142    }
143
144    pub fn first_entity_index(&self) -> usize {
145        self.first_entity_index
146    }
147
148    pub fn first_position(&self) -> usize {
149        self.first_position
150    }
151
152    pub fn second_entity_index(&self) -> usize {
153        self.second_entity_index
154    }
155
156    pub fn second_position(&self) -> usize {
157        self.second_position
158    }
159
160    pub fn is_intra_list(&self) -> bool {
161        self.first_entity_index == self.second_entity_index
162    }
163}
164
165impl<S, V> Move<S> for ListSwapMove<S, V>
166where
167    S: PlanningSolution,
168    V: Clone + PartialEq + Send + Sync + Debug + 'static,
169{
170    fn is_doable<D: Director<S>>(&self, score_director: &D) -> bool {
171        let solution = score_director.working_solution();
172
173        // Check first position is valid
174        let first_len = (self.list_len)(solution, self.first_entity_index);
175        if self.first_position >= first_len {
176            return false;
177        }
178
179        // Check second position is valid
180        let second_len = (self.list_len)(solution, self.second_entity_index);
181        if self.second_position >= second_len {
182            return false;
183        }
184
185        // For intra-list, can't swap with self
186        if self.is_intra_list() && self.first_position == self.second_position {
187            return false;
188        }
189
190        // Get values and check they're different
191        let first_val = (self.list_get)(solution, self.first_entity_index, self.first_position);
192        let second_val = (self.list_get)(solution, self.second_entity_index, self.second_position);
193
194        first_val != second_val
195    }
196
197    fn do_move<D: Director<S>>(&self, score_director: &mut D) {
198        // Get both values
199        let first_val = (self.list_get)(
200            score_director.working_solution(),
201            self.first_entity_index,
202            self.first_position,
203        )
204        .expect("first position should be valid");
205
206        let second_val = (self.list_get)(
207            score_director.working_solution(),
208            self.second_entity_index,
209            self.second_position,
210        )
211        .expect("second position should be valid");
212
213        // Notify before changes
214        score_director.before_variable_changed(self.descriptor_index, self.first_entity_index);
215        if !self.is_intra_list() {
216            score_director.before_variable_changed(self.descriptor_index, self.second_entity_index);
217        }
218
219        // Swap: first gets second's value, second gets first's value
220        (self.list_set)(
221            score_director.working_solution_mut(),
222            self.first_entity_index,
223            self.first_position,
224            second_val.clone(),
225        );
226        (self.list_set)(
227            score_director.working_solution_mut(),
228            self.second_entity_index,
229            self.second_position,
230            first_val.clone(),
231        );
232
233        // Notify after changes
234        score_director.after_variable_changed(self.descriptor_index, self.first_entity_index);
235        if !self.is_intra_list() {
236            score_director.after_variable_changed(self.descriptor_index, self.second_entity_index);
237        }
238
239        // Register undo - swap back
240        let list_set = self.list_set;
241        let first_entity = self.first_entity_index;
242        let first_pos = self.first_position;
243        let second_entity = self.second_entity_index;
244        let second_pos = self.second_position;
245
246        score_director.register_undo(Box::new(move |s: &mut S| {
247            // Restore original values
248            list_set(s, first_entity, first_pos, first_val);
249            list_set(s, second_entity, second_pos, second_val);
250        }));
251    }
252
253    fn descriptor_index(&self) -> usize {
254        self.descriptor_index
255    }
256
257    fn entity_indices(&self) -> &[usize] {
258        if self.is_intra_list() {
259            &self.indices[0..1]
260        } else {
261            &self.indices
262        }
263    }
264
265    fn variable_name(&self) -> &str {
266        self.variable_name
267    }
268}