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
//! Provides `Booster` trait.
use crate::{
WeakLearner,
CombinedHypothesis,
};
/// State of the boosting.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum State {
/// Terminate the boosting process
Terminate,
/// Continue the boosting process
Continue,
}
/// The trait [`Booster`](Booster) defines the standard framework of Boosting.
///
/// You need to implement [`Booster::preprocess`](Booster::preprocess),
/// [`Booster::boost`](Booster::boost),
/// and [`Booster::postprocess`](Booster::postprocess)
/// to write a new boosting algorithm.
pub trait Booster<F> {
/// A main function that runs boosting algorithm.
fn run<W>(
&mut self,
weak_learner: &W,
) -> CombinedHypothesis<F>
where W: WeakLearner<Hypothesis = F>
{
self.preprocess(weak_learner);
for it in 1.. {
let state = self.boost(weak_learner, it);
if state == State::Terminate {
break;
}
}
self.postprocess(weak_learner)
}
/// Pre-processing for `self`.
/// As you can see in [`Booster::run`](Booster::run),
/// This method is called before the boosting process.
fn preprocess<W>(
&mut self,
weak_learner: &W,
)
where W: WeakLearner<Hypothesis = F>;
/// Boosting step per iteration.
/// This method returns
/// `State::Continue` if the stopping criterion is satisfied,
/// `State::Terminate` otherwise.
/// See [`State`](State)
fn boost<W>(
&mut self,
weak_learner: &W,
iteration: usize,
) -> State
where W: WeakLearner<Hypothesis = F>;
/// Post-processing.
/// This method returns a [`CombinedHypothesis<F>`](CombinedHypothesis).
fn postprocess<W>(
&mut self,
weak_learner: &W,
) -> CombinedHypothesis<F>
where W: WeakLearner<Hypothesis = F>;
}