annotate toolboxes/FullBNT-1.0.7/bnt/examples/dynamic/SLAM/slam_stationary_loopy.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 % This is like skf_data_assoc_gmux, except the objects don't move.
Daniel@0 2 % We are uncertain of their initial positions, and get more and more observations
Daniel@0 3 % over time. The goal is to test deterministic links (0 covariance).
Daniel@0 4 % This is like robot1, except the robot doesn't move and is always at [0 0],
Daniel@0 5 % so the relative location is simply L(s).
Daniel@0 6
Daniel@0 7 nobj = 2;
Daniel@0 8 N = nobj+2;
Daniel@0 9 Xs = 1:nobj;
Daniel@0 10 S = nobj+1;
Daniel@0 11 Y = nobj+2;
Daniel@0 12
Daniel@0 13 intra = zeros(N,N);
Daniel@0 14 inter = zeros(N,N);
Daniel@0 15 intra([Xs S], Y) =1;
Daniel@0 16 for i=1:nobj
Daniel@0 17 inter(Xs(i), Xs(i))=1;
Daniel@0 18 end
Daniel@0 19
Daniel@0 20 Xsz = 2; % state space = (x y)
Daniel@0 21 Ysz = 2;
Daniel@0 22 ns = zeros(1,N);
Daniel@0 23 ns(Xs) = Xsz;
Daniel@0 24 ns(Y) = Ysz;
Daniel@0 25 ns(S) = nobj;
Daniel@0 26
Daniel@0 27 bnet = mk_dbn(intra, inter, ns, 'discrete', S, 'observed', [S Y]);
Daniel@0 28
Daniel@0 29 % For each object, we have
Daniel@0 30 % X(t+1) = F X(t) + noise(Q)
Daniel@0 31 % Y(t) = H X(t) + noise(R)
Daniel@0 32 F = eye(2);
Daniel@0 33 H = eye(2);
Daniel@0 34 Q = 0*eye(Xsz); % no noise in dynamics
Daniel@0 35 R = eye(Ysz);
Daniel@0 36
Daniel@0 37 init_state{1} = [10 10]';
Daniel@0 38 init_state{2} = [10 -10]';
Daniel@0 39 init_cov = eye(2);
Daniel@0 40
Daniel@0 41 % Uncertain of initial state (position)
Daniel@0 42 for i=1:nobj
Daniel@0 43 bnet.CPD{Xs(i)} = gaussian_CPD(bnet, Xs(i), 'mean', init_state{i}, 'cov', init_cov);
Daniel@0 44 end
Daniel@0 45 bnet.CPD{S} = root_CPD(bnet, S); % always observed
Daniel@0 46 bnet.CPD{Y} = gmux_CPD(bnet, Y, 'cov', repmat(R, [1 1 nobj]), 'weights', repmat(H, [1 1 nobj]));
Daniel@0 47 % slice 2
Daniel@0 48 eclass = bnet.equiv_class;
Daniel@0 49 for i=1:nobj
Daniel@0 50 bnet.CPD{eclass(Xs(i), 2)} = gaussian_CPD(bnet, Xs(i)+N, 'mean', zeros(Xsz,1), 'cov', Q, 'weights', F);
Daniel@0 51 end
Daniel@0 52
Daniel@0 53 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 54 % Create LDS params
Daniel@0 55
Daniel@0 56 % X(t) = A X(t-1) + B U(t) + noise(Q)
Daniel@0 57
Daniel@0 58 % [L11] = [1 ] * [L1] + [Q ]
Daniel@0 59 % [L2] [ 1] [L2] [ Q]
Daniel@0 60
Daniel@0 61 % Y(t)|S(t)=s = C(s) X(t) + noise(R)
Daniel@0 62 % Yt|St=1 = [1 0] * [L1] + R
Daniel@0 63 % [L2]
Daniel@0 64
Daniel@0 65 nlandmarks = nobj;
Daniel@0 66
Daniel@0 67 % Create indices into block structure
Daniel@0 68 bs = 2*ones(1, nobj); % sizes of blocks in state space
Daniel@0 69 for i=1:nlandmarks
Daniel@0 70 landmark_block(:,i) = block(i, bs)';
Daniel@0 71 end
Daniel@0 72 Xsz = 2*(nlandmarks); % 2 values for each landmark plus robot
Daniel@0 73 Ysz = 2; % observe relative location
Daniel@0 74
Daniel@0 75 % create block-diagonal trans matrix for each switch
Daniel@0 76 A = zeros(Xsz, Xsz);
Daniel@0 77 for i=1:nlandmarks
Daniel@0 78 bi = landmark_block(:,i);
Daniel@0 79 A(bi, bi) = eye(2);
Daniel@0 80 end
Daniel@0 81 A = repmat(A, [1 1 nlandmarks]); % same for all switch values
Daniel@0 82
Daniel@0 83 % create block-diagonal system cov
Daniel@0 84 Qbig = zeros(Xsz, Xsz);
Daniel@0 85 Qbig = repmat(Qbig, [1 1 nlandmarks]);
Daniel@0 86
Daniel@0 87
Daniel@0 88 % create observation matrix for each value of the switch node
Daniel@0 89 % C(:,:,i) = (0 ... I ...) where the I is in the i'th posn.
Daniel@0 90 C = zeros(Ysz, Xsz, nlandmarks);
Daniel@0 91 for i=1:nlandmarks
Daniel@0 92 C(:, landmark_block(:,i), i) = eye(2);
Daniel@0 93 end
Daniel@0 94
Daniel@0 95 % create observation cov for each value of the switch node
Daniel@0 96 Rbig = repmat(R, [1 1 nlandmarks]);
Daniel@0 97
Daniel@0 98 % initial conditions
Daniel@0 99 init_x = [init_state{1}; init_state{2}];
Daniel@0 100 init_V = zeros(Xsz, Xsz);
Daniel@0 101 for i=1:nlandmarks
Daniel@0 102 bi = landmark_block(:,i);
Daniel@0 103 init_V(bi,bi) = init_cov;
Daniel@0 104 end
Daniel@0 105
Daniel@0 106
Daniel@0 107
Daniel@0 108 %%%%%%%%%%%%%%%%
Daniel@0 109 % Observe objects at random
Daniel@0 110 T = 10;
Daniel@0 111 evidence = cell(N, T);
Daniel@0 112 data_assoc = sample_discrete(normalise(ones(1,nobj)), 1, T);
Daniel@0 113 evidence(S,:) = num2cell(data_assoc);
Daniel@0 114 evidence = sample_dbn(bnet, 'evidence', evidence);
Daniel@0 115
Daniel@0 116
Daniel@0 117 % Inference
Daniel@0 118 ev = cell(N,T);
Daniel@0 119 ev(bnet.observed,:) = evidence(bnet.observed, :);
Daniel@0 120 y = cell2num(evidence(Y,:));
Daniel@0 121
Daniel@0 122 engine = pearl_unrolled_dbn_inf_engine(bnet);
Daniel@0 123 engine = enter_evidence(engine, ev);
Daniel@0 124
Daniel@0 125 loopy_est_pos = zeros(2, nlandmarks);
Daniel@0 126 loopy_est_pos_cov = zeros(2, 2, nlandmarks);
Daniel@0 127 for i=1:nobj
Daniel@0 128 m = marginal_nodes(engine, Xs(i), T);
Daniel@0 129 loopy_est_pos(:,i) = m.mu;
Daniel@0 130 loopy_est_pos_cov(:,:,i) = m.Sigma;
Daniel@0 131 end
Daniel@0 132
Daniel@0 133
Daniel@0 134 [xsmooth, Vsmooth] = kalman_smoother(y, A, C, Qbig, Rbig, init_x, init_V, 'model', data_assoc);
Daniel@0 135
Daniel@0 136 kf_est_pos = zeros(2, nlandmarks);
Daniel@0 137 kf_est_pos_cov = zeros(2, 2, nlandmarks);
Daniel@0 138 for i=1:nlandmarks
Daniel@0 139 bi = landmark_block(:,i);
Daniel@0 140 kf_est_pos(:,i) = xsmooth(bi, T);
Daniel@0 141 kf_est_pos_cov(:,:,i) = Vsmooth(bi, bi, T);
Daniel@0 142 end
Daniel@0 143
Daniel@0 144
Daniel@0 145 kf_est_pos
Daniel@0 146 loopy_est_pos
Daniel@0 147
Daniel@0 148 kf_est_pos_time = zeros(2, nlandmarks, T);
Daniel@0 149 for t=1:T
Daniel@0 150 for i=1:nlandmarks
Daniel@0 151 bi = landmark_block(:,i);
Daniel@0 152 kf_est_pos_time(:,i,t) = xsmooth(bi, t);
Daniel@0 153 end
Daniel@0 154 end
Daniel@0 155 kf_est_pos_time % same for all t since smoothed