From 1a8cf84717273623785aae6337c5322309767acc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raoul=20Luqu=C3=A9?= Date: Sat, 11 May 2024 11:22:26 +0200 Subject: [PATCH] Added tests for test graphs with fixed hash for reproducible results --- .../clique_graph_edge_weight_heuristics.rs | 2 +- .../src/compute_treewidth_upper_bound.rs | 79 +++++++++++++++---- treewidth_heuristic/src/lib.rs | 29 ++++--- 3 files changed, 80 insertions(+), 30 deletions(-) diff --git a/treewidth_heuristic/src/clique_graph_edge_weight_heuristics.rs b/treewidth_heuristic/src/clique_graph_edge_weight_heuristics.rs index ef6b445..6cc4198 100644 --- a/treewidth_heuristic/src/clique_graph_edge_weight_heuristics.rs +++ b/treewidth_heuristic/src/clique_graph_edge_weight_heuristics.rs @@ -2,7 +2,7 @@ use std::{collections::HashSet, hash::BuildHasher}; use petgraph::graph::NodeIndex; -pub fn neutral_heuristic(_: &HashSet, _: &HashSet) -> i32 { +pub fn neutral_heuristic(_: &HashSet, _: &HashSet) -> i32 { 0 } diff --git a/treewidth_heuristic/src/compute_treewidth_upper_bound.rs b/treewidth_heuristic/src/compute_treewidth_upper_bound.rs index 90ca2e7..0892581 100644 --- a/treewidth_heuristic/src/compute_treewidth_upper_bound.rs +++ b/treewidth_heuristic/src/compute_treewidth_upper_bound.rs @@ -7,7 +7,7 @@ use crate::*; use itertools::Itertools; use petgraph::{graph::NodeIndex, Graph, Undirected}; -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Debug)] pub enum TreewidthComputationMethod { MSTAndFill, MSTAndUseTreeStructure, @@ -217,23 +217,68 @@ mod tests { #[test] fn test_treewidth_heuristic_and_check_result_neutral_weight_heuristic() { - for i in vec![0, 2] { - let test_graph = setup_test_graph(i); - let computed_treewidth = compute_treewidth_upper_bound_not_connected::<_, _, RandomState>( - &test_graph.graph, - neutral_heuristic, - TreewidthComputationMethod::MSTAndUseTreeStructure, - false, - ); - assert_eq!(computed_treewidth, test_graph.treewidth); + for i in 0..3 { + for computation_method in COMPUTATION_METHODS { + let test_graph = setup_test_graph(i); + let computed_treewidth = compute_treewidth_upper_bound_not_connected::< + _, + _, + std::hash::BuildHasherDefault, + >( + &test_graph.graph, + neutral_heuristic, + computation_method, + false, + ); + assert_eq!(computed_treewidth, test_graph.treewidth); + } + } + } - let computed_treewidth = compute_treewidth_upper_bound_not_connected::<_, _, RandomState>( - &test_graph.graph, - neutral_heuristic, - TreewidthComputationMethod::MSTAndFill, - false, - ); - assert_eq!(computed_treewidth, test_graph.treewidth); + #[test] + #[should_panic] + fn test_treewidth_heuristic_and_check_result_negative_intersection_weight_heuristic() { + for i in 0..3 { + for computation_method in COMPUTATION_METHODS { + let test_graph = setup_test_graph(i); + let computed_treewidth = compute_treewidth_upper_bound_not_connected::< + _, + _, + std::hash::BuildHasherDefault, + >( + &test_graph.graph, + negative_intersection_heuristic, + computation_method, + false, + ); + assert_eq!( + computed_treewidth, + test_graph.treewidth, + "computation method: {:?}. Test graph {:?}", + computation_method, + i + 1 + ); + } + } + } + + #[test] + fn test_treewidth_heuristic_and_check_result_least_difference_weight_heuristic() { + for i in 0..3 { + for computation_method in COMPUTATION_METHODS { + let test_graph = setup_test_graph(i); + let computed_treewidth = compute_treewidth_upper_bound_not_connected::< + _, + _, + std::hash::BuildHasherDefault, + >( + &test_graph.graph, + least_difference_heuristic, + computation_method, + false, + ); + assert_eq!(computed_treewidth, test_graph.treewidth); + } } } } diff --git a/treewidth_heuristic/src/lib.rs b/treewidth_heuristic/src/lib.rs index f2bfc2a..9535641 100644 --- a/treewidth_heuristic/src/lib.rs +++ b/treewidth_heuristic/src/lib.rs @@ -73,6 +73,12 @@ pub(crate) mod tests { pub expected_connected_components: Vec>, } + pub const COMPUTATION_METHODS: [TreewidthComputationMethod; 3] = [ + TreewidthComputationMethod::FillWhilstMST, + TreewidthComputationMethod::MSTAndFill, + TreewidthComputationMethod::MSTAndUseTreeStructure, + ]; + /// Sets up test graph: /// /// Test graph 0 has: @@ -307,29 +313,28 @@ pub(crate) mod tests { fn test_graph_on_all_heuristics( graph: Graph, expected_treewidth: usize, + msg: &str, ) { - for computation_method in vec![ - TreewidthComputationMethod::FillWhilstMST, - TreewidthComputationMethod::MSTAndUseTreeStructure, - TreewidthComputationMethod::MSTAndFill, - ] - .iter() - { + for computation_method in COMPUTATION_METHODS { let treewidth = compute_treewidth_upper_bound_not_connected( &graph, negative_intersection_heuristic::, - *computation_method, + computation_method, true, ); - assert_eq!(treewidth, expected_treewidth); + assert_eq!(treewidth, expected_treewidth, "{}", msg); let treewidth = compute_treewidth_upper_bound_not_connected( &graph, least_difference_heuristic::, - *computation_method, + computation_method, true, ); - assert_eq!(treewidth, expected_treewidth); + assert_eq!( + treewidth, expected_treewidth, + "{} computation method: {:?}", + msg, computation_method + ); } } @@ -347,7 +352,7 @@ pub(crate) mod tests { let k_tree: Graph = generate_k_tree(k, n).expect("k should be smaller or eq to n"); - test_graph_on_all_heuristics(k_tree, k); + test_graph_on_all_heuristics(k_tree, k, &format!("k_tree with n: {} and k: {}", n, k)); } } }