Skip to main content

solverforge_solver/heuristic/move/
list_change.rs

1/* ListChangeMove - relocates an element within or between list variables.
2
3This move removes an element from one position and inserts it at another.
4Essential for vehicle routing and scheduling problems.
5
6# Zero-Erasure Design
7
8Uses concrete function pointers for list operations. No `dyn Any`, no downcasting.
9*/
10
11use std::fmt::Debug;
12
13use smallvec::{smallvec, SmallVec};
14use solverforge_core::domain::PlanningSolution;
15use solverforge_scoring::Director;
16
17use super::metadata::{
18    encode_option_debug, encode_usize, hash_str, MoveTabuScope, ScopedEntityTabuToken,
19};
20use super::{Move, MoveTabuSignature};
21
22/// A move that relocates an element from one list position to another.
23///
24/// Supports both intra-list moves (within same entity) and inter-list moves
25/// (between different entities). Uses concrete function pointers for zero-erasure.
26///
27/// # Type Parameters
28/// * `S` - The planning solution type
29/// * `V` - The list element value type
30///
31/// # Example
32///
33/// ```
34/// use solverforge_solver::heuristic::r#move::ListChangeMove;
35/// use solverforge_core::domain::PlanningSolution;
36/// use solverforge_core::score::SoftScore;
37///
38/// #[derive(Clone, Debug)]
39/// struct Vehicle { id: usize, visits: Vec<i32> }
40///
41/// #[derive(Clone, Debug)]
42/// struct Solution { vehicles: Vec<Vehicle>, score: Option<SoftScore> }
43///
44/// impl PlanningSolution for Solution {
45///     type Score = SoftScore;
46///     fn score(&self) -> Option<Self::Score> { self.score }
47///     fn set_score(&mut self, score: Option<Self::Score>) { self.score = score; }
48/// }
49///
50/// fn list_len(s: &Solution, entity_idx: usize) -> usize {
51///     s.vehicles.get(entity_idx).map_or(0, |v| v.visits.len())
52/// }
53/// fn list_get(s: &Solution, entity_idx: usize, pos: usize) -> Option<i32> {
54///     s.vehicles
55///         .get(entity_idx)
56///         .and_then(|v| v.visits.get(pos))
57///         .copied()
58/// }
59/// fn list_remove(s: &mut Solution, entity_idx: usize, pos: usize) -> Option<i32> {
60///     s.vehicles.get_mut(entity_idx).map(|v| v.visits.remove(pos))
61/// }
62/// fn list_insert(s: &mut Solution, entity_idx: usize, pos: usize, val: i32) {
63///     if let Some(v) = s.vehicles.get_mut(entity_idx) { v.visits.insert(pos, val); }
64/// }
65///
66/// // Move element from vehicle 0 position 2 to vehicle 1 position 0
67/// let m = ListChangeMove::<Solution, i32>::new(
68///     0, 2, 1, 0,
69///     list_len, list_get, list_remove, list_insert,
70///     "visits", 0,
71/// );
72/// ```
73pub struct ListChangeMove<S, V> {
74    // Source entity index (which entity's list to remove from)
75    source_entity_index: usize,
76    // Position in source list to remove from
77    source_position: usize,
78    // Destination entity index (which entity's list to insert into)
79    dest_entity_index: usize,
80    // Position in destination list to insert at
81    dest_position: usize,
82    list_len: fn(&S, usize) -> usize,
83    list_get: fn(&S, usize, usize) -> Option<V>,
84    // Remove element at position, returns removed value
85    list_remove: fn(&mut S, usize, usize) -> Option<V>,
86    // Insert element at position
87    list_insert: fn(&mut S, usize, usize, V),
88    variable_name: &'static str,
89    descriptor_index: usize,
90    // Store indices for entity_indices()
91    indices: [usize; 2],
92}
93
94impl<S, V> Clone for ListChangeMove<S, V> {
95    fn clone(&self) -> Self {
96        *self
97    }
98}
99
100impl<S, V> Copy for ListChangeMove<S, V> {}
101
102impl<S, V: Debug> Debug for ListChangeMove<S, V> {
103    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
104        f.debug_struct("ListChangeMove")
105            .field("source_entity", &self.source_entity_index)
106            .field("source_position", &self.source_position)
107            .field("dest_entity", &self.dest_entity_index)
108            .field("dest_position", &self.dest_position)
109            .field("variable_name", &self.variable_name)
110            .finish()
111    }
112}
113
114impl<S, V> ListChangeMove<S, V> {
115    /* Creates a new list change move with concrete function pointers.
116
117    # Arguments
118    * `source_entity_index` - Entity index to remove from
119    * `source_position` - Position in source list
120    * `dest_entity_index` - Entity index to insert into
121    * `dest_position` - Position in destination list
122    * `list_len` - Function to get list length
123    * `list_remove` - Function to remove element at position
124    * `list_insert` - Function to insert element at position
125    * `variable_name` - Name of the list variable
126    * `descriptor_index` - Entity descriptor index
127    */
128    #[allow(clippy::too_many_arguments)]
129    pub fn new(
130        source_entity_index: usize,
131        source_position: usize,
132        dest_entity_index: usize,
133        dest_position: usize,
134        list_len: fn(&S, usize) -> usize,
135        list_get: fn(&S, usize, usize) -> Option<V>,
136        list_remove: fn(&mut S, usize, usize) -> Option<V>,
137        list_insert: fn(&mut S, usize, usize, V),
138        variable_name: &'static str,
139        descriptor_index: usize,
140    ) -> Self {
141        Self {
142            source_entity_index,
143            source_position,
144            dest_entity_index,
145            dest_position,
146            list_len,
147            list_get,
148            list_remove,
149            list_insert,
150            variable_name,
151            descriptor_index,
152            indices: [source_entity_index, dest_entity_index],
153        }
154    }
155
156    pub fn source_entity_index(&self) -> usize {
157        self.source_entity_index
158    }
159
160    pub fn source_position(&self) -> usize {
161        self.source_position
162    }
163
164    pub fn dest_entity_index(&self) -> usize {
165        self.dest_entity_index
166    }
167
168    pub fn dest_position(&self) -> usize {
169        self.dest_position
170    }
171
172    pub fn is_intra_list(&self) -> bool {
173        self.source_entity_index == self.dest_entity_index
174    }
175}
176
177impl<S, V> Move<S> for ListChangeMove<S, V>
178where
179    S: PlanningSolution,
180    V: Clone + PartialEq + Send + Sync + Debug + 'static,
181{
182    type Undo = ();
183
184    fn is_doable<D: Director<S>>(&self, score_director: &D) -> bool {
185        let solution = score_director.working_solution();
186
187        // Check source position is valid
188        let source_len = (self.list_len)(solution, self.source_entity_index);
189        if self.source_position >= source_len {
190            return false;
191        }
192
193        /* Check destination position is valid
194        For intra-list, dest can be 0..=len-1 (after removal)
195        For inter-list, dest can be 0..=len
196        */
197        let dest_len = (self.list_len)(solution, self.dest_entity_index);
198        let max_dest = if self.is_intra_list() {
199            source_len - 1 // After removal, list is shorter
200        } else {
201            dest_len
202        };
203
204        if self.dest_position > max_dest {
205            return false;
206        }
207
208        /* For intra-list moves, check for no-ops
209        Moving to same position is obviously a no-op
210        Moving forward by 1 position is also a no-op due to index adjustment:
211        remove at source, adjusted_dest = dest-1 = source, insert at source → same list
212        */
213        if self.is_intra_list() {
214            if self.source_position == self.dest_position {
215                return false;
216            }
217            // Forward move by exactly 1 is a no-op
218            if self.dest_position == self.source_position + 1 {
219                return false;
220            }
221        }
222
223        true
224    }
225
226    fn do_move<D: Director<S>>(&self, score_director: &mut D) -> Self::Undo {
227        // Notify before changes
228        score_director.before_variable_changed(self.descriptor_index, self.source_entity_index);
229        if !self.is_intra_list() {
230            score_director.before_variable_changed(self.descriptor_index, self.dest_entity_index);
231        }
232
233        // Remove from source
234        let value = (self.list_remove)(
235            score_director.working_solution_mut(),
236            self.source_entity_index,
237            self.source_position,
238        )
239        .expect("source position should be valid");
240
241        // For intra-list moves, adjust dest position if it was after source
242        let adjusted_dest = if self.is_intra_list() && self.dest_position > self.source_position {
243            self.dest_position - 1
244        } else {
245            self.dest_position
246        };
247
248        // Insert at destination
249        (self.list_insert)(
250            score_director.working_solution_mut(),
251            self.dest_entity_index,
252            adjusted_dest,
253            value.clone(),
254        );
255
256        // Notify after changes
257        score_director.after_variable_changed(self.descriptor_index, self.source_entity_index);
258        if !self.is_intra_list() {
259            score_director.after_variable_changed(self.descriptor_index, self.dest_entity_index);
260        }
261    }
262
263    fn undo_move<D: Director<S>>(&self, score_director: &mut D, (): Self::Undo) {
264        let adjusted_dest = if self.is_intra_list() && self.dest_position > self.source_position {
265            self.dest_position - 1
266        } else {
267            self.dest_position
268        };
269        score_director.before_variable_changed(self.descriptor_index, self.dest_entity_index);
270        if !self.is_intra_list() {
271            score_director.before_variable_changed(self.descriptor_index, self.source_entity_index);
272        }
273        let removed = (self.list_remove)(
274            score_director.working_solution_mut(),
275            self.dest_entity_index,
276            adjusted_dest,
277        )
278        .expect("undo destination position should contain moved element");
279        (self.list_insert)(
280            score_director.working_solution_mut(),
281            self.source_entity_index,
282            self.source_position,
283            removed,
284        );
285        score_director.after_variable_changed(self.descriptor_index, self.dest_entity_index);
286        if !self.is_intra_list() {
287            score_director.after_variable_changed(self.descriptor_index, self.source_entity_index);
288        }
289    }
290
291    fn descriptor_index(&self) -> usize {
292        self.descriptor_index
293    }
294
295    fn entity_indices(&self) -> &[usize] {
296        if self.is_intra_list() {
297            &self.indices[0..1]
298        } else {
299            &self.indices
300        }
301    }
302
303    fn variable_name(&self) -> &str {
304        self.variable_name
305    }
306
307    fn tabu_signature<D: Director<S>>(&self, score_director: &D) -> MoveTabuSignature {
308        let moved_value = (self.list_get)(
309            score_director.working_solution(),
310            self.source_entity_index,
311            self.source_position,
312        );
313        let moved_id = encode_option_debug(moved_value.as_ref());
314        let source_entity_id = encode_usize(self.source_entity_index);
315        let dest_entity_id = encode_usize(self.dest_entity_index);
316        let variable_id = hash_str(self.variable_name);
317        let scope = MoveTabuScope::new(self.descriptor_index, self.variable_name);
318        let adjusted_dest = if self.is_intra_list() && self.dest_position > self.source_position {
319            self.dest_position - 1
320        } else {
321            self.dest_position
322        };
323        let mut entity_tokens: SmallVec<[ScopedEntityTabuToken; 2]> =
324            smallvec![scope.entity_token(source_entity_id)];
325        if !self.is_intra_list() {
326            entity_tokens.push(scope.entity_token(dest_entity_id));
327        }
328
329        MoveTabuSignature::new(
330            scope,
331            smallvec![
332                encode_usize(self.descriptor_index),
333                variable_id,
334                source_entity_id,
335                encode_usize(self.source_position),
336                dest_entity_id,
337                encode_usize(adjusted_dest),
338                moved_id
339            ],
340            smallvec![
341                encode_usize(self.descriptor_index),
342                variable_id,
343                dest_entity_id,
344                encode_usize(adjusted_dest),
345                source_entity_id,
346                encode_usize(self.source_position),
347                moved_id
348            ],
349        )
350        .with_entity_tokens(entity_tokens)
351        .with_destination_value_tokens([scope.value_token(moved_id)])
352    }
353}