Ability to execute a battery of benchmarks from a json and print results to a csv

master
Meliurwen 2 years ago
parent b870da3a16
commit 41a87e1e9c
Signed by: meliurwen
GPG Key ID: 818A8B35E9F1CE10
  1. 6
      .gitignore
  2. 2
      Cargo.toml
  3. 115
      src/main.rs

6
.gitignore vendored

@ -1,2 +1,8 @@
/target /target
Cargo.lock Cargo.lock
*.json
*.csv
# Heaptrack
*.zst

@ -6,3 +6,5 @@ edition = "2021"
[dependencies] [dependencies]
reCTBN = { path = "deps/reCTBN/reCTBN", package = "reCTBN", version="0.1.0" } reCTBN = { path = "deps/reCTBN/reCTBN", package = "reCTBN", version="0.1.0" }
json = "0.12.*" json = "0.12.*"
csv = "1.2.*"
duration-string = "0.2.*"

@ -1,6 +1,11 @@
#![allow(non_snake_case)] #![allow(non_snake_case)]
use csv;
use duration_string::DurationString;
use json;
use std::collections::BTreeSet; use std::collections::BTreeSet;
use std::fs;
use std::time::Instant;
use reCTBN::parameter_learning::MLE; use reCTBN::parameter_learning::MLE;
use reCTBN::params::DiscreteStatesContinousTimeParams; use reCTBN::params::DiscreteStatesContinousTimeParams;
@ -17,10 +22,19 @@ use reCTBN::tools::RandomParametersGenerator;
use reCTBN::tools::UniformGraphGenerator; use reCTBN::tools::UniformGraphGenerator;
use reCTBN::tools::UniformParametersGenerator; 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<u64>,
cg_seed: Option<u64>,
tg_seed: Option<u64>,
) -> (CtbnNetwork, Dataset) {
let mut net = CtbnNetwork::new(); let mut net = CtbnNetwork::new();
let nodes_cardinality = 20;
let domain_cardinality = 3;
for node in 0..nodes_cardinality { for node in 0..nodes_cardinality {
// Create the domain for a discrete node // Create the domain for a discrete node
let mut domain = BTreeSet::new(); 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 // Initialize the Graph Generator using the one with an
// uniform distribution // 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 // Generate the graph directly on the network
structure_generator.generate_graph(&mut net); structure_generator.generate_graph(&mut net);
// Initialize the parameters generator with uniform distributin // 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. // Generate CIMs with uniformly distributed parameters.
cim_generator.generate_parameters(&mut net); 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); return (net, dataset);
} }
fn write_csv_record<I, T>(file_name: &str, record: I) -> std::io::Result<()>
where
I: IntoIterator<Item = T>,
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) { fn structure_learning_CTPC(net: CtbnNetwork, dataset: &Dataset) {
// Initialize the hypothesis tests to pass to the CTPC with their // Initialize the hypothesis tests to pass to the CTPC with their
// respective significance level `alpha` // respective significance level `alpha`
@ -67,6 +100,72 @@ fn structure_learning_CTPC(net: CtbnNetwork, dataset: &Dataset) {
} }
fn main() { fn main() {
let (net, dataset) = uniform_parameters_generator_right_densities_ctmp(); let file_path = "./networks-settings.json";
structure_learning_CTPC(net, &dataset); 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();
}
} }

Loading…
Cancel
Save