Skip to main content

solverforge_solver/heuristic/move/
pillar_change.rs

1/* PillarChangeMove - assigns a value to all entities in a pillar.
2
3A pillar is a group of entities that share the same variable value.
4This move changes all of them to a new value atomically.
5
6# Zero-Erasure Design
7
8PillarChangeMove uses concrete function pointers instead of `dyn Any` for complete
9compile-time type safety. No runtime type checks or downcasting.
10*/
11
12use 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
23/// A move that assigns a value to all entities in a pillar.
24///
25/// Stores entity indices and concrete function pointers for zero-erasure access.
26/// `do_move` returns the previous pillar values as typed undo data.
27///
28/// # Type Parameters
29/// * `S` - The planning solution type
30/// * `V` - The variable value type
31pub struct PillarChangeMove<S, V> {
32    entity_indices: Vec<usize>,
33    descriptor_index: usize,
34    variable_name: &'static str,
35    to_value: Option<V>,
36    // Concrete getter function pointer - zero erasure.
37    getter: fn(&S, usize, usize) -> Option<V>,
38    // Concrete setter function pointer - zero erasure.
39    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    /// Creates a new pillar change move with concrete function pointers.
71    ///
72    /// # Arguments
73    /// * `entity_indices` - Indices of entities in the pillar
74    /// * `to_value` - The new value to assign to all entities
75    /// * `getter` - Concrete getter function pointer
76    /// * `setter` - Concrete setter function pointer
77    /// * `variable_name` - Name of the variable being changed
78    /// * `descriptor_index` - Index in the entity descriptor
79    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    type Undo = Vec<(usize, Option<V>)>;
114
115    fn is_doable<D: Director<S>>(&self, score_director: &D) -> bool {
116        if self.entity_indices.is_empty() {
117            return false;
118        }
119
120        // Check first entity exists
121        let count = score_director.entity_count(self.descriptor_index);
122        if let Some(&first_idx) = self.entity_indices.first() {
123            if count.is_none_or(|c| first_idx >= c) {
124                return false;
125            }
126
127            // Get current value using concrete getter - zero erasure
128            let current = (self.getter)(
129                score_director.working_solution(),
130                first_idx,
131                self.variable_index,
132            );
133
134            match (&current, &self.to_value) {
135                (None, None) => false,
136                (Some(cur), Some(target)) => cur != target,
137                _ => true,
138            }
139        } else {
140            false
141        }
142    }
143
144    fn do_move<D: Director<S>>(&self, score_director: &mut D) -> Self::Undo {
145        // Capture old values using concrete getter - zero erasure
146        let old_values: Vec<(usize, Option<V>)> = self
147            .entity_indices
148            .iter()
149            .map(|&idx| {
150                (
151                    idx,
152                    (self.getter)(score_director.working_solution(), idx, self.variable_index),
153                )
154            })
155            .collect();
156
157        // Notify before changes for all entities
158        for &idx in &self.entity_indices {
159            score_director.before_variable_changed(self.descriptor_index, idx);
160        }
161
162        // Apply new value to all entities using concrete setter - zero erasure
163        for &idx in &self.entity_indices {
164            (self.setter)(
165                score_director.working_solution_mut(),
166                idx,
167                self.variable_index,
168                self.to_value.clone(),
169            );
170        }
171
172        // Notify after changes
173        for &idx in &self.entity_indices {
174            score_director.after_variable_changed(self.descriptor_index, idx);
175        }
176
177        old_values
178    }
179
180    fn undo_move<D: Director<S>>(&self, score_director: &mut D, undo: Self::Undo) {
181        for (idx, _) in &undo {
182            score_director.before_variable_changed(self.descriptor_index, *idx);
183        }
184        for (idx, old_value) in undo {
185            (self.setter)(
186                score_director.working_solution_mut(),
187                idx,
188                self.variable_index,
189                old_value,
190            );
191        }
192        for &idx in &self.entity_indices {
193            score_director.after_variable_changed(self.descriptor_index, idx);
194        }
195    }
196
197    fn descriptor_index(&self) -> usize {
198        self.descriptor_index
199    }
200
201    fn entity_indices(&self) -> &[usize] {
202        &self.entity_indices
203    }
204
205    fn variable_name(&self) -> &str {
206        self.variable_name
207    }
208
209    fn tabu_signature<D: Director<S>>(&self, score_director: &D) -> MoveTabuSignature {
210        let from_value = self.entity_indices.first().and_then(|&idx| {
211            (self.getter)(score_director.working_solution(), idx, self.variable_index)
212        });
213        let from_id = encode_option_debug(from_value.as_ref());
214        let to_id = encode_option_debug(self.to_value.as_ref());
215        let variable_id = hash_str(self.variable_name);
216        let scope = MoveTabuScope::new(self.descriptor_index, self.variable_name);
217        let entity_ids: SmallVec<[u64; 2]> = self
218            .entity_indices
219            .iter()
220            .map(|&idx| encode_usize(idx))
221            .collect();
222        let entity_tokens: SmallVec<[ScopedEntityTabuToken; 2]> = entity_ids
223            .iter()
224            .copied()
225            .map(|entity_id| scope.entity_token(entity_id))
226            .collect();
227        let mut move_id = smallvec![
228            encode_usize(self.descriptor_index),
229            variable_id,
230            encode_usize(self.entity_indices.len()),
231            from_id,
232            to_id
233        ];
234        move_id.extend(entity_ids.iter().copied());
235
236        let mut undo_move_id = smallvec![
237            encode_usize(self.descriptor_index),
238            variable_id,
239            encode_usize(self.entity_indices.len()),
240            to_id,
241            from_id
242        ];
243        undo_move_id.extend(entity_ids.iter().copied());
244
245        MoveTabuSignature::new(scope, move_id, undo_move_id)
246            .with_entity_tokens(entity_tokens)
247            .with_destination_value_tokens([scope.value_token(to_id)])
248    }
249}