use crate::Sample;
use super::Logger;
const DEFAULT_ROUND: usize = 100;
const DEFAULT_TIMELIMIT_MILLIS: u128 = u128::MAX;
pub struct LoggerBuilder<'a, B, W, F, G> {
booster: Option<B>,
weak_learner: Option<W>,
objective_func: Option<F>,
loss_func: Option<G>,
train: Option<&'a Sample>,
test: Option<&'a Sample>,
time_limit: u128,
round: usize,
}
impl<'a, B, W, F, G> LoggerBuilder<'a, B, W, F, G> {
pub fn new() -> Self {
Self {
booster: None,
weak_learner: None,
objective_func: None,
loss_func: None,
train: None,
test: None,
time_limit: DEFAULT_TIMELIMIT_MILLIS,
round: DEFAULT_ROUND,
}
}
pub fn booster(mut self, booster: B) -> Self {
self.booster = Some(booster);
self
}
pub fn weak_learner(mut self, weak_learner: W) -> Self {
self.weak_learner = Some(weak_learner);
self
}
pub fn objective_function(mut self, objective_func: F) -> Self {
self.objective_func = Some(objective_func);
self
}
pub fn loss_function(mut self, loss_func: G) -> Self {
self.loss_func = Some(loss_func);
self
}
pub fn train_sample(mut self, train: &'a Sample) -> Self {
self.train = Some(train);
self
}
pub fn test_sample(mut self, test: &'a Sample) -> Self {
self.test = Some(test);
self
}
#[inline(always)]
pub fn time_limit_as_millis(mut self, time_limit: u128) -> Self {
self.time_limit = time_limit;
self
}
#[inline(always)]
pub fn time_limit_as_secs(mut self, time_limit: u64) -> Self {
self.time_limit = (time_limit as u128).checked_mul(1_000_u128)
.expect("The time limit (ms) cannot be represented as u128");
self
}
#[inline(always)]
pub fn time_limit_as_mins(mut self, time_limit: u64) -> Self {
self.time_limit = (time_limit as u128).checked_mul(60_u128)
.expect("The time limit (s) cannot be represented as u128")
.checked_mul(1_000u128)
.expect("The time limit (ms) cannot be represented as u128");
self
}
#[inline(always)]
pub fn print_every(mut self, round: usize) -> Self {
self.round = round;
self
}
pub fn build(self) -> Logger<'a, B, W, F, G> {
let booster = self.booster
.expect("Boosting algorithm is not specified");
let weak_learner = self.weak_learner
.expect("Weak learner is not specified");
let objective_func = self.objective_func
.expect("Objective function is not specified");
let loss_func = self.loss_func
.expect("Loss function is not specified");
let train = self.train
.expect("Training sample is not specified");
let test = self.test
.expect("Test sample is not specified");
let time_limit = self.time_limit;
let round = self.round;
Logger {
booster,
weak_learner,
objective_func,
loss_func,
train,
test,
time_limit,
round,
}
}
}