solverforge_solver/heuristic/move/
ruin.rs1use std::fmt::Debug;
13
14use smallvec::SmallVec;
15use solverforge_core::domain::PlanningSolution;
16use solverforge_scoring::Director;
17
18use super::metadata::{
19 encode_option_debug, encode_usize, hash_str, MoveTabuScope, ScopedEntityTabuToken,
20};
21use super::{Move, MoveTabuSignature};
22
23pub struct RuinMove<S, V> {
66 entity_indices: SmallVec<[usize; 8]>,
68 getter: fn(&S, usize, usize) -> Option<V>,
69 setter: fn(&mut S, usize, usize, Option<V>),
71 variable_index: usize,
72 variable_name: &'static str,
73 descriptor_index: usize,
74}
75
76impl<S, V> Clone for RuinMove<S, V> {
77 fn clone(&self) -> Self {
78 Self {
79 entity_indices: self.entity_indices.clone(),
80 getter: self.getter,
81 setter: self.setter,
82 variable_index: self.variable_index,
83 variable_name: self.variable_name,
84 descriptor_index: self.descriptor_index,
85 }
86 }
87}
88
89impl<S, V: Debug> Debug for RuinMove<S, V> {
90 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
91 f.debug_struct("RuinMove")
92 .field("entities", &self.entity_indices.as_slice())
93 .field("variable_index", &self.variable_index)
94 .field("variable_name", &self.variable_name)
95 .finish()
96 }
97}
98
99impl<S, V> RuinMove<S, V> {
100 pub fn new(
109 entity_indices: &[usize],
110 getter: fn(&S, usize, usize) -> Option<V>,
111 setter: fn(&mut S, usize, usize, Option<V>),
112 variable_index: usize,
113 variable_name: &'static str,
114 descriptor_index: usize,
115 ) -> Self {
116 Self {
117 entity_indices: SmallVec::from_slice(entity_indices),
118 getter,
119 setter,
120 variable_index,
121 variable_name,
122 descriptor_index,
123 }
124 }
125
126 pub fn entity_indices_slice(&self) -> &[usize] {
127 &self.entity_indices
128 }
129
130 pub fn ruin_count(&self) -> usize {
131 self.entity_indices.len()
132 }
133}
134
135impl<S, V> Move<S> for RuinMove<S, V>
136where
137 S: PlanningSolution,
138 V: Clone + Send + Sync + Debug + 'static,
139{
140 fn is_doable<D: Director<S>>(&self, score_director: &D) -> bool {
141 let solution = score_director.working_solution();
143 self.entity_indices
144 .iter()
145 .any(|&idx| (self.getter)(solution, idx, self.variable_index).is_some())
146 }
147
148 fn do_move<D: Director<S>>(&self, score_director: &mut D) {
149 let getter = self.getter;
150 let setter = self.setter;
151 let descriptor = self.descriptor_index;
152 let variable_index = self.variable_index;
153
154 let old_values: SmallVec<[(usize, Option<V>); 8]> = self
156 .entity_indices
157 .iter()
158 .map(|&idx| {
159 let old = getter(score_director.working_solution(), idx, variable_index);
160 (idx, old)
161 })
162 .collect();
163
164 for &idx in &self.entity_indices {
166 score_director.before_variable_changed(descriptor, idx);
167 setter(
168 score_director.working_solution_mut(),
169 idx,
170 variable_index,
171 None,
172 );
173 score_director.after_variable_changed(descriptor, idx);
174 }
175
176 score_director.register_undo(Box::new(move |s: &mut S| {
178 for (idx, old_value) in old_values {
179 setter(s, idx, variable_index, old_value);
180 }
181 }));
182 }
183
184 fn descriptor_index(&self) -> usize {
185 self.descriptor_index
186 }
187
188 fn entity_indices(&self) -> &[usize] {
189 &self.entity_indices
190 }
191
192 fn variable_name(&self) -> &str {
193 self.variable_name
194 }
195
196 fn tabu_signature<D: Director<S>>(&self, score_director: &D) -> MoveTabuSignature {
197 let scope = MoveTabuScope::new(self.descriptor_index, self.variable_name);
198 let entity_ids: SmallVec<[u64; 2]> = self
199 .entity_indices
200 .iter()
201 .map(|&idx| encode_usize(idx))
202 .collect();
203 let entity_tokens: SmallVec<[ScopedEntityTabuToken; 2]> = entity_ids
204 .iter()
205 .copied()
206 .map(|entity_id| scope.entity_token(entity_id))
207 .collect();
208 let mut value_ids: SmallVec<[u64; 2]> = SmallVec::new();
209 for &idx in &self.entity_indices {
210 let value = (self.getter)(score_director.working_solution(), idx, self.variable_index);
211 value_ids.push(encode_option_debug(value.as_ref()));
212 }
213 let variable_id = hash_str(self.variable_name);
214 let mut move_id = SmallVec::<[u64; 8]>::from_slice(&[
215 encode_usize(self.descriptor_index),
216 variable_id,
217 encode_usize(self.entity_indices.len()),
218 ]);
219 move_id.extend(entity_ids.iter().copied());
220 move_id.extend(value_ids.iter().copied());
221
222 MoveTabuSignature::new(scope, move_id.clone(), move_id).with_entity_tokens(entity_tokens)
223 }
224}