diff --git a/.gitignore b/.gitignore index 96ef6c0..d06c1a5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,8 @@ /target Cargo.lock + +*.json +*.csv + +# Heaptrack +*.zst diff --git a/Cargo.toml b/Cargo.toml index b7462e4..88322b7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,5 @@ edition = "2021" [dependencies] reCTBN = { path = "deps/reCTBN/reCTBN", package = "reCTBN", version="0.1.0" } json = "0.12.*" +csv = "1.2.*" +duration-string = "0.2.*" diff --git a/src/main.rs b/src/main.rs index 5003247..9af122e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,11 @@ #![allow(non_snake_case)] +use csv; +use duration_string::DurationString; +use json; use std::collections::BTreeSet; +use std::fs; +use std::time::Instant; use reCTBN::parameter_learning::MLE; use reCTBN::params::DiscreteStatesContinousTimeParams; @@ -17,10 +22,19 @@ use reCTBN::tools::RandomParametersGenerator; use reCTBN::tools::UniformGraphGenerator; use reCTBN::tools::UniformParametersGenerator; -fn uniform_parameters_generator_right_densities_ctmp() -> (CtbnNetwork, Dataset) { +fn uniform_parameters_generator_right_densities_ctmp( + nodes_cardinality: usize, + domain_cardinality: usize, + density: f64, + interval_s: f64, + interval_f: f64, + n_trajectories: u64, + t_end: f64, + sg_seed: Option, + cg_seed: Option, + tg_seed: Option, +) -> (CtbnNetwork, Dataset) { let mut net = CtbnNetwork::new(); - let nodes_cardinality = 20; - let domain_cardinality = 3; for node in 0..nodes_cardinality { // Create the domain for a discrete node let mut domain = BTreeSet::new(); @@ -37,22 +51,41 @@ fn uniform_parameters_generator_right_densities_ctmp() -> (CtbnNetwork, Dataset) // Initialize the Graph Generator using the one with an // uniform distribution - let mut structure_generator = UniformGraphGenerator::new(1.0 / 3.0, Some(7641630759785120)); + let mut structure_generator = UniformGraphGenerator::new(density, sg_seed); // Generate the graph directly on the network structure_generator.generate_graph(&mut net); // Initialize the parameters generator with uniform distributin - let mut cim_generator = UniformParametersGenerator::new(3.0..7.0, Some(7641630759785120)); + let mut cim_generator = UniformParametersGenerator::new(interval_s..interval_f, cg_seed); // Generate CIMs with uniformly distributed parameters. cim_generator.generate_parameters(&mut net); - let dataset = trajectory_generator(&net, 300, 200.0, Some(30230423)); + let dataset = trajectory_generator(&net, n_trajectories, t_end, tg_seed); return (net, dataset); } +fn write_csv_record(file_name: &str, record: I) -> std::io::Result<()> +where + I: IntoIterator, + T: AsRef<[u8]>, +{ + let file = fs::OpenOptions::new() + .write(true) + .create(true) + .append(true) + .open(file_name) + .unwrap(); + let mut wtr = csv::Writer::from_writer(file); + + //let mut wtr = csv::Writer::from_path(file_name)?; + wtr.write_record(record)?; + wtr.flush()?; + Ok(()) +} + fn structure_learning_CTPC(net: CtbnNetwork, dataset: &Dataset) { // Initialize the hypothesis tests to pass to the CTPC with their // respective significance level `alpha` @@ -67,6 +100,72 @@ fn structure_learning_CTPC(net: CtbnNetwork, dataset: &Dataset) { } fn main() { - let (net, dataset) = uniform_parameters_generator_right_densities_ctmp(); - structure_learning_CTPC(net, &dataset); + let file_path = "./networks-settings.json"; + let csv_path = "./results.csv"; + eprintln!("Opening file {}...", file_path); + let file_content = + fs::read_to_string(file_path).expect("File not found! Check the README for instructions!"); + let parsed_json = json::parse(&file_content).unwrap(); + + write_csv_record( + csv_path, + &[ + "nodes_cardinality", + "domain_cardinality", + "density", + "duration", + ], + ) + .unwrap(); + + let benchmarks_cardinality = parsed_json.len(); + for idx in 0..benchmarks_cardinality { + eprintln!( + "[{}/{}] Generating network...", + idx + 1, + benchmarks_cardinality + ); + let (net, dataset) = uniform_parameters_generator_right_densities_ctmp( + parsed_json[idx]["nodes_cardinality"].as_usize().unwrap(), + parsed_json[idx]["domain_cardinality"].as_usize().unwrap(), + parsed_json[idx]["density"].as_f64().unwrap(), + parsed_json[idx]["interval_s"].as_f64().unwrap(), + parsed_json[idx]["interval_f"].as_f64().unwrap(), + parsed_json[idx]["n_trajectories"].as_u64().unwrap(), + parsed_json[idx]["t_end"].as_f64().unwrap(), + parsed_json[idx]["sg_seed"].as_u64(), + parsed_json[idx]["cg_seed"].as_u64(), + parsed_json[idx]["tg_seed"].as_u64(), + ); + eprintln!( + "[{}/{}] Structure learning CTPC...", + idx + 1, + benchmarks_cardinality + ); + let start = Instant::now(); + structure_learning_CTPC(net, &dataset); + let duration = start.elapsed(); + eprintln!( + "[{}/{}] Strucure learned with CTPC in {:?}.", + idx + 1, + benchmarks_cardinality, + duration + ); + write_csv_record( + csv_path, + &[ + parsed_json[idx]["nodes_cardinality"] + .as_usize() + .unwrap() + .to_string(), + parsed_json[idx]["domain_cardinality"] + .as_usize() + .unwrap() + .to_string(), + parsed_json[idx]["density"].as_f64().unwrap().to_string(), + DurationString::from(duration).to_string(), + ], + ) + .unwrap(); + } }