test.cpp
Go to the documentation of this file.
00001 /************************************************************************
00002  *  Copyright (C) 2012 Eindhoven University of Technology (TU/e).       *
00003  *  All rights reserved.                                                *
00004  ************************************************************************
00005  *  Redistribution and use in source and binary forms, with or without  *
00006  *  modification, are permitted provided that the following conditions  *
00007  *  are met:                                                            *
00008  *                                                                      *
00009  *      1.  Redistributions of source code must retain the above        *
00010  *          copyright notice, this list of conditions and the following *
00011  *          disclaimer.                                                 *
00012  *                                                                      *
00013  *      2.  Redistributions in binary form must reproduce the above     *
00014  *          copyright notice, this list of conditions and the following *
00015  *          disclaimer in the documentation and/or other materials      *
00016  *          provided with the distribution.                             *
00017  *                                                                      *
00018  *  THIS SOFTWARE IS PROVIDED BY TU/e "AS IS" AND ANY EXPRESS OR        *
00019  *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED      *
00020  *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE  *
00021  *  ARE DISCLAIMED. IN NO EVENT SHALL TU/e OR CONTRIBUTORS BE LIABLE    *
00022  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR        *
00023  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT   *
00024  *  OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;     *
00025  *  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF       *
00026  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           *
00027  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE   *
00028  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH    *
00029  *  DAMAGE.                                                             *
00030  *                                                                      *
00031  *  The views and conclusions contained in the software and             *
00032  *  documentation are those of the authors and should not be            *
00033  *  interpreted as representing official policies, either expressed or  *
00034  *  implied, of TU/e.                                                   *
00035  ************************************************************************/
00036 
00037 #include "ros/ros.h"
00038 #include "problib/conversions.h"
00039 #include "problib/datatypes.h"
00040 
00041 #include <iostream>
00042 #include <armadillo>
00043 
00044 #include <time.h>
00045 
00046 using namespace std;
00047 using namespace arma;
00048 
00049 list<timespec> TIMERS;
00050 
00051 stringstream TIMER_LOG;
00052 stringstream OUTPUT_LOG;
00053 
00054 inline void startTimer(int ID = 0) {
00055         timespec t_start;
00056         clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t_start);
00057         TIMERS.push_back(t_start);
00058 }
00059 
00060 inline void stopTimer(string msg, int ID = 0, double factor = 1) {
00061         timespec t_end;
00062         clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t_end);
00063 
00064         timespec& t_start = TIMERS.back();
00065         TIMER_LOG << msg << ": " << factor * ((t_end.tv_sec - t_start.tv_sec) + double(t_end.tv_nsec - t_start.tv_nsec) / 1e9) << " secs." << endl;
00066         TIMERS.pop_back();
00067 }
00068 
00069 void showTimerLog() {
00070         cout << endl;
00071         cout << "------------- TIME -----------" << endl;
00072         cout << TIMER_LOG.str();
00073         cout << "------------------------------" << endl;
00074 }
00075 
00076 void testOutput(string msg, double out, double actual, double epsilon = 1e-9) {
00077         double diff = fabs(out - actual);
00078         if (diff < epsilon) {
00079                 OUTPUT_LOG << "OK   - " << msg << ": " << out << endl;
00080         } else {
00081                 OUTPUT_LOG << "DIFF - " << msg << ": " << out << " - " << actual << " = " << diff << endl;
00082         }
00083 }
00084 
00085 void showOutputLog() {
00086         cout << endl;
00087         cout << "----------- OUTPUT -----------" << endl;
00088         cout << OUTPUT_LOG.str();
00089         cout << "------------------------------" << endl;
00090 }
00091 
00092 void test() {
00093 
00094         using namespace pbl;
00095 
00096     Uniform U1(pbl::Vector3(1, 2, 0.8), pbl::Vector3(2, 1, 0.1));
00097     Uniform U2(pbl::Vector3(1.2, 2.3, 0.85), pbl::Vector3(2, 1, 0.1));
00098 
00099 
00100     cout << U1.toString() << endl;
00101     cout << U2.toString() << endl;
00102     cout << U1.getLikelihood(U2) << endl;
00103     cout << U2.getLikelihood(U1) << endl;
00104 
00105     Hybrid H;
00106 
00107     PMF pmf1;
00108     pmf1.setProbability("test", 0.8);
00109     H.addPDF(pmf1, 1);
00110 
00111     problib::PDF H_msg;
00112     pbl::PDFtoMsg(H, H_msg);
00113 
00114     cout << H_msg << endl;
00115 
00116     PDF* H_received = pbl::msgToPDF(H_msg);
00117 
00118     cout << H_received->toString() << endl;
00119 
00120         Vector3 mean1(0, 0, 0);
00121         Matrix3 var1(1, 1, 1);
00122         Gaussian pdf1(mean1, var1);
00123 
00124         Vector3 mean2(1, 1, 1);
00125         Matrix3 var2(0.1, 0.1, 0.1);
00126         Gaussian pdf2(mean2, var2);
00127 
00128         Vector3 mean3(0.3, 2, -0.7);
00129         Matrix3 var3(0.2, 0.1, 0.4);
00130         Gaussian pdf3(mean3, var3);
00131 
00132         Mixture mix;
00133         mix.addComponent(pdf1, 0.7);
00134         mix.addComponent(pdf2, 0.3);
00135 
00136         Mixture mix2;
00137         mix2.addComponent(mix, 0.1);
00138         mix2.addComponent(pdf3, 0.9);
00139 
00140         Gaussian exact2(3);
00141         cout << "Before initialization: " << exact2.toString() << endl;
00142         exact2.setMean(mean2);
00143 
00144         cout << "Mixture: " << endl << mix2.toString() << endl;
00145 
00146         double d;
00147         for (int i = 0; i < 1000; ++i) {
00148                 d = mix2.getLikelihood(exact2);
00149         }
00150         cout << "Density of mixture at " << mean2 << " = " << d << endl << endl;
00151 
00152         cout << "Converting to msg ..." << endl << endl;
00153         problib::PDF pdf_msg;
00154         pbl::PDFtoMsg(mix2, pdf_msg);
00155         cout << "Result:" << endl << pdf_msg << endl;
00156 
00157         cout << "Converting back to pdf ..." << endl << endl;
00158         PDF* received_pdf = pbl::msgToPDF(pdf_msg);
00159         cout << "Result:" << endl << received_pdf->toString() << endl << endl;
00160 
00161         cout << "Density of mixture at " << mean2 << " = " << received_pdf->getLikelihood(exact2) << endl << endl;
00162 
00163         delete received_pdf;
00164 
00165         cout << "Creating pmf ..." << endl;
00166         PMF pmf;
00167         pmf.setDomainSize(3);
00168 
00169         PMF pmf2;
00170         pmf2.setDomainSize(3);
00171         pmf2.setProbability("A", 0.25);
00172         pmf2.setProbability("B", 0.5);
00173         //pmf2.setProbability("C", 0.25);
00174 
00175         cout << pmf.toString() << endl << endl;
00176 
00177         cout << "Updating pmf ..." << endl;
00178         pmf.update(pmf2);
00179         cout << pmf.toString() << endl << endl;
00180 
00181         PMF pmf3;
00182         pmf3.setDomainSize(3);
00183         pmf3.setProbability("C", 0.999);
00184 
00185         cout << "Updating pmf ..." << endl;
00186         pmf.update(pmf3);
00187         cout << pmf.toString() << endl;
00188 
00189 
00190         PMF pmf_copy;
00191         pmf_copy = pmf;
00192 
00193         for(int i = 0; i < 1000; ++i) {
00194                 pmf_copy = pmf;
00195         }
00196 
00197         pmf_copy.update(pmf3);
00198         cout << pmf_copy.toString() << endl;
00199         cout << pmf.toString() << endl;
00200 
00201         cout << "Converting to msg ..." << endl << endl;
00202         problib::PDF pmf_msg;
00203         pbl::PDFtoMsg(pmf, pmf_msg);
00204         cout << "Result:" << endl << pmf_msg << endl;
00205 
00206         cout << "Converting back to pdf ..." << endl << endl;
00207         PDF* received_pdf2 = pbl::msgToPDF(pmf_msg);
00208         cout << "Result:" << endl << received_pdf2->toString() << endl << endl;
00209 
00210         delete received_pdf2;
00211 
00212         cout << "Testing simple population of msg for exact (string) value ..." << endl;
00213         problib::PDF exact_str;
00214         exact_str.exact_value_str = "test";
00215         PDF* pdf_exact_str = pbl::msgToPDF(exact_str);
00216         cout << "exact_str:" << endl << pdf_exact_str->toString("    ") << endl << endl;
00217         delete pdf_exact_str;
00218 
00219         cout << "Testing simple population of msg for exact (real) value ..." << endl;
00220         problib::PDF exact_real;
00221         exact_real.exact_value_vec.push_back(1);
00222         exact_real.exact_value_vec.push_back(1);
00223         exact_real.exact_value_vec.push_back(1);
00224         PDF* pdf_exact_real = pbl::msgToPDF(exact_real);
00225         cout << "exact_real:" << endl << pdf_exact_real->toString("    ") << endl << endl;
00226 
00227         startTimer();
00228 
00229         double d2;
00230         for(int i = 0; i < 1000000; ++i) {
00231                 d2 = mix2.getLikelihood(*pdf_exact_real);
00232         }
00233 
00234         stopTimer("Likelihood on mixture", (double)1 / 1000000);
00235 
00236         cout << "Likelihood with mixture = " << mix2.getLikelihood(*pdf_exact_real) << endl << endl;
00237 
00238         testOutput("Likelihood with mixture", d2, 0.0612611897752479);
00239 
00240         printf("%.16f\n", d2);
00241 
00242         //cout << "Likelihood with itself = " << pdf_exact_real->getLikelihood(*pdf_exact_real) << endl << endl;
00243 
00244         Mixture mix_copy = mix2;
00245         mix2.clear();
00246 
00247         cout << mix_copy.toString() << endl;
00248 
00249         double d3 = mix_copy.getLikelihood(*pdf_exact_real);
00250         testOutput("Likelihood with mixture (copy)", d3, d2);
00251 
00252         delete pdf_exact_real;
00253 }
00254 
00255 int main(int argc, char **argv) {
00256 
00257         startTimer();
00258         test();
00259         stopTimer("TOTAL");
00260 
00261         showTimerLog();
00262         showOutputLog();
00263 }
00264 
00265 


problib
Author(s): Sjoerd van den Dries
autogenerated on Tue Jan 7 2014 11:42:42