Debugged the thinTree. Now works with tree indexing starting at 1 at the root. TODO : make it work with index 0 on a leaf.

release/4.3a0
jdurand7 2012-09-14 23:46:21 +00:00
parent 0357559827
commit 8d85d679cd
4 changed files with 36 additions and 16 deletions

View File

@ -20,4 +20,4 @@
%% Run the tests %% Run the tests
import gtsam.* import gtsam.*
bayesTree = thinBayesTree(3,2); bayesTree = thinBayesTree(3,2);
EQUALITY('7 = bayesTree.size', 7, bayesTree.size); EQUALITY('7 = bayesTree.size', 7, bayesTree.size);

View File

@ -17,7 +17,11 @@
% * @author Jean-Guillaume Durand % * @author Jean-Guillaume Durand
% */ % */
%% Clean the workspace
clc, clear all, close all;
%% Run the tests %% Run the tests
import gtsam.* import gtsam.*
bayesNet = thinTreeBayesNet(3,2); [bayesNet tree] = thinTreeBayesNet(4,2);
EQUALITY('7 = bayesNet.size', 7, bayesNet.size); EQUALITY('7 = bayesNet.size', 7, bayesNet.size);
bayesNet.saveGraph('thinTreeBayesNet.dot');

View File

@ -49,12 +49,17 @@ classdef thinTree
% Function to return the ID's of a node's parents % Function to return the ID's of a node's parents
function ids = getParents(obj, nodeID) function ids = getParents(obj, nodeID)
% Initialisation % Initialisation
ids = zeros(1,obj.w);
node = nodeID; node = nodeID;
% Loop on w, the number of parents associated to one node depthOfNode = obj.getNodeDepth(nodeID);
for i=1:obj.w if depthOfNode == 1
ids(i) = floor(node/2); ids = 1;
node = floor(node/2); 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 end
% Return % Return
return return
@ -74,5 +79,10 @@ classdef thinTree
function output = getNumberOfElements(obj) function output = getNumberOfElements(obj)
output = 2^obj.depth - 1; output = 2^obj.depth - 1;
end end
% Returns the depth of a node
function output = getNodeDepth(obj, nodeID)
output = ceil(log(nodeID+1)/log(2));
end
end % Methods end % Methods
end % Class end % Class

View File

@ -1,7 +1,7 @@
function [bayesNet, tree] = thinTreeBayesNet(d,w) function [bayesNet, tree] = thinTreeBayesNet(d,w)
import gtsam.* import gtsam.*
bayesNet = GaussianBayesNet; bayesNet = GaussianBayesNet;
tree = thinTree(3,2); tree = thinTree(d,w);
% Filling the tree % 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 % Getting it into the GaussianBayesNet
bayesNet.push_front(gc); bayesNet.push_front(gc);
for i=1:2^tree.getNumberOfElements() for i=1:tree.getNumberOfElements()
% Getting the parents of that node % Getting the parents of that node
parents = tree.getParents(i); parents = tree.getParents(i);
% Create and link the corresponding GaussianConditionals % Create and link the corresponding GaussianConditionals
if tree.getW == 1 if tree.getW == 1 || tree.getNodeDepth(i) <= 2
% Creation of the GaussianConditional % 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 % Getting it into the GaussianBayesNet
bayesNet.push_front(gc); bayesNet.push_front(gc);
% Getting it in the thinTree % Getting it in the thinTree
t = tree.addContent({gc,parents}, i); tree = tree.addContent({gc,parents}, i);
elseif tree.getW == 2 elseif tree.getW == 2 && tree.getNodeDepth(i) > 2
% Creation of the GaussianConditional % Creation of the GaussianConditional associated with the first
gc = gtsam.GaussianConditional(parents(2), 5*rand(1), 5*rand(1), parents(1), 5*rand(1), 5*rand(1)); % 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 % Getting it into the GaussianBayesNet
bayesNet.push_front(gc); bayesNet.push_front(gc);
% Getting it in the thinTree % Getting it in the thinTree
t = tree.addContent({gc,parents}, i); tree = tree.addContent({gc,parents}, i);
end end
end end
end end