about summary refs log tree commit diff
path: root/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map
diff options
context:
space:
mode:
Diffstat (limited to 'sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map')
-rw-r--r--sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/CVS/Entries6
-rw-r--r--sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/CVS/Repository1
-rw-r--r--sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/CVS/Root1
-rw-r--r--sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/Old/CVS/Entries2
-rw-r--r--sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/Old/CVS/Repository1
-rw-r--r--sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/Old/CVS/Root1
-rw-r--r--sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/Old/mk_map_hhmm.m156
-rw-r--r--sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/disp_map_hhmm.m13
-rw-r--r--sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/learn_map.m40
-rw-r--r--sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/mk_map_hhmm.m181
-rw-r--r--sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/mk_rnd_map_hhmm.m73
-rw-r--r--sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/sample_from_map.m41
12 files changed, 516 insertions, 0 deletions
diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/CVS/Entries b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/CVS/Entries
new file mode 100644
index 00000000..dc32f52b
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/CVS/Entries
@@ -0,0 +1,6 @@
+/disp_map_hhmm.m/1.1.1.1/Tue Sep 24 22:45:56 2002//
+/learn_map.m/1.1.1.1/Sat Jan 11 18:48:46 2003//
+/mk_map_hhmm.m/1.1.1.1/Tue Sep 24 10:49:52 2002//
+/mk_rnd_map_hhmm.m/1.1.1.1/Tue Sep 24 22:13:48 2002//
+/sample_from_map.m/1.1.1.1/Tue Sep 24 13:02:30 2002//
+D/Old////
diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/CVS/Repository b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/CVS/Repository
new file mode 100644
index 00000000..66b47bbc
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/CVS/Repository
@@ -0,0 +1 @@
+FullBNT/BNT/examples/dynamic/HHMM/Map
diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/CVS/Root b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/CVS/Root
new file mode 100644
index 00000000..f3bd14a6
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/CVS/Root
@@ -0,0 +1 @@
+:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt
diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/Old/CVS/Entries b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/Old/CVS/Entries
new file mode 100644
index 00000000..6079d451
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/Old/CVS/Entries
@@ -0,0 +1,2 @@
+/mk_map_hhmm.m/1.1.1.1/Tue Sep 24 07:02:44 2002//
+D
diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/Old/CVS/Repository b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/Old/CVS/Repository
new file mode 100644
index 00000000..354057a9
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/Old/CVS/Repository
@@ -0,0 +1 @@
+FullBNT/BNT/examples/dynamic/HHMM/Map/Old
diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/Old/CVS/Root b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/Old/CVS/Root
new file mode 100644
index 00000000..f3bd14a6
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/Old/CVS/Root
@@ -0,0 +1 @@
+:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt
diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/Old/mk_map_hhmm.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/Old/mk_map_hhmm.m
new file mode 100644
index 00000000..7b646745
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/Old/mk_map_hhmm.m
@@ -0,0 +1,156 @@
+function bnet = mk_map_hhmm(varargin)
+
+% p is the prob of a successful move (defines the reliability of motors)
+p = 1;
+num_obs_nodes = 1;
+
+for i=1:2:length(varargin)
+  switch varargin{i},
+   case 'p', p = varargin{i+1};
+   case 'numobs', num_obs_node = varargin{i+1};
+  end
+end
+
+
+q = 1-p;
+
+% assign numbers to the nodes in topological order
+U = 1; A = 2; C = 3; F = 4; O = 5;
+
+% create graph structure
+
+ss = 5; % slice size
+intra = zeros(ss,ss);
+intra(U,F)=1;
+intra(A,[C F O])=1;
+intra(C,[F O])=1;
+
+inter = zeros(ss,ss);
+inter(U,[A C])=1;
+inter(A,[A C])=1;
+inter(F,[A C])=1;
+inter(C,C)=1;
+
+% node sizes
+ns = zeros(1,ss);
+ns(U) = 2; % left/right
+ns(A) = 2;
+ns(C) = 3;
+ns(F) = 2;
+ns(O) = 5; % we will assign each state a unique symbol
+l = 1; r = 2; % left/right
+L = 1; R = 2;
+
+% Make the DBN
+bnet = mk_dbn(intra, inter, ns, 'observed', O);
+eclass = bnet.equiv_class;
+
+
+
+% Define CPDs for slice 1
+% We clamp all of them, i.e., do not try to learn them.
+
+% uniform probs over actions (the input could be chosen from a policy)
+bnet.CPD{eclass(U,1)} = tabular_CPD(bnet, U, 'CPT', mk_stochastic(ones(ns(U),1)), ...
+				    'adjustable', 0);
+
+% uniform probs over starting abstract state
+bnet.CPD{eclass(A,1)} = tabular_CPD(bnet, A, 'CPT', mk_stochastic(ones(ns(A),1)), ...
+				    'adjustable', 0);
+
+% Uniform probs over starting concrete state, modulo the fact
+% that corridor 2 is only of length 2.
+CPT = zeros(ns(A), ns(C)); % CPT(i,j) = P(C starts in j | A=i)
+CPT(1, :) = [1/3 1/3 1/3];
+CPT(2, :) = [1/2 1/2 0];
+bnet.CPD{eclass(C,1)} = tabular_CPD(bnet, C, 'CPT', CPT, 'adjustable', 0);
+
+% Termination probs
+CPT = zeros(ns(U), ns(A), ns(C), ns(F));
+CPT(r,1,1,:) = [1 0];
+CPT(r,1,2,:) = [1 0];
+CPT(r,1,3,:) = [q p];
+CPT(r,2,1,:) = [1 0];
+CPT(r,2,2,:) = [q p];
+CPT(l,1,1,:) = [q p];
+CPT(l,1,2,:) = [1 0];
+CPT(l,1,3,:) = [1 0];
+CPT(l,2,1,:) = [q p];
+CPT(l,2,2,:) = [1 0];
+
+bnet.CPD{eclass(F,1)} = tabular_CPD(bnet, F, 'CPT', CPT);
+
+
+% Assign each state a unique observation
+CPT = zeros(ns(A), ns(C), ns(O));
+CPT(1,1,1)=1; 
+CPT(1,2,2)=1;
+CPT(1,3,3)=1;
+CPT(2,1,4)=1;
+CPT(2,2,5)=1;
+%CPT(2,3,:) undefined
+
+bnet.CPD{eclass(O,1)} = tabular_CPD(bnet, O, 'CPT', CPT);
+
+
+% Define the CPDs for slice 2
+
+% Abstract
+
+% Since the top level never resets, the starting distribution is irrelevant:
+% A2 will be determined by sampling from transmat(A1,:).
+% But the code requires we specify it anyway; we make it all 0s, a dummy value.
+startprob = zeros(ns(U), ns(A));
+
+transmat = zeros(ns(U), ns(A), ns(A));
+transmat(R,1,:) = [q p];
+transmat(R,2,:) = [0 1];
+transmat(L,1,:) = [1 0];
+transmat(L,2,:) = [p q];
+
+% Qps are the parents we condition the parameters on, in this case just
+% the past action.
+bnet.CPD{eclass(A,2)} = hhmm2Q_CPD(bnet, A+ss, 'Fbelow', F, ...
+				  'startprob', startprob, 'transprob', transmat);
+
+
+
+% Concrete
+
+transmat = zeros(ns(C), ns(U), ns(A), ns(C));
+transmat(1,r,1,:) = [q p 0.0];
+transmat(2,r,1,:) = [0.0 q p];
+transmat(3,r,1,:) = [0.0 0.0 1.0];
+transmat(1,r,2,:) = [q p 0.0];
+transmat(2,r,2,:) = [0.0 1.0 0.0];
+%
+transmat(1,l,1,:) = [1.0 0.0 0.0];
+transmat(2,l,1,:) = [p q 0.0];
+transmat(3,l,1,:) = [0.0 p q];
+transmat(1,l,2,:) = [1.0 0.0 0.0];
+transmat(2,l,2,:) = [p q 0.0];
+
+% Add a new dimension for A(t-1), by copying old vals,
+% so the matrix is the same size as startprob
+
+
+transmat = reshape(transmat, [ns(C) ns(U) ns(A) 1 ns(C)]);
+transmat = repmat(transmat, [1 1 1 ns(A) 1]);
+
+% startprob(C(t-1), U(t-1), A(t-1), A(t), C(t))
+startprob = zeros(ns(C), ns(U), ns(A), ns(A), ns(C));
+startprob(1,L,1,1,:) = [1.0 0.0 0.0];
+startprob(3,R,1,2,:) = [1.0 0.0 0.0];
+startprob(3,R,1,1,:) = [0.0 0.0 1.0];
+% 
+startprob(1,L,2,1,:) = [0.0 0.0 010];
+startprob(2,L,2,1,:) = [1.0 0.0 0.0];
+startprob(2,R,2,2,:) = [0.0 1.0 0.0];
+
+% want transmat(U,A,C,At,Ct), ie. in topo order
+transmat = permute(transmat, [2 3 1 4 5]);
+startprob  = permute(startprob, [2 3 1 4 5]);
+bnet.CPD{eclass(C,2)} = hhmm2Q_CPD(bnet, C+ss, 'Fself', F, ...
+				  'startprob', startprob, 'transprob', transmat);
+
+
diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/disp_map_hhmm.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/disp_map_hhmm.m
new file mode 100644
index 00000000..0aadf2bb
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/disp_map_hhmm.m
@@ -0,0 +1,13 @@
+function disp_map_hhmm(bnet)
+
+eclass = bnet.equiv_class;
+U = 1; A = 2; C = 3; F = 4;
+
+S = struct(bnet.CPD{eclass(A,2)});
+disp('abstract trans')
+dispcpt(S.transprob)
+
+S = struct(bnet.CPD{eclass(C,2)});
+disp('concrete trans for go left') % UAC AC
+dispcpt(squeeze(S.transprob(1,:,:,:,:)))
+
diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/learn_map.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/learn_map.m
new file mode 100644
index 00000000..ac36586a
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/learn_map.m
@@ -0,0 +1,40 @@
+seed = 1;
+rand('state', seed);
+randn('state', seed);
+
+obs_model = 'unique';  % each cell has a unique label (essentially fully observable)
+%obs_model = 'four'; % each cell generates 4 observations, NESW
+
+% Generate the true network, and a randomization of it
+realnet = mk_map_hhmm('p', 0.9, 'obs_model', obs_model);
+rndnet = mk_rnd_map_hhmm('obs_model', obs_model);
+eclass = realnet.equiv_class;
+U = 1; A = 2; C = 3; F = 4; onodes = 5;
+
+ss = realnet.nnodes_per_slice;
+T = 100;
+evidence = sample_dbn(realnet, 'length', T);
+ev = cell(ss,T);
+ev(onodes,:) = evidence(onodes,:);
+
+infeng = jtree_dbn_inf_engine(rndnet);
+
+if 0
+% suppose we do not observe the final finish node, but only know 
+% it is more likely to be on that off
+ev2 = ev;
+infeng = enter_evidence(infeng, ev2, 'soft_evidence_nodes', [F T], 'soft_evidence',  {[0.3 0.7]'});
+end
+
+
+learnednet = learn_params_dbn_em(infeng, {evidence}, 'max_iter', 5);
+
+disp('real model')
+disp_map_hhmm(realnet)
+
+disp('learned model')
+disp_map_hhmm(learnednet)
+
+disp('rnd model')
+disp_map_hhmm(rndnet)
+
diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/mk_map_hhmm.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/mk_map_hhmm.m
new file mode 100644
index 00000000..7b077ddb
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/mk_map_hhmm.m
@@ -0,0 +1,181 @@
+function bnet = mk_map_hhmm(varargin)
+
+% p is the prob of a successful move (defines the reliability of motors)
+p = 1;
+obs_model = 'unique';
+
+for i=1:2:length(varargin)
+  switch varargin{i},
+   case 'p', p = varargin{i+1};
+   case 'obs_model', obs_model = varargin{i+1};
+  end
+end
+
+
+q = 1-p;
+unique_obs = strcmp(obs_model, 'unique');
+
+% assign numbers to the nodes in topological order
+U = 1; A = 2; C = 3; F = 4;
+if unique_obs
+  onodes = 5;
+else
+  N = 5; E = 6; S = 7; W = 8; % north, east, south, west
+  onodes = [N E S W];
+end
+
+% create graph structure
+
+ss = 4 + length(onodes); % slice size
+intra = zeros(ss,ss);
+intra(U,F)=1;
+intra(A,[C F onodes])=1;
+intra(C,[F onodes])=1;
+
+inter = zeros(ss,ss);
+inter(U,[A C])=1;
+inter(A,[A C])=1;
+inter(F,[A C])=1;
+inter(C,C)=1;
+
+% node sizes
+ns = zeros(1,ss);
+ns(U) = 2; % left/right
+ns(A) = 2;
+ns(C) = 3;
+ns(F) = 2;
+if unique_obs
+  ns(onodes) = 5; % we will assign each state a unique symbol
+else
+  ns(onodes) = 2;
+end
+l = 1; r = 2; % left/right
+L = 1; R = 2;
+
+% Make the DBN
+bnet = mk_dbn(intra, inter, ns, 'observed', onodes);
+eclass = bnet.equiv_class;
+
+
+
+% Define CPDs for slice 1
+% We clamp all the CPDs that are not tied,
+% since we cannot learn them from a single sequence.
+
+% uniform probs over actions (the input could be chosen from a policy)
+bnet.CPD{eclass(U,1)} = tabular_CPD(bnet, U, 'CPT', mk_stochastic(ones(ns(U),1)), ...
+				    'adjustable', 0);
+
+% uniform probs over starting abstract state
+bnet.CPD{eclass(A,1)} = tabular_CPD(bnet, A, 'CPT', mk_stochastic(ones(ns(A),1)), ...
+				    'adjustable', 0);
+
+% Uniform probs over starting concrete state, modulo the fact
+% that corridor 2 is only of length 2.
+CPT = zeros(ns(A), ns(C)); % CPT(i,j) = P(C starts in j | A=i)
+CPT(1, :) = [1/3 1/3 1/3];
+CPT(2, :) = [1/2 1/2 0];
+bnet.CPD{eclass(C,1)} = tabular_CPD(bnet, C, 'CPT', CPT, 'adjustable', 0);
+
+% Termination probs
+CPT = zeros(ns(U), ns(A), ns(C), ns(F));
+CPT(r,1,1,:) = [1 0];
+CPT(r,1,2,:) = [1 0];
+CPT(r,1,3,:) = [q p];
+CPT(r,2,1,:) = [1 0];
+CPT(r,2,2,:) = [q p];
+CPT(l,1,1,:) = [q p];
+CPT(l,1,2,:) = [1 0];
+CPT(l,1,3,:) = [1 0];
+CPT(l,2,1,:) = [q p];
+CPT(l,2,2,:) = [1 0];
+
+bnet.CPD{eclass(F,1)} = tabular_CPD(bnet, F, 'CPT', CPT);
+
+
+% Observation model
+if unique_obs
+  CPT = zeros(ns(A), ns(C), 5);
+  CPT(1,1,1)=1;  % Theo state 4
+  CPT(1,2,2)=1;  % Theo state 5
+  CPT(1,3,3)=1; % Theo state 6
+  CPT(2,1,4)=1; % Theo state 9
+  CPT(2,2,5)=1; % Theo state 10
+  %CPT(2,3,:) undefined
+  O = onodes(1);
+  bnet.CPD{eclass(O,1)} = tabular_CPD(bnet, O, 'CPT', CPT);
+else
+  % north/east/south/west can see wall (1) or opening (2)
+  CPT = zeros(ns(A), ns(C), 2);
+  CPT(:,:,1) = q;
+  CPT(:,:,2) = p;
+  bnet.CPD{eclass(W,1)} = tabular_CPD(bnet, W, 'CPT', CPT);
+  bnet.CPD{eclass(E,1)} = tabular_CPD(bnet, E, 'CPT', CPT);
+  CPT = zeros(ns(A), ns(C), 2);
+  CPT(:,:,1) = p;
+  CPT(:,:,2) = q;
+  bnet.CPD{eclass(S,1)} = tabular_CPD(bnet, S, 'CPT', CPT);
+  bnet.CPD{eclass(N,1)} = tabular_CPD(bnet, N, 'CPT', CPT);
+end
+
+% Define the CPDs for slice 2
+
+% Abstract
+
+% Since the top level never resets, the starting distribution is irrelevant:
+% A2 will be determined by sampling from transmat(A1,:).
+% But the code requires we specify it anyway; we make it all 0s, a dummy value.
+startprob = zeros(ns(U), ns(A));
+
+transmat = zeros(ns(U), ns(A), ns(A));
+transmat(R,1,:) = [q p];
+transmat(R,2,:) = [0 1];
+transmat(L,1,:) = [1 0];
+transmat(L,2,:) = [p q];
+
+% Qps are the parents we condition the parameters on, in this case just
+% the past action.
+bnet.CPD{eclass(A,2)} = hhmm2Q_CPD(bnet, A+ss, 'Fbelow', F, ...
+				  'startprob', startprob, 'transprob', transmat);
+
+
+
+% Concrete
+
+transmat = zeros(ns(C), ns(U), ns(A), ns(C));
+transmat(1,r,1,:) = [q p 0.0];
+transmat(2,r,1,:) = [0.0 q p];
+transmat(3,r,1,:) = [0.0 0.0 1.0];
+transmat(1,r,2,:) = [q p 0.0];
+transmat(2,r,2,:) = [0.0 1.0 0.0];
+%
+transmat(1,l,1,:) = [1.0 0.0 0.0];
+transmat(2,l,1,:) = [p q 0.0];
+transmat(3,l,1,:) = [0.0 p q];
+transmat(1,l,2,:) = [1.0 0.0 0.0];
+transmat(2,l,2,:) = [p q 0.0];
+
+% Add a new dimension for A(t-1), by copying old vals,
+% so the matrix is the same size as startprob
+
+
+transmat = reshape(transmat, [ns(C) ns(U) ns(A) 1 ns(C)]);
+transmat = repmat(transmat, [1 1 1 ns(A) 1]);
+
+% startprob(C(t-1), U(t-1), A(t-1), A(t), C(t))
+startprob = zeros(ns(C), ns(U), ns(A), ns(A), ns(C));
+startprob(1,L,1,1,:) = [1.0 0.0 0.0];
+startprob(3,R,1,2,:) = [1.0 0.0 0.0];
+startprob(3,R,1,1,:) = [0.0 0.0 1.0];
+% 
+startprob(1,L,2,1,:) = [0.0 0.0 010];
+startprob(2,L,2,1,:) = [1.0 0.0 0.0];
+startprob(2,R,2,2,:) = [0.0 1.0 0.0];
+
+% want transmat(U,A,C,At,Ct), ie. in topo order
+transmat = permute(transmat, [2 3 1 4 5]);
+startprob  = permute(startprob, [2 3 1 4 5]);
+bnet.CPD{eclass(C,2)} = hhmm2Q_CPD(bnet, C+ss, 'Fself', F, ...
+				  'startprob', startprob, 'transprob', transmat);
+
+
diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/mk_rnd_map_hhmm.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/mk_rnd_map_hhmm.m
new file mode 100644
index 00000000..76b06fc7
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/mk_rnd_map_hhmm.m
@@ -0,0 +1,73 @@
+function bnet = mk_rnd_map_hhmm(varargin)
+
+% We copy the deterministic structure of the real HHMM,
+% but randomize the probabilities of the adjustable CPDs.
+% The key trick is that 0s in the real HHMM remain 0
+% even when multiplied by a randon number.
+
+obs_model = 'unique';
+
+for i=1:2:length(varargin)
+  switch varargin{i},
+   case 'obs_model', obs_model = varargin{i+1};
+  end
+end
+
+
+unique_obs = strcmp(obs_model, 'unique');
+
+psuccess = 0.9;
+% must be less than 1, so that pfail > 0
+% otherwise we copy too many 0s
+bnet = mk_map_hhmm('p', psuccess, 'obs_model', obs_model);
+ns = bnet.node_sizes;
+ss = bnet.nnodes_per_slice;
+
+U = 1; A = 2; C = 3; F = 4;
+%unique_obs = (bnet.nnodes_per_slice == 5);
+if unique_obs
+  onodes = 5;
+else
+  north = 5; east = 6; south = 7; west = 8;
+  onodes = [north east south west];
+end
+
+eclass = bnet.equiv_class;
+S=struct(bnet.CPD{eclass(F,1)});
+CPT = mk_stochastic(rand(size(S.CPT)) .* S.CPT);
+bnet.CPD{eclass(F,1)} = tabular_CPD(bnet, F, 'CPT', CPT);
+
+
+% Observation model
+if unique_obs
+  CPT = zeros(ns(A), ns(C), 5);
+  CPT(1,1,1)=1;  % Theo state 4
+  CPT(1,2,2)=1;  % Theo state 5
+  CPT(1,3,3)=1; % Theo state 6
+  CPT(2,1,4)=1; % Theo state 9
+  CPT(2,2,5)=1; % Theo state 10
+  %CPT(2,3,:) undefined
+  O = onodes(1);
+  bnet.CPD{eclass(O,1)} = tabular_CPD(bnet, O, 'CPT', CPT);
+else
+  for i=[north east south west]
+    CPT = mk_stochastic(rand(ns(A), ns(C), 2));
+    bnet.CPD{eclass(i,1)} = tabular_CPD(bnet, i, 'CPT', CPT);
+  end
+end
+
+% Define the CPDs for slice 2
+
+startprob = zeros(ns(U), ns(A));
+S = struct(bnet.CPD{eclass(A,2)});
+transprob = mk_stochastic(rand(size(S.transprob)) .* S.transprob);
+bnet.CPD{eclass(A,2)} = hhmm2Q_CPD(bnet, A+ss, 'Fbelow', F, ...
+				  'startprob', startprob, 'transprob', transprob);
+
+S = struct(bnet.CPD{eclass(C,2)});
+transprob = mk_stochastic(rand(size(S.transprob)) .* S.transprob);
+startprob = mk_stochastic(rand(size(S.startprob)) .* S.startprob);
+bnet.CPD{eclass(C,2)} = hhmm2Q_CPD(bnet, C+ss, 'Fself', F, ...
+				  'startprob', startprob, 'transprob', transprob);
+
+
diff --git a/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/sample_from_map.m b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/sample_from_map.m
new file mode 100644
index 00000000..816b741e
--- /dev/null
+++ b/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/Map/sample_from_map.m
@@ -0,0 +1,41 @@
+if 0
+% Generate some sample paths
+
+bnet = mk_map_hhmm('p', 1);
+% assign numbers to the nodes in topological order
+U = 1; A = 2; C = 3; F = 4; O = 5;
+
+
+seed = 0;
+rand('state', seed);
+randn('state', seed);
+
+% control policy = sweep right then left
+T = 10;
+ss = 5;
+ev = cell(ss, T);
+ev(U,:) = num2cell([R*ones(1,5) L*ones(1,5)]);
+
+% fix initial conditions to be in left most state
+ev{A,1} = 1; 
+ev{C,1} = 1; 
+evidence = sample_dbn(bnet, 'length', T, 'evidence', ev)
+
+
+% Now do same but with noisy actuators
+
+bnet = mk_map_hhmm('p', 0.8);
+evidence = sample_dbn(bnet, 'length', T, 'evidence', ev)
+
+end
+
+% Now do same but with 4 observations per slice
+
+bnet = mk_map_hhmm('p', 0.8, 'obs_model', 'four');
+ss = bnet.nnodes_per_slice;
+
+ev = cell(ss, T);
+ev(U,:) = num2cell([R*ones(1,5) L*ones(1,5)]);
+ev{A,1} = 1; 
+ev{C,1} = 1; 
+evidence = sample_dbn(bnet, 'length', T, 'evidence', ev)