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