annotate toolboxes/FullBNT-1.0.7/bnt/examples/dynamic/SLAM/Old/offline_loopy_slam.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 % We navigate a robot around a square using a fixed control policy and no noise.
Daniel@0 2 % We assume the robot observes the relative distance to the nearest landmark.
Daniel@0 3 % Everything is linear-Gaussian.
Daniel@0 4
Daniel@0 5 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 6 % Create toy data set
Daniel@0 7
Daniel@0 8 seed = 0;
Daniel@0 9 rand('state', seed);
Daniel@0 10 randn('state', seed);
Daniel@0 11
Daniel@0 12 if 1
Daniel@0 13 T = 20;
Daniel@0 14 ctrl_signal = [repmat([1 0]', 1, T/4) repmat([0 1]', 1, T/4) ...
Daniel@0 15 repmat([-1 0]', 1, T/4) repmat([0 -1]', 1, T/4)];
Daniel@0 16 else
Daniel@0 17 T = 5;
Daniel@0 18 ctrl_signal = repmat([1 0]', 1, T);
Daniel@0 19 end
Daniel@0 20
Daniel@0 21 nlandmarks = 4;
Daniel@0 22 true_landmark_pos = [1 1;
Daniel@0 23 4 1;
Daniel@0 24 4 4;
Daniel@0 25 1 4]';
Daniel@0 26 init_robot_pos = [0 0]';
Daniel@0 27
Daniel@0 28 true_robot_pos = zeros(2, T);
Daniel@0 29 true_data_assoc = zeros(1, T);
Daniel@0 30 true_rel_dist = zeros(2, T);
Daniel@0 31 for t=1:T
Daniel@0 32 if t>1
Daniel@0 33 true_robot_pos(:,t) = true_robot_pos(:,t-1) + ctrl_signal(:,t);
Daniel@0 34 else
Daniel@0 35 true_robot_pos(:,t) = init_robot_pos + ctrl_signal(:,t);
Daniel@0 36 end
Daniel@0 37 nn = argmin(dist2(true_robot_pos(:,t)', true_landmark_pos'));
Daniel@0 38 %nn = t; % observe 1, 2, 3
Daniel@0 39 true_data_assoc(t) = nn;
Daniel@0 40 true_rel_dist(:,t) = true_landmark_pos(:, nn) - true_robot_pos(:,t);
Daniel@0 41 end
Daniel@0 42
Daniel@0 43 figure(1);
Daniel@0 44 %clf;
Daniel@0 45 hold on
Daniel@0 46 %plot(true_landmark_pos(1,:), true_landmark_pos(2,:), '*');
Daniel@0 47 for i=1:nlandmarks
Daniel@0 48 text(true_landmark_pos(1,i), true_landmark_pos(2,i), sprintf('L%d',i));
Daniel@0 49 end
Daniel@0 50 for t=1:T
Daniel@0 51 text(true_robot_pos(1,t), true_robot_pos(2,t), sprintf('%d',t));
Daniel@0 52 end
Daniel@0 53 hold off
Daniel@0 54 axis([-1 6 -1 6])
Daniel@0 55
Daniel@0 56 R = 1e-3*eye(2); % noise added to observation
Daniel@0 57 Q = 1e-3*eye(2); % noise added to robot motion
Daniel@0 58
Daniel@0 59 % Create data set
Daniel@0 60 obs_noise_seq = sample_gaussian([0 0]', R, T)';
Daniel@0 61 obs_rel_pos = true_rel_dist + obs_noise_seq;
Daniel@0 62 %obs_rel_pos = true_rel_dist;
Daniel@0 63
Daniel@0 64
Daniel@0 65 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 66 % Create params for inference
Daniel@0 67
Daniel@0 68 % X(t) = A X(t-1) + B U(t) + noise(Q)
Daniel@0 69
Daniel@0 70 % [L1] = [1 ] * [L1] + [0] * Ut + [0 ]
Daniel@0 71 % [L2] [ 1 ] [L2] [0] [ 0 ]
Daniel@0 72 % [R ]t [ 1] [R ]t-1 [1] [ Q]
Daniel@0 73
Daniel@0 74 % Y(t)|S(t)=s = C(s) X(t) + noise(R)
Daniel@0 75 % Yt|St=1 = [1 0 -1] * [L1] + R
Daniel@0 76 % [L2]
Daniel@0 77 % [R ]
Daniel@0 78
Daniel@0 79 % Create indices into block structure
Daniel@0 80 bs = 2*ones(1, nlandmarks+1); % sizes of blocks in state space
Daniel@0 81 robot_block = block(nlandmarks+1, bs);
Daniel@0 82 for i=1:nlandmarks
Daniel@0 83 landmark_block(:,i) = block(i, bs)';
Daniel@0 84 end
Daniel@0 85 Xsz = 2*(nlandmarks+1); % 2 values for each landmark plus robot
Daniel@0 86 Ysz = 2; % observe relative location
Daniel@0 87 Usz = 2; % input is (dx, dy)
Daniel@0 88
Daniel@0 89
Daniel@0 90 % create block-diagonal trans matrix for each switch
Daniel@0 91 A = zeros(Xsz, Xsz);
Daniel@0 92 for i=1:nlandmarks
Daniel@0 93 bi = landmark_block(:,i);
Daniel@0 94 A(bi, bi) = eye(2);
Daniel@0 95 end
Daniel@0 96 bi = robot_block;
Daniel@0 97 A(bi, bi) = eye(2);
Daniel@0 98 A = repmat(A, [1 1 nlandmarks]); % same for all switch values
Daniel@0 99
Daniel@0 100 % create block-diagonal system cov
Daniel@0 101
Daniel@0 102
Daniel@0 103 Qbig = zeros(Xsz, Xsz);
Daniel@0 104 bi = robot_block;
Daniel@0 105 Qbig(bi,bi) = Q; % only add noise to robot motion
Daniel@0 106 Qbig = repmat(Qbig, [1 1 nlandmarks]);
Daniel@0 107
Daniel@0 108 % create input matrix
Daniel@0 109 B = zeros(Xsz, Usz);
Daniel@0 110 B(robot_block,:) = eye(2); % only add input to robot position
Daniel@0 111 B = repmat(B, [1 1 nlandmarks]);
Daniel@0 112
Daniel@0 113 % create observation matrix for each value of the switch node
Daniel@0 114 % C(:,:,i) = (0 ... I ... -I) where the I is in the i'th posn.
Daniel@0 115 % This computes L(i) - R
Daniel@0 116 C = zeros(Ysz, Xsz, nlandmarks);
Daniel@0 117 for i=1:nlandmarks
Daniel@0 118 C(:, landmark_block(:,i), i) = eye(2);
Daniel@0 119 C(:, robot_block, i) = -eye(2);
Daniel@0 120 end
Daniel@0 121
Daniel@0 122 % create observation cov for each value of the switch node
Daniel@0 123 Rbig = repmat(R, [1 1 nlandmarks]);
Daniel@0 124
Daniel@0 125 % initial conditions
Daniel@0 126 init_x = zeros(Xsz, 1);
Daniel@0 127 init_v = zeros(Xsz, Xsz);
Daniel@0 128 bi = robot_block;
Daniel@0 129 init_x(bi) = init_robot_pos;
Daniel@0 130 init_V(bi, bi) = 1e-5*eye(2); % very sure of robot posn
Daniel@0 131 for i=1:nlandmarks
Daniel@0 132 bi = landmark_block(:,i);
Daniel@0 133 init_V(bi,bi)= 1e5*eye(2); % very uncertain of landmark psosns
Daniel@0 134 %init_x(bi) = true_landmark_pos(:,i);
Daniel@0 135 %init_V(bi,bi)= 1e-5*eye(2); % very sure of landmark psosns
Daniel@0 136 end
Daniel@0 137
Daniel@0 138 %%%%%%%%%%%%%%%%%%%%%
Daniel@0 139 % Inference
Daniel@0 140 if 1
Daniel@0 141 [xsmooth, Vsmooth] = kalman_smoother(obs_rel_pos, A, C, Qbig, Rbig, init_x, init_V, ...
Daniel@0 142 'model', true_data_assoc, 'u', ctrl_signal, 'B', B);
Daniel@0 143
Daniel@0 144 est_robot_pos = xsmooth(robot_block, :);
Daniel@0 145 est_robot_pos_cov = Vsmooth(robot_block, robot_block, :);
Daniel@0 146
Daniel@0 147 for i=1:nlandmarks
Daniel@0 148 bi = landmark_block(:,i);
Daniel@0 149 est_landmark_pos(:,i) = xsmooth(bi, T);
Daniel@0 150 est_landmark_pos_cov(:,:,i) = Vsmooth(bi, bi, T);
Daniel@0 151 end
Daniel@0 152 end
Daniel@0 153
Daniel@0 154
Daniel@0 155 if 0
Daniel@0 156 figure(1); hold on
Daniel@0 157 for i=1:nlandmarks
Daniel@0 158 h=plotgauss2d(est_landmark_pos(:,i), est_landmark_pos_cov(:,:,i));
Daniel@0 159 set(h, 'color', 'r')
Daniel@0 160 end
Daniel@0 161 hold off
Daniel@0 162
Daniel@0 163 hold on
Daniel@0 164 for t=1:T
Daniel@0 165 h=plotgauss2d(est_robot_pos(:,t), est_robot_pos_cov(:,:,t));
Daniel@0 166 set(h,'color','r')
Daniel@0 167 h=text(est_robot_pos(1,t), est_robot_pos(2,2), sprintf('R%d', t));
Daniel@0 168 set(h,'color','r')
Daniel@0 169 end
Daniel@0 170 hold off
Daniel@0 171 end
Daniel@0 172
Daniel@0 173
Daniel@0 174 if 0
Daniel@0 175 figure(3)
Daniel@0 176 if 0
Daniel@0 177 for t=1:T
Daniel@0 178 imagesc(inv(Vsmooth(:,:,t)))
Daniel@0 179 colorbar
Daniel@0 180 fprintf('t=%d; press key to continue\n', t);
Daniel@0 181 pause
Daniel@0 182 end
Daniel@0 183 else
Daniel@0 184 for t=1:T
Daniel@0 185 subplot(5,4,t)
Daniel@0 186 imagesc(inv(Vsmooth(:,:,t)))
Daniel@0 187 end
Daniel@0 188 end
Daniel@0 189 end
Daniel@0 190
Daniel@0 191
Daniel@0 192
Daniel@0 193
Daniel@0 194
Daniel@0 195 %%%%%%%%%%%%%%%%%
Daniel@0 196 % DBN inference
Daniel@0 197
Daniel@0 198 if 1
Daniel@0 199 [bnet, Unode, Snode, Lnodes, Rnode, Ynode, Lsnode] = ...
Daniel@0 200 mk_gmux_robot_dbn(nlandmarks, Q, R, init_x, init_V, robot_block, landmark_block);
Daniel@0 201 engine = pearl_unrolled_dbn_inf_engine(bnet, 'max_iter', 50, 'filename', ...
Daniel@0 202 '/home/eecs/murphyk/matlab/loopyslam.txt');
Daniel@0 203 else
Daniel@0 204 [bnet, Unode, Snode, Lnodes, Rnode, Ynode] = ...
Daniel@0 205 mk_gmux2_robot_dbn(nlandmarks, Q, R, init_x, init_V, robot_block, landmark_block);
Daniel@0 206 engine = jtree_dbn_inf_engine(bnet);
Daniel@0 207 end
Daniel@0 208
Daniel@0 209 nnodes = bnet.nnodes_per_slice;
Daniel@0 210 evidence = cell(nnodes, T);
Daniel@0 211 evidence(Ynode, :) = num2cell(obs_rel_pos, 1);
Daniel@0 212 evidence(Unode, :) = num2cell(ctrl_signal, 1);
Daniel@0 213 evidence(Snode, :) = num2cell(true_data_assoc);
Daniel@0 214
Daniel@0 215
Daniel@0 216 [engine, ll, niter] = enter_evidence(engine, evidence);
Daniel@0 217 niter
Daniel@0 218
Daniel@0 219 loopy_est_robot_pos = zeros(2, T);
Daniel@0 220 for t=1:T
Daniel@0 221 m = marginal_nodes(engine, Rnode, t);
Daniel@0 222 loopy_est_robot_pos(:,t) = m.mu;
Daniel@0 223 end
Daniel@0 224
Daniel@0 225 for i=1:nlandmarks
Daniel@0 226 m = marginal_nodes(engine, Lnodes(i), T);
Daniel@0 227 loopy_est_landmark_pos(:,i) = m.mu;
Daniel@0 228 loopy_est_landmark_pos_cov(:,:,i) = m.Sigma;
Daniel@0 229 end
Daniel@0 230
Daniel@0 231