wolffd@0: function bnet = mk_map_hhmm(varargin) wolffd@0: wolffd@0: % p is the prob of a successful move (defines the reliability of motors) wolffd@0: p = 1; wolffd@0: obs_model = 'unique'; wolffd@0: wolffd@0: for i=1:2:length(varargin) wolffd@0: switch varargin{i}, wolffd@0: case 'p', p = varargin{i+1}; wolffd@0: case 'obs_model', obs_model = varargin{i+1}; wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: wolffd@0: q = 1-p; wolffd@0: unique_obs = strcmp(obs_model, 'unique'); wolffd@0: wolffd@0: % assign numbers to the nodes in topological order wolffd@0: U = 1; A = 2; C = 3; F = 4; wolffd@0: if unique_obs wolffd@0: onodes = 5; wolffd@0: else wolffd@0: N = 5; E = 6; S = 7; W = 8; % north, east, south, west wolffd@0: onodes = [N E S W]; wolffd@0: end wolffd@0: wolffd@0: % create graph structure wolffd@0: wolffd@0: ss = 4 + length(onodes); % slice size wolffd@0: intra = zeros(ss,ss); wolffd@0: intra(U,F)=1; wolffd@0: intra(A,[C F onodes])=1; wolffd@0: intra(C,[F onodes])=1; wolffd@0: wolffd@0: inter = zeros(ss,ss); wolffd@0: inter(U,[A C])=1; wolffd@0: inter(A,[A C])=1; wolffd@0: inter(F,[A C])=1; wolffd@0: inter(C,C)=1; wolffd@0: wolffd@0: % node sizes wolffd@0: ns = zeros(1,ss); wolffd@0: ns(U) = 2; % left/right wolffd@0: ns(A) = 2; wolffd@0: ns(C) = 3; wolffd@0: ns(F) = 2; wolffd@0: if unique_obs wolffd@0: ns(onodes) = 5; % we will assign each state a unique symbol wolffd@0: else wolffd@0: ns(onodes) = 2; wolffd@0: end wolffd@0: l = 1; r = 2; % left/right wolffd@0: L = 1; R = 2; wolffd@0: wolffd@0: % Make the DBN wolffd@0: bnet = mk_dbn(intra, inter, ns, 'observed', onodes); wolffd@0: eclass = bnet.equiv_class; wolffd@0: wolffd@0: wolffd@0: wolffd@0: % Define CPDs for slice 1 wolffd@0: % We clamp all the CPDs that are not tied, wolffd@0: % since we cannot learn them from a single sequence. wolffd@0: wolffd@0: % uniform probs over actions (the input could be chosen from a policy) wolffd@0: bnet.CPD{eclass(U,1)} = tabular_CPD(bnet, U, 'CPT', mk_stochastic(ones(ns(U),1)), ... wolffd@0: 'adjustable', 0); wolffd@0: wolffd@0: % uniform probs over starting abstract state wolffd@0: bnet.CPD{eclass(A,1)} = tabular_CPD(bnet, A, 'CPT', mk_stochastic(ones(ns(A),1)), ... wolffd@0: 'adjustable', 0); wolffd@0: wolffd@0: % Uniform probs over starting concrete state, modulo the fact wolffd@0: % that corridor 2 is only of length 2. wolffd@0: CPT = zeros(ns(A), ns(C)); % CPT(i,j) = P(C starts in j | A=i) wolffd@0: CPT(1, :) = [1/3 1/3 1/3]; wolffd@0: CPT(2, :) = [1/2 1/2 0]; wolffd@0: bnet.CPD{eclass(C,1)} = tabular_CPD(bnet, C, 'CPT', CPT, 'adjustable', 0); wolffd@0: wolffd@0: % Termination probs wolffd@0: CPT = zeros(ns(U), ns(A), ns(C), ns(F)); wolffd@0: CPT(r,1,1,:) = [1 0]; wolffd@0: CPT(r,1,2,:) = [1 0]; wolffd@0: CPT(r,1,3,:) = [q p]; wolffd@0: CPT(r,2,1,:) = [1 0]; wolffd@0: CPT(r,2,2,:) = [q p]; wolffd@0: CPT(l,1,1,:) = [q p]; wolffd@0: CPT(l,1,2,:) = [1 0]; wolffd@0: CPT(l,1,3,:) = [1 0]; wolffd@0: CPT(l,2,1,:) = [q p]; wolffd@0: CPT(l,2,2,:) = [1 0]; wolffd@0: wolffd@0: bnet.CPD{eclass(F,1)} = tabular_CPD(bnet, F, 'CPT', CPT); wolffd@0: wolffd@0: wolffd@0: % Observation model wolffd@0: if unique_obs wolffd@0: CPT = zeros(ns(A), ns(C), 5); wolffd@0: CPT(1,1,1)=1; % Theo state 4 wolffd@0: CPT(1,2,2)=1; % Theo state 5 wolffd@0: CPT(1,3,3)=1; % Theo state 6 wolffd@0: CPT(2,1,4)=1; % Theo state 9 wolffd@0: CPT(2,2,5)=1; % Theo state 10 wolffd@0: %CPT(2,3,:) undefined wolffd@0: O = onodes(1); wolffd@0: bnet.CPD{eclass(O,1)} = tabular_CPD(bnet, O, 'CPT', CPT); wolffd@0: else wolffd@0: % north/east/south/west can see wall (1) or opening (2) wolffd@0: CPT = zeros(ns(A), ns(C), 2); wolffd@0: CPT(:,:,1) = q; wolffd@0: CPT(:,:,2) = p; wolffd@0: bnet.CPD{eclass(W,1)} = tabular_CPD(bnet, W, 'CPT', CPT); wolffd@0: bnet.CPD{eclass(E,1)} = tabular_CPD(bnet, E, 'CPT', CPT); wolffd@0: CPT = zeros(ns(A), ns(C), 2); wolffd@0: CPT(:,:,1) = p; wolffd@0: CPT(:,:,2) = q; wolffd@0: bnet.CPD{eclass(S,1)} = tabular_CPD(bnet, S, 'CPT', CPT); wolffd@0: bnet.CPD{eclass(N,1)} = tabular_CPD(bnet, N, 'CPT', CPT); wolffd@0: end wolffd@0: wolffd@0: % Define the CPDs for slice 2 wolffd@0: wolffd@0: % Abstract wolffd@0: wolffd@0: % Since the top level never resets, the starting distribution is irrelevant: wolffd@0: % A2 will be determined by sampling from transmat(A1,:). wolffd@0: % But the code requires we specify it anyway; we make it all 0s, a dummy value. wolffd@0: startprob = zeros(ns(U), ns(A)); wolffd@0: wolffd@0: transmat = zeros(ns(U), ns(A), ns(A)); wolffd@0: transmat(R,1,:) = [q p]; wolffd@0: transmat(R,2,:) = [0 1]; wolffd@0: transmat(L,1,:) = [1 0]; wolffd@0: transmat(L,2,:) = [p q]; wolffd@0: wolffd@0: % Qps are the parents we condition the parameters on, in this case just wolffd@0: % the past action. wolffd@0: bnet.CPD{eclass(A,2)} = hhmm2Q_CPD(bnet, A+ss, 'Fbelow', F, ... wolffd@0: 'startprob', startprob, 'transprob', transmat); wolffd@0: wolffd@0: wolffd@0: wolffd@0: % Concrete wolffd@0: wolffd@0: transmat = zeros(ns(C), ns(U), ns(A), ns(C)); wolffd@0: transmat(1,r,1,:) = [q p 0.0]; wolffd@0: transmat(2,r,1,:) = [0.0 q p]; wolffd@0: transmat(3,r,1,:) = [0.0 0.0 1.0]; wolffd@0: transmat(1,r,2,:) = [q p 0.0]; wolffd@0: transmat(2,r,2,:) = [0.0 1.0 0.0]; wolffd@0: % wolffd@0: transmat(1,l,1,:) = [1.0 0.0 0.0]; wolffd@0: transmat(2,l,1,:) = [p q 0.0]; wolffd@0: transmat(3,l,1,:) = [0.0 p q]; wolffd@0: transmat(1,l,2,:) = [1.0 0.0 0.0]; wolffd@0: transmat(2,l,2,:) = [p q 0.0]; wolffd@0: wolffd@0: % Add a new dimension for A(t-1), by copying old vals, wolffd@0: % so the matrix is the same size as startprob wolffd@0: wolffd@0: wolffd@0: transmat = reshape(transmat, [ns(C) ns(U) ns(A) 1 ns(C)]); wolffd@0: transmat = repmat(transmat, [1 1 1 ns(A) 1]); wolffd@0: wolffd@0: % startprob(C(t-1), U(t-1), A(t-1), A(t), C(t)) wolffd@0: startprob = zeros(ns(C), ns(U), ns(A), ns(A), ns(C)); wolffd@0: startprob(1,L,1,1,:) = [1.0 0.0 0.0]; wolffd@0: startprob(3,R,1,2,:) = [1.0 0.0 0.0]; wolffd@0: startprob(3,R,1,1,:) = [0.0 0.0 1.0]; wolffd@0: % wolffd@0: startprob(1,L,2,1,:) = [0.0 0.0 010]; wolffd@0: startprob(2,L,2,1,:) = [1.0 0.0 0.0]; wolffd@0: startprob(2,R,2,2,:) = [0.0 1.0 0.0]; wolffd@0: wolffd@0: % want transmat(U,A,C,At,Ct), ie. in topo order wolffd@0: transmat = permute(transmat, [2 3 1 4 5]); wolffd@0: startprob = permute(startprob, [2 3 1 4 5]); wolffd@0: bnet.CPD{eclass(C,2)} = hhmm2Q_CPD(bnet, C+ss, 'Fself', F, ... wolffd@0: 'startprob', startprob, 'transprob', transmat); wolffd@0: wolffd@0: