Mercurial > hg > camir-aes2014
comparison toolboxes/FullBNT-1.0.7/bnt/examples/dynamic/HHMM/Map/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 obs_model = 'unique'; | |
6 | |
7 for i=1:2:length(varargin) | |
8 switch varargin{i}, | |
9 case 'p', p = varargin{i+1}; | |
10 case 'obs_model', obs_model = varargin{i+1}; | |
11 end | |
12 end | |
13 | |
14 | |
15 q = 1-p; | |
16 unique_obs = strcmp(obs_model, 'unique'); | |
17 | |
18 % assign numbers to the nodes in topological order | |
19 U = 1; A = 2; C = 3; F = 4; | |
20 if unique_obs | |
21 onodes = 5; | |
22 else | |
23 N = 5; E = 6; S = 7; W = 8; % north, east, south, west | |
24 onodes = [N E S W]; | |
25 end | |
26 | |
27 % create graph structure | |
28 | |
29 ss = 4 + length(onodes); % slice size | |
30 intra = zeros(ss,ss); | |
31 intra(U,F)=1; | |
32 intra(A,[C F onodes])=1; | |
33 intra(C,[F onodes])=1; | |
34 | |
35 inter = zeros(ss,ss); | |
36 inter(U,[A C])=1; | |
37 inter(A,[A C])=1; | |
38 inter(F,[A C])=1; | |
39 inter(C,C)=1; | |
40 | |
41 % node sizes | |
42 ns = zeros(1,ss); | |
43 ns(U) = 2; % left/right | |
44 ns(A) = 2; | |
45 ns(C) = 3; | |
46 ns(F) = 2; | |
47 if unique_obs | |
48 ns(onodes) = 5; % we will assign each state a unique symbol | |
49 else | |
50 ns(onodes) = 2; | |
51 end | |
52 l = 1; r = 2; % left/right | |
53 L = 1; R = 2; | |
54 | |
55 % Make the DBN | |
56 bnet = mk_dbn(intra, inter, ns, 'observed', onodes); | |
57 eclass = bnet.equiv_class; | |
58 | |
59 | |
60 | |
61 % Define CPDs for slice 1 | |
62 % We clamp all the CPDs that are not tied, | |
63 % since we cannot learn them from a single sequence. | |
64 | |
65 % uniform probs over actions (the input could be chosen from a policy) | |
66 bnet.CPD{eclass(U,1)} = tabular_CPD(bnet, U, 'CPT', mk_stochastic(ones(ns(U),1)), ... | |
67 'adjustable', 0); | |
68 | |
69 % uniform probs over starting abstract state | |
70 bnet.CPD{eclass(A,1)} = tabular_CPD(bnet, A, 'CPT', mk_stochastic(ones(ns(A),1)), ... | |
71 'adjustable', 0); | |
72 | |
73 % Uniform probs over starting concrete state, modulo the fact | |
74 % that corridor 2 is only of length 2. | |
75 CPT = zeros(ns(A), ns(C)); % CPT(i,j) = P(C starts in j | A=i) | |
76 CPT(1, :) = [1/3 1/3 1/3]; | |
77 CPT(2, :) = [1/2 1/2 0]; | |
78 bnet.CPD{eclass(C,1)} = tabular_CPD(bnet, C, 'CPT', CPT, 'adjustable', 0); | |
79 | |
80 % Termination probs | |
81 CPT = zeros(ns(U), ns(A), ns(C), ns(F)); | |
82 CPT(r,1,1,:) = [1 0]; | |
83 CPT(r,1,2,:) = [1 0]; | |
84 CPT(r,1,3,:) = [q p]; | |
85 CPT(r,2,1,:) = [1 0]; | |
86 CPT(r,2,2,:) = [q p]; | |
87 CPT(l,1,1,:) = [q p]; | |
88 CPT(l,1,2,:) = [1 0]; | |
89 CPT(l,1,3,:) = [1 0]; | |
90 CPT(l,2,1,:) = [q p]; | |
91 CPT(l,2,2,:) = [1 0]; | |
92 | |
93 bnet.CPD{eclass(F,1)} = tabular_CPD(bnet, F, 'CPT', CPT); | |
94 | |
95 | |
96 % Observation model | |
97 if unique_obs | |
98 CPT = zeros(ns(A), ns(C), 5); | |
99 CPT(1,1,1)=1; % Theo state 4 | |
100 CPT(1,2,2)=1; % Theo state 5 | |
101 CPT(1,3,3)=1; % Theo state 6 | |
102 CPT(2,1,4)=1; % Theo state 9 | |
103 CPT(2,2,5)=1; % Theo state 10 | |
104 %CPT(2,3,:) undefined | |
105 O = onodes(1); | |
106 bnet.CPD{eclass(O,1)} = tabular_CPD(bnet, O, 'CPT', CPT); | |
107 else | |
108 % north/east/south/west can see wall (1) or opening (2) | |
109 CPT = zeros(ns(A), ns(C), 2); | |
110 CPT(:,:,1) = q; | |
111 CPT(:,:,2) = p; | |
112 bnet.CPD{eclass(W,1)} = tabular_CPD(bnet, W, 'CPT', CPT); | |
113 bnet.CPD{eclass(E,1)} = tabular_CPD(bnet, E, 'CPT', CPT); | |
114 CPT = zeros(ns(A), ns(C), 2); | |
115 CPT(:,:,1) = p; | |
116 CPT(:,:,2) = q; | |
117 bnet.CPD{eclass(S,1)} = tabular_CPD(bnet, S, 'CPT', CPT); | |
118 bnet.CPD{eclass(N,1)} = tabular_CPD(bnet, N, 'CPT', CPT); | |
119 end | |
120 | |
121 % Define the CPDs for slice 2 | |
122 | |
123 % Abstract | |
124 | |
125 % Since the top level never resets, the starting distribution is irrelevant: | |
126 % A2 will be determined by sampling from transmat(A1,:). | |
127 % But the code requires we specify it anyway; we make it all 0s, a dummy value. | |
128 startprob = zeros(ns(U), ns(A)); | |
129 | |
130 transmat = zeros(ns(U), ns(A), ns(A)); | |
131 transmat(R,1,:) = [q p]; | |
132 transmat(R,2,:) = [0 1]; | |
133 transmat(L,1,:) = [1 0]; | |
134 transmat(L,2,:) = [p q]; | |
135 | |
136 % Qps are the parents we condition the parameters on, in this case just | |
137 % the past action. | |
138 bnet.CPD{eclass(A,2)} = hhmm2Q_CPD(bnet, A+ss, 'Fbelow', F, ... | |
139 'startprob', startprob, 'transprob', transmat); | |
140 | |
141 | |
142 | |
143 % Concrete | |
144 | |
145 transmat = zeros(ns(C), ns(U), ns(A), ns(C)); | |
146 transmat(1,r,1,:) = [q p 0.0]; | |
147 transmat(2,r,1,:) = [0.0 q p]; | |
148 transmat(3,r,1,:) = [0.0 0.0 1.0]; | |
149 transmat(1,r,2,:) = [q p 0.0]; | |
150 transmat(2,r,2,:) = [0.0 1.0 0.0]; | |
151 % | |
152 transmat(1,l,1,:) = [1.0 0.0 0.0]; | |
153 transmat(2,l,1,:) = [p q 0.0]; | |
154 transmat(3,l,1,:) = [0.0 p q]; | |
155 transmat(1,l,2,:) = [1.0 0.0 0.0]; | |
156 transmat(2,l,2,:) = [p q 0.0]; | |
157 | |
158 % Add a new dimension for A(t-1), by copying old vals, | |
159 % so the matrix is the same size as startprob | |
160 | |
161 | |
162 transmat = reshape(transmat, [ns(C) ns(U) ns(A) 1 ns(C)]); | |
163 transmat = repmat(transmat, [1 1 1 ns(A) 1]); | |
164 | |
165 % startprob(C(t-1), U(t-1), A(t-1), A(t), C(t)) | |
166 startprob = zeros(ns(C), ns(U), ns(A), ns(A), ns(C)); | |
167 startprob(1,L,1,1,:) = [1.0 0.0 0.0]; | |
168 startprob(3,R,1,2,:) = [1.0 0.0 0.0]; | |
169 startprob(3,R,1,1,:) = [0.0 0.0 1.0]; | |
170 % | |
171 startprob(1,L,2,1,:) = [0.0 0.0 010]; | |
172 startprob(2,L,2,1,:) = [1.0 0.0 0.0]; | |
173 startprob(2,R,2,2,:) = [0.0 1.0 0.0]; | |
174 | |
175 % want transmat(U,A,C,At,Ct), ie. in topo order | |
176 transmat = permute(transmat, [2 3 1 4 5]); | |
177 startprob = permute(startprob, [2 3 1 4 5]); | |
178 bnet.CPD{eclass(C,2)} = hhmm2Q_CPD(bnet, C+ss, 'Fself', F, ... | |
179 'startprob', startprob, 'transprob', transmat); | |
180 | |
181 |