comparison toolboxes/FullBNT-1.0.7/bnt/examples/dynamic/HHMM/Map/Old/mk_map_hhmm.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 function bnet = mk_map_hhmm(varargin)
2
3 % p is the prob of a successful move (defines the reliability of motors)
4 p = 1;
5 num_obs_nodes = 1;
6
7 for i=1:2:length(varargin)
8 switch varargin{i},
9 case 'p', p = varargin{i+1};
10 case 'numobs', num_obs_node = varargin{i+1};
11 end
12 end
13
14
15 q = 1-p;
16
17 % assign numbers to the nodes in topological order
18 U = 1; A = 2; C = 3; F = 4; O = 5;
19
20 % create graph structure
21
22 ss = 5; % slice size
23 intra = zeros(ss,ss);
24 intra(U,F)=1;
25 intra(A,[C F O])=1;
26 intra(C,[F O])=1;
27
28 inter = zeros(ss,ss);
29 inter(U,[A C])=1;
30 inter(A,[A C])=1;
31 inter(F,[A C])=1;
32 inter(C,C)=1;
33
34 % node sizes
35 ns = zeros(1,ss);
36 ns(U) = 2; % left/right
37 ns(A) = 2;
38 ns(C) = 3;
39 ns(F) = 2;
40 ns(O) = 5; % we will assign each state a unique symbol
41 l = 1; r = 2; % left/right
42 L = 1; R = 2;
43
44 % Make the DBN
45 bnet = mk_dbn(intra, inter, ns, 'observed', O);
46 eclass = bnet.equiv_class;
47
48
49
50 % Define CPDs for slice 1
51 % We clamp all of them, i.e., do not try to learn them.
52
53 % uniform probs over actions (the input could be chosen from a policy)
54 bnet.CPD{eclass(U,1)} = tabular_CPD(bnet, U, 'CPT', mk_stochastic(ones(ns(U),1)), ...
55 'adjustable', 0);
56
57 % uniform probs over starting abstract state
58 bnet.CPD{eclass(A,1)} = tabular_CPD(bnet, A, 'CPT', mk_stochastic(ones(ns(A),1)), ...
59 'adjustable', 0);
60
61 % Uniform probs over starting concrete state, modulo the fact
62 % that corridor 2 is only of length 2.
63 CPT = zeros(ns(A), ns(C)); % CPT(i,j) = P(C starts in j | A=i)
64 CPT(1, :) = [1/3 1/3 1/3];
65 CPT(2, :) = [1/2 1/2 0];
66 bnet.CPD{eclass(C,1)} = tabular_CPD(bnet, C, 'CPT', CPT, 'adjustable', 0);
67
68 % Termination probs
69 CPT = zeros(ns(U), ns(A), ns(C), ns(F));
70 CPT(r,1,1,:) = [1 0];
71 CPT(r,1,2,:) = [1 0];
72 CPT(r,1,3,:) = [q p];
73 CPT(r,2,1,:) = [1 0];
74 CPT(r,2,2,:) = [q p];
75 CPT(l,1,1,:) = [q p];
76 CPT(l,1,2,:) = [1 0];
77 CPT(l,1,3,:) = [1 0];
78 CPT(l,2,1,:) = [q p];
79 CPT(l,2,2,:) = [1 0];
80
81 bnet.CPD{eclass(F,1)} = tabular_CPD(bnet, F, 'CPT', CPT);
82
83
84 % Assign each state a unique observation
85 CPT = zeros(ns(A), ns(C), ns(O));
86 CPT(1,1,1)=1;
87 CPT(1,2,2)=1;
88 CPT(1,3,3)=1;
89 CPT(2,1,4)=1;
90 CPT(2,2,5)=1;
91 %CPT(2,3,:) undefined
92
93 bnet.CPD{eclass(O,1)} = tabular_CPD(bnet, O, 'CPT', CPT);
94
95
96 % Define the CPDs for slice 2
97
98 % Abstract
99
100 % Since the top level never resets, the starting distribution is irrelevant:
101 % A2 will be determined by sampling from transmat(A1,:).
102 % But the code requires we specify it anyway; we make it all 0s, a dummy value.
103 startprob = zeros(ns(U), ns(A));
104
105 transmat = zeros(ns(U), ns(A), ns(A));
106 transmat(R,1,:) = [q p];
107 transmat(R,2,:) = [0 1];
108 transmat(L,1,:) = [1 0];
109 transmat(L,2,:) = [p q];
110
111 % Qps are the parents we condition the parameters on, in this case just
112 % the past action.
113 bnet.CPD{eclass(A,2)} = hhmm2Q_CPD(bnet, A+ss, 'Fbelow', F, ...
114 'startprob', startprob, 'transprob', transmat);
115
116
117
118 % Concrete
119
120 transmat = zeros(ns(C), ns(U), ns(A), ns(C));
121 transmat(1,r,1,:) = [q p 0.0];
122 transmat(2,r,1,:) = [0.0 q p];
123 transmat(3,r,1,:) = [0.0 0.0 1.0];
124 transmat(1,r,2,:) = [q p 0.0];
125 transmat(2,r,2,:) = [0.0 1.0 0.0];
126 %
127 transmat(1,l,1,:) = [1.0 0.0 0.0];
128 transmat(2,l,1,:) = [p q 0.0];
129 transmat(3,l,1,:) = [0.0 p q];
130 transmat(1,l,2,:) = [1.0 0.0 0.0];
131 transmat(2,l,2,:) = [p q 0.0];
132
133 % Add a new dimension for A(t-1), by copying old vals,
134 % so the matrix is the same size as startprob
135
136
137 transmat = reshape(transmat, [ns(C) ns(U) ns(A) 1 ns(C)]);
138 transmat = repmat(transmat, [1 1 1 ns(A) 1]);
139
140 % startprob(C(t-1), U(t-1), A(t-1), A(t), C(t))
141 startprob = zeros(ns(C), ns(U), ns(A), ns(A), ns(C));
142 startprob(1,L,1,1,:) = [1.0 0.0 0.0];
143 startprob(3,R,1,2,:) = [1.0 0.0 0.0];
144 startprob(3,R,1,1,:) = [0.0 0.0 1.0];
145 %
146 startprob(1,L,2,1,:) = [0.0 0.0 010];
147 startprob(2,L,2,1,:) = [1.0 0.0 0.0];
148 startprob(2,R,2,2,:) = [0.0 1.0 0.0];
149
150 % want transmat(U,A,C,At,Ct), ie. in topo order
151 transmat = permute(transmat, [2 3 1 4 5]);
152 startprob = permute(startprob, [2 3 1 4 5]);
153 bnet.CPD{eclass(C,2)} = hhmm2Q_CPD(bnet, C+ss, 'Fself', F, ...
154 'startprob', startprob, 'transprob', transmat);
155
156