13 feature add ctmp #73
Merged
AlessandroBregoli
merged 6 commits from 13-feature-add-ctmp
into dev
2 years ago
@ -0,0 +1,118 @@ |
||||
use std::collections::BTreeSet; |
||||
|
||||
use crate::{ |
||||
params::{Params, StateType}, |
||||
process, |
||||
}; |
||||
|
||||
use super::NetworkProcess; |
||||
|
||||
pub struct CtmpProcess { |
||||
param: Option<Params>, |
||||
} |
||||
|
||||
impl CtmpProcess { |
||||
pub fn new() -> CtmpProcess { |
||||
CtmpProcess { param: None } |
||||
} |
||||
} |
||||
|
||||
impl NetworkProcess for CtmpProcess { |
||||
fn initialize_adj_matrix(&mut self) { |
||||
unimplemented!("CtmpProcess has only one node") |
||||
} |
||||
|
||||
fn add_node(&mut self, n: crate::params::Params) -> Result<usize, process::NetworkError> { |
||||
match self.param { |
||||
None => { |
||||
self.param = Some(n); |
||||
Ok(0) |
||||
} |
||||
Some(_) => Err(process::NetworkError::NodeInsertionError( |
||||
"CtmpProcess has only one node".to_string(), |
||||
)), |
||||
} |
||||
} |
||||
|
||||
fn add_edge(&mut self, _parent: usize, _child: usize) { |
||||
unimplemented!("CtmpProcess has only one node") |
||||
} |
||||
|
||||
fn get_node_indices(&self) -> std::ops::Range<usize> { |
||||
match self.param { |
||||
None => 0..0, |
||||
Some(_) => 0..1, |
||||
} |
||||
} |
||||
|
||||
fn get_number_of_nodes(&self) -> usize { |
||||
match self.param { |
||||
None => 0, |
||||
Some(_) => 1, |
||||
} |
||||
} |
||||
|
||||
fn get_node(&self, node_idx: usize) -> &crate::params::Params { |
||||
if node_idx == 0 { |
||||
self.param.as_ref().unwrap() |
||||
} else { |
||||
unimplemented!("CtmpProcess has only one node") |
||||
} |
||||
} |
||||
|
||||
fn get_node_mut(&mut self, node_idx: usize) -> &mut crate::params::Params { |
||||
if node_idx == 0 { |
||||
self.param.as_mut().unwrap() |
||||
} else { |
||||
unimplemented!("CtmpProcess has only one node") |
||||
} |
||||
} |
||||
|
||||
fn get_param_index_network( |
||||
&self, |
||||
node: usize, |
||||
current_state: &Vec<crate::params::StateType>, |
||||
) -> usize { |
||||
if node == 0 { |
||||
match current_state[0] { |
||||
StateType::Discrete(x) => x, |
||||
} |
||||
} else { |
||||
unimplemented!("CtmpProcess has only one node") |
||||
} |
||||
} |
||||
|
||||
fn get_param_index_from_custom_parent_set( |
||||
&self, |
||||
_current_state: &Vec<crate::params::StateType>, |
||||
_parent_set: &std::collections::BTreeSet<usize>, |
||||
) -> usize { |
||||
unimplemented!("CtmpProcess has only one node") |
||||
} |
||||
|
||||
fn get_parent_set(&self, node: usize) -> std::collections::BTreeSet<usize> { |
||||
match self.param { |
||||
Some(_) => { |
||||
if node == 0 { |
||||
BTreeSet::new() |
||||
} else { |
||||
unimplemented!("CtmpProcess has only one node") |
||||
} |
||||
} |
||||
None => panic!("Uninitialized CtmpProcess"), |
||||
} |
||||
} |
||||
|
||||
fn get_children_set(&self, node: usize) -> std::collections::BTreeSet<usize> { |
||||
match self.param { |
||||
Some(_) => { |
||||
if node == 0 { |
||||
BTreeSet::new() |
||||
} else { |
||||
unimplemented!("CtmpProcess has only one node") |
||||
} |
||||
} |
||||
None => panic!("Uninitialized CtmpProcess"), |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,127 @@ |
||||
mod utils; |
||||
|
||||
use std::collections::BTreeSet; |
||||
|
||||
use reCTBN::{ |
||||
params, |
||||
params::ParamsTrait, |
||||
process::{ctmp::*, NetworkProcess}, |
||||
}; |
||||
use utils::generate_discrete_time_continous_node; |
||||
|
||||
#[test] |
||||
fn define_simple_ctmp() { |
||||
let _ = CtmpProcess::new(); |
||||
assert!(true); |
||||
} |
||||
|
||||
#[test] |
||||
fn add_node_to_ctmp() { |
||||
let mut net = CtmpProcess::new(); |
||||
let n1 = net |
||||
.add_node(generate_discrete_time_continous_node(String::from("n1"), 2)) |
||||
.unwrap(); |
||||
assert_eq!(&String::from("n1"), net.get_node(n1).get_label()); |
||||
} |
||||
|
||||
#[test] |
||||
fn add_two_nodes_to_ctmp() { |
||||
let mut net = CtmpProcess::new(); |
||||
let _n1 = net |
||||
.add_node(generate_discrete_time_continous_node(String::from("n1"), 2)) |
||||
.unwrap(); |
||||
let n2 = net.add_node(generate_discrete_time_continous_node(String::from("n1"), 2)); |
||||
|
||||
match n2 { |
||||
Ok(_) => assert!(false), |
||||
Err(_) => assert!(true), |
||||
}; |
||||
} |
||||
|
||||
#[test] |
||||
#[should_panic] |
||||
fn add_edge_to_ctmp() { |
||||
let mut net = CtmpProcess::new(); |
||||
let _n1 = net |
||||
.add_node(generate_discrete_time_continous_node(String::from("n1"), 2)) |
||||
.unwrap(); |
||||
let _n2 = net.add_node(generate_discrete_time_continous_node(String::from("n1"), 2)); |
||||
|
||||
net.add_edge(0, 1) |
||||
} |
||||
|
||||
#[test] |
||||
fn childen_and_parents() { |
||||
let mut net = CtmpProcess::new(); |
||||
let _n1 = net |
||||
.add_node(generate_discrete_time_continous_node(String::from("n1"), 2)) |
||||
.unwrap(); |
||||
|
||||
assert_eq!(0, net.get_parent_set(0).len()); |
||||
assert_eq!(0, net.get_children_set(0).len()); |
||||
} |
||||
|
||||
#[test] |
||||
#[should_panic] |
||||
fn get_childen_panic() { |
||||
let net = CtmpProcess::new(); |
||||
net.get_children_set(0); |
||||
} |
||||
|
||||
#[test] |
||||
#[should_panic] |
||||
fn get_childen_panic2() { |
||||
let mut net = CtmpProcess::new(); |
||||
let _n1 = net |
||||
.add_node(generate_discrete_time_continous_node(String::from("n1"), 2)) |
||||
.unwrap(); |
||||
net.get_children_set(1); |
||||
} |
||||
|
||||
#[test] |
||||
#[should_panic] |
||||
fn get_parent_panic() { |
||||
let net = CtmpProcess::new(); |
||||
net.get_parent_set(0); |
||||
} |
||||
|
||||
#[test] |
||||
#[should_panic] |
||||
fn get_parent_panic2() { |
||||
let mut net = CtmpProcess::new(); |
||||
let _n1 = net |
||||
.add_node(generate_discrete_time_continous_node(String::from("n1"), 2)) |
||||
.unwrap(); |
||||
net.get_parent_set(1); |
||||
} |
||||
|
||||
#[test] |
||||
fn compute_index_ctmp() { |
||||
let mut net = CtmpProcess::new(); |
||||
let n1 = net |
||||
.add_node(generate_discrete_time_continous_node( |
||||
String::from("n1"), |
||||
10, |
||||
)) |
||||
.unwrap(); |
||||
|
||||
let idx = net.get_param_index_network(n1, &vec![params::StateType::Discrete(6)]); |
||||
assert_eq!(6, idx); |
||||
} |
||||
|
||||
#[test] |
||||
#[should_panic] |
||||
fn compute_index_from_custom_parent_set_ctmp() { |
||||
let mut net = CtmpProcess::new(); |
||||
let _n1 = net |
||||
.add_node(generate_discrete_time_continous_node( |
||||
String::from("n1"), |
||||
10, |
||||
)) |
||||
.unwrap(); |
||||
|
||||
let _idx = net.get_param_index_from_custom_parent_set( |
||||
&vec![params::StateType::Discrete(6)], |
||||
&BTreeSet::from([0]) |
||||
); |
||||
} |
Loading…
Reference in new issue