solverforge_solver/heuristic/move/
sublist_swap.rs1use std::borrow::Cow;
12use std::fmt::Debug;
13use std::marker::PhantomData;
14
15use solverforge_core::domain::PlanningSolution;
16use solverforge_scoring::Director;
17
18use super::list_kernel::{
19 sublist_swap_candidate_trace_identity, sublist_swap_do_move, sublist_swap_is_doable,
20 sublist_swap_tabu_signature, sublist_swap_undo_move, StaticListWindowAccess,
21};
22use super::segment_layout::SegmentSwapCoords;
23use super::{Move, MoveTabuSignature};
24
25pub struct SublistSwapMove<S, V> {
84 first_entity_index: usize,
86 first_start: usize,
88 first_end: usize,
90 second_entity_index: usize,
92 second_start: usize,
94 second_end: usize,
96 list_len: fn(&S, usize) -> usize,
97 list_get: fn(&S, usize, usize) -> Option<V>,
98 sublist_remove: fn(&mut S, usize, usize, usize) -> Vec<V>,
100 sublist_insert: fn(&mut S, usize, usize, Vec<V>),
102 variable_name: &'static str,
103 descriptor_index: usize,
104 indices: [usize; 2],
106 _phantom: PhantomData<fn() -> V>,
107}
108
109impl<S, V> Clone for SublistSwapMove<S, V> {
110 fn clone(&self) -> Self {
111 *self
112 }
113}
114
115impl<S, V> Copy for SublistSwapMove<S, V> {}
116
117impl<S, V: Debug> Debug for SublistSwapMove<S, V> {
118 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
119 f.debug_struct("SublistSwapMove")
120 .field("first_entity", &self.first_entity_index)
121 .field("first_range", &(self.first_start..self.first_end))
122 .field("second_entity", &self.second_entity_index)
123 .field("second_range", &(self.second_start..self.second_end))
124 .field("variable_name", &self.variable_name)
125 .finish()
126 }
127}
128
129impl<S, V> SublistSwapMove<S, V> {
130 #[allow(clippy::too_many_arguments)]
146 pub fn new(
147 first_entity_index: usize,
148 first_start: usize,
149 first_end: usize,
150 second_entity_index: usize,
151 second_start: usize,
152 second_end: usize,
153 list_len: fn(&S, usize) -> usize,
154 list_get: fn(&S, usize, usize) -> Option<V>,
155 sublist_remove: fn(&mut S, usize, usize, usize) -> Vec<V>,
156 sublist_insert: fn(&mut S, usize, usize, Vec<V>),
157 variable_name: &'static str,
158 descriptor_index: usize,
159 ) -> Self {
160 Self {
161 first_entity_index,
162 first_start,
163 first_end,
164 second_entity_index,
165 second_start,
166 second_end,
167 list_len,
168 list_get,
169 sublist_remove,
170 sublist_insert,
171 variable_name,
172 descriptor_index,
173 indices: [first_entity_index, second_entity_index],
174 _phantom: PhantomData,
175 }
176 }
177
178 pub fn first_entity_index(&self) -> usize {
179 self.first_entity_index
180 }
181
182 pub fn first_start(&self) -> usize {
183 self.first_start
184 }
185
186 pub fn first_end(&self) -> usize {
187 self.first_end
188 }
189
190 pub fn first_len(&self) -> usize {
191 self.first_end.saturating_sub(self.first_start)
192 }
193
194 pub fn second_entity_index(&self) -> usize {
195 self.second_entity_index
196 }
197
198 pub fn second_start(&self) -> usize {
199 self.second_start
200 }
201
202 pub fn second_end(&self) -> usize {
203 self.second_end
204 }
205
206 pub fn second_len(&self) -> usize {
207 self.second_end.saturating_sub(self.second_start)
208 }
209
210 pub fn is_intra_list(&self) -> bool {
211 self.first_entity_index == self.second_entity_index
212 }
213
214 fn access(&self) -> StaticListWindowAccess<S, V> {
215 StaticListWindowAccess {
216 list_len: self.list_len,
217 list_get: self.list_get,
218 sublist_remove: self.sublist_remove,
219 sublist_insert: self.sublist_insert,
220 variable_name: self.variable_name,
221 descriptor_index: self.descriptor_index,
222 }
223 }
224
225 fn coordinates(&self) -> SegmentSwapCoords {
226 SegmentSwapCoords::new(
227 self.first_entity_index,
228 self.first_start,
229 self.first_end,
230 self.second_entity_index,
231 self.second_start,
232 self.second_end,
233 )
234 }
235}
236
237impl<S, V> Move<S> for SublistSwapMove<S, V>
238where
239 S: PlanningSolution,
240 V: Clone + Send + Sync + Debug + 'static,
241{
242 type Undo = ();
243
244 fn is_doable<D: Director<S>>(&self, score_director: &D) -> bool {
245 sublist_swap_is_doable(&self.access(), self.coordinates(), score_director)
246 }
247
248 fn do_move<D: Director<S>>(&self, score_director: &mut D) -> Self::Undo {
249 sublist_swap_do_move(&self.access(), self.coordinates(), score_director);
250 }
251
252 fn undo_move<D: Director<S>>(&self, score_director: &mut D, (): Self::Undo) {
253 sublist_swap_undo_move(&self.access(), self.coordinates(), score_director);
254 }
255
256 fn descriptor_index(&self) -> usize {
257 self.descriptor_index
258 }
259
260 fn entity_indices(&self) -> &[usize] {
261 if self.is_intra_list() {
262 &self.indices[0..1]
263 } else {
264 &self.indices
265 }
266 }
267
268 fn variable_name(&self) -> &str {
269 self.variable_name
270 }
271
272 fn telemetry_label(&self) -> &'static str {
273 "sublist_swap"
274 }
275
276 fn tabu_signature<D: Director<S>>(&self, score_director: &D) -> Cow<'_, MoveTabuSignature> {
277 Cow::Owned(sublist_swap_tabu_signature(
278 &self.access(),
279 self.coordinates(),
280 score_director,
281 ))
282 }
283
284 fn candidate_trace_identity(&self) -> Option<crate::stats::CandidateTraceIdentity> {
285 Some(sublist_swap_candidate_trace_identity(
286 &self.access(),
287 self.coordinates(),
288 ))
289 }
290}