solverforge_solver/phase/localsearch/acceptor/
value_tabu.rs1use std::fmt::Debug;
4
5use solverforge_core::domain::PlanningSolution;
6
7use super::Acceptor;
8
9pub struct ValueTabuAcceptor {
24 value_tabu_size: usize,
26 value_tabu_list: Vec<u64>,
28 current_step_values: Vec<u64>,
30}
31
32impl Debug for ValueTabuAcceptor {
33 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34 f.debug_struct("ValueTabuAcceptor")
35 .field("value_tabu_size", &self.value_tabu_size)
36 .field("tabu_list_len", &self.value_tabu_list.len())
37 .finish()
38 }
39}
40
41impl Clone for ValueTabuAcceptor {
42 fn clone(&self) -> Self {
43 Self {
44 value_tabu_size: self.value_tabu_size,
45 value_tabu_list: self.value_tabu_list.clone(),
46 current_step_values: self.current_step_values.clone(),
47 }
48 }
49}
50
51impl ValueTabuAcceptor {
52 pub fn new(value_tabu_size: usize) -> Self {
61 assert!(value_tabu_size > 0, "value_tabu_size must be > 0, got 0");
62 Self {
63 value_tabu_size,
64 value_tabu_list: Vec::with_capacity(value_tabu_size),
65 current_step_values: Vec::new(),
66 }
67 }
68
69 pub fn record_value_assignment(&mut self, value_hash: u64) {
73 self.current_step_values.push(value_hash);
74 }
75
76 pub fn is_value_tabu(&self, value_hash: u64) -> bool {
77 self.value_tabu_list.contains(&value_hash)
78 }
79}
80
81impl Default for ValueTabuAcceptor {
82 fn default() -> Self {
83 Self::new(7)
84 }
85}
86
87impl<S: PlanningSolution> Acceptor<S> for ValueTabuAcceptor {
88 fn is_accepted(&mut self, last_step_score: &S::Score, move_score: &S::Score) -> bool {
89 if move_score > last_step_score {
91 return true;
92 }
93
94 if move_score >= last_step_score {
96 return true;
97 }
98
99 false
100 }
101
102 fn phase_started(&mut self, _initial_score: &S::Score) {
103 self.value_tabu_list.clear();
104 self.current_step_values.clear();
105 }
106
107 fn phase_ended(&mut self) {
108 self.value_tabu_list.clear();
109 }
110
111 fn step_started(&mut self) {
112 self.current_step_values.clear();
113 }
114
115 fn step_ended(&mut self, _step_score: &S::Score) {
116 for value_hash in &self.current_step_values {
118 if self.value_tabu_list.len() >= self.value_tabu_size {
119 self.value_tabu_list.remove(0);
120 }
121 self.value_tabu_list.push(*value_hash);
122 }
123 self.current_step_values.clear();
124 }
125}
126
127#[cfg(test)]
128#[path = "value_tabu_tests.rs"]
129mod tests;