1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
use std::{cell::RefCell, marker::PhantomData, rc::Rc};
use rayon::prelude::*;
use super::LocalSearchOptimizer;
use crate::{
Duration, Instant, OptModel,
callback::{OptCallbackFn, OptProgress},
counter::AcceptanceCounter,
};
/// Trait that a tabu list must satisfies
pub trait TabuList: Default {
/// The type of item stored in the tabu list.
type Item: Clone + Sync + Send;
/// Set the length of the tabu list
fn set_size(&mut self, n: usize);
/// Check if the item is a Tabu
fn contains(&self, transition: &Self::Item) -> bool;
/// Append the item to the list
fn append(&mut self, transition: Self::Item);
}
/// Optimizer that implements the tabu search algorithm
pub struct TabuSearchOptimizer<T: TabuList> {
patience: usize,
n_trials: usize,
return_iter: usize,
default_tabu_size: usize,
phantom: PhantomData<T>,
}
fn find_accepted_solution<M, L>(
samples: Vec<(M::SolutionType, M::TransitionType, M::ScoreType)>,
tabu_list: &L,
best_score: M::ScoreType,
) -> Option<(M::SolutionType, M::TransitionType, M::ScoreType)>
where
M: OptModel,
L: TabuList<Item = M::TransitionType>,
{
for (solution, transition, score) in samples.into_iter() {
#[allow(unused_parens)]
if (
// Aspiration Criterion
score < best_score ||
// Not Tabu
!tabu_list.contains( &transition)
) {
return Some((solution, transition, score));
}
}
None
}
impl<T: TabuList> TabuSearchOptimizer<T> {
/// Constructor of TabuSearchOptimizer
///
/// - `patience` : the optimizer will give up
/// if there is no improvement of the score after this number of iterations
/// - `n_trials` : number of trial solutions to generate and evaluate at each iteration
/// - `return_iter` : returns to the current best solution if there is no improvement after this number of iterations.
pub fn new(
patience: usize,
n_trials: usize,
return_iter: usize,
default_tabu_size: usize,
) -> Self {
Self {
patience,
n_trials,
return_iter,
default_tabu_size,
phantom: PhantomData,
}
}
}
impl<T> TabuSearchOptimizer<T>
where
T: TabuList,
{
#[allow(clippy::too_many_arguments)]
/// Start optimization
///
/// - `model` : the model to optimize
/// - `initial_solution` : the initial solution to start optimization
/// - `initial_score` : the initial score of the initial solution
/// - `n_iter`: maximum iterations
/// - `time_limit`: maximum iteration time
/// - `callback` : callback function that will be invoked at the end of each iteration
/// - `tabu_list` : initial tabu list
fn optimize_with_tabu_list<M: OptModel<TransitionType = T::Item>>(
&self,
model: &M,
initial_solution: M::SolutionType,
initial_score: M::ScoreType,
n_iter: usize,
time_limit: Duration,
callback: &mut dyn OptCallbackFn<M::SolutionType, M::ScoreType>,
mut tabu_list: T,
) -> (M::SolutionType, M::ScoreType, T) {
let start_time = Instant::now();
let mut current_solution = initial_solution;
let mut current_score = initial_score;
let best_solution = Rc::new(RefCell::new(current_solution.clone()));
let mut best_score = current_score;
let mut return_stagnation_counter = 0;
let mut patience_stagnation_counter = 0;
let mut acceptance_counter = AcceptanceCounter::new(100);
for it in 0..n_iter {
let duration = Instant::now().duration_since(start_time);
if duration > time_limit {
break;
}
let mut samples = vec![];
(0..self.n_trials)
.into_par_iter()
.map(|_| {
let mut rng = rand::rng();
let (solution, transitions, score) = model.generate_trial_solution(
current_solution.clone(),
current_score,
&mut rng,
);
(solution, transitions, score)
})
.collect_into_vec(&mut samples);
samples.sort_unstable_by_key(|(_, _, score)| *score);
let res = find_accepted_solution::<M, T>(samples, &tabu_list, best_score);
let accepted = res.is_some();
acceptance_counter.enqueue(accepted);
if accepted {
let (solution, trans, score) = res.unwrap();
// Accepted
// 2. Update best solution and score
if score < best_score {
best_score = score;
best_solution.replace(solution.clone());
return_stagnation_counter = 0;
patience_stagnation_counter = 0;
} else {
return_stagnation_counter += 1;
patience_stagnation_counter += 1;
}
// 3. Update accepted counter and transitions (no transitions here)
// 4. Update current solution and score
current_score = score;
current_solution = solution;
// 7. Update algorithm-specific state
tabu_list.append(trans);
} else {
// rejected
// If no accepted, increment stagnation
return_stagnation_counter += 1;
patience_stagnation_counter += 1;
}
// 5. Check and handle return to best
if return_stagnation_counter == self.return_iter {
current_solution = best_solution.borrow().clone();
current_score = best_score;
return_stagnation_counter = 0;
}
// 6. Check patience
if patience_stagnation_counter == self.patience {
break;
}
// 8. Invoke callback
let progress = OptProgress::new(
it,
acceptance_counter.acceptance_ratio(),
best_solution.clone(),
best_score,
);
callback(progress);
}
let best_solution = (*best_solution.borrow()).clone();
(best_solution, best_score, tabu_list)
}
}
impl<T: TabuList, M: OptModel<TransitionType = T::Item>> LocalSearchOptimizer<M>
for TabuSearchOptimizer<T>
{
/// Start optimization
///
/// - `model`: the model to optimize
/// - `initial_solution`: the initial solution to start optimization
/// - `initial_score`: the initial score of the initial solution
/// - `n_iter`: maximum iterations
/// - `time_limit`: maximum iteration time
/// - `callback`: callback function that will be invoked at the end of each iteration
fn optimize(
&self,
model: &M,
initial_solution: M::SolutionType,
initial_score: M::ScoreType,
n_iter: usize,
time_limit: Duration,
callback: &mut dyn OptCallbackFn<M::SolutionType, M::ScoreType>,
) -> (M::SolutionType, M::ScoreType) {
let mut tabu_list = T::default();
tabu_list.set_size(self.default_tabu_size);
let (solution, score, _) = self.optimize_with_tabu_list(
model,
initial_solution,
initial_score,
n_iter,
time_limit,
callback,
tabu_list,
);
(solution, score)
}
}