diff --git a/reCTBN/src/structure_learning/score_based_algorithm.rs b/reCTBN/src/structure_learning/score_based_algorithm.rs index d65ea88..6850027 100644 --- a/reCTBN/src/structure_learning/score_based_algorithm.rs +++ b/reCTBN/src/structure_learning/score_based_algorithm.rs @@ -6,6 +6,9 @@ use crate::structure_learning::score_function::ScoreFunction; use crate::structure_learning::StructureLearningAlgorithm; use crate::{process, tools::Dataset}; +use rayon::iter::{IntoParallelIterator, ParallelIterator}; +use rayon::prelude::ParallelExtend; + pub struct HillClimbing { score_function: S, max_parent_set: Option, @@ -36,8 +39,9 @@ impl StructureLearningAlgorithm for HillClimbing { let max_parent_set = self.max_parent_set.unwrap_or(net.get_number_of_nodes()); //Reset the adj matrix net.initialize_adj_matrix(); + let mut learned_parent_sets: Vec<(usize, BTreeSet)> = vec![]; //Iterate over each node to learn their parent set. - for node in net.get_node_indices() { + learned_parent_sets.par_extend(net.get_node_indices().into_par_iter().map(|node| { //Initialize an empty parent set. let mut parent_set: BTreeSet = BTreeSet::new(); //Compute the score for the empty parent set @@ -76,10 +80,14 @@ impl StructureLearningAlgorithm for HillClimbing { } } } - //Apply the learned parent_set to the network struct. - parent_set.iter().for_each(|p| net.add_edge(*p, node)); + (node, parent_set) + })); + + for (child_node, candidate_parent_set) in learned_parent_sets { + for parent_node in candidate_parent_set.iter() { + net.add_edge(*parent_node, child_node); + } } - return net; } } diff --git a/reCTBN/src/structure_learning/score_function.rs b/reCTBN/src/structure_learning/score_function.rs index f8b38b5..5a56594 100644 --- a/reCTBN/src/structure_learning/score_function.rs +++ b/reCTBN/src/structure_learning/score_function.rs @@ -7,7 +7,7 @@ use statrs::function::gamma; use crate::{parameter_learning, params, process, tools}; -pub trait ScoreFunction { +pub trait ScoreFunction: Sync { fn call( &self, net: &T,