test.cpp
Go to the documentation of this file.
1 /************************************************************************
2  * Copyright (C) 2012 Eindhoven University of Technology (TU/e). *
3  * All rights reserved. *
4  ************************************************************************
5  * Redistribution and use in source and binary forms, with or without *
6  * modification, are permitted provided that the following conditions *
7  * are met: *
8  * *
9  * 1. Redistributions of source code must retain the above *
10  * copyright notice, this list of conditions and the following *
11  * disclaimer. *
12  * *
13  * 2. Redistributions in binary form must reproduce the above *
14  * copyright notice, this list of conditions and the following *
15  * disclaimer in the documentation and/or other materials *
16  * provided with the distribution. *
17  * *
18  * THIS SOFTWARE IS PROVIDED BY TU/e "AS IS" AND ANY EXPRESS OR *
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED *
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE *
21  * ARE DISCLAIMED. IN NO EVENT SHALL TU/e OR CONTRIBUTORS BE LIABLE *
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR *
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT *
24  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; *
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
26  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE *
28  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH *
29  * DAMAGE. *
30  * *
31  * The views and conclusions contained in the software and *
32  * documentation are those of the authors and should not be *
33  * interpreted as representing official policies, either expressed or *
34  * implied, of TU/e. *
35  ************************************************************************/
36 
37 #include "ros/ros.h"
38 #include "problib/conversions.h"
39 #include "problib/datatypes.h"
40 
41 #include <iostream>
42 #include <armadillo>
43 
44 #include <time.h>
45 
46 using namespace std;
47 using namespace arma;
48 
49 list<timespec> TIMERS;
50 
51 stringstream TIMER_LOG;
52 stringstream OUTPUT_LOG;
53 
54 inline void startTimer(int ID = 0) {
55  timespec t_start;
56  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t_start);
57  TIMERS.push_back(t_start);
58 }
59 
60 inline void stopTimer(string msg, int ID = 0, double factor = 1) {
61  timespec t_end;
62  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t_end);
63 
64  timespec& t_start = TIMERS.back();
65  TIMER_LOG << msg << ": " << factor * ((t_end.tv_sec - t_start.tv_sec) + double(t_end.tv_nsec - t_start.tv_nsec) / 1e9) << " secs." << endl;
66  TIMERS.pop_back();
67 }
68 
69 void showTimerLog() {
70  cout << endl;
71  cout << "------------- TIME -----------" << endl;
72  cout << TIMER_LOG.str();
73  cout << "------------------------------" << endl;
74 }
75 
76 void testOutput(string msg, double out, double actual, double epsilon = 1e-9) {
77  double diff = fabs(out - actual);
78  if (diff < epsilon) {
79  OUTPUT_LOG << "OK - " << msg << ": " << out << endl;
80  } else {
81  OUTPUT_LOG << "DIFF - " << msg << ": " << out << " - " << actual << " = " << diff << endl;
82  }
83 }
84 
85 void showOutputLog() {
86  cout << endl;
87  cout << "----------- OUTPUT -----------" << endl;
88  cout << OUTPUT_LOG.str();
89  cout << "------------------------------" << endl;
90 }
91 
92 void test() {
93 
94  using namespace pbl;
95 
96  Uniform U1(pbl::Vector3(1, 2, 0.8), pbl::Vector3(2, 1, 0.1));
97  Uniform U2(pbl::Vector3(1.2, 2.3, 0.85), pbl::Vector3(2, 1, 0.1));
98 
99 
100  cout << U1.toString() << endl;
101  cout << U2.toString() << endl;
102  cout << U1.getLikelihood(U2) << endl;
103  cout << U2.getLikelihood(U1) << endl;
104 
105  Hybrid H;
106 
107  PMF pmf1;
108  pmf1.setProbability("test", 0.8);
109  H.addPDF(pmf1, 1);
110 
111  problib::PDF H_msg;
112  pbl::PDFtoMsg(H, H_msg);
113 
114  cout << H_msg << endl;
115 
116  PDF* H_received = pbl::msgToPDF(H_msg);
117 
118  cout << H_received->toString() << endl;
119 
120  Vector3 mean1(0, 0, 0);
121  Matrix3 var1(1, 1, 1);
122  Gaussian pdf1(mean1, var1);
123 
124  Vector3 mean2(1, 1, 1);
125  Matrix3 var2(0.1, 0.1, 0.1);
126  Gaussian pdf2(mean2, var2);
127 
128  Vector3 mean3(0.3, 2, -0.7);
129  Matrix3 var3(0.2, 0.1, 0.4);
130  Gaussian pdf3(mean3, var3);
131 
132  Mixture mix;
133  mix.addComponent(pdf1, 0.7);
134  mix.addComponent(pdf2, 0.3);
135 
136  Mixture mix2;
137  mix2.addComponent(mix, 0.1);
138  mix2.addComponent(pdf3, 0.9);
139 
140  Gaussian exact2(3);
141  cout << "Before initialization: " << exact2.toString() << endl;
142  exact2.setMean(mean2);
143 
144  cout << "Mixture: " << endl << mix2.toString() << endl;
145 
146  double d;
147  for (int i = 0; i < 1000; ++i) {
148  d = mix2.getLikelihood(exact2);
149  }
150  cout << "Density of mixture at " << mean2 << " = " << d << endl << endl;
151 
152  cout << "Converting to msg ..." << endl << endl;
153  problib::PDF pdf_msg;
154  pbl::PDFtoMsg(mix2, pdf_msg);
155  cout << "Result:" << endl << pdf_msg << endl;
156 
157  cout << "Converting back to pdf ..." << endl << endl;
158  PDF* received_pdf = pbl::msgToPDF(pdf_msg);
159  cout << "Result:" << endl << received_pdf->toString() << endl << endl;
160 
161  cout << "Density of mixture at " << mean2 << " = " << received_pdf->getLikelihood(exact2) << endl << endl;
162 
163  delete received_pdf;
164 
165  cout << "Creating pmf ..." << endl;
166  PMF pmf;
167  pmf.setDomainSize(3);
168 
169  PMF pmf2;
170  pmf2.setDomainSize(3);
171  pmf2.setProbability("A", 0.25);
172  pmf2.setProbability("B", 0.5);
173  //pmf2.setProbability("C", 0.25);
174 
175  cout << pmf.toString() << endl << endl;
176 
177  cout << "Updating pmf ..." << endl;
178  pmf.update(pmf2);
179  cout << pmf.toString() << endl << endl;
180 
181  PMF pmf3;
182  pmf3.setDomainSize(3);
183  pmf3.setProbability("C", 0.999);
184 
185  cout << "Updating pmf ..." << endl;
186  pmf.update(pmf3);
187  cout << pmf.toString() << endl;
188 
189 
190  PMF pmf_copy;
191  pmf_copy = pmf;
192 
193  for(int i = 0; i < 1000; ++i) {
194  pmf_copy = pmf;
195  }
196 
197  pmf_copy.update(pmf3);
198  cout << pmf_copy.toString() << endl;
199  cout << pmf.toString() << endl;
200 
201  cout << "Converting to msg ..." << endl << endl;
202  problib::PDF pmf_msg;
203  pbl::PDFtoMsg(pmf, pmf_msg);
204  cout << "Result:" << endl << pmf_msg << endl;
205 
206  cout << "Converting back to pdf ..." << endl << endl;
207  PDF* received_pdf2 = pbl::msgToPDF(pmf_msg);
208  cout << "Result:" << endl << received_pdf2->toString() << endl << endl;
209 
210  delete received_pdf2;
211 
212  cout << "Testing simple population of msg for exact (string) value ..." << endl;
213  problib::PDF exact_str;
214  exact_str.exact_value_str = "test";
215  PDF* pdf_exact_str = pbl::msgToPDF(exact_str);
216  cout << "exact_str:" << endl << pdf_exact_str->toString(" ") << endl << endl;
217  delete pdf_exact_str;
218 
219  cout << "Testing simple population of msg for exact (real) value ..." << endl;
220  problib::PDF exact_real;
221  exact_real.exact_value_vec.push_back(1);
222  exact_real.exact_value_vec.push_back(1);
223  exact_real.exact_value_vec.push_back(1);
224  PDF* pdf_exact_real = pbl::msgToPDF(exact_real);
225  cout << "exact_real:" << endl << pdf_exact_real->toString(" ") << endl << endl;
226 
227  startTimer();
228 
229  double d2;
230  for(int i = 0; i < 1000000; ++i) {
231  d2 = mix2.getLikelihood(*pdf_exact_real);
232  }
233 
234  stopTimer("Likelihood on mixture", (double)1 / 1000000);
235 
236  cout << "Likelihood with mixture = " << mix2.getLikelihood(*pdf_exact_real) << endl << endl;
237 
238  testOutput("Likelihood with mixture", d2, 0.0612611897752479);
239 
240  printf("%.16f\n", d2);
241 
242  //cout << "Likelihood with itself = " << pdf_exact_real->getLikelihood(*pdf_exact_real) << endl << endl;
243 
244  Mixture mix_copy = mix2;
245  mix2.clear();
246 
247  cout << mix_copy.toString() << endl;
248 
249  double d3 = mix_copy.getLikelihood(*pdf_exact_real);
250  testOutput("Likelihood with mixture (copy)", d3, d2);
251 
252  delete pdf_exact_real;
253 }
254 
255 int main(int argc, char **argv) {
256 
257  startTimer();
258  test();
259  stopTimer("TOTAL");
260 
261  showTimerLog();
262  showOutputLog();
263 }
264 
265 
d
list< timespec > TIMERS
Definition: test.cpp:49
PDF * msgToPDF(const problib::PDF &msg)
Converts a PDF ROS message to a PDF object.
Definition: conversions.cpp:58
double epsilon
This class represents the weighted sum of a finite set of probability density functions.
Definition: Mixture.h:54
void startTimer(int ID=0)
Definition: test.cpp:54
void showTimerLog()
Definition: test.cpp:69
This class represents a multi-variate Gaussian (Normal) distribution.
Definition: Gaussian.h:51
void addPDF(const PDF &pdf, double priority)
Definition: Hybrid.cpp:106
void clear()
Removes all components.
Definition: Mixture.cpp:101
void testOutput(string msg, double out, double actual, double epsilon=1e-9)
Definition: test.cpp:76
std::string toString(const std::string &indent="") const
Represents the uniform distribution as a string for easier console output.
Definition: Uniform.cpp:154
void showOutputLog()
Definition: test.cpp:85
void addComponent(const PDF &pdf, double w)
Adds a component pdf with given weight.
Definition: Mixture.cpp:120
void setDomainSize(int domain_size)
Sets the domain size of this discrete distribution.
Definition: PMF.cpp:247
void setMean(const arma::vec &mu)
Sets the mean of the Gaussian.
Definition: Gaussian.cpp:161
void stopTimer(string msg, int ID=0, double factor=1)
Definition: test.cpp:60
std::string toString(const std::string &indent="") const
Represents the PMF as a string for easier console output.
Definition: PMF.cpp:293
stringstream OUTPUT_LOG
Definition: test.cpp:52
double getLikelihood(const PDF &pdf) const
Definition: Mixture.cpp:87
void update(const pbl::PMF &pmf)
Definition: PMF.cpp:214
int main(int argc, char **argv)
Definition: test.cpp:255
This class represents a hyper-cube shaped uniform distribution.
Definition: Uniform.h:51
Definition: PDF.h:47
void setProbability(const std::string &value, double p)
Set the probability of a given value.
Definition: PMF.cpp:100
virtual std::string toString(const std::string &indent="") const =0
void test()
Definition: test.cpp:92
stringstream TIMER_LOG
Definition: test.cpp:51
double getLikelihood(const PDF &pdf) const
Definition: Uniform.cpp:74
std::string toString(const std::string &indent="") const
Represents the Mixture as a string for easier console output.
Definition: Mixture.cpp:162
This class represents a discrete probability distribution (or probability mass function). Currently, this PMF can only take strings as values.
Definition: PMF.h:52
virtual double getLikelihood(const PDF &pdf) const =0
std::string toString(const std::string &indent="") const
Represents the Gaussian as a string for easier console output.
Definition: Gaussian.cpp:191
void PDFtoMsg(const PDF &pdf, problib::PDF &msg)
Converts a PDF object to a ROS message.
Definition: conversions.cpp:41


problib
Author(s): Sjoerd van den Dries, Jos Elfring
autogenerated on Fri Apr 16 2021 02:32:19