solverforge_solver/heuristic/move/
change.rs1use std::fmt::Debug;
13
14use solverforge_core::domain::PlanningSolution;
15use solverforge_scoring::Director;
16
17use super::Move;
18
19pub struct ChangeMove<S, V> {
28 entity_index: usize,
29 to_value: Option<V>,
30 getter: fn(&S, usize) -> Option<V>,
31 setter: fn(&mut S, usize, Option<V>),
32 variable_name: &'static str,
33 descriptor_index: usize,
34}
35
36impl<S, V: Clone> Clone for ChangeMove<S, V> {
37 fn clone(&self) -> Self {
38 Self {
39 entity_index: self.entity_index,
40 to_value: self.to_value.clone(),
41 getter: self.getter,
42 setter: self.setter,
43 variable_name: self.variable_name,
44 descriptor_index: self.descriptor_index,
45 }
46 }
47}
48
49impl<S, V: Copy> Copy for ChangeMove<S, V> {}
50
51impl<S, V: Debug> Debug for ChangeMove<S, V> {
52 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53 f.debug_struct("ChangeMove")
54 .field("entity_index", &self.entity_index)
55 .field("descriptor_index", &self.descriptor_index)
56 .field("variable_name", &self.variable_name)
57 .field("to_value", &self.to_value)
58 .finish()
59 }
60}
61
62impl<S, V> ChangeMove<S, V> {
63 pub fn new(
73 entity_index: usize,
74 to_value: Option<V>,
75 getter: fn(&S, usize) -> Option<V>,
76 setter: fn(&mut S, usize, Option<V>),
77 variable_name: &'static str,
78 descriptor_index: usize,
79 ) -> Self {
80 Self {
81 entity_index,
82 to_value,
83 getter,
84 setter,
85 variable_name,
86 descriptor_index,
87 }
88 }
89
90 pub fn entity_index(&self) -> usize {
91 self.entity_index
92 }
93
94 pub fn to_value(&self) -> Option<&V> {
95 self.to_value.as_ref()
96 }
97
98 pub fn getter(&self) -> fn(&S, usize) -> Option<V> {
99 self.getter
100 }
101
102 pub fn setter(&self) -> fn(&mut S, usize, Option<V>) {
103 self.setter
104 }
105}
106
107impl<S, V> Move<S> for ChangeMove<S, V>
108where
109 S: PlanningSolution,
110 V: Clone + PartialEq + Send + Sync + Debug + 'static,
111{
112 fn is_doable<D: Director<S>>(&self, score_director: &D) -> bool {
113 let current = (self.getter)(score_director.working_solution(), self.entity_index);
115
116 match (¤t, &self.to_value) {
118 (None, None) => false, (Some(cur), Some(target)) => cur != target, _ => true, }
122 }
123
124 fn do_move<D: Director<S>>(&self, score_director: &mut D) {
125 let old_value = (self.getter)(score_director.working_solution(), self.entity_index);
127
128 score_director.before_variable_changed(self.descriptor_index, self.entity_index);
130
131 (self.setter)(
133 score_director.working_solution_mut(),
134 self.entity_index,
135 self.to_value.clone(),
136 );
137
138 score_director.after_variable_changed(self.descriptor_index, self.entity_index);
140
141 let setter = self.setter;
143 let idx = self.entity_index;
144 score_director.register_undo(Box::new(move |s: &mut S| {
145 setter(s, idx, old_value);
146 }));
147 }
148
149 fn descriptor_index(&self) -> usize {
150 self.descriptor_index
151 }
152
153 fn entity_indices(&self) -> &[usize] {
154 std::slice::from_ref(&self.entity_index)
155 }
156
157 fn variable_name(&self) -> &str {
158 self.variable_name
159 }
160}