Daniel@0: % This is like skf_data_assoc_gmux, except the objects don't move. Daniel@0: % We are uncertain of their initial positions, and get more and more observations Daniel@0: % over time. The goal is to test deterministic links (0 covariance). Daniel@0: % This is like robot1, except the robot doesn't move and is always at [0 0], Daniel@0: % so the relative location is simply L(s). Daniel@0: Daniel@0: nobj = 2; Daniel@0: N = nobj+2; Daniel@0: Xs = 1:nobj; Daniel@0: S = nobj+1; Daniel@0: Y = nobj+2; Daniel@0: Daniel@0: intra = zeros(N,N); Daniel@0: inter = zeros(N,N); Daniel@0: intra([Xs S], Y) =1; Daniel@0: for i=1:nobj Daniel@0: inter(Xs(i), Xs(i))=1; Daniel@0: end Daniel@0: Daniel@0: Xsz = 2; % state space = (x y) Daniel@0: Ysz = 2; Daniel@0: ns = zeros(1,N); Daniel@0: ns(Xs) = Xsz; Daniel@0: ns(Y) = Ysz; Daniel@0: ns(S) = nobj; Daniel@0: Daniel@0: bnet = mk_dbn(intra, inter, ns, 'discrete', S, 'observed', [S Y]); Daniel@0: Daniel@0: % For each object, we have Daniel@0: % X(t+1) = F X(t) + noise(Q) Daniel@0: % Y(t) = H X(t) + noise(R) Daniel@0: F = eye(2); Daniel@0: H = eye(2); Daniel@0: Q = 0*eye(Xsz); % no noise in dynamics Daniel@0: R = eye(Ysz); Daniel@0: Daniel@0: init_state{1} = [10 10]'; Daniel@0: init_state{2} = [10 -10]'; Daniel@0: init_cov = eye(2); Daniel@0: Daniel@0: % Uncertain of initial state (position) Daniel@0: for i=1:nobj Daniel@0: bnet.CPD{Xs(i)} = gaussian_CPD(bnet, Xs(i), 'mean', init_state{i}, 'cov', init_cov); Daniel@0: end Daniel@0: bnet.CPD{S} = root_CPD(bnet, S); % always observed Daniel@0: bnet.CPD{Y} = gmux_CPD(bnet, Y, 'cov', repmat(R, [1 1 nobj]), 'weights', repmat(H, [1 1 nobj])); Daniel@0: % slice 2 Daniel@0: eclass = bnet.equiv_class; Daniel@0: for i=1:nobj Daniel@0: bnet.CPD{eclass(Xs(i), 2)} = gaussian_CPD(bnet, Xs(i)+N, 'mean', zeros(Xsz,1), 'cov', Q, 'weights', F); Daniel@0: end Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: % Create LDS params Daniel@0: Daniel@0: % X(t) = A X(t-1) + B U(t) + noise(Q) Daniel@0: Daniel@0: % [L11] = [1 ] * [L1] + [Q ] Daniel@0: % [L2] [ 1] [L2] [ Q] Daniel@0: Daniel@0: % Y(t)|S(t)=s = C(s) X(t) + noise(R) Daniel@0: % Yt|St=1 = [1 0] * [L1] + R Daniel@0: % [L2] Daniel@0: Daniel@0: nlandmarks = nobj; Daniel@0: Daniel@0: % Create indices into block structure Daniel@0: bs = 2*ones(1, nobj); % sizes of blocks in state space Daniel@0: for i=1:nlandmarks Daniel@0: landmark_block(:,i) = block(i, bs)'; Daniel@0: end Daniel@0: Xsz = 2*(nlandmarks); % 2 values for each landmark plus robot Daniel@0: Ysz = 2; % observe relative location Daniel@0: Daniel@0: % create block-diagonal trans matrix for each switch Daniel@0: A = zeros(Xsz, Xsz); Daniel@0: for i=1:nlandmarks Daniel@0: bi = landmark_block(:,i); Daniel@0: A(bi, bi) = eye(2); Daniel@0: end Daniel@0: A = repmat(A, [1 1 nlandmarks]); % same for all switch values Daniel@0: Daniel@0: % create block-diagonal system cov Daniel@0: Qbig = zeros(Xsz, Xsz); Daniel@0: Qbig = repmat(Qbig, [1 1 nlandmarks]); Daniel@0: Daniel@0: Daniel@0: % create observation matrix for each value of the switch node Daniel@0: % C(:,:,i) = (0 ... I ...) where the I is in the i'th posn. Daniel@0: C = zeros(Ysz, Xsz, nlandmarks); Daniel@0: for i=1:nlandmarks Daniel@0: C(:, landmark_block(:,i), i) = eye(2); Daniel@0: end Daniel@0: Daniel@0: % create observation cov for each value of the switch node Daniel@0: Rbig = repmat(R, [1 1 nlandmarks]); Daniel@0: Daniel@0: % initial conditions Daniel@0: init_x = [init_state{1}; init_state{2}]; Daniel@0: init_V = zeros(Xsz, Xsz); Daniel@0: for i=1:nlandmarks Daniel@0: bi = landmark_block(:,i); Daniel@0: init_V(bi,bi) = init_cov; Daniel@0: end Daniel@0: Daniel@0: Daniel@0: Daniel@0: %%%%%%%%%%%%%%%% Daniel@0: % Observe objects at random Daniel@0: T = 10; Daniel@0: evidence = cell(N, T); Daniel@0: data_assoc = sample_discrete(normalise(ones(1,nobj)), 1, T); Daniel@0: evidence(S,:) = num2cell(data_assoc); Daniel@0: evidence = sample_dbn(bnet, 'evidence', evidence); Daniel@0: Daniel@0: Daniel@0: % Inference Daniel@0: ev = cell(N,T); Daniel@0: ev(bnet.observed,:) = evidence(bnet.observed, :); Daniel@0: y = cell2num(evidence(Y,:)); Daniel@0: Daniel@0: engine = pearl_unrolled_dbn_inf_engine(bnet); Daniel@0: engine = enter_evidence(engine, ev); Daniel@0: Daniel@0: loopy_est_pos = zeros(2, nlandmarks); Daniel@0: loopy_est_pos_cov = zeros(2, 2, nlandmarks); Daniel@0: for i=1:nobj Daniel@0: m = marginal_nodes(engine, Xs(i), T); Daniel@0: loopy_est_pos(:,i) = m.mu; Daniel@0: loopy_est_pos_cov(:,:,i) = m.Sigma; Daniel@0: end Daniel@0: Daniel@0: Daniel@0: [xsmooth, Vsmooth] = kalman_smoother(y, A, C, Qbig, Rbig, init_x, init_V, 'model', data_assoc); Daniel@0: Daniel@0: kf_est_pos = zeros(2, nlandmarks); Daniel@0: kf_est_pos_cov = zeros(2, 2, nlandmarks); Daniel@0: for i=1:nlandmarks Daniel@0: bi = landmark_block(:,i); Daniel@0: kf_est_pos(:,i) = xsmooth(bi, T); Daniel@0: kf_est_pos_cov(:,:,i) = Vsmooth(bi, bi, T); Daniel@0: end Daniel@0: Daniel@0: Daniel@0: kf_est_pos Daniel@0: loopy_est_pos Daniel@0: Daniel@0: kf_est_pos_time = zeros(2, nlandmarks, T); Daniel@0: for t=1:T Daniel@0: for i=1:nlandmarks Daniel@0: bi = landmark_block(:,i); Daniel@0: kf_est_pos_time(:,i,t) = xsmooth(bi, t); Daniel@0: end Daniel@0: end Daniel@0: kf_est_pos_time % same for all t since smoothed