Files and test files for the thinTree. To be debugged.
parent
ea2c13bca3
commit
0357559827
|
@ -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);
|
|
@ -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);
|
|
@ -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);
|
|
@ -0,0 +1,5 @@
|
|||
function bayesTree = thinBayesTree(depth, width)
|
||||
import gtsam.*
|
||||
bayesNet = thinTreeBayesNet(depth, width);
|
||||
bayesTree = GaussianBayesTree(bayesNet);
|
||||
end
|
|
@ -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
|
|
@ -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
|
Loading…
Reference in New Issue