Skip to main content

dtrees_rs/algorithms/
mod.rs

1//! The tree learners: optimal ([`optimal`]: DL8.5 and depth-2 solvers) and
2//! greedy with lookahead ([`greedy`]: LGDT).
3
4use crate::algorithms::common::types::FitError;
5use crate::algorithms::common::utils::find_valid_split_attributes;
6use crate::cover::Cover;
7use crate::tree::Tree;
8
9pub mod common;
10pub mod greedy;
11pub mod optimal;
12
13/// A tree learner.
14pub trait TreeSearchAlgorithm {
15    /// Learns a tree on the instances of `cover`.
16    fn fit(&mut self, cover: &mut Cover) -> Result<(), FitError>;
17
18    /// The tree learned by the last `fit`.
19    fn tree(&self) -> &Tree;
20
21    /// Training error of the learned tree.
22    fn error(&self) -> f64 {
23        self.tree().root_error()
24    }
25
26    /// Features that can split the current node. See
27    /// [`find_valid_split_attributes`].
28    #[inline]
29    fn get_candidates(
30        &self,
31        cover: &mut Cover,
32        min_sup: usize,
33        provided_candidates: Option<&[usize]>,
34        previous: Option<usize>,
35    ) -> Vec<usize> {
36        find_valid_split_attributes(cover, min_sup, provided_candidates, previous)
37    }
38}