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
use super::{EpsilonGreedyOptimizer, LocalSearchOptimizer};
use crate::{Duration, OptModel, callback::OptCallbackFn};
/// Optimizer that implements random search algorithm
#[derive(Clone, Copy)]
pub struct RandomSearchOptimizer {
patience: usize,
}
impl RandomSearchOptimizer {
/// - `patience` : the optimizer will give up
/// if there is no improvement of the score after this number of iterations
pub fn new(patience: usize) -> Self {
Self { patience }
}
}
impl<M: OptModel> LocalSearchOptimizer<M> for RandomSearchOptimizer {
/// 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 optimizer = EpsilonGreedyOptimizer::new(self.patience, 1, usize::MAX, 1.0);
optimizer.optimize(
model,
initial_solution,
initial_score,
n_iter,
time_limit,
callback,
)
}
}