model_test.cpp
Go to the documentation of this file.
1 // Copyright (C) 2007 Tinne De Laet <first dot last at mech dot kuleuven dot be>
2 //
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 2 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 //
17 
18 #include "model_test.hpp"
19 #include <cmath> // For sinus
20 
21 #define NUM_DS 5
22 #define NUM_COND_ARGS 1
23 
24 //testLinearAnalyticSystemModelGaussianUncertainty
25 #define STATE_SIZE 3
26 #define INPUT_SIZE 2
27 
28 #define SIGMA_NOISE 0.0001 // Noise variance (constant for every input here)
29 #define CORRELATION_NOISE 0.0 // Correlation between different noise (idem)
30 
31 #define LINEAR_SPEED 1.0
32 #define ROT_SPEED 0.0
33 
34 //testLinearAnalyticMeasurementModelGaussianUncertainty
35 #define MEASUREMENT_SIZE 1
36 // Coordinates of wall
37 #define RICO_WALL 0.5
38 #define OFFSET_WALL 30
39 #define DELTA_T 1
40 
41 // Measurement noise
42 #define WALL_CT 1/(sqrt(pow(RICO_WALL,2) + 1))
43 #define MU_MEAS_NOISE OFFSET_WALL*WALL_CT
44 #define SIGMA_MEAS_NOISE 0.5
45 // Registers the fixture into the 'registry'
47 
48 using namespace BFL;
49 
50 void
52 {
53 }
54 
55 void
57 {
58 }
59 
60 void
62 {
63  int cond_arg_dims[NUM_COND_ARGS] = { NUM_DS };
64  int state_k;int state_kMinusOne;
65 
66  DiscreteConditionalPdf a_discretecondpdf(NUM_DS,NUM_COND_ARGS,cond_arg_dims);
67  std::vector<int> cond_args(NUM_COND_ARGS);
68  /* Set and Get all Probabilities*/
69  double prob_diag = 0.98;
70  double prob_nondiag = (1-prob_diag)/(NUM_DS-1);
71  for (state_kMinusOne = 0 ; state_kMinusOne < NUM_DS ; state_kMinusOne++)
72  {
73  cond_args[0] = state_kMinusOne;
74  for (state_k = 0 ; state_k < NUM_DS ; state_k++)
75  {
76  if (state_kMinusOne == state_k) a_discretecondpdf.ProbabilitySet(prob_diag,state_k,cond_args);
77  else a_discretecondpdf.ProbabilitySet(prob_nondiag,state_k,cond_args);
78  }
79  }
80 
81  /* Construction */
82  DiscreteSystemModel a_discreteSysModel(&a_discretecondpdf);
83 
84  /* Number of discrete states*/
85  CPPUNIT_ASSERT_EQUAL( NUM_DS , (int)a_discreteSysModel.NumStatesGet());
86 
87  /* State size*/
88  CPPUNIT_ASSERT_EQUAL( 1 , a_discreteSysModel.StateSizeGet());
89 
90  /* SystemWithoutInputs*/
91  CPPUNIT_ASSERT_EQUAL( true , a_discreteSysModel.SystemWithoutInputs());
92 
93  /* ProbabilityGet without inputs*/
94  for (state_kMinusOne = 0 ; state_kMinusOne < NUM_DS ; state_kMinusOne++)
95  {
96  cond_args[0] = state_kMinusOne;
97  for (state_k = 0 ; state_k < NUM_DS ; state_k++)
98  {
99  CPPUNIT_ASSERT_EQUAL( (double)a_discretecondpdf.ProbabilityGet(state_k),(double)a_discreteSysModel.ProbabilityGet(state_k,state_kMinusOne));
100  }
101  }
102 
103  /* Simulate*/// ???
104  //for (state_kMinusOne = 0 ; state_kMinusOne < NUM_DS ; state_kMinusOne++)
105  // {
106  // a_discretecondpdf.ConditionalArgumentSet(0,state_kMinusOne);
107  // CPPUNIT_ASSERT_EQUAL( a_discretecondpdf.ExpectedValueGet() , a_discreteSysModel.Simulate(state_kMinusOne));
108  // }
109 
110  /* Copy constructor */
111  DiscreteSystemModel b_discreteSysModel(a_discreteSysModel);
112  for (state_kMinusOne = 0 ; state_kMinusOne < NUM_DS ; state_kMinusOne++)
113  {
114  cond_args[0] = state_kMinusOne;
115  for (state_k = 0 ; state_k < NUM_DS ; state_k++)
116  {
117  CPPUNIT_ASSERT_EQUAL( (double)a_discreteSysModel.ProbabilityGet(state_k,state_kMinusOne),(double)b_discreteSysModel.ProbabilityGet(state_k,state_kMinusOne));
118  }
119  }
120 
121  // creating system model with input
122  int num_cond_args_new = 2;
123  int size_input = 2;
124  int cond_args_dims_new[num_cond_args_new];
125  cond_args_dims_new[0] = NUM_DS; cond_args_dims_new[1] = size_input;
126  int input;
127 
128  DiscreteConditionalPdf c_discretecondpdf(NUM_DS,num_cond_args_new,cond_args_dims_new);
129  std::vector<int> cond_args_new(num_cond_args_new);
130  /* Set and Get all Probabilities*/
131  for (state_kMinusOne = 0 ; state_kMinusOne < NUM_DS ; state_kMinusOne++)
132  {
133  cond_args_new[0] = state_kMinusOne;
134  for (input = 0 ; input < size_input ; input++)
135  {
136  cond_args_new[1] = input;
137  for (state_k = 0 ; state_k < NUM_DS ; state_k++)
138  {
139  if (state_kMinusOne == state_k) c_discretecondpdf.ProbabilitySet(prob_diag,state_k,cond_args_new);
140  else c_discretecondpdf.ProbabilitySet(prob_nondiag,state_k,cond_args_new);
141  }
142  }
143  }
144 
145  DiscreteSystemModel c_discreteSysModel;
146  c_discreteSysModel.SystemPdfSet(&c_discretecondpdf);
147 
148  /* SystemWithoutInputs*/
149  CPPUNIT_ASSERT_EQUAL( false , c_discreteSysModel.SystemWithoutInputs());
150 
151  /* ProbabilityGet without inputs*/
152  for (state_kMinusOne = 0 ; state_kMinusOne < NUM_DS ; state_kMinusOne++)
153  {
154  cond_args[0] = state_kMinusOne;
155  for (input = 0 ; input < size_input ; input++)
156  {
157  cond_args_new[1] = input;
158  for (state_k = 0 ; state_k < NUM_DS ; state_k++)
159  {
160  CPPUNIT_ASSERT_EQUAL( (double)c_discretecondpdf.ProbabilityGet(state_k),(double)c_discreteSysModel.ProbabilityGet(state_k,state_kMinusOne,input));
161  }
162  }
163  }
164 }
165 
166 
167 void
169 {
170  Matrix A(STATE_SIZE,STATE_SIZE);
171  Matrix B(STATE_SIZE,INPUT_SIZE);
172 
173  ColumnVector state(STATE_SIZE);
174  ColumnVector initial_state(3);
175  initial_state(1) = 1.0;
176  initial_state(2) = 1.0;
177  initial_state(3) = 0.5;
178  ColumnVector input(INPUT_SIZE);
179  ColumnVector initial_input(2);
180  initial_input(1) = 1.0;
181  initial_input(2) = 1.0;
182 
183  for (int i=1; i < STATE_SIZE+1; i++){state(i) = initial_state(i);}
184  for (int i=1; i < INPUT_SIZE+1; i++){input(i) = initial_input(i);}
185 
186  // Uncertainty or Noice (Additive)
187  ColumnVector Noise_Mu(STATE_SIZE);
188  for (int row=0; row < STATE_SIZE; row++){Noise_Mu(row+1) = 0;}
189  SymmetricMatrix Noise_Cov(STATE_SIZE);
190  for (int row=0; row < STATE_SIZE; row++)
191  {
192  for (int column=0; column < STATE_SIZE; column++)
193  {
194  if (row == column) {Noise_Cov(row+1,column+1) = SIGMA_NOISE;}
195  else {Noise_Cov(row+1,column+1) = CORRELATION_NOISE;}
196  }
197  }
198  Gaussian System_Uncertainty(Noise_Mu,Noise_Cov);
199 
200  // MATRIX A: unit matrix
201  for (int row=0; row < STATE_SIZE; row++)
202  {
203  for (int column=0; column < STATE_SIZE; column++)
204  {
205  if (row == column) A(row+1,column+1)=1;
206  else A(row+1,column+1)=0;
207  }
208  }
209  // MATRIX B
210  B(STATE_SIZE,1) = 0.0; B(1,INPUT_SIZE) = 0.0; B(2,INPUT_SIZE) = 0.0;
211  B(STATE_SIZE,INPUT_SIZE) = 1.0;
212  B(1,1) = cos(state(STATE_SIZE)) * DELTA_T;
213  B(2,1) = sin(state(STATE_SIZE)) * DELTA_T;
214 
215  vector<Matrix> v(2);
216  v[0] = A;
217  v[1] = B;
218  LinearAnalyticConditionalGaussian pdf(v,System_Uncertainty);
219  LinearAnalyticSystemModelGaussianUncertainty a_linAnSysModel(&pdf);
220 
221  /* State size Get */
222  CPPUNIT_ASSERT_EQUAL( STATE_SIZE , a_linAnSysModel.StateSizeGet());
223 
224  /* System without inputs */
225  CPPUNIT_ASSERT_EQUAL( false , a_linAnSysModel.SystemWithoutInputs());
226 
227  /* Get the system model matrices */
228  CPPUNIT_ASSERT_EQUAL( A , a_linAnSysModel.AGet());
229  CPPUNIT_ASSERT_EQUAL( B , a_linAnSysModel.BGet());
230 
231  /* df_dxGet */
232  CPPUNIT_ASSERT_EQUAL( A , a_linAnSysModel.df_dxGet(input,state));
233  CPPUNIT_ASSERT_EQUAL( B , a_linAnSysModel.BGet());
234 
235  /* PredictionGet */
236  pdf.ConditionalArgumentSet(0,state);
237  pdf.ConditionalArgumentSet(1,input);
238  CPPUNIT_ASSERT_EQUAL( pdf.ExpectedValueGet() , a_linAnSysModel.PredictionGet(input,state) );
239 
240  /* CovarianceGet */
241  CPPUNIT_ASSERT_EQUAL( pdf.CovarianceGet() , a_linAnSysModel.CovarianceGet(input,state) );
242 
243  /* Simulate */
244  ColumnVector state_new(STATE_SIZE);
245  state_new = a_linAnSysModel.Simulate(state, input);
246  // TODO: can we check this in any way?
247 
248  /* ProbabilityGet */
249  CPPUNIT_ASSERT_EQUAL((double)pdf.ProbabilityGet(state_new) , (double)a_linAnSysModel.ProbabilityGet(state_new,state,input) );
250 
251  // build new A and B matrices for new state
252  // MATRIX B
253  B(1,1) = cos(state_new(STATE_SIZE)) * DELTA_T;
254  B(2,1) = sin(state_new(STATE_SIZE)) * DELTA_T;
255 
256  /* A and B set and get */
257  a_linAnSysModel.ASet(A);
258  a_linAnSysModel.BSet(B);
259  CPPUNIT_ASSERT_EQUAL(A , a_linAnSysModel.AGet());
260  CPPUNIT_ASSERT_EQUAL(B , a_linAnSysModel.BGet());
261 
262  //TODO: SystemPdfSet
263 }
264 
265 void
267 {
268  Matrix H(MEASUREMENT_SIZE,STATE_SIZE);
269 
270  ColumnVector state(STATE_SIZE);
271  ColumnVector initial_state(3);
272  initial_state(1) = 1.0;
273  initial_state(2) = 1.0;
274  initial_state(3) = 0.5;
275  for (int i=1; i < STATE_SIZE+1; i++){state(i) = initial_state(i);}
276  ColumnVector measurement(MEASUREMENT_SIZE);
277  ColumnVector MeasNoise_Mu(MEASUREMENT_SIZE);
278  SymmetricMatrix MeasNoise_Cov(MEASUREMENT_SIZE);
279 
280  // Fill up H
281  H(1,1) = WALL_CT * RICO_WALL;
282  H(1,2) = 0 - WALL_CT;
283  H(1,STATE_SIZE) = 0;
284 
285  // Construct the measurement noise (a scalar in this case)
286  MeasNoise_Mu(1) = MU_MEAS_NOISE;
287  MeasNoise_Cov(1,1) = SIGMA_MEAS_NOISE;
288 
289  Gaussian Measurement_Uncertainty(MeasNoise_Mu,MeasNoise_Cov);
290  LinearAnalyticConditionalGaussian pdf(H,Measurement_Uncertainty);
292 
293  /* Measurement size Get */
294  CPPUNIT_ASSERT_EQUAL( MEASUREMENT_SIZE , a_linAnMeasModel.MeasurementSizeGet());
295 
296  /* System without sensor parameters */
297  CPPUNIT_ASSERT_EQUAL( true , a_linAnMeasModel.SystemWithoutSensorParams());
298 
299  /* Get the measurement model matrices */
300  CPPUNIT_ASSERT_EQUAL( H , a_linAnMeasModel.HGet());
301  //CPPUNIT_ASSERT_EQUAL( J , a_linAnMeasModel.JGet());
302 
303  /* df_dxGet */
304  CPPUNIT_ASSERT_EQUAL( H , a_linAnMeasModel.df_dxGet(0,state));
305 
306  /* PredictionGet */
307  pdf.ConditionalArgumentSet(0,state);
308  CPPUNIT_ASSERT_EQUAL( pdf.ExpectedValueGet() , a_linAnMeasModel.PredictionGet(0,state) );
309 
310  /* CovarianceGet */
311  CPPUNIT_ASSERT_EQUAL( pdf.CovarianceGet() , a_linAnMeasModel.CovarianceGet(0,state) );
312 
313  /* Simulate */
314  ColumnVector meas_new(MEASUREMENT_SIZE);
315  meas_new = a_linAnMeasModel.Simulate(state);
316  // TODO: can we check this in any way?
317 
318  /* ProbabilityGet */
319  CPPUNIT_ASSERT_EQUAL((double)pdf.ProbabilityGet(meas_new) , (double)a_linAnMeasModel.ProbabilityGet(meas_new,state) );
320 
321  // build new H
322  H = 2;
323 
324  /* H set and get */
325  a_linAnMeasModel.HSet(H);
326  CPPUNIT_ASSERT_EQUAL(H , a_linAnMeasModel.HGet());
327 
328  //TODO: SystemPdfSet
329 
330 
331 }
#define INPUT_SIZE
Definition: model_test.cpp:26
void testLinearAnalyticSystemModelGaussianUncertainty()
Definition: model_test.cpp:266
bool SystemWithoutInputs() const
Has the system inputs or not.
Definition: systemmodel.cpp:84
MatrixWrapper::ColumnVector PredictionGet(const MatrixWrapper::ColumnVector &u, const MatrixWrapper::ColumnVector &x)
Returns prediction of state.
#define WALL_CT
Definition: model_test.cpp:42
MatrixWrapper::SymmetricMatrix CovarianceGet(const MatrixWrapper::ColumnVector &u, const MatrixWrapper::ColumnVector &x)
Covariance of system noise.
void testLinearAnalyticMeasurementModelGaussianUncertainty()
Definition: model_test.cpp:168
Class representing Gaussian (or normal density)
Definition: gaussian.h:27
#define DELTA_T
Definition: model_test.cpp:39
void testDiscreteSystemModel()
Definition: model_test.cpp:61
MatrixWrapper::Matrix df_dxGet(const MatrixWrapper::ColumnVector &u, const MatrixWrapper::ColumnVector &x)
Returns F-matrix.
#define MEASUREMENT_SIZE
Definition: model_test.cpp:35
#define NUM_COND_ARGS
Definition: model_test.cpp:22
int MeasurementSizeGet() const
Get Measurement Size.
int StateSizeGet() const
Get State Size.
Definition: systemmodel.cpp:78
void SystemPdfSet(ConditionalPdf< T, T > *pdf)
Set the SystemPDF.
Definition: systemmodel.cpp:98
virtual void ConditionalArgumentSet(unsigned int n_argument, const CondArg &argument)
Set the n-th argument of the list.
#define NUM_DS
Definition: model_test.cpp:21
virtual MatrixWrapper::ColumnVector ExpectedValueGet() const
Get the expected value E[x] of the pdf.
Probability ProbabilityGet(const MeasVar &z, const StateVar &x, const StateVar &s)
Get the probability of a certain measurement.
virtual MatrixWrapper::ColumnVector PredictionGet(const MatrixWrapper::ColumnVector &u, const MatrixWrapper::ColumnVector &x)
Returns estimation of measurement.
MeasVar Simulate(const StateVar &x, const StateVar &s, int sampling_method=DEFAULT, void *sampling_args=NULL)
Simulate the Measurement, given a certain state, and an input.
void tearDown()
Definition: model_test.cpp:56
#define MU_MEAS_NOISE
Definition: model_test.cpp:43
#define RICO_WALL
Definition: model_test.cpp:37
CPPUNIT_TEST_SUITE_REGISTRATION(ModelTest)
virtual MatrixWrapper::Matrix df_dxGet(const MatrixWrapper::ColumnVector &u, const MatrixWrapper::ColumnVector &x)
Returns H-matrix.
virtual Probability ProbabilityGet(const MatrixWrapper::ColumnVector &input) const
Get the probability of a certain argument.
void setUp()
Definition: model_test.cpp:51
#define CORRELATION_NOISE
Definition: model_test.cpp:29
void ProbabilitySet(const double &prob, const int &input, const std::vector< int > &condargs) const
Set the probability (Typical for discrete Pdf&#39;s)
Class for linear analytic systemmodels with additive gaussian noise.
#define SIGMA_MEAS_NOISE
Definition: model_test.cpp:44
unsigned int NumStatesGet() const
Get the number of discrete states.
Abstract Class representing all FULLY Discrete Conditional PDF&#39;s.
Class for linear analytic measurementmodels with additive gaussian noise.
#define STATE_SIZE
Definition: model_test.cpp:25
Class for discrete System Models.
Probability ProbabilityGet(const T &x_k, const T &x_kminusone, const T &u)
Get the probability of arriving in a next state.
T Simulate(const T &x, const T &u, int sampling_method=DEFAULT, void *sampling_args=NULL)
Simulate the system.
Definition: systemmodel.h:126
Probability ProbabilityGet(const int &input) const
Get the probability of a certain argument.
virtual MatrixWrapper::SymmetricMatrix CovarianceGet() const
Get the Covariance Matrix E[(x - E[x])^2] of the Analytic pdf.
virtual MatrixWrapper::SymmetricMatrix CovarianceGet(const MatrixWrapper::ColumnVector &u, const MatrixWrapper::ColumnVector &x)
Returns covariance on the measurement.
#define SIGMA_NOISE
Definition: model_test.cpp:28
bool SystemWithoutSensorParams() const
Number of Conditional Arguments.


bfl
Author(s): Klaas Gadeyne, Wim Meeussen, Tinne Delaet and many others. See web page for a full contributor list. ROS package maintained by Wim Meeussen.
autogenerated on Mon Feb 28 2022 21:56:33