From 67f77c3361549662c687c4cdd6feeab5b9b21894 Mon Sep 17 00:00:00 2001 From: AlessandroBregoli Date: Thu, 17 Feb 2022 16:23:36 +0100 Subject: [PATCH] Many things --- Cargo.toml | 2 ++ src/ctbn.rs | 46 +++++++++++++++++++++++----------------------- src/lib.rs | 1 + src/network.rs | 7 ++++--- src/node.rs | 13 +++++++++---- src/params.rs | 38 +++++++++++++++++++++++++++++++------- src/tools.rs | 30 ++++++++++++++++++++++++++++++ 7 files changed, 100 insertions(+), 37 deletions(-) create mode 100644 src/tools.rs diff --git a/Cargo.toml b/Cargo.toml index de0a17b..f8d3b03 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,3 +10,5 @@ edition = "2021" petgraph = "*" ndarray = "*" thiserror = "*" +rand = "*" +bimap = "*" diff --git a/src/ctbn.rs b/src/ctbn.rs index 43f973d..996b76c 100644 --- a/src/ctbn.rs +++ b/src/ctbn.rs @@ -1,4 +1,4 @@ -use std::collections::HashMap; +use std::collections::{HashMap, BTreeSet}; use petgraph::prelude::*; use crate::node; @@ -7,39 +7,39 @@ use crate::network; -pub struct CtbnParams { - cim: Option, - transitions: Option, - residence_time: Option -} - -impl CtbnParams { - fn init() -> CtbnParams { - CtbnParams { - cim: Option::None, - transitions: Option::None, - residence_time: Option::None - } - } -} pub struct CtbnNetwork { - network: petgraph::stable_graph::StableGraph, - params: HashMap, + network: petgraph::stable_graph::StableGraph, } impl network::Network for CtbnNetwork { fn add_node(&mut self, n: node::Node) -> Result { - match &n.domain { - node::DomainType::Discrete(_) => { + match &n.params { + node::ParamsType::DiscreteStatesContinousTime(_) => { + if self.network.node_weights().any(|x| x.label == n.label) { + //TODO: Insert a better error description + return Err(network::NetworkError::NodeInsertionError(String::from("Label already used"))); + } let idx = self.network.add_node(n); - self.params.insert(idx, CtbnParams::init()); Ok(idx) }, - _ => Err(network::NetworkError::InsertionError(String::from("unsupported node"))) + //TODO: Insert a better error description + _ => Err(network::NetworkError::NodeInsertionError(String::from("unsupported node"))) } + } + fn add_edge(&mut self, parent: &petgraph::stable_graph::NodeIndex, child: &petgraph::graph::NodeIndex) { + self.network.add_edge(parent.clone(), child.clone(), {}); + let mut p = self.network.node_weight(child.clone()); + match p. } -} + fn get_node_indices(&self) -> petgraph::stable_graph::NodeIndices{ + self.network.node_indices() + } + fn get_node_weight(&self, node_idx: &petgraph::stable_graph::NodeIndex) -> &node::Node{ + self.network.node_weight(node_idx.clone()).unwrap() + } + +} diff --git a/src/lib.rs b/src/lib.rs index c14b6fb..987aa96 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ mod node; mod params; mod network; mod ctbn; +mod tools; #[cfg(test)] mod tests { diff --git a/src/network.rs b/src/network.rs index 9367269..0bc5a48 100644 --- a/src/network.rs +++ b/src/network.rs @@ -5,11 +5,12 @@ use thiserror::Error; #[derive(Error, Debug)] pub enum NetworkError { #[error("Error during node insertion")] - InsertionError(String) + NodeInsertionError(String) } pub trait Network { fn add_node(&mut self, n: node::Node) -> Result; + fn add_edge(&mut self, parent: &petgraph::stable_graph::NodeIndex, child: &petgraph::graph::NodeIndex); + fn get_node_indices(&self) -> petgraph::stable_graph::NodeIndices; + fn get_node_weight(&self, node_idx: &petgraph::stable_graph::NodeIndex) -> &node::Node; } - - diff --git a/src/node.rs b/src/node.rs index cf23dcb..628e3ea 100644 --- a/src/node.rs +++ b/src/node.rs @@ -1,11 +1,16 @@ +use std::collections::BTreeSet; +use crate::params; -pub enum DomainType { - Discrete(Vec) -} pub struct Node { - pub domain: DomainType, + pub params: Box, pub label: String } +impl PartialEq for Node { + fn eq(&self, other: &Node) -> bool{ + self.label == other.label + } +} + diff --git a/src/params.rs b/src/params.rs index 21f8c3d..c92176f 100644 --- a/src/params.rs +++ b/src/params.rs @@ -1,15 +1,39 @@ use ndarray::prelude::*; +use std::collections::{HashMap, BTreeSet}; +use petgraph::prelude::*; -pub struct CIM { - cim: Array3, -} -pub struct M { - transitions: Array3, + +pub trait Params { + + fn add_parent(&mut self, p: &petgraph::stable_graph::NodeIndex); } -pub struct T { - residence_time: Array2, +pub struct DiscreteStatesContinousTimeParams { + domain: BTreeSet, + parents: BTreeSet, + cim: Option>, + transitions: Option>, + residence_time: Option> } +impl DiscreteStatesContinousTimeParams { + fn init(domain: BTreeSet) -> DiscreteStatesContinousTimeParams { + DiscreteStatesContinousTimeParams { + domain: domain, + parents: BTreeSet::new(), + cim: Option::None, + transitions: Option::None, + residence_time: Option::None + } + } +} +impl Params for DiscreteStatesContinousTimeParams { + fn add_parent(&mut self, p: &petgraph::stable_graph::NodeIndex) { + self.parents.insert(p.clone()); + self.cim = Option::None; + self.transitions = Option::None; + self.residence_time = Option::None; + } +} diff --git a/src/tools.rs b/src/tools.rs new file mode 100644 index 0000000..1500cc8 --- /dev/null +++ b/src/tools.rs @@ -0,0 +1,30 @@ +use ndarray::prelude::*; +use crate::network; +use petgraph::prelude::*; +use rand::Rng; + +pub struct Trajectory { + time: Array1, + events: Array2 +} + +pub struct Dataset { + trajectories: Vec +} + + +fn trajectory_generator(net: &Box, n_trajectories: u64, t_end: f64) -> Dataset { + let mut dataset = Dataset{ + trajectories: Vec::new() + }; + for _ in 0..n_trajectories { + let mut rng = rand::thread_rng(); + let t = 0.0; + let mut time: Vec = Vec::new(); + let mut events: Vec> = Vec::new(); + let current_state: Vec = net.get_node_indices().map(|x| rng.gen_range(0..2)).collect(); + + } + + dataset +}