diff --git a/matlab/gtsam_tests/testThinBayesTree.m b/matlab/gtsam_tests/testThinBayesTree.m index accb40245..034fc38de 100644 --- a/matlab/gtsam_tests/testThinBayesTree.m +++ b/matlab/gtsam_tests/testThinBayesTree.m @@ -20,4 +20,4 @@ %% Run the tests import gtsam.* bayesTree = thinBayesTree(3,2); -EQUALITY('7 = bayesTree.size', 7, bayesTree.size); +EQUALITY('7 = bayesTree.size', 7, bayesTree.size); \ No newline at end of file diff --git a/matlab/gtsam_tests/testThinTreeBayesNet.m b/matlab/gtsam_tests/testThinTreeBayesNet.m index dceb54e58..55499e7ce 100644 --- a/matlab/gtsam_tests/testThinTreeBayesNet.m +++ b/matlab/gtsam_tests/testThinTreeBayesNet.m @@ -17,7 +17,11 @@ % * @author Jean-Guillaume Durand % */ +%% Clean the workspace +clc, clear all, close all; + %% Run the tests import gtsam.* -bayesNet = thinTreeBayesNet(3,2); +[bayesNet tree] = thinTreeBayesNet(4,2); EQUALITY('7 = bayesNet.size', 7, bayesNet.size); +bayesNet.saveGraph('thinTreeBayesNet.dot'); \ No newline at end of file diff --git a/matlab/gtsam_tests/thinTree.m b/matlab/gtsam_tests/thinTree.m index 32d2efff5..72e411419 100644 --- a/matlab/gtsam_tests/thinTree.m +++ b/matlab/gtsam_tests/thinTree.m @@ -49,12 +49,17 @@ classdef thinTree % 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); + depthOfNode = obj.getNodeDepth(nodeID); + if depthOfNode == 1 + ids = 1; + else + ids = zeros(1,min(obj.w, depthOfNode-1)); + % Loop on w, the number of parents associated to one node + for i=1:min(obj.w, depthOfNode-1) + ids(i) = floor(node/2); + node = floor(node/2); + end end % Return return @@ -74,5 +79,10 @@ classdef thinTree function output = getNumberOfElements(obj) output = 2^obj.depth - 1; end + + % Returns the depth of a node + function output = getNodeDepth(obj, nodeID) + output = ceil(log(nodeID+1)/log(2)); + 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 index c7dfed6c0..4d21db896 100644 --- a/matlab/gtsam_tests/thinTreeBayesNet.m +++ b/matlab/gtsam_tests/thinTreeBayesNet.m @@ -1,7 +1,7 @@ function [bayesNet, tree] = thinTreeBayesNet(d,w) import gtsam.* bayesNet = GaussianBayesNet; -tree = thinTree(3,2); +tree = thinTree(d,w); % Filling the tree @@ -10,24 +10,30 @@ 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() +for i=1:tree.getNumberOfElements() % Getting the parents of that node parents = tree.getParents(i); % Create and link the corresponding GaussianConditionals - if tree.getW == 1 + if tree.getW == 1 || tree.getNodeDepth(i) <= 2 % Creation of the GaussianConditional - gc = gtsam.GaussianConditional(parents(1), 5*rand(1), 5*rand(1)); + gc = gtsam.GaussianConditional(parents(1), 5*rand(1), 5*rand(1), i, 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)); + tree = tree.addContent({gc,parents}, i); + elseif tree.getW == 2 && tree.getNodeDepth(i) > 2 + % Creation of the GaussianConditional associated with the first + % parent + gc = gtsam.GaussianConditional(parents(1), 5*rand(1), 5*rand(1), i, 5*rand(1), 5*rand(1)); + % Getting it into the GaussianBayesNet + bayesNet.push_front(gc); + % Creation of the GaussianConditionalj associated with the second + % parent + gc = gtsam.GaussianConditional(parents(2), 5*rand(1), 5*rand(1), i, 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); + tree = tree.addContent({gc,parents}, i); end end end \ No newline at end of file