solverforge_solver/heuristic/move/
pillar_change.rs1use std::fmt::Debug;
13
14use smallvec::{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 PillarChangeMove<S, V> {
32 entity_indices: Vec<usize>,
33 descriptor_index: usize,
34 variable_name: &'static str,
35 to_value: Option<V>,
36 getter: fn(&S, usize, usize) -> Option<V>,
38 setter: fn(&mut S, usize, usize, Option<V>),
40 variable_index: usize,
41}
42
43impl<S, V: Clone> Clone for PillarChangeMove<S, V> {
44 fn clone(&self) -> Self {
45 Self {
46 entity_indices: self.entity_indices.clone(),
47 descriptor_index: self.descriptor_index,
48 variable_name: self.variable_name,
49 to_value: self.to_value.clone(),
50 getter: self.getter,
51 setter: self.setter,
52 variable_index: self.variable_index,
53 }
54 }
55}
56
57impl<S, V: Debug> Debug for PillarChangeMove<S, V> {
58 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59 f.debug_struct("PillarChangeMove")
60 .field("entity_indices", &self.entity_indices)
61 .field("descriptor_index", &self.descriptor_index)
62 .field("variable_index", &self.variable_index)
63 .field("variable_name", &self.variable_name)
64 .field("to_value", &self.to_value)
65 .finish()
66 }
67}
68
69impl<S, V> PillarChangeMove<S, V> {
70 pub fn new(
80 entity_indices: Vec<usize>,
81 to_value: Option<V>,
82 getter: fn(&S, usize, usize) -> Option<V>,
83 setter: fn(&mut S, usize, usize, Option<V>),
84 variable_index: usize,
85 variable_name: &'static str,
86 descriptor_index: usize,
87 ) -> Self {
88 Self {
89 entity_indices,
90 descriptor_index,
91 variable_name,
92 to_value,
93 getter,
94 setter,
95 variable_index,
96 }
97 }
98
99 pub fn pillar_size(&self) -> usize {
100 self.entity_indices.len()
101 }
102
103 pub fn to_value(&self) -> Option<&V> {
104 self.to_value.as_ref()
105 }
106}
107
108impl<S, V> Move<S> for PillarChangeMove<S, V>
109where
110 S: PlanningSolution,
111 V: Clone + PartialEq + Send + Sync + Debug + 'static,
112{
113 fn is_doable<D: Director<S>>(&self, score_director: &D) -> bool {
114 if self.entity_indices.is_empty() {
115 return false;
116 }
117
118 let count = score_director.entity_count(self.descriptor_index);
120 if let Some(&first_idx) = self.entity_indices.first() {
121 if count.is_none_or(|c| first_idx >= c) {
122 return false;
123 }
124
125 let current = (self.getter)(
127 score_director.working_solution(),
128 first_idx,
129 self.variable_index,
130 );
131
132 match (¤t, &self.to_value) {
133 (None, None) => false,
134 (Some(cur), Some(target)) => cur != target,
135 _ => true,
136 }
137 } else {
138 false
139 }
140 }
141
142 fn do_move<D: Director<S>>(&self, score_director: &mut D) {
143 let old_values: Vec<(usize, Option<V>)> = self
145 .entity_indices
146 .iter()
147 .map(|&idx| {
148 (
149 idx,
150 (self.getter)(score_director.working_solution(), idx, self.variable_index),
151 )
152 })
153 .collect();
154
155 for &idx in &self.entity_indices {
157 score_director.before_variable_changed(self.descriptor_index, idx);
158 }
159
160 for &idx in &self.entity_indices {
162 (self.setter)(
163 score_director.working_solution_mut(),
164 idx,
165 self.variable_index,
166 self.to_value.clone(),
167 );
168 }
169
170 for &idx in &self.entity_indices {
172 score_director.after_variable_changed(self.descriptor_index, idx);
173 }
174
175 let setter = self.setter;
177 let variable_index = self.variable_index;
178 score_director.register_undo(Box::new(move |s: &mut S| {
179 for (idx, old_value) in old_values {
180 setter(s, idx, variable_index, old_value);
181 }
182 }));
183 }
184
185 fn descriptor_index(&self) -> usize {
186 self.descriptor_index
187 }
188
189 fn entity_indices(&self) -> &[usize] {
190 &self.entity_indices
191 }
192
193 fn variable_name(&self) -> &str {
194 self.variable_name
195 }
196
197 fn tabu_signature<D: Director<S>>(&self, score_director: &D) -> MoveTabuSignature {
198 let from_value = self.entity_indices.first().and_then(|&idx| {
199 (self.getter)(score_director.working_solution(), idx, self.variable_index)
200 });
201 let from_id = encode_option_debug(from_value.as_ref());
202 let to_id = encode_option_debug(self.to_value.as_ref());
203 let variable_id = hash_str(self.variable_name);
204 let scope = MoveTabuScope::new(self.descriptor_index, self.variable_name);
205 let entity_ids: SmallVec<[u64; 2]> = self
206 .entity_indices
207 .iter()
208 .map(|&idx| encode_usize(idx))
209 .collect();
210 let entity_tokens: SmallVec<[ScopedEntityTabuToken; 2]> = entity_ids
211 .iter()
212 .copied()
213 .map(|entity_id| scope.entity_token(entity_id))
214 .collect();
215 let mut move_id = smallvec![
216 encode_usize(self.descriptor_index),
217 variable_id,
218 encode_usize(self.entity_indices.len()),
219 from_id,
220 to_id
221 ];
222 move_id.extend(entity_ids.iter().copied());
223
224 let mut undo_move_id = smallvec![
225 encode_usize(self.descriptor_index),
226 variable_id,
227 encode_usize(self.entity_indices.len()),
228 to_id,
229 from_id
230 ];
231 undo_move_id.extend(entity_ids.iter().copied());
232
233 MoveTabuSignature::new(scope, move_id, undo_move_id)
234 .with_entity_tokens(entity_tokens)
235 .with_destination_value_tokens([scope.value_token(to_id)])
236 }
237}