solverforge_solver/heuristic/move/
list_swap.rs1use std::fmt::Debug;
12
13use solverforge_core::domain::PlanningSolution;
14use solverforge_scoring::Director;
15
16use super::list_kernel::{
17 swap_candidate_trace_identity, swap_do_move, swap_is_doable, swap_tabu_signature,
18 StaticListSwapAccess, SwapCoordinates,
19};
20use super::{Move, MoveTabuSignature};
21
22pub struct ListSwapMove<S, V> {
70 first_entity_index: usize,
72 first_position: usize,
74 second_entity_index: usize,
76 second_position: usize,
78 list_len: fn(&S, usize) -> usize,
79 list_get: fn(&S, usize, usize) -> Option<V>,
80 list_set: fn(&mut S, usize, usize, V),
82 variable_name: &'static str,
83 descriptor_index: usize,
84 indices: [usize; 2],
86}
87
88impl<S, V> Clone for ListSwapMove<S, V> {
89 fn clone(&self) -> Self {
90 *self
91 }
92}
93
94impl<S, V> Copy for ListSwapMove<S, V> {}
95
96impl<S, V: Debug> Debug for ListSwapMove<S, V> {
97 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
98 f.debug_struct("ListSwapMove")
99 .field("first_entity", &self.first_entity_index)
100 .field("first_position", &self.first_position)
101 .field("second_entity", &self.second_entity_index)
102 .field("second_position", &self.second_position)
103 .field("variable_name", &self.variable_name)
104 .finish()
105 }
106}
107
108impl<S, V> ListSwapMove<S, V> {
109 #[allow(clippy::too_many_arguments)]
123 pub fn new(
124 first_entity_index: usize,
125 first_position: usize,
126 second_entity_index: usize,
127 second_position: usize,
128 list_len: fn(&S, usize) -> usize,
129 list_get: fn(&S, usize, usize) -> Option<V>,
130 list_set: fn(&mut S, usize, usize, V),
131 variable_name: &'static str,
132 descriptor_index: usize,
133 ) -> Self {
134 Self {
135 first_entity_index,
136 first_position,
137 second_entity_index,
138 second_position,
139 list_len,
140 list_get,
141 list_set,
142 variable_name,
143 descriptor_index,
144 indices: [first_entity_index, second_entity_index],
145 }
146 }
147
148 pub fn first_entity_index(&self) -> usize {
149 self.first_entity_index
150 }
151
152 pub fn first_position(&self) -> usize {
153 self.first_position
154 }
155
156 pub fn second_entity_index(&self) -> usize {
157 self.second_entity_index
158 }
159
160 pub fn second_position(&self) -> usize {
161 self.second_position
162 }
163
164 pub fn is_intra_list(&self) -> bool {
165 self.first_entity_index == self.second_entity_index
166 }
167
168 fn access(&self) -> StaticListSwapAccess<S, V> {
169 StaticListSwapAccess {
170 list_len: self.list_len,
171 list_get: self.list_get,
172 list_set: self.list_set,
173 variable_name: self.variable_name,
174 descriptor_index: self.descriptor_index,
175 }
176 }
177
178 fn coordinates(&self) -> SwapCoordinates {
179 SwapCoordinates {
180 first_entity: self.first_entity_index,
181 first_position: self.first_position,
182 second_entity: self.second_entity_index,
183 second_position: self.second_position,
184 }
185 }
186}
187
188impl<S, V> Move<S> for ListSwapMove<S, V>
189where
190 S: PlanningSolution,
191 V: Clone + PartialEq + Send + Sync + Debug + 'static,
192{
193 type Undo = ();
194
195 fn is_doable<D: Director<S>>(&self, score_director: &D) -> bool {
196 swap_is_doable(&self.access(), self.coordinates(), score_director)
197 }
198
199 fn do_move<D: Director<S>>(&self, score_director: &mut D) -> Self::Undo {
200 swap_do_move(&self.access(), self.coordinates(), score_director);
201 }
202
203 fn undo_move<D: Director<S>>(&self, score_director: &mut D, (): Self::Undo) {
204 swap_do_move(&self.access(), self.coordinates(), score_director);
205 }
206
207 fn descriptor_index(&self) -> usize {
208 self.descriptor_index
209 }
210
211 fn entity_indices(&self) -> &[usize] {
212 if self.is_intra_list() {
213 &self.indices[0..1]
214 } else {
215 &self.indices
216 }
217 }
218
219 fn variable_name(&self) -> &str {
220 self.variable_name
221 }
222
223 fn telemetry_label(&self) -> &'static str {
224 "list_swap"
225 }
226
227 fn tabu_signature<D: Director<S>>(&self, score_director: &D) -> MoveTabuSignature {
228 swap_tabu_signature(&self.access(), self.coordinates(), score_director)
229 }
230
231 fn candidate_trace_identity(&self) -> Option<crate::stats::CandidateTraceIdentity> {
232 Some(swap_candidate_trace_identity(
233 &self.access(),
234 self.coordinates(),
235 ))
236 }
237}