From 0357559827f9cf72243d5d1cd92934993387ba6d Mon Sep 17 00:00:00 2001 From: jdurand7 Date: Fri, 14 Sep 2012 22:14:37 +0000 Subject: [PATCH] Files and test files for the thinTree. To be debugged. --- matlab/gtsam_tests/testThinBayesTree.m | 23 +++++++ matlab/gtsam_tests/testThinTree.m | 49 ++++++++++++++ matlab/gtsam_tests/testThinTreeBayesNet.m | 23 +++++++ matlab/gtsam_tests/thinBayesTree.m | 5 ++ matlab/gtsam_tests/thinTree.m | 78 +++++++++++++++++++++++ matlab/gtsam_tests/thinTreeBayesNet.m | 33 ++++++++++ 6 files changed, 211 insertions(+) create mode 100644 matlab/gtsam_tests/testThinBayesTree.m create mode 100644 matlab/gtsam_tests/testThinTree.m create mode 100644 matlab/gtsam_tests/testThinTreeBayesNet.m create mode 100644 matlab/gtsam_tests/thinBayesTree.m create mode 100644 matlab/gtsam_tests/thinTree.m create mode 100644 matlab/gtsam_tests/thinTreeBayesNet.m diff --git a/matlab/gtsam_tests/testThinBayesTree.m b/matlab/gtsam_tests/testThinBayesTree.m new file mode 100644 index 000000000..accb40245 --- /dev/null +++ b/matlab/gtsam_tests/testThinBayesTree.m @@ -0,0 +1,23 @@ +% /* ---------------------------------------------------------------------------- +% +% * GTSAM Copyright 2010, Georgia Tech Research Corporation, +% * Atlanta, Georgia 30332-0415 +% * All Rights Reserved +% * Authors: Frank Dellaert, et al. (see THANKS for the full author list) +% +% * See LICENSE for the license information +% +% * -------------------------------------------------------------------------- */ +% +% /** +% * @file testThinBayesTree.cpp +% * @brief Test of binary tree +% * @date Sep 14, 2012 +% * @author Frank Dellaert +% * @author Jean-Guillaume Durand +% */ + +%% Run the tests +import gtsam.* +bayesTree = thinBayesTree(3,2); +EQUALITY('7 = bayesTree.size', 7, bayesTree.size); diff --git a/matlab/gtsam_tests/testThinTree.m b/matlab/gtsam_tests/testThinTree.m new file mode 100644 index 000000000..987af7333 --- /dev/null +++ b/matlab/gtsam_tests/testThinTree.m @@ -0,0 +1,49 @@ +% /* ---------------------------------------------------------------------------- +% +% * GTSAM Copyright 2010, Georgia Tech Research Corporation, +% * Atlanta, Georgia 30332-0415 +% * All Rights Reserved +% * Authors: Frank Dellaert, et al. (see THANKS for the full author list) +% +% * See LICENSE for the license information +% +% * -------------------------------------------------------------------------- */ +% +% /** +% * @file testThinTree.cpp +% * @brief Test of binary tree +% * @date Sep 13, 2012 +% * @author Frank Dellaert +% * @author Jean-Guillaume Durand +% */ + +%% Clear working space +clc, close all, clear all; + +%% Create different trees for our example +import gtsam.* +t0 = thinTree(2,1); +t1 = thinTree(3,2); +% Add contents in it +% TODO +%% Create the set of expected output TestValues +expectedNumberOfNodes0 = 3; +expectedNumberOfNodes1 = 7; +expectedParentsOf6in1 = [3 1]; +expectedParentsOf7in1 = [3 1]; + +%% Run the tests +% Tree depth +%TODO +% Number of parents for each node +%TODO +% Number of elements +EQUALITY('expectedNumberOfNodes0,t0.getNumberOfElements', expectedNumberOfNodes0,t0.getNumberOfElements); +EQUALITY('expectedNumberOfNodes1,t1.getNumberOfElements', expectedNumberOfNodes1,t1.getNumberOfElements); +% Parents linking +EQUALITY('expectedParentsOf6in1,t1.getParents(6)', expectedParentsOf6in1,t1.getParents(6)); +EQUALITY('expectedParentsOf7in1,t1.getParents(7)', expectedParentsOf7in1,t1.getParents(7)); +% Adding an element + +bn = thinTreeBayesNet(3,2); +EQUALITY('7 = bn.size', 7, bn.size); \ No newline at end of file diff --git a/matlab/gtsam_tests/testThinTreeBayesNet.m b/matlab/gtsam_tests/testThinTreeBayesNet.m new file mode 100644 index 000000000..dceb54e58 --- /dev/null +++ b/matlab/gtsam_tests/testThinTreeBayesNet.m @@ -0,0 +1,23 @@ +% /* ---------------------------------------------------------------------------- +% +% * GTSAM Copyright 2010, Georgia Tech Research Corporation, +% * Atlanta, Georgia 30332-0415 +% * All Rights Reserved +% * Authors: Frank Dellaert, et al. (see THANKS for the full author list) +% +% * See LICENSE for the license information +% +% * -------------------------------------------------------------------------- */ +% +% /** +% * @file testThinTree.cpp +% * @brief Test of binary tree +% * @date Sep 13, 2012 +% * @author Frank Dellaert +% * @author Jean-Guillaume Durand +% */ + +%% Run the tests +import gtsam.* +bayesNet = thinTreeBayesNet(3,2); +EQUALITY('7 = bayesNet.size', 7, bayesNet.size); diff --git a/matlab/gtsam_tests/thinBayesTree.m b/matlab/gtsam_tests/thinBayesTree.m new file mode 100644 index 000000000..6c2f496c6 --- /dev/null +++ b/matlab/gtsam_tests/thinBayesTree.m @@ -0,0 +1,5 @@ +function bayesTree = thinBayesTree(depth, width) + import gtsam.* + bayesNet = thinTreeBayesNet(depth, width); + bayesTree = GaussianBayesTree(bayesNet); +end \ No newline at end of file diff --git a/matlab/gtsam_tests/thinTree.m b/matlab/gtsam_tests/thinTree.m new file mode 100644 index 000000000..32d2efff5 --- /dev/null +++ b/matlab/gtsam_tests/thinTree.m @@ -0,0 +1,78 @@ +classdef thinTree + + % Attributes + properties (SetAccess = private) + nodes = { [] }; % Array of the nodes + depth = 0; % Depth of the tree + w = 0 % Number of parents for each node + links = []; % The matrix representing the links between the nodes + end + + % Methods + methods + % Constructor + function [obj root_ID] = thinTree(d, w) + + % If 0 input arguments, assume d = 1 and w = 1 + if nargin < 1 + [obj root_ID] = thinTree(1, 1); + return + end + % If 1 input argument, assume w = 1 + if nargin < 1 + [obj root_ID] = thinTree(d, 1); + return + end + % Else + + if w > d-1 + error('MATLAB:thinTree:thinTree', ... + 'Cannot have %d parents on a binary tree of depth %d. You must have nParents < %d here.\n', w, d, d); + end + + root_ID = 1; + obj.nodes = cell(2^d - 1,1); % Creation of the d^2 empty cells + obj.depth = d; + obj.w = w; + obj.links = eye(2^d - 1); % Creation of the links matrix + + % Link the cells + + end + + % Function to add a content for a specific node + function [obj] = addContent(obj, content, nodeID) + obj.nodes{nodeID} = content; + return + end + + % Function to return the ID's of a node's parents + function ids = getParents(obj, nodeID) + % Initialisation + ids = zeros(1,obj.w); + node = nodeID; + % Loop on w, the number of parents associated to one node + for i=1:obj.w + ids(i) = floor(node/2); + node = floor(node/2); + end + % Return + return + end + + % Accessors + function output = getDepth(obj) + output = obj.depth; + return + end + + function output = getW(obj) + output = obj.w; + return + end + + function output = getNumberOfElements(obj) + output = 2^obj.depth - 1; + end + end % Methods +end % Class \ No newline at end of file diff --git a/matlab/gtsam_tests/thinTreeBayesNet.m b/matlab/gtsam_tests/thinTreeBayesNet.m new file mode 100644 index 000000000..c7dfed6c0 --- /dev/null +++ b/matlab/gtsam_tests/thinTreeBayesNet.m @@ -0,0 +1,33 @@ +function [bayesNet, tree] = thinTreeBayesNet(d,w) +import gtsam.* +bayesNet = GaussianBayesNet; +tree = thinTree(3,2); + +% Filling the tree + +% Creation of the root +gc = gtsam.GaussianConditional(1, 5*rand(1), 5*rand(1), 3*rand(1)); +% Getting it into the GaussianBayesNet +bayesNet.push_front(gc); + +for i=1:2^tree.getNumberOfElements() + % Getting the parents of that node + parents = tree.getParents(i); + % Create and link the corresponding GaussianConditionals + if tree.getW == 1 + % Creation of the GaussianConditional + gc = gtsam.GaussianConditional(parents(1), 5*rand(1), 5*rand(1)); + % Getting it into the GaussianBayesNet + bayesNet.push_front(gc); + % Getting it in the thinTree + t = tree.addContent({gc,parents}, i); + elseif tree.getW == 2 + % Creation of the GaussianConditional + gc = gtsam.GaussianConditional(parents(2), 5*rand(1), 5*rand(1), parents(1), 5*rand(1), 5*rand(1)); + % Getting it into the GaussianBayesNet + bayesNet.push_front(gc); + % Getting it in the thinTree + t = tree.addContent({gc,parents}, i); + end +end +end \ No newline at end of file