use crate::api::ModelError;
use crate::data::MatrixView;
use crate::numeric::{OwnedRng, derive_tree_seed};
use crate::tree::{
ClassTree, GrowerConfig, Objective, PackedTree, grow_class_tree, grow_tree,
unbootstrapped_sample,
};
use std::thread;
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct ForestConfig {
pub(crate) n_estimators: usize,
pub(crate) bootstrap: bool,
pub(crate) random_state: u64,
pub(crate) n_jobs: usize,
pub(crate) grower: GrowerConfig,
}
fn train_trees<T, F>(config: &ForestConfig, build: F) -> Result<Vec<T>, ModelError>
where
T: Send,
F: Fn(usize) -> Result<T, ModelError> + Sync,
{
let worker_count = config.n_jobs.min(config.n_estimators);
if worker_count == 1 {
return (0..config.n_estimators).map(build).collect();
}
let build = &build;
let mut indexed = thread::scope(|scope| {
let mut handles = Vec::with_capacity(worker_count);
for worker in 0..worker_count {
handles.push(scope.spawn(move || {
let mut trees = Vec::new();
for index in (worker..config.n_estimators).step_by(worker_count) {
trees.push((index, build(index)));
}
trees
}));
}
let mut results = Vec::with_capacity(config.n_estimators);
for handle in handles {
results.extend(handle.join().map_err(|_| ModelError::WorkerPanicked)?);
}
Ok::<_, ModelError>(results)
})?;
indexed.sort_unstable_by_key(|(index, _)| *index);
indexed.into_iter().map(|(_, tree)| tree).collect()
}
fn tree_sample(
rows: usize,
bootstrap: bool,
sample_weights: Option<&[f32]>,
rng: &mut OwnedRng,
) -> (Vec<f64>, Vec<usize>) {
if !bootstrap {
return unbootstrapped_sample(rows, sample_weights);
}
let Some(sample_weights) = sample_weights else {
let mut weights = vec![0.0_f64; rows];
for _ in 0..rows {
weights[rng.index(rows)] += 1.0;
}
let retained = weights
.iter()
.enumerate()
.filter_map(|(row, &weight)| (weight != 0.0).then_some(row))
.collect();
return (weights, retained);
};
let eligible: Vec<usize> = (0..rows).filter(|&row| sample_weights[row] > 0.0).collect();
let mut weights = vec![0.0_f64; rows];
for _ in 0..eligible.len() {
weights[eligible[rng.index(eligible.len())]] += 1.0;
}
let mut retained = Vec::with_capacity(eligible.len());
for &row in &eligible {
if weights[row] != 0.0 {
weights[row] *= f64::from(sample_weights[row]);
retained.push(row);
}
}
(weights, retained)
}
pub(crate) fn train_forest<Y, O>(
data: &MatrixView<'_>,
targets: &[Y],
sample_weights: Option<&[f32]>,
config: &ForestConfig,
objective: O,
) -> Result<Vec<PackedTree>, ModelError>
where
Y: Sync,
O: Objective<Y>,
{
train_trees(config, |index| {
let mut rng = OwnedRng::new(derive_tree_seed(config.random_state, index as u64));
let (weights, rows) = tree_sample(data.rows(), config.bootstrap, sample_weights, &mut rng);
grow_tree(
data,
targets,
&weights,
rows,
&config.grower,
objective,
&mut rng,
)
})
}
pub(crate) fn train_class_forest(
data: &MatrixView<'_>,
class_of_row: &[usize],
classes: usize,
sample_weights: Option<&[f32]>,
config: &ForestConfig,
) -> Result<Vec<ClassTree>, ModelError> {
train_trees(config, |index| {
let mut rng = OwnedRng::new(derive_tree_seed(config.random_state, index as u64));
let (weights, rows) = tree_sample(data.rows(), config.bootstrap, sample_weights, &mut rng);
grow_class_tree(
data,
class_of_row,
classes,
&weights,
rows,
&config.grower,
&mut rng,
)
})
}