solverforge_solver/heuristic/move/
list_swap.rs1use std::fmt::Debug;
12
13use solverforge_core::domain::PlanningSolution;
14use solverforge_scoring::Director;
15
16use super::Move;
17
18pub struct ListSwapMove<S, V> {
66 first_entity_index: usize,
68 first_position: usize,
70 second_entity_index: usize,
72 second_position: usize,
74 list_len: fn(&S, usize) -> usize,
75 list_get: fn(&S, usize, usize) -> Option<V>,
76 list_set: fn(&mut S, usize, usize, V),
78 variable_name: &'static str,
79 descriptor_index: usize,
80 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 #[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 let first_len = (self.list_len)(solution, self.first_entity_index);
175 if self.first_position >= first_len {
176 return false;
177 }
178
179 let second_len = (self.list_len)(solution, self.second_entity_index);
181 if self.second_position >= second_len {
182 return false;
183 }
184
185 if self.is_intra_list() && self.first_position == self.second_position {
187 return false;
188 }
189
190 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 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 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 (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 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 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 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}