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