-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improved error output #30
- Loading branch information
Showing
22 changed files
with
516 additions
and
36 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
cpp-factor-graph/matlab/ffg/+ffg/+tests/testDynamicNetwork.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
function test_suite = testDynamicNetwork | ||
initTestSuite; | ||
|
||
|
||
function testAdjacencyTemporal | ||
|
||
a = ffg.EvidenceNode; | ||
b = ffg.EvidenceNode; | ||
c = ffg.EvidenceNode; | ||
node = ffg.AddNode; | ||
|
||
nwk = ffg.DynamicNetwork; | ||
|
||
nwk.addEdge(a, node); | ||
nwk.addEdge(b, node); | ||
nwk.addEdge(node, c); | ||
|
||
nwk.addTemporalEdge(a, b); | ||
|
||
EXPECTED_MATRIX = zeros(4,4); | ||
EXPECTED_MATRIX(1,2) = 1; | ||
|
||
assertElementsAlmostEqual(nwk.adjacencyMatrixTemporal(), EXPECTED_MATRIX); | ||
|
||
|
||
function testOverallKalmanFilter | ||
% testing whether the dynamic network works in general | ||
xin = ffg.EvidenceNode; | ||
xout = ffg.EvidenceNode; | ||
n = ffg.EvidenceNode; | ||
y = ffg.EvidenceNode; | ||
e = ffg.EqualityNode; | ||
a = ffg.AddNode; | ||
u = ffg.EvidenceNode; | ||
b = ffg.AddNode; | ||
|
||
nwk = ffg.DynamicNetwork; | ||
|
||
nwk.addEdge(xin, e); | ||
nwk.addEdge(e, b); | ||
nwk.addEdge(u, b); | ||
nwk.addEdge(b, xout); | ||
nwk.addEdge(e, a); | ||
nwk.addEdge(a, y); | ||
nwk.addEdge(n, a); | ||
|
||
nwk.addTemporalEdge(xout, xin); | ||
|
||
schedule = {xin, e; ... | ||
n, a; ... | ||
y, a; ... | ||
a, e; ... | ||
e, b; ... | ||
u, b; ... | ||
b, xout}; | ||
|
||
nwk.setSchedule(schedule); | ||
|
||
sd = 10.0; | ||
sd2 = sd*sd; | ||
u_const = 1.0; | ||
|
||
xout.receive(ffg.gaussMessage(1+randn()*sd, sd2, 'VARIANCE')); | ||
n.receive(ffg.gaussMessage(0, sd2, 'VARIANCE')); | ||
u.receive(ffg.gaussMessage(u_const, 0,'VARIANCE')); | ||
xin.receive(ffg.gaussMessage(1+randn()*sd, sd2, 'VARIANCE')); | ||
|
||
ffg.drawNetwork(nwk, 3); | ||
|
||
NUM_ITERATIONS = 1000; | ||
results = struct('id', {}, 'message', {}); | ||
for i = 1:NUM_ITERATIONS | ||
results = [results struct('id', y.id, 'message', ffg.gaussMessage(i+randn()*sd, 0, 'VARIANCE'))]; | ||
end | ||
nwk.makeStep(results, 1); | ||
|
||
EXPECTED_MEAN = NUM_ITERATIONS; | ||
EXPECTED_VAR = 1e-1; | ||
|
||
assertElementsAlmostEqual(EXPECTED_MEAN, xout.evidence().mean, 'absolute', 2); | ||
assertElementsAlmostEqual(EXPECTED_VAR, xout.evidence().var, 'absolute', 1e-1); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
classdef DynamicNetwork < ffg.Network | ||
%DYNAMICNETWORK network with repeated structure | ||
% The dynamic network consists of interconnected time slices, each | ||
% represented by a smaller static network | ||
|
||
methods | ||
function this = DynamicNetwork() | ||
this = this@ffg.Network('DynamicNetwork'); | ||
end | ||
|
||
function addTemporalEdge(this, src, dst) | ||
% (ffg) add a temporal edge between two nodes of consequent time slices | ||
% INPUTS: | ||
% src - source node at the step (n-1) | ||
% dst - destination node at the step (n) | ||
mexfactorgraph('addTemporalEdge', this.type_name, this.cpp_handle, src.cpp_handle, dst.cpp_handle); | ||
end | ||
|
||
function result = adjacencyMatrixTemporal(this) | ||
% (ffg) get an adjacency matrix of the network, for edges between different time slices | ||
result = mexfactorgraph('adjacencyMatrixTemporal', this.type_name, this.cpp_handle); | ||
end | ||
|
||
function makeStep(this, data, count) | ||
% (ffg) make several steps with data (or one if no data) | ||
% INPUTS: | ||
% data - array of structs struct('id',node_id, 'message', msg), | ||
% where 'id' contains id of the EvidenceNode that will | ||
% receive the message contained in 'message'. the size of the | ||
% array should be <number_of_steps> * count | ||
% count - the number of messages per step | ||
if nargin == 3 | ||
mexfactorgraph('step', this.type_name, this.cpp_handle, data, count); | ||
else | ||
mexfactorgraph('step', this.type_name, this.cpp_handle); | ||
end | ||
end | ||
|
||
end | ||
|
||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
function drawExample | ||
%DRAWEXAMPLE drawing network of a kalman filter | ||
|
||
xin = ffg.EvidenceNode; | ||
xout = ffg.EvidenceNode; | ||
n = ffg.EvidenceNode; | ||
y = ffg.EvidenceNode; | ||
e = ffg.EqualityNode; | ||
a = ffg.AddNode; | ||
u = ffg.EvidenceNode; | ||
b = ffg.AddNode; | ||
|
||
nwk = ffg.Network; | ||
nwk.addEdge(xin, e); | ||
nwk.addEdge(e, b); | ||
nwk.addEdge(u, b); | ||
nwk.addEdge(b, xout); | ||
nwk.addEdge(e, a); | ||
nwk.addEdge(a, y); | ||
nwk.addEdge(n, a); | ||
|
||
|
||
ffg.drawNetwork(nwk); | ||
end | ||
|
56 changes: 56 additions & 0 deletions
56
cpp-factor-graph/matlab/ffg/examples/dynamicNetworkExample.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
function dynamicNetworkExample | ||
%DYNAMICNETWORKEXAMPLE example of using DynamicNetwork for filtering | ||
|
||
xin = ffg.EvidenceNode; | ||
xout = ffg.EvidenceNode; | ||
n = ffg.EvidenceNode; | ||
y = ffg.EvidenceNode; | ||
e = ffg.EqualityNode; | ||
a = ffg.AddNode; | ||
u = ffg.EvidenceNode; | ||
b = ffg.AddNode; | ||
|
||
nwk = ffg.DynamicNetwork; | ||
|
||
nwk.addEdge(xin, e); | ||
nwk.addEdge(e, b); | ||
nwk.addEdge(u, b); | ||
nwk.addEdge(b, xout); | ||
nwk.addEdge(e, a); | ||
nwk.addEdge(a, y); | ||
nwk.addEdge(n, a); | ||
|
||
nwk.addTemporalEdge(xout, xin); | ||
|
||
schedule = {xin, e; ... | ||
n, a; ... | ||
y, a; ... | ||
a, e; ... | ||
e, b; ... | ||
u, b; ... | ||
b, xout}; | ||
|
||
nwk.setSchedule(schedule); | ||
|
||
sd = 10.0; | ||
sd2 = sd*sd; | ||
u_const = 1.0; | ||
|
||
xout.receive(ffg.gaussMessage(1+randn()*sd, sd2, 'VARIANCE')); | ||
n.receive(ffg.gaussMessage(0, sd2, 'VARIANCE')); | ||
u.receive(ffg.gaussMessage(u_const, 0,'VARIANCE')); | ||
xin.receive(ffg.gaussMessage(1+randn()*sd, sd2, 'VARIANCE')); | ||
|
||
ffg.drawNetwork(nwk, 3); | ||
|
||
results = struct('id', {}, 'message', {}); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
for i = 1:1000 | ||
results = [results struct('id', y.id, 'message', ffg.gaussMessage(i+randn()*sd, 0, 'VARIANCE'))]; | ||
end | ||
nwk.makeStep(results, 1); | ||
|
||
xout.evidence() | ||
|
||
|
||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
yeah I know, will fix that, not really familiar with matlab best practices)