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