solverforge_solver/heuristic/move/
swap.rs1use std::fmt::Debug;
13
14use solverforge_core::domain::PlanningSolution;
15use solverforge_scoring::Director;
16
17use super::Move;
18
19pub struct SwapMove<S, V> {
51 left_entity_index: usize,
52 right_entity_index: usize,
53 getter: fn(&S, usize) -> Option<V>,
55 setter: fn(&mut S, usize, Option<V>),
57 variable_name: &'static str,
58 descriptor_index: usize,
59 indices: [usize; 2],
61}
62
63impl<S, V> Clone for SwapMove<S, V> {
64 fn clone(&self) -> Self {
65 *self
66 }
67}
68
69impl<S, V> Copy for SwapMove<S, V> {}
70
71impl<S, V: Debug> Debug for SwapMove<S, V> {
72 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
73 f.debug_struct("SwapMove")
74 .field("left_entity_index", &self.left_entity_index)
75 .field("right_entity_index", &self.right_entity_index)
76 .field("descriptor_index", &self.descriptor_index)
77 .field("variable_name", &self.variable_name)
78 .finish()
79 }
80}
81
82impl<S, V> SwapMove<S, V> {
83 pub fn new(
93 left_entity_index: usize,
94 right_entity_index: usize,
95 getter: fn(&S, usize) -> Option<V>,
96 setter: fn(&mut S, usize, Option<V>),
97 variable_name: &'static str,
98 descriptor_index: usize,
99 ) -> Self {
100 Self {
101 left_entity_index,
102 right_entity_index,
103 getter,
104 setter,
105 variable_name,
106 descriptor_index,
107 indices: [left_entity_index, right_entity_index],
108 }
109 }
110
111 pub fn left_entity_index(&self) -> usize {
112 self.left_entity_index
113 }
114
115 pub fn right_entity_index(&self) -> usize {
116 self.right_entity_index
117 }
118}
119
120impl<S, V> Move<S> for SwapMove<S, V>
121where
122 S: PlanningSolution,
123 V: Clone + PartialEq + Send + Sync + Debug + 'static,
124{
125 fn is_doable<D: Director<S>>(&self, score_director: &D) -> bool {
126 if self.left_entity_index == self.right_entity_index {
128 return false;
129 }
130
131 let left_val = (self.getter)(score_director.working_solution(), self.left_entity_index);
133 let right_val = (self.getter)(score_director.working_solution(), self.right_entity_index);
134
135 left_val != right_val
137 }
138
139 fn do_move<D: Director<S>>(&self, score_director: &mut D) {
140 let left_value = (self.getter)(score_director.working_solution(), self.left_entity_index);
142 let right_value = (self.getter)(score_director.working_solution(), self.right_entity_index);
143
144 score_director.before_variable_changed(self.descriptor_index, self.left_entity_index);
146 score_director.before_variable_changed(self.descriptor_index, self.right_entity_index);
147
148 (self.setter)(
150 score_director.working_solution_mut(),
151 self.left_entity_index,
152 right_value.clone(),
153 );
154 (self.setter)(
155 score_director.working_solution_mut(),
156 self.right_entity_index,
157 left_value.clone(),
158 );
159
160 score_director.after_variable_changed(self.descriptor_index, self.left_entity_index);
162 score_director.after_variable_changed(self.descriptor_index, self.right_entity_index);
163
164 let setter = self.setter;
166 let left_idx = self.left_entity_index;
167 let right_idx = self.right_entity_index;
168 score_director.register_undo(Box::new(move |s: &mut S| {
169 setter(s, left_idx, left_value);
171 setter(s, right_idx, right_value);
172 }));
173 }
174
175 fn descriptor_index(&self) -> usize {
176 self.descriptor_index
177 }
178
179 fn entity_indices(&self) -> &[usize] {
180 &self.indices
181 }
182
183 fn variable_name(&self) -> &str {
184 self.variable_name
185 }
186}