00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
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         
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         
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