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
00032 #include <mtt/mht_declaration.h>
00033
00034 ostream null_stream(NULL);
00035
00036 ostream& ldb(int level=0)
00037 {
00038 if(level==0)
00039 return null_stream;
00040 if(level==1)
00041 return cout;
00042
00043 return cout;
00044 }
00045
00046 MatrixXd ellipseParametric(Matrix2d& cov,Vector2d& mu,double MahThreshold)
00047 {
00048
00049
00050
00051
00052
00053
00054
00055 assert(is_finite(mu));
00056 assert(is_finite(cov));
00057
00058
00059 Eigen::SelfAdjointEigenSolver<Matrix2d> eigensolver(cov);
00060 if(eigensolver.info() != Eigen::Success)
00061 abort();
00062
00063 Matrix2d eigvectors=eigensolver.eigenvectors();
00064
00065 Vector2d ev1=eigvectors.row(0);
00066 Vector2d ev2=eigvectors.row(1);
00067
00068
00069 Vector2d p;
00070
00071
00072 double k_step=0.2;
00073
00074 double k=0;
00075
00076 double d;
00077
00078 double ea,eb;
00079
00080 double g;
00081
00082
00083 do
00084 {
00085 p=k*ev1+mu;
00086 k+=k_step;
00087
00088 d=mahalanobis(p,mu,cov);
00089
00090 if(MahThreshold-d < 0.5)
00091 k_step=0.01;
00092
00093 }while(d<MahThreshold);
00094
00095 k_step=0.2;
00096 ea=k;
00097
00098 k=0;
00099 do
00100 {
00101 p=k*ev2+mu;
00102 k+=k_step;
00103
00104 d=mahalanobis(p,mu,cov);
00105
00106 if(MahThreshold-d < 0.5)
00107 k_step=0.01;
00108
00109 }while(d<MahThreshold);
00110
00111 eb=k;
00112
00113 g=atan2(ev1(1),ev1(0));
00114
00115
00116 double t_step=(2*M_PI)/20;
00117
00118
00119 double t_max=2*M_PI+t_step;
00120
00121 double steps=ceil(t_max/t_step);
00122
00123 MatrixXd el(2,steps);
00124
00125 for(uint i=0;i<steps;i++)
00126 {
00127 double t=i*t_step;
00128 el(0,i)=mu(0)+ea*cos(t)*cos(g)-eb*sin(t)*sin(g);
00129 el(1,i)=mu(1)+ea*cos(t)*sin(g)+eb*sin(t)*cos(g);
00130 }
00131
00132 return el;
00133 }
00134
00135 Color::Color(std_msgs::ColorRGBA c)
00136 {
00137 r=c.r;
00138 g=c.g;
00139 b=c.b;
00140 a=c.a;
00141 }
00142
00143 Color& Color::changeColorBrightness(double correctionFactor)
00144 {
00145 if (correctionFactor < 0)
00146 {
00147 correctionFactor = 1 + correctionFactor;
00148 r *= correctionFactor;
00149 g *= correctionFactor;
00150 b *= correctionFactor;
00151 }
00152 else
00153 {
00154 r = (1. - r) * correctionFactor + r;
00155 g = (1. - g) * correctionFactor + g;
00156 b = (1. - b) * correctionFactor + b;
00157 }
00158
00159 if(r>1)
00160 r=1;
00161
00162 if(g>1)
00163 g=1;
00164
00165 if(b>1)
00166 b=1;
00167
00168 return *this;
00169 }
00170
00171 string Color::str()
00172 {
00173 boost::format fm("#%02x%02x%02x");
00174 fm % (int)(r*255) % (int)(g*255) % (int)(b*255);
00175 return fm.str();
00176 }
00177
00178
00179 ostream& Color::operator<<(ostream& o)
00180 {
00181 o<<"rbg("<<this->r<<","<<this->g<<","<<this->b<<")";
00182 return o;
00183 }
00184
00185 Mht::Mht(void)
00186 {
00187
00188 _max_gating = 6.0;
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199 _miss_association_threshold = 80;
00200
00201 _hypothesisTree.reset(new HypothesisTree);
00202
00203 _k = -1;
00204 _j = -1;
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219 _history_size = 6;
00220
00221
00222 _minimum_representativity = 1.0;
00223
00224 _remove_unused = true;
00225 _parent_tagging = true;
00226 _use_minimum_representativity = true;
00227 _remove_empty_clusters = true;
00228 _clean_old_modifying_tree = true;
00229 iteration = 0;
00230 _cluster_id = 0;
00231 }
00232
00233 Mht::~Mht(void)
00234 {
00235 }
00236
00237 double Mht::getMaxGating(void)
00238 {
00239 return _max_gating;
00240 }
00241
00242 void Mht::clearTargetAssociations(void)
00243 {
00244
00245 for(uint c=0;c<_clusters.size();c++)
00246 {
00247 ClusterPtr cluster = _clusters[c];
00248
00249 for(uint h=0;h<cluster->assigned_hypotheses.size();h++)
00250 {
00251 HypothesisPtr hypothesis = cluster->assigned_hypotheses[h];
00252
00253 for(uint t=0;t<hypothesis->_targets.size();t++)
00254 {
00255 TargetPtr target = hypothesis->_targets[t];
00256 target->cleanTargetAssociations();
00257 }
00258 }
00259 }
00260 }
00261
00262 void Mht::simplifySingleTargetClusters(vector<MeasurementPtr>& global_measurements)
00263 {
00264 for(uint c=0;c<_clusters.size();c++)
00265 {
00266 ClusterPtr cluster = _clusters[c];
00267
00268
00269
00270
00271 vector<MeasurementPtr> measurements;
00272
00273 for(uint m=0;m<cluster->assigned_measurements.size();m++)
00274 {
00275
00276
00277
00278
00279 measurements.push_back(cluster->assigned_measurements[m]);
00280 }
00281
00282 bool single_target = true;
00283
00284 for(vector<HypothesisPtr>::iterator it=cluster->assigned_hypotheses.begin();it!=cluster->assigned_hypotheses.end();it++)
00285 if((*it)->_targets.size()>1)
00286 single_target=false;
00287
00288
00289
00290 if(measurements.size() == 1 && cluster->assigned_hypotheses.size() > 1 && single_target)
00291 {
00292
00293
00294
00295 HypothesisPtr parent_hypothesis = cluster->assigned_hypotheses[0];
00296
00297 for(vector<HypothesisPtr>::iterator it=cluster->assigned_hypotheses.begin()+1;it!=cluster->assigned_hypotheses.end();)
00298 {
00299 HypothesisPtr aux_hypothesis = *it;
00300
00301
00302
00303 aux_hypothesis->_targets.clear();
00304 aux_hypothesis->_status=DEAD;
00305
00306 HypothesisTree::iterator ith = find(_hypothesisTree->begin(),_hypothesisTree->end(),aux_hypothesis);
00307
00308 recursiveHypothesisRemove(ith);
00309
00310
00311 it=cluster->assigned_hypotheses.erase(it);
00312 }
00313
00314 assert(parent_hypothesis->_targets.size()>0);
00315
00316 for(uint t=0;t<parent_hypothesis->_targets.size();t++)
00317 {
00318 parent_hypothesis->_targets[t]->_hypothesis = parent_hypothesis->_uid;
00319 parent_hypothesis->_targets[t]->_variant="";
00320 }
00321 }
00322 }
00323 }
00324
00325 void Mht::solveHypothesis(HypothesisPtr parent_hypothesis,vector<MeasurementPtr>& measurements, vector<HypothesisPtr>& list_of_hypotheses)
00326 {
00327 cout<<"solve hypo stx"<<endl;
00328 static boost::mutex mtx;
00329
00330 vector<TargetPtr> targets = parent_hypothesis->_targets;
00331 assert(targets.size()>0);
00332
00333 if(parent_hypothesis->_status==DEAD)
00334 return;
00335
00336
00337
00338
00339 if(targets.size()==1 && measurements.size()==1)
00340 {
00341
00342
00343 HypothesisPtr hypothesis(new Hypothesis);
00344
00345 hypothesis->_parent_uid = parent_hypothesis->_uid;
00346 hypothesis->_iteration=iteration;
00347 hypothesis->_cluster=parent_hypothesis->_cluster;
00348
00349 hypothesis->_n_det=1;
00350 cout<<"get dist"<<endl;
00351 hypothesis->_prod = parent_hypothesis->_targets[0]->getDistance(measurements[0],1);
00352
00353 TargetPtr new_target = associate(targets[0],measurements[0]);
00354
00355 if(new_target!=NULL)
00356 {
00357 new_target->_hypothesis = hypothesis->_uid;
00358 new_target->_cluster = hypothesis->_cluster;
00359
00360 if(is_finite(new_target->_xp))
00361 hypothesis->_targets.push_back(new_target);
00362
00363
00364 if(hypothesis->_targets.size()!=0)
00365 {
00366 mtx.lock();
00367 list_of_hypotheses.push_back(hypothesis);
00368 mtx.unlock();
00369 }
00370 }else
00371 {
00372 const char COL_RESET[] = "\x1b[0m";
00373 const char RED[] = "\x1b[31m";
00374 cout << RED << "Error!!, Major" << COL_RESET << endl;
00375 cout<<"failed a trivial association"<<endl;
00376 }
00377
00378 return;
00379 }
00380
00381 if(measurements.size()==0)
00382 {
00383
00384
00385
00386 HypothesisPtr hypothesis(new Hypothesis);
00387
00388 hypothesis->_parent_uid = parent_hypothesis->_uid;
00389 hypothesis->_iteration=iteration;
00390 hypothesis->_cluster=parent_hypothesis->_cluster;
00391
00392 cout<<"c2"<<endl;
00393
00394 for(uint t=0;t<targets.size();t++)
00395 {
00396 if(targets[t]->_missed_associations < _miss_association_threshold)
00397 {
00398
00399 cout<<"iterate over failed"<<endl;
00400 TargetPtr new_target(new Target(targets[t]));
00401
00402 new_target->_hypothesis=hypothesis->_uid;
00403 new_target->_cluster=hypothesis->_cluster;
00404
00405 if(is_finite(new_target->_xp))
00406 {
00407 hypothesis->_targets.push_back(new_target);
00408 hypothesis->_n_occ++;
00409 }
00410
00411 cout<<"done iterate over failed"<<endl;
00412 }else
00413 {
00414 hypothesis->_n_del++;
00415
00416 }
00417 }
00418
00419
00420 if(hypothesis->_targets.size()>0)
00421 {
00422 mtx.lock();
00423 list_of_hypotheses.push_back(hypothesis);
00424 mtx.unlock();
00425 }
00426
00427 return;
00428 }
00429
00430 cout<<"Matrixxd"<<endl;
00431
00432
00433 MatrixXd probability(measurements.size(),targets.size());
00434
00435 TargetPtr empty;
00436
00437 for(uint it=0;it<targets.size();++it)
00438 {
00439 assert(targets[it]!=empty);
00440 for(uint im=0;im<measurements.size();++im)
00441 {
00442 probability(im,it)=targets[it]->getDistance(measurements[im],1);
00443
00444 }
00445 }
00446
00447 uint nT = targets.size();
00448 uint nM = measurements.size();
00449
00450
00451 MatrixXd cost(probability.rows(),probability.cols());
00452
00453 for(uint r=0;r<cost.rows();++r)
00454 for(uint c=0;c<cost.cols();++c)
00455 {
00456 cost(r,c)=-log(probability(r,c));
00457 if(isinf(cost(r,c)))
00458 cost(r,c)=1e6;
00459 }
00460
00461
00462
00463
00464
00465
00466
00467
00468 assert(_k!=-1);
00469 vector<AssignmentsPtr> assignments = k_best_assignment(cost,_k);
00470
00471
00472
00473
00474
00475
00476 for(uint i=0;i<assignments.size();++i)
00477 {
00478
00479 vector<orderedPairPtr> assignment = assignments[i]->pairs;
00480
00481
00482 HypothesisPtr hypothesis(new Hypothesis);
00483
00484 hypothesis->_parent_uid = parent_hypothesis->_uid;
00485 hypothesis->_iteration=iteration;
00486 hypothesis->_cluster=parent_hypothesis->_cluster;
00487
00488
00489 vector<int> mes(nM,0);
00490 vector<int> tar(nT,0);
00491
00492 for(uint e=0;e<assignment.size();++e)
00493 {
00494 mes[assignment[e]->row]=1;
00495 tar[assignment[e]->col]=1;
00496 }
00497
00498 if(nM>nT)
00499 {
00500
00501
00502 for(uint im=0;im<mes.size();++im)
00503 if(mes[im]==0)
00504 {
00505
00506 TargetPtr new_target(new Target(measurements[im]));
00507
00508 new_target->_hypothesis=hypothesis->_uid;
00509 new_target->_cluster=hypothesis->_cluster;
00510
00511 if(is_finite(new_target->_xp))
00512 {
00513 hypothesis->_n_new++;
00514 hypothesis->_targets.push_back(new_target);
00515 }
00516 }
00517
00518 }else if(nT>nM)
00519 {
00520 for(uint it=0;it<tar.size();++it)
00521 if(tar[it]==0)
00522 {
00523 if(targets[it]->_missed_associations < _miss_association_threshold)
00524 {
00525
00526 TargetPtr new_target(new Target(targets[it]));
00527
00528 new_target->_hypothesis=hypothesis->_uid;
00529 new_target->_cluster=hypothesis->_cluster;
00530
00531 if(is_finite(new_target->_xp))
00532 {
00533 hypothesis->_targets.push_back(new_target);
00534 hypothesis->_n_occ++;
00535 }
00536
00537 }else
00538 {
00539 hypothesis->_n_del++;
00540
00541 }
00542 }
00543 }
00544
00545
00546 for(uint p=0;p<assignment.size();++p)
00547 {
00548 uint im = assignment[p]->row;
00549 uint it = assignment[p]->col;
00550
00551 TargetPtr new_target = associate(targets[it],measurements[im]);
00552
00553 if(new_target!=NULL)
00554 {
00555 new_target->_hypothesis=hypothesis->_uid;
00556 new_target->_cluster=hypothesis->_cluster;
00557
00558 if(is_finite(new_target->_xp))
00559 {
00560 hypothesis->_targets.push_back(new_target);
00561 hypothesis->_n_det++;
00562 hypothesis->_prod*=targets[it]->getDistance(measurements[im],1);
00563
00564 if(new_target->_missed_associations>0)
00565 hypothesis->_n_occ++;
00566 }
00567 }else
00568 {
00569 hypothesis->_n_del++;
00570 }
00571 }
00572
00573
00574 if(hypothesis->_targets.size()>0)
00575 {
00576 mtx.lock();
00577 list_of_hypotheses.push_back(hypothesis);
00578 mtx.unlock();
00579 }
00580 }
00581
00582 cout<<"solve hypo enx"<<endl;
00583 }
00584
00585 void Mht::solveClusterMultiTread(ClusterPtr cluster)
00586 {
00587 vector<HypothesisPtr> list_of_hypotheses;
00588
00589
00590 vector<MeasurementPtr> measurements = cluster->assigned_measurements;
00591 vector<HypothesisPtr> hypotheses = cluster->assigned_hypotheses;
00592
00593 assert(hypotheses.size()>0);
00594
00595 cout<<"start"<<endl;
00596
00597 if(measurements.size() == 1 && hypotheses.size() == 1 && hypotheses[0]->_targets.size()==1)
00598 {
00599 cout<<"trivial problem"<<endl;
00600 HypothesisPtr parent_hypothesis = hypotheses[0];
00601
00602
00603
00604
00605
00606
00607
00608
00609 HypothesisPtr hypothesis(new Hypothesis);
00610
00611
00612 hypothesis->_parent_uid = parent_hypothesis->_uid;
00613 hypothesis->_iteration = iteration;
00614 hypothesis->_cluster = parent_hypothesis->_cluster;
00615 hypothesis->_n_det = 1;
00616 hypothesis->_prod = parent_hypothesis->_targets[0]->getDistance(measurements[0],1);
00617
00618
00619
00620 TargetPtr new_target = associate(parent_hypothesis->_targets[0],measurements[0]);
00621
00622
00623 cout<<"cenas"<<endl;
00624
00625 if(new_target!=NULL)
00626 {
00627 new_target->_hypothesis = hypothesis->_uid;
00628 new_target->_cluster = hypothesis->_cluster;
00629
00630 if(is_finite(new_target->_xp))
00631 hypothesis->_targets.push_back(new_target);
00632
00633
00634 if(hypothesis->_targets.size()>0)
00635 list_of_hypotheses.push_back(hypothesis);
00636 }else
00637 {
00638 const char COL_RESET[] = "\x1b[0m";
00639 const char RED[] = "\x1b[31m";
00640 cout << RED << "Error!!, Major" << COL_RESET << endl;
00641 cout<<"failed a trivial association"<<endl;
00642 }
00643
00644 }else
00645 {
00646 cout<<"non trivial"<<endl;
00647
00648
00649
00650
00651 vector<boost::thread*> threads;
00652
00653 for(uint h=0;h<hypotheses.size();++h)
00654 {
00655 boost::thread*thr = new boost::thread(boost::bind(&Mht::solveHypothesis,this,boost::ref(hypotheses[h]),boost::ref(measurements),boost::ref(list_of_hypotheses)));
00656 threads.push_back(thr);
00657 }
00658
00659 for(uint c=0;c<threads.size();c++)
00660 {
00661 threads[c]->join();
00662 delete threads[c];
00663 }
00664 }
00665
00666 cout<<"here"<<endl;
00667
00668 for(uint i=0;i<list_of_hypotheses.size();++i)
00669 {
00670 HypothesisPtr h = list_of_hypotheses[i];
00671 getProbability(h);
00672 }
00673
00674
00675
00676 sort(list_of_hypotheses.begin(),list_of_hypotheses.end(),compareHypothesesByProbabilityDescending);
00677
00678
00679 assert(_j!=-1);
00680 normalize(list_of_hypotheses,_j);
00681
00682 double probability_sum = 0;
00683
00684
00685
00686 for(uint i=0;i<(uint)_j && i<list_of_hypotheses.size();++i)
00687 {
00688 HypothesisPtr hypothesis = list_of_hypotheses[i];
00689 HypothesisTree::iterator it;
00690
00691 for(uint t=0;t<hypothesis->_targets.size();t++)
00692 {
00693 TargetPtr target = hypothesis->_targets[t];
00694 if(!is_finite(target->_xp) || !is_finite(target->_P))
00695 {
00696 cout<<"trying to add invalid target, iteration: "<<iteration<<endl;
00697 cout<<*target<<endl;
00698 cout<<*hypothesis<<endl;
00699 cout<<target->_xp<<endl;
00700 }
00701
00702 assert(is_finite(target->_xp));
00703 assert(is_finite(target->_P));
00704 }
00705
00706 for(HypothesisTree::iterator fi=_hypothesisTree->begin();fi!=_hypothesisTree->end();fi++)
00707 if((*fi)->_uid==hypothesis->_parent_uid)
00708 {
00709 it=fi;
00710 break;
00711 }
00712
00713 _hypothesisTree->append_child(it,hypothesis);
00714
00715 cluster->assigned_hypotheses.push_back(hypothesis);
00716
00717 probability_sum+=hypothesis->_probability;
00718
00719 if(_use_minimum_representativity && probability_sum>_minimum_representativity)
00720 break;
00721 }
00722 }
00723
00724 void Mht::solveCluster(ClusterPtr cluster)
00725 {
00726
00727
00728 vector<HypothesisPtr> list_of_hypotheses;
00729
00730
00731 vector<MeasurementPtr> measurements = cluster->assigned_measurements;
00732 vector<HypothesisPtr> hypotheses = cluster->assigned_hypotheses;
00733
00734 assert(hypotheses.size()>0);
00735
00736
00737 if(measurements.size() == 1 && hypotheses.size() == 1 && hypotheses[0]->_targets.size()==1)
00738 {
00739
00740 HypothesisPtr parent_hypothesis = hypotheses[0];
00741
00742
00743
00744
00745
00746
00747
00748
00749 HypothesisPtr hypothesis(new Hypothesis);
00750
00751
00752 hypothesis->_parent_uid = parent_hypothesis->_uid;
00753 hypothesis->_iteration = iteration;
00754 hypothesis->_cluster = parent_hypothesis->_cluster;
00755 hypothesis->_n_det = 1;
00756 hypothesis->_prod = parent_hypothesis->_targets[0]->getDistance(measurements[0],1);
00757
00758
00759
00760 TargetPtr new_target = associate(parent_hypothesis->_targets[0],measurements[0]);
00761
00762
00763 if(new_target!=NULL)
00764 {
00765 new_target->_hypothesis = hypothesis->_uid;
00766 new_target->_cluster = hypothesis->_cluster;
00767
00768
00769
00770
00771
00772 if(is_finite(new_target->_xp))
00773 hypothesis->_targets.push_back(new_target);
00774
00775
00776 if(hypothesis->_targets.size()>0)
00777 list_of_hypotheses.push_back(hypothesis);
00778 }else
00779 {
00780 const char COL_RESET[] = "\x1b[0m";
00781 const char RED[] = "\x1b[31m";
00782 cout << RED << "Error!!, Major" << COL_RESET << endl;
00783 cout<<"failed a trivial association"<<endl;
00784 }
00785
00786 }else
00787 {
00788
00789
00790
00791
00792
00793 for(uint h=0;h<hypotheses.size();++h)
00794 {
00795 HypothesisPtr parent_hypothesis = hypotheses[h];
00796 vector<TargetPtr> targets = parent_hypothesis->_targets;
00797
00798 assert(targets.size()>0);
00799
00800 if(parent_hypothesis->_status==DEAD)
00801 continue;
00802
00803
00804
00805
00806
00807
00808
00809 if(targets.size()==1 && measurements.size()==1)
00810 {
00811
00812
00813 HypothesisPtr hypothesis(new Hypothesis);
00814
00815
00816 hypothesis->_parent_uid = parent_hypothesis->_uid;
00817 hypothesis->_iteration=iteration;
00818 hypothesis->_cluster=parent_hypothesis->_cluster;
00819
00820 hypothesis->_n_det=1;
00821 hypothesis->_prod = parent_hypothesis->_targets[0]->getDistance(measurements[0],1);
00822
00823 TargetPtr new_target = associate(targets[0],measurements[0]);
00824
00825 if(new_target!=NULL)
00826 {
00827 new_target->_hypothesis = hypothesis->_uid;
00828 new_target->_cluster = hypothesis->_cluster;
00829
00830
00831
00832
00833
00834 if(is_finite(new_target->_xp))
00835 hypothesis->_targets.push_back(new_target);
00836
00837
00838 if(hypothesis->_targets.size()!=0)
00839 list_of_hypotheses.push_back(hypothesis);
00840
00841 }else
00842 {
00843 const char COL_RESET[] = "\x1b[0m";
00844 const char RED[] = "\x1b[31m";
00845 cout << RED << "Error!!, Major" << COL_RESET << endl;
00846 cout<<"failed a trivial association"<<endl;
00847 }
00848
00849 continue;
00850 }
00851
00852 if(measurements.size()==0)
00853 {
00854
00855
00856
00857 HypothesisPtr hypothesis(new Hypothesis);
00858
00859
00860 hypothesis->_parent_uid = parent_hypothesis->_uid;
00861 hypothesis->_iteration=iteration;
00862 hypothesis->_cluster=parent_hypothesis->_cluster;
00863
00864 for(uint t=0;t<targets.size();t++)
00865 {
00866 if(targets[t]->_missed_associations < _miss_association_threshold)
00867 {
00868
00869 TargetPtr new_target(new Target(targets[t]));
00870
00871 new_target->_hypothesis=hypothesis->_uid;
00872 new_target->_cluster=hypothesis->_cluster;
00873
00874
00875
00876
00877
00878 if(is_finite(new_target->_xp))
00879 {
00880 hypothesis->_targets.push_back(new_target);
00881 hypothesis->_n_occ++;
00882 }
00883
00884 }else
00885 {
00886 hypothesis->_n_del++;
00887
00888
00889 }
00890 }
00891
00892
00893 if(hypothesis->_targets.size()>0)
00894 list_of_hypotheses.push_back(hypothesis);
00895
00896 continue;
00897 }
00898
00899
00900
00901
00902 MatrixXd probability(measurements.size(),targets.size());
00903
00904 TargetPtr empty;
00905
00906 for(uint it=0;it<targets.size();++it)
00907 {
00908 assert(targets[it]!=empty);
00909 for(uint im=0;im<measurements.size();++im)
00910 {
00911
00912
00913 probability(im,it)=targets[it]->getDistance(measurements[im],1);
00914
00915 }
00916 }
00917
00918 uint nT = targets.size();
00919 uint nM = measurements.size();
00920
00921
00922 MatrixXd cost(probability.rows(),probability.cols());
00923
00924 for(uint r=0;r<cost.rows();++r)
00925 for(uint c=0;c<cost.cols();++c)
00926 {
00927 cost(r,c)=-log(probability(r,c));
00928 if(isinf(cost(r,c)))
00929 cost(r,c)=1e6;
00930 }
00931
00932
00933
00934
00935
00936
00937
00938
00939 assert(_k!=-1);
00940 vector<AssignmentsPtr> assignments = k_best_assignment(cost,_k);
00941
00942
00943
00944
00945
00946
00947 for(uint i=0;i<assignments.size();++i)
00948 {
00949
00950
00951
00952 vector<orderedPairPtr> assignment = assignments[i]->pairs;
00953
00954
00955 HypothesisPtr hypothesis(new Hypothesis);
00956
00957
00958 hypothesis->_parent_uid = parent_hypothesis->_uid;
00959 hypothesis->_iteration=iteration;
00960 hypothesis->_cluster=parent_hypothesis->_cluster;
00961
00962
00963 vector<int> mes(nM,0);
00964 vector<int> tar(nT,0);
00965
00966 for(uint e=0;e<assignment.size();++e)
00967 {
00968 mes[assignment[e]->row]=1;
00969 tar[assignment[e]->col]=1;
00970 }
00971
00972 if(nM>nT)
00973 {
00974
00975
00976 for(uint im=0;im<mes.size();++im)
00977 if(mes[im]==0)
00978 {
00979
00980 TargetPtr new_target(new Target(measurements[im]));
00981
00982 new_target->_hypothesis=hypothesis->_uid;
00983 new_target->_cluster=hypothesis->_cluster;
00984
00985
00986
00987
00988
00989 if(is_finite(new_target->_xp))
00990 {
00991 hypothesis->_n_new++;
00992 hypothesis->_targets.push_back(new_target);
00993 }
00994 }
00995
00996 }else if(nT>nM)
00997 {
00998 for(uint it=0;it<tar.size();++it)
00999 if(tar[it]==0)
01000 {
01001 if(targets[it]->_missed_associations < _miss_association_threshold)
01002 {
01003
01004
01005 TargetPtr new_target(new Target(targets[it]));
01006
01007 new_target->_hypothesis=hypothesis->_uid;
01008 new_target->_cluster=hypothesis->_cluster;
01009
01010
01011
01012
01013
01014 if(is_finite(new_target->_xp))
01015 {
01016 hypothesis->_targets.push_back(new_target);
01017 hypothesis->_n_occ++;
01018 }
01019
01020 }else
01021 {
01022 hypothesis->_n_del++;
01023
01024
01025
01026 }
01027 }
01028 }
01029
01030
01031 for(uint p=0;p<assignment.size();++p)
01032 {
01033 uint im = assignment[p]->row;
01034 uint it = assignment[p]->col;
01035
01036
01037
01038 TargetPtr new_target = associate(targets[it],measurements[im]);
01039
01040 if(new_target!=NULL)
01041 {
01042 new_target->_hypothesis=hypothesis->_uid;
01043 new_target->_cluster=hypothesis->_cluster;
01044
01045
01046
01047
01048
01049
01050
01051
01052
01053
01054 if(is_finite(new_target->_xp))
01055 {
01056 hypothesis->_targets.push_back(new_target);
01057 hypothesis->_n_det++;
01058 hypothesis->_prod*=targets[it]->getDistance(measurements[im],1);
01059
01060 if(new_target->_missed_associations>0)
01061 hypothesis->_n_occ++;
01062 }
01063 }else
01064 {
01065 hypothesis->_n_del++;
01066 }
01067 }
01068
01069
01070 if(hypothesis->_targets.size()>0)
01071 list_of_hypotheses.push_back(hypothesis);
01072 }
01073 }
01074 }
01075
01076
01077
01078
01079 for(uint i=0;i<list_of_hypotheses.size();++i)
01080 {
01081 HypothesisPtr h = list_of_hypotheses[i];
01082 getProbability(h);
01083 }
01084
01085
01086
01087
01088
01089 sort(list_of_hypotheses.begin(),list_of_hypotheses.end(),compareHypothesesByProbabilityDescending);
01090
01091
01092 assert(_j!=-1);
01093 normalize(list_of_hypotheses,_j);
01094
01095
01096
01097 double probability_sum = 0;
01098
01099
01100
01101
01102 for(uint i=0;i<(uint)_j && i<list_of_hypotheses.size();++i)
01103 {
01104 HypothesisPtr hypothesis = list_of_hypotheses[i];
01105 HypothesisTree::iterator it;
01106
01107 for(uint t=0;t<hypothesis->_targets.size();t++)
01108 {
01109 TargetPtr target = hypothesis->_targets[t];
01110 if(!is_finite(target->_xp) || !is_finite(target->_P))
01111 {
01112 cout<<"trying to add invalid target, iteration: "<<iteration<<endl;
01113 cout<<*target<<endl;
01114 cout<<*hypothesis<<endl;
01115 cout<<target->_xp<<endl;
01116 }
01117
01118 assert(is_finite(target->_xp));
01119 assert(is_finite(target->_P));
01120 }
01121
01122
01123
01124
01125 for(HypothesisTree::iterator fi=_hypothesisTree->begin();fi!=_hypothesisTree->end();fi++)
01126 if((*fi)->_uid==hypothesis->_parent_uid)
01127 {
01128 it=fi;
01129 break;
01130 }
01131
01132
01133 _hypothesisTree->append_child(it,hypothesis);
01134
01135
01136
01137 cluster->assigned_hypotheses.push_back(hypothesis);
01138
01139 probability_sum+=hypothesis->_probability;
01140
01141
01142
01143 if(_use_minimum_representativity && probability_sum>_minimum_representativity)
01144 break;
01145 }
01146 }
01147
01148
01149
01150
01151
01152
01153
01154
01155 void Mht::iterate(vector<MeasurementPtr>& measurements)
01156 {
01157 ldb(1)<<endl<<endl;
01158 ldb(1)<<"*********************************************************************"<<endl;
01159 ldb(1)<<"k = "<<iteration<<endl<<endl;;
01160
01161 _hypothesisTree->ready_from_draw=false;
01162
01163 HypothesisTree::iterator it;
01164 HypothesisTree::sibling_iterator sib;
01165
01166 ldb()<<"createClusters"<<endl;
01167 createClusters();
01168
01169 ldb()<<"clearTargetAssociations"<<endl;
01170 clearTargetAssociations();
01171 consistencyCheck();
01172
01173
01174 ldb()<<"clusterAssignemnt"<<endl;
01175 clusterAssignemnt(measurements);
01176 consistencyCheck();
01177
01178 ldb()<<"breakClusters"<<endl;
01179
01180 breakClusters(measurements);
01181 cleanClusters();
01182 ldb()<<"done breakClusters"<<endl;
01183 consistencyCheck();
01184
01185
01186 ldb()<<"clusterMerger"<<endl;
01187 clusterMerger(measurements);
01188
01189 ldb()<<"cleanClusters"<<endl;
01190 cleanClusters();
01191 consistencyCheck();
01192
01193 ldb()<<"simplifySingleTargetClusters"<<endl;
01194 simplifySingleTargetClusters(measurements);
01195 consistencyCheck();
01196
01197
01198 for(uint c=0;c<_clusters.size();c++)
01199 {
01200
01201 ldb()<<"solveClusterMultiTread"<<endl;
01202 solveClusterMultiTread(_clusters[c]);
01203 ldb()<<"done solveClusterMultiTread"<<endl;
01204
01205
01206 }
01207
01208
01209
01210
01211
01212
01213
01214 ldb()<<"consistencyCheck"<<endl;
01215 consistencyCheck();
01216
01217
01218
01219
01220 for(_hleaf=_hypothesisTree->begin_leaf();_hleaf!=_hypothesisTree->end_leaf();++_hleaf)
01221 if((*_hleaf)->_iteration != iteration)
01222 (*_hleaf)->_status=DEAD;
01223
01224
01225
01226
01227
01228 for(uint c=0;c<_clusters.size();c++)
01229 {
01230 ClusterPtr cluster = _clusters[c];
01231
01232 vector<HypothesisPtr>::iterator it;
01233 for(it=cluster->assigned_hypotheses.begin();it!=cluster->assigned_hypotheses.end();)
01234 {
01235 HypothesisPtr hypothesis = *it;
01236
01237
01238 if(hypothesis->_iteration<iteration && hypothesis->_status!=DEAD)
01239 {
01240
01241 it = cluster->assigned_hypotheses.erase(it);
01242 }else
01243 it++;
01244 }
01245 }
01246
01247
01248
01249
01250
01251
01252 ldb()<<"removeTheDead"<<endl;
01253
01254 removeTheDead();
01255 ldb()<<"removeIrrelevant"<<endl;
01256
01257 removeIrrelevant();
01258
01259
01260
01261 for(uint m=0;m<measurements.size();m++)
01262 {
01263 cout<<"measurement: "<<measurements[m]->id<<" assigned to: "<<measurements[m]->assigned_clusters.size()<<" clusters"<<endl;
01264 if(measurements[m]->assigned_clusters.size()==0)
01265 {
01266
01267
01268 HypothesisPtr hypothesis(new Hypothesis);
01269
01270 hypothesis->_iteration=iteration;
01271 hypothesis->_n_new=1;
01272
01273 hypothesis->_probability=1;
01274
01275
01276 ClusterPtr cluster(new Cluster);
01277
01278 cluster->id=_cluster_id++;
01279 hypothesis->_cluster=cluster->id;
01280
01281 cluster->assigned_measurements.push_back(measurements[m]);
01282 cluster->assigned_hypotheses.push_back(hypothesis);
01283
01284 measurements[m]->assigned_clusters.push_back(cluster);
01285
01286
01287 TargetPtr target(new Target(measurements[m]));
01288
01289 target->_hypothesis=hypothesis->_uid;
01290 target->_cluster=hypothesis->_cluster;
01291
01292 getProbability(hypothesis);
01293
01294
01295 hypothesis->_targets.push_back(target);
01296
01297
01298 _hypothesisTree->insert(_hypothesisTree->begin(),hypothesis);
01299
01300
01301
01302 _clusters.push_back(cluster);
01303 }
01304 }
01305
01306
01307
01308
01309 updateTargetList();
01310 checkCurrentTargets(measurements);
01311
01312
01313 for(_iterator=_hypothesisTree->begin();_iterator!=_hypothesisTree->end();++_iterator)
01314 switchColor(*_iterator);
01315
01316 _hypothesisTree->need_layout=true;
01317 iteration++;
01318
01319
01320
01321
01322
01323 _clusters.clear();
01324
01325
01326
01327
01328
01329
01330
01331
01332
01333
01334 ldb(1)<<endl<<"Iteration end: "<<iteration-1<<endl;
01335 }
01336
01337 hypothesisTreePtr Mht::getHypothesisTree(void)
01338 {
01339 return _hypothesisTree;
01340 }
01341
01342 vector<TargetPtr> Mht::getTargets(void)
01343 {
01344
01345 return _current_targets;
01346 }
01347
01348 void Mht::clear(void)
01349 {
01350 _hypothesisTree->clear();
01351 _current_targets.clear();
01352 _clusters.clear();
01353 iteration=0;
01354 Hypothesis::_euid=0;
01355 Hypothesis::_euid=0;
01356 Target::_euid=0;
01357 _cluster_id = 0;
01358 }
01359
01360 string Mht::getReportName(string extra)
01361 {
01362 stringstream stream(fileName);
01363
01364 string name;
01365
01366 while(getline(stream,name,'/'));
01367
01368 stream.clear();
01369 stream.str(name);
01370
01371 getline(stream,name,'.');
01372
01373
01374 string report_name = _aux1;
01375
01376 report_name+=name;
01377 report_name+=extra;
01378 report_name+=".txt";
01379
01380 return report_name;
01381 }
01382
01383 void Mht::recursiveHypothesisRemove(HypothesisTree::iterator it)
01384 {
01385 if(_hypothesisTree->number_of_children(it)==0)
01386 {
01387 HypothesisTree::iterator parent_it = _hypothesisTree->parent(it);
01388
01389
01390 for(uint t=0;t<(*it)->_targets.size();t++)
01391 (*it)->_targets[t]->_assigned_measurements.clear();
01392 (*it)->_targets.clear();
01393
01394 _hypothesisTree->erase(it);
01395
01396 if(_hypothesisTree->is_valid(parent_it))
01397 recursiveHypothesisRemove(parent_it);
01398 }
01399 }
01400
01401 void Mht::removeIrrelevant(void)
01402 {
01403 for(uint c=0;c<_clusters.size();++c)
01404 {
01405 ClusterPtr cluster = _clusters[c];
01406
01407 HypothesisTree::iterator first = find(_hypothesisTree->begin(),_hypothesisTree->end(),cluster->assigned_hypotheses[0]);
01408
01409 assert(_hypothesisTree->is_valid(first));
01410
01411 HypothesisTree::iterator min = first;
01412 HypothesisTree::iterator aux;
01413
01414 if(cluster->assigned_hypotheses.size()==1)
01415 {
01416
01417
01418 min=first;
01419 for(uint h=0;h<_history_size-1;++h)
01420 {
01421 if(_hypothesisTree->is_valid(_hypothesisTree->parent(min)))
01422 min=_hypothesisTree->parent(min);
01423 else
01424 break;
01425 }
01426
01427 aux=min;
01428 while(_hypothesisTree->is_valid(_hypothesisTree->parent(aux)))
01429 aux=_hypothesisTree->parent(aux);
01430
01431 (*min)->_status=PARENT;
01432
01433 if(_remove_unused)
01434 {
01435 _hypothesisTree->insert_subtree(aux,min);
01436
01437
01438
01439 for(uint t=0;t<(*aux)->_targets.size();t++)
01440 (*aux)->_targets[t]->_assigned_measurements.clear();
01441 (*aux)->_targets.clear();
01442
01443
01444 _hypothesisTree->erase(aux);
01445 }
01446
01447
01448 continue;
01449 }
01450
01451 for(uint h=1;h<cluster->assigned_hypotheses.size();++h)
01452 {
01453 HypothesisPtr hypothesis = cluster->assigned_hypotheses[h];
01454
01455
01456
01457 HypothesisTree::iterator current = find(_hypothesisTree->begin(),_hypothesisTree->end(),hypothesis);
01458
01459 assert(_hypothesisTree->is_valid(current));
01460 assert(_hypothesisTree->is_valid(first));
01461
01462
01463
01464
01465 aux=_hypothesisTree->lowest_common_ancestor(first,current);
01466
01467 assert(_hypothesisTree->is_valid(aux));
01468
01469 if((*aux)->_iteration<(*min)->_iteration)
01470 min=aux;
01471 }
01472
01473 if( (iteration - (*min)->_iteration) > _history_size )
01474 {
01475 int iter = iteration - _history_size + 1;
01476
01477
01478
01479
01480
01481 vector<HypothesisPtr> positions;
01482 HypothesisPtr parent;
01483 HypothesisPtr minhyp;
01484 HypothesisTree::iterator aux_parent;
01485 HypothesisTree::iterator abs_parent;
01486
01487 for(aux=_hypothesisTree->begin();aux!=_hypothesisTree->end();aux++)
01488 {
01489 if((*aux)->_cluster==cluster->id)
01490 if((*aux)->_iteration==iter)
01491 {
01492 if(parent==NULL && _hypothesisTree->is_valid(_hypothesisTree->parent(aux)))
01493 {
01494 aux_parent=_hypothesisTree->parent(aux);
01495 parent=*aux_parent;
01496 }else
01497 positions.push_back(*aux);
01498 }
01499 }
01500
01501 parent=*aux_parent;
01502 minhyp=*min;
01503
01504 parent->_status=PARENT;
01505
01506
01507 _hypothesisTree->insert_subtree(min,aux_parent);
01508
01509 for(uint i=0;i<positions.size();i++)
01510 {
01511
01512
01513
01514 aux_parent=find(_hypothesisTree->begin(),_hypothesisTree->end(),parent);
01515 aux=find(_hypothesisTree->begin(),_hypothesisTree->end(),positions[i]);
01516 _hypothesisTree->append_child(aux_parent,aux);
01517
01518 _hypothesisTree->erase(aux);
01519 }
01520
01521 min=find(_hypothesisTree->begin(),_hypothesisTree->end(),minhyp);
01522
01523 _hypothesisTree->erase(min);
01524
01525
01526
01527 for(aux=_hypothesisTree->begin();aux!=_hypothesisTree->end();aux++)
01528 {
01529 int c=0;
01530 for(HypothesisTree::iterator aux2=_hypothesisTree->begin();aux2!=_hypothesisTree->end();aux2++)
01531 if(*aux==*aux2)
01532 c++;
01533
01534 if(c>1)
01535 cout<<"Repeated hypothesis ("<<c<<") : "<<**aux<<endl;
01536 assert(c==1);
01537 }
01538
01539 continue;
01540 }
01541
01542 aux=min;
01543 while(_hypothesisTree->is_valid(_hypothesisTree->parent(aux)))
01544 aux=_hypothesisTree->parent(aux);
01545
01546 if((*aux)->name()==(*min)->name())
01547 continue;
01548 else
01549 {
01550 (*min)->_status=PARENT;
01551
01552 if(_remove_unused)
01553 {
01554 _hypothesisTree->insert_subtree(aux,min);
01555 _hypothesisTree->erase(aux);
01556 }
01557 }
01558 }
01559 }
01560
01561 void Mht::removeTheDead(void)
01562 {
01563 vector<ClusterPtr>::iterator it_cluster;
01564
01565 for(it_cluster=_clusters.begin();it_cluster!=_clusters.end();)
01566 {
01567 ClusterPtr cluster = *it_cluster;
01568
01569 vector<HypothesisPtr>::iterator it_assigned;
01570
01571 for(it_assigned=cluster->assigned_hypotheses.begin();it_assigned!=cluster->assigned_hypotheses.end();)
01572 {
01573 HypothesisPtr hypothesis = *it_assigned;
01574
01575 if(hypothesis->_status == DEAD)
01576 {
01577 it_assigned = cluster->assigned_hypotheses.erase(it_assigned);
01578
01579 HypothesisTree::iterator it = find(_hypothesisTree->begin(),_hypothesisTree->end(),hypothesis);
01580
01581
01582 recursiveHypothesisRemove(it);
01583 }else
01584 it_assigned++;
01585
01586 if(it_assigned==cluster->assigned_hypotheses.end())
01587 break;
01588 }
01589
01590 if(cluster->assigned_hypotheses.size()==0)
01591 it_cluster = completeRemoveCluster(it_cluster);
01592 else
01593 it_cluster++;
01594
01595 if(it_cluster==_clusters.end())
01596 break;
01597 }
01598 }
01599
01600 vector<ClusterPtr>::iterator Mht::completeRemoveCluster(long id)
01601 {
01602 cout<<"removing cluster id: "<<id<<endl;
01603
01604 vector<ClusterPtr>::iterator it;
01605
01606 for(it=_clusters.begin();it!=_clusters.end();it++)
01607 if((*it)->id==id)
01608 return completeRemoveCluster(it);
01609
01610 return _clusters.end();
01611 }
01612
01613 vector<ClusterPtr>::iterator Mht::completeRemoveCluster(vector<ClusterPtr>::iterator it_cluster)
01614 {
01615 ClusterPtr cluster = *it_cluster;
01616 cout<<"removing cluster it id: "<<cluster->id<<endl;
01617
01618
01619
01620 vector<HypothesisPtr> hypotheses = cluster->assigned_hypotheses;
01621
01622
01623
01624
01625
01626 if(hypotheses.size()>0)
01627 {
01628
01629 HypothesisTree::iterator aux = find(_hypothesisTree->begin(),_hypothesisTree->end(),hypotheses[0]);
01630
01631 if(aux!=NULL)
01632 {
01633
01634
01635
01636 while(_hypothesisTree->is_valid(_hypothesisTree->parent(aux)))
01637 aux=_hypothesisTree->parent(aux);
01638
01639
01640 _hypothesisTree->erase(aux);
01641 }
01642 }
01643
01644
01645 (*it_cluster)->assigned_measurements.clear();
01646
01647 (*it_cluster)->assigned_hypotheses.clear();
01648
01649
01650 it_cluster = _clusters.erase(it_cluster);
01651
01652 return it_cluster;
01653 }
01654
01655 MeasurementPtr Mht::findMeasurement(vector<MeasurementPtr>& measurements, long id)
01656 {
01657 for(uint i=0;i<measurements.size();i++)
01658 if(measurements[i]->id==id)
01659 return measurements[i];
01660 return MeasurementPtr();
01661 }
01662
01663 void Mht::switchColor(HypothesisPtr hypothesis)
01664 {
01665 assert(hypothesis!=0);
01666
01667 static class_colormap*colormap=NULL;
01668 int total = 10;
01669
01670 if(!colormap)
01671 colormap = new class_colormap(string("hsv"),total,1.0);
01672
01673 Color color(colormap->color(hypothesis->_cluster));
01674
01675 hypothesis->setAttribute(string("style"),string("bold, filled"));
01676 hypothesis->setAttribute(string("fillcolor"),string("white"));
01677 hypothesis->setAttribute(string("color"),color.str());
01678
01679 switch(hypothesis->_status)
01680 {
01681 case NORMAL:
01682 break;
01683
01684 case MOVED:
01685 color.changeColorBrightness(0.7);
01686 hypothesis->setAttribute(string("fillcolor"),color.str());
01687 break;
01688
01689 case PARENT:
01690 color.changeColorBrightness(0.0);
01691 hypothesis->setAttribute(string("fillcolor"),color.str());
01692 break;
01693
01694 case FORCED_PARENT:
01695
01696 color.changeColorBrightness(0.2);
01697 hypothesis->setAttribute(string("fillcolor"),color.str());
01698 break;
01699
01700 case DEAD:
01701 hypothesis->setAttribute(string("fillcolor"),string("#D8D8D8"));
01702
01703
01704
01705
01706 break;
01707
01708 case DEAD_FORCED_PARENT:
01709 hypothesis->setAttribute(string("color"),string("white"));
01710 hypothesis->setAttribute(string("fillcolor"),string("black"));
01711 hypothesis->setAttribute(string("fontcolor"),string("white"));
01712 break;
01713
01714 case ERROR:
01715 hypothesis->setAttribute(string("color"),string("#FFFF33"));
01716 hypothesis->setAttribute(string("fillcolor"),string("#FF0000"));
01717 hypothesis->setAttribute(string("fontcolor"),string("white"));
01718 break;
01719 }
01720 }
01721
01722 vector<HypothesisPtr> Mht::findHypotheses(int cl,int it_past)
01723 {
01724 vector<HypothesisPtr> hypotheses;
01725
01726 HypothesisTree::leaf_iterator hleaf;
01727
01728 for(hleaf=_hypothesisTree->begin_leaf();hleaf!=_hypothesisTree->end_leaf();++hleaf)
01729 {
01730 HypothesisPtr hypothesis = *hleaf;
01731
01732 if(hypothesis->_status==DEAD)
01733 continue;
01734
01735 if(it_past==-1)
01736 {
01737 if(hypothesis->_cluster==cl)
01738 hypotheses.push_back(*hleaf);
01739 }else
01740 {
01741 if( (hypothesis->_cluster==cl) && (hypothesis->_iteration==iteration-it_past))
01742 hypotheses.push_back(hypothesis);
01743 }
01744 }
01745
01746 return hypotheses;
01747 }
01748
01749 ClusterPtr Mht::findCluster(vector<ClusterPtr>& clusters, int id)
01750 {
01751 ClusterPtr empty;
01752
01753 for(uint i=0;i<clusters.size();++i)
01754 if(clusters[i]->id==id)
01755 return clusters[i];
01756
01757 return empty;
01758 }
01759
01760 void Mht::createClusters(void)
01761 {
01762 assert(_clusters.size()==0);
01763
01764 ClusterPtr empty;
01765
01766
01767 for(_hleaf=_hypothesisTree->begin_leaf();_hleaf!=_hypothesisTree->end_leaf();++_hleaf)
01768 {
01769 HypothesisPtr hypothesis = *_hleaf;
01770
01771 if( hypothesis->_iteration < (iteration-1))
01772 {
01773
01774
01775 const char COL_RESET[] = "\x1b[0m";
01776 const char RED[] = "\x1b[31m";
01777 cout << RED << "Error!!, Major" << COL_RESET;
01778
01779
01780 cout<<" leaf from the wrong iteration: "<<(*_hleaf)->name()<<endl;
01781
01782 hypothesis->_status=ERROR;
01783
01784
01785
01786
01787
01788
01789
01790
01791
01792
01793
01794
01795
01796
01797
01798 }
01799
01800 if(findCluster(_clusters,hypothesis->_cluster)==empty)
01801 {
01802
01803 ClusterPtr cluster(new Cluster);
01804 cluster->id=hypothesis->_cluster;
01805 cluster->assigned_hypotheses = findHypotheses(cluster->id);
01806
01807 _clusters.push_back(cluster);
01808 }
01809 }
01810 }
01811
01812 void Mht::clusterAssignemnt(vector<MeasurementPtr>& measurements)
01813 {
01814 TargetPtr empty;
01815
01816
01817 for(uint c=0;c<_clusters.size();c++)
01818 {
01819 ClusterPtr cluster = _clusters[c];
01820 vector<HypothesisPtr> hypotheses = cluster->assigned_hypotheses;
01821
01822
01823 for(uint h=0;h<hypotheses.size();h++)
01824 {
01825 HypothesisPtr hypothesis = hypotheses[h];
01826
01827
01828 for(uint t=0;t<hypothesis->_targets.size();++t)
01829 {
01830
01831 TargetPtr target=hypothesis->_targets[t];
01832
01833 assert(target!=empty);
01834
01835
01836 for(uint m=0;m<measurements.size();++m)
01837 {
01838
01839
01840 if(target->getDistance(measurements[m])<_max_gating)
01841 {
01842 target->_assigned_measurements.push_back(measurements[m]);
01843
01844 }else
01845 continue;
01846
01847
01848 if(find(measurements[m]->assigned_clusters.begin(),measurements[m]->assigned_clusters.end(),cluster)!=measurements[m]->assigned_clusters.end())
01849 continue;
01850
01851
01852 measurements[m]->assigned_clusters.push_back(cluster);
01853
01854 if(find(cluster->assigned_measurements.begin(),cluster->assigned_measurements.end(),measurements[m])!=cluster->assigned_measurements.end())
01855 continue;
01856
01857 cluster->assigned_measurements.push_back(measurements[m]);
01858 }
01859 }
01860 }
01861 }
01862 }
01863
01864 void Mht::cleanClusters(void)
01865 {
01866 cout<<"begin clean"<<endl;
01867 vector<ClusterPtr>::iterator it;
01868
01869 for(it=_clusters.begin();it!=_clusters.end();)
01870 if((*it)->isEmpty())
01871 it = completeRemoveCluster(it);
01872 else
01873 it++;
01874
01875 cout<<"end clean"<<endl;
01876 }
01877
01878 void Mht::removeEmptyClusters(void)
01879 {
01880 ClusterPtr empty;
01881
01882 if(_remove_empty_clusters==false)
01883 return;
01884
01885 for(_hleaf=_hypothesisTree->begin_leaf();_hleaf!=_hypothesisTree->end_leaf();++_hleaf)
01886 {
01887 HypothesisPtr hypothesis = *_hleaf;
01888
01889 if(hypothesis->_status==DEAD || hypothesis->_iteration!=iteration)
01890 {
01891 long cluster_id = hypothesis->_cluster;
01892
01893 if(findCluster(_clusters,cluster_id)==empty)
01894 {
01895 cout<<"cluster: "<<cluster_id<<" marked for deletion"<<endl;
01896
01897 HypothesisTree::iterator aux=_hleaf;
01898
01899 while(_hypothesisTree->is_valid(_hypothesisTree->parent(aux)))
01900 aux=_hypothesisTree->parent(aux);
01901
01902 _hypothesisTree->erase(aux);
01903
01904
01905 _hleaf=_hypothesisTree->begin();
01906 }
01907 }
01908 }
01909 }
01910
01911 bool Mht::consistencyCheck(void)
01912 {
01913
01914 for(uint c=0;c<_clusters.size();c++)
01915 {
01916 ClusterPtr cluster = _clusters[c];
01917
01918
01919
01920 if(cluster->assigned_hypotheses.size()==0)
01921 {
01922 cout<<"major error! cluster without hypotheses"<<endl;
01923 cout<<*cluster<<endl;
01924 }
01925
01926 assert(cluster->assigned_hypotheses.size()>0);
01927
01928 for(uint h=0;h<cluster->assigned_hypotheses.size();h++)
01929 {
01930 HypothesisPtr hypothesis = cluster->assigned_hypotheses[h];
01931
01932
01933
01934
01935
01936
01937
01938
01939
01940 assert(hypothesis->_cluster==cluster->id);
01941
01942
01943
01944
01945
01946
01947 assert(hypothesis->_targets.size()>0);
01948
01949 for(uint t=0;t<hypothesis->_targets.size();t++)
01950 {
01951 TargetPtr target = hypothesis->_targets[t];
01952
01953
01954 if(target->_hypothesis!=hypothesis->_uid)
01955 {
01956 cout<<"major error"<<endl;
01957 cout<<*hypothesis<<endl;
01958 cout<<*target<<endl;
01959 }
01960
01961 if(!is_finite(target->_xp) || !is_finite(target->_P))
01962 {
01963 cout<<"major error, iteration "<<iteration<<endl;
01964 cout<<*target<<endl;
01965 cout<<*hypothesis<<endl;
01966 cout<<*cluster<<endl;
01967 cout<<target->_xp<<endl;
01968 }
01969
01970
01971
01972
01973 assert(is_finite(target->_xp));
01974 assert(is_finite(target->_P));
01975
01976
01977 assert(target->_cluster==cluster->id);
01978 assert(target->_hypothesis==hypothesis->_uid);
01979 }
01980 }
01981 }
01982
01983 return true;
01984 }
01985
01986 void Mht::breakClusters(vector<MeasurementPtr>& measurements)
01987 {
01988
01989
01990 vector<ClusterPtr>::iterator it_cluster;
01991 vector<ClusterPtr> new_clusters;
01992
01993
01994 for(it_cluster=_clusters.begin();it_cluster!=_clusters.end();it_cluster++)
01995 {
01996
01997 vector<TargetPtr> break_targets;
01998
01999
02000 ClusterPtr cluster = *it_cluster;
02001
02002 ldb()<<"checking:"<<endl;
02003 ldb()<<*cluster<<endl;
02004
02005
02006 vector<HypothesisPtr>::iterator it_hypothesis;
02007
02008
02009 for(it_hypothesis=cluster->assigned_hypotheses.begin();it_hypothesis!=cluster->assigned_hypotheses.end();it_hypothesis++)
02010 {
02011
02012 HypothesisPtr hypothesis = *it_hypothesis;
02013
02014
02015 assert(hypothesis->_cluster==cluster->id);
02016 assert(hypothesis->_targets.size()>0);
02017
02018 ldb()<<*hypothesis<<endl;
02019
02020
02021 if(hypothesis->_targets.size()<2)
02022 continue;
02023
02024
02025 vector<TargetPtr>::iterator it_target;
02026
02027
02028 for(it_target=hypothesis->_targets.begin();it_target!=hypothesis->_targets.end();it_target++)
02029 {
02030
02031 bool separate_target = true;
02032
02033
02034 TargetPtr target = *it_target;
02035
02036 ldb()<<"check: "<<*target<<endl;
02037
02038
02039 assert(target!=NULL);
02040 assert(target->_cluster==cluster->id);
02041 assert(target->_hypothesis==hypothesis->_uid);
02042
02043
02044 if(target->_assigned_measurements.size()>1)
02045 separate_target=false;
02046
02047
02048
02049 vector<TargetPtr>::iterator it_target_aux;
02050
02051
02052
02053
02054
02055 for(it_target_aux=hypothesis->_targets.begin();it_target_aux!=hypothesis->_targets.end();it_target_aux++)
02056 {
02057
02058
02059 if(it_target_aux==it_target)
02060 {
02061
02062 continue;
02063 }
02064
02065
02066 TargetPtr other_target = *it_target_aux;
02067
02068 assert(other_target!=NULL);
02069
02070
02071
02072
02073
02074
02075
02076 if(target->_assigned_measurements.size()>0)
02077 {
02078 if(find(other_target->_assigned_measurements.begin(),other_target->_assigned_measurements.end(),target->_assigned_measurements[0])!=other_target->_assigned_measurements.end())
02079 separate_target=false;
02080 }
02081
02082
02083
02084
02085
02086
02087
02088 if(target->getDistanceToPredictedPosition(other_target)<_max_gating)
02089 separate_target=false;
02090
02091
02092
02093 }
02094
02095 ldb()<<"done run for targets"<<endl;
02096
02097 if(separate_target==false)
02098 continue;
02099
02100
02101
02102 ldb()<<"possible breaking: "<<*target<<endl;
02103
02104 break_targets.push_back(target);
02105 }
02106
02107 ldb()<<"done checking hypotheses"<<endl;
02108 }
02109
02110 ldb()<<"done checking: possible targets: "<<break_targets.size()<<endl;
02111
02112
02113 if(break_targets.size()==0)
02114 {
02115 ldb()<<"continuing"<<endl;
02116 if(it_cluster==_clusters.end())
02117 {
02118 ldb()<<"breaking"<<endl;
02119 break;
02120 }
02121 continue;
02122 }
02123 ldb()<<"creating list of valid targets"<<endl;
02124
02125 vector<TargetPtr> valid_targets;
02126
02127
02128 assert(cluster!=NULL);
02129
02130
02131
02132
02133
02134
02135
02136
02137
02138
02139
02140
02141
02142 for(uint t=0;t<break_targets.size();t++)
02143 {
02144 HypothesisPtr hypothesis = getHypothesisPtr(break_targets[t]->_hypothesis,cluster);
02145 break_targets[t]->_aux1=hypothesis->_probability;
02146 }
02147
02148
02149
02150 sort(break_targets.begin(),break_targets.end(),compareTargetsByAux1Descending);
02151
02152
02153
02154
02155
02156
02157 for(uint t=0;t<break_targets.size();t++)
02158 {
02159
02160
02161 bool existing=false;
02162
02163 for(uint v=0;v<valid_targets.size();v++)
02164 {
02165 if(break_targets[t]->_id==valid_targets[v]->_id)
02166 {
02167 existing=true;
02168 break;
02169 }
02170
02171 if(break_targets[t]->_assigned_measurements.size()>0 && valid_targets[v]->_assigned_measurements.size()>0)
02172 if(break_targets[t]->_assigned_measurements[0]->id==valid_targets[v]->_assigned_measurements[0]->id)
02173 {
02174 existing=true;
02175 break;
02176 }
02177 }
02178
02179 if(!existing)
02180 valid_targets.push_back(break_targets[t]);
02181 }
02182
02183
02184
02185
02186
02187 for(uint t=0;t<valid_targets.size();t++)
02188 {
02189 TargetPtr target = valid_targets[t];
02190
02191
02192
02193 bool remove=true;
02194
02195
02196 for(vector<HypothesisPtr>::iterator itc=cluster->assigned_hypotheses.begin();itc!=cluster->assigned_hypotheses.end();)
02197 {
02198 HypothesisPtr hypothesis = *itc;
02199
02200 if(hypothesis->_targets.size()==1)
02201 {
02202 remove = false;
02203 break;
02204 }
02205
02206 uint number_of_targets=hypothesis->_targets.size();
02207
02208 for(vector<TargetPtr>::iterator it=hypothesis->_targets.begin();it!=hypothesis->_targets.end();)
02209 {
02210 if((*it)->_id==target->_id)
02211 {
02212
02213 it=hypothesis->_targets.erase(it);
02214 }else if((*it)->_assigned_measurements.size()>0 && target->_assigned_measurements.size()>0)
02215 {
02216 if((*it)->_assigned_measurements[0]->id == target->_assigned_measurements[0]->id)
02217 {
02218
02219 it=hypothesis->_targets.erase(it);
02220 }else
02221 it++;
02222 }else
02223 it++;
02224 }
02225
02226
02227 if(hypothesis->_targets.size()!=number_of_targets-1)
02228 {
02229 hypothesis->_status=DEAD;
02230
02231 itc = cluster->assigned_hypotheses.erase(itc);
02232
02233 HypothesisTree::iterator ith = find(_hypothesisTree->begin(),_hypothesisTree->end(),hypothesis);
02234
02235 recursiveHypothesisRemove(ith);
02236 }else
02237 itc++;
02238 }
02239
02240
02241
02242 if(!remove)
02243 continue;
02244
02245
02246 if(target->_assigned_measurements.size()>0)
02247 {
02248
02249
02250 removeLocal(target->_assigned_measurements[0]->assigned_clusters,cluster);
02251
02252
02253 removeLocal(cluster->assigned_measurements,target->_assigned_measurements[0]);
02254 }
02255
02256 if(cluster->assigned_hypotheses.size()==0)
02257 {
02258 ldb()<<"cluster without any hypothesis: "<<*cluster<<endl;
02259
02260 }
02261
02262
02263
02264
02265
02266
02267 HypothesisPtr new_hypothesis(new Hypothesis);
02268
02269 new_hypothesis->_iteration=iteration-1;
02270 new_hypothesis->_cluster=_cluster_id++;
02271 new_hypothesis->_n_new=1;
02272
02273 target->_cluster = new_hypothesis->_cluster;
02274 target->_hypothesis = new_hypothesis->_uid;
02275
02276
02277
02278
02279 new_hypothesis->_targets.push_back(target);
02280
02281 getProbability(new_hypothesis);
02282
02283
02284 _hypothesisTree->insert(_hypothesisTree->begin(),new_hypothesis);
02285
02286
02287 ClusterPtr new_cluster(new Cluster);
02288
02289 new_cluster->id = new_hypothesis->_cluster;
02290
02291 if(target->_assigned_measurements.size()>0)
02292 new_cluster->assigned_measurements.push_back(target->_assigned_measurements[0]);
02293
02294 new_cluster->assigned_hypotheses.push_back(new_hypothesis);
02295
02296 new_clusters.push_back(new_cluster);
02297
02298
02299
02300
02301
02302 if(target->_assigned_measurements.size()>0 && target->_assigned_measurements[0]!=NULL)
02303 {
02304 for(uint m=0;m<measurements.size();m++)
02305 {
02306 if(measurements[m]->id==target->_assigned_measurements[0]->id)
02307 {
02308 measurements[m]->assigned_clusters.push_back(new_cluster);
02309 break;
02310 }
02311 }
02312
02313
02314 }
02315
02316
02317
02318 }
02319
02320 ldb()<<"finish with cluster"<<endl;
02321 }
02322
02323
02324 ldb()<<"addining: "<<new_clusters.size()<<" new clusters"<<endl;
02325 _clusters.insert(_clusters.end(),new_clusters.begin(),new_clusters.end());
02326
02327 ldb()<<"end cluster break"<<endl;
02328 }
02329
02330 HypothesisTree::iterator Mht::insertForcedParent(long cluster)
02331 {
02332
02333 HypothesisPtr parent_hypothesis(new Hypothesis);
02334
02335 parent_hypothesis->_iteration=-1;
02336 parent_hypothesis->_cluster=cluster;
02337 parent_hypothesis->_status=FORCED_PARENT;
02338 parent_hypothesis->_probability=1.;
02339
02340
02341 return _hypothesisTree->insert(_hypothesisTree->begin(),parent_hypothesis);
02342 }
02343
02344 vector<TargetPtr> Mht::createCopy(vector<TargetPtr>& original)
02345 {
02346
02347
02348 vector<TargetPtr> new_targets;
02349
02350 for(uint t=0;t<original.size();t++)
02351 {
02352 TargetPtr target(new Target);
02353
02354 long uid=target->_uid;
02355
02356 *target=*(original[t]);
02357
02358 target->_uid=uid;
02359 target->_variant+="*";
02360
02361
02362
02363
02364
02365
02366
02367 assert(is_finite(target->_xp));
02368 assert(is_finite(target->_P));
02369
02370
02371 new_targets.push_back(target);
02372 }
02373
02374 return new_targets;
02375 }
02376
02377 void Mht::clusterMerger(vector<MeasurementPtr>& measurements)
02378 {
02379
02380
02381
02382
02383
02384
02385
02386
02387
02388
02389 vector<MeasurementPtr>::iterator it_m;
02390 for(it_m=measurements.begin();it_m!=measurements.end();it_m++)
02391 {
02392 MeasurementPtr measurement = *it_m;
02393
02394
02395 sort(measurement->assigned_clusters.begin(),measurement->assigned_clusters.end(),compareClusters);
02396
02397
02398
02399
02400
02401
02402
02403
02404 if(measurement->assigned_clusters.size()<2)
02405 continue;
02406
02407
02408
02409 ClusterPtr main_cluster = measurement->assigned_clusters[0];
02410
02411
02412 if(main_cluster->isEmpty())
02413 continue;
02414
02415
02416
02417
02418
02419
02420
02421
02422
02423 vector<HypothesisPtr> hypotheses_main = main_cluster->assigned_hypotheses;
02424
02425 assert(hypotheses_main.size()!=0);
02426
02427
02428
02429 vector<ClusterPtr>::iterator it_c;
02430 for(it_c=measurement->assigned_clusters.begin()+1; it_c!=measurement->assigned_clusters.end();it_c++)
02431 {
02432 ClusterPtr secondary_cluster = *it_c;
02433
02434
02435
02436
02437
02438
02439
02440
02441
02442
02443
02444
02445
02446
02447
02448
02449
02450
02451
02452 for(uint h=0;h<secondary_cluster->assigned_hypotheses.size();++h)
02453 {
02454
02455
02456
02457
02458
02459
02460
02461 for(uint g=0;g<hypotheses_main.size();++g)
02462 {
02463 HypothesisPtr hypothesis_main = hypotheses_main[g];
02464
02465
02466 vector<TargetPtr> new_targets = createCopy(secondary_cluster->assigned_hypotheses[h]->_targets);
02467
02468
02469 hypothesis_main->_targets.insert(hypothesis_main->_targets.begin(),new_targets.begin(),new_targets.end());
02470
02471 for(uint i=0;i<hypothesis_main->_targets.size();i++)
02472 {
02473 TargetPtr target = hypothesis_main->_targets[i];
02474
02475 target->_cluster = main_cluster->id;
02476
02477 assert(is_finite(target->_xp));
02478 assert(is_finite(target->_P));
02479 }
02480 }
02481
02482
02483
02484
02485 secondary_cluster->assigned_hypotheses[h]->_targets.clear();
02486 secondary_cluster->assigned_hypotheses[h]->_status=DEAD;
02487
02488 }
02489
02490
02491 for(uint mh=0;mh<hypotheses_main.size();mh++)
02492 {
02493 vector<TargetPtr> valid_targets;
02494 vector<TargetPtr> all_targets = hypotheses_main[mh]->_targets;
02495
02496
02497 for(uint t=0;t<all_targets.size();t++)
02498 for(uint h=0;h<secondary_cluster->assigned_hypotheses.size();h++)
02499 if(all_targets[t]->_hypothesis==secondary_cluster->assigned_hypotheses[h]->_uid)
02500 {
02501 all_targets[t]->_aux1=secondary_cluster->assigned_hypotheses[h]->_probability;
02502 break;
02503 }
02504
02505
02506 for(uint t=0;t<all_targets.size();t++)
02507 {
02508 bool existing=false;
02509
02510 for(uint v=0;v<valid_targets.size();v++)
02511 {
02512 if(all_targets[t]->_id==valid_targets[v]->_id)
02513 {
02514 if(all_targets[t]->_aux1>valid_targets[v]->_aux1)
02515 valid_targets[v]=all_targets[t];
02516
02517 existing=true;
02518 break;
02519 }
02520 }
02521
02522 if(!existing)
02523 valid_targets.push_back(all_targets[t]);
02524 }
02525
02526 hypotheses_main[mh]->_targets=valid_targets;
02527
02528
02529 for(uint t=0;t<hypotheses_main[mh]->_targets.size();t++)
02530 {
02531 hypotheses_main[mh]->_targets[t]->_hypothesis=hypotheses_main[mh]->_uid;
02532 hypotheses_main[mh]->_targets[t]->_cluster=hypotheses_main[mh]->_cluster;
02533 }
02534 }
02535
02536
02537
02538
02539
02540
02541
02542
02543 for(uint im=0;im<secondary_cluster->assigned_measurements.size();im++)
02544 {
02545
02546
02547
02548
02549 MeasurementPtr measurement_move = secondary_cluster->assigned_measurements[im];
02550 assert(measurement_move!=MeasurementPtr());
02551
02552
02553
02554
02555
02556
02557
02558
02559 if(measurement_move == measurement)
02560 continue;
02561
02562
02563 removeLocal(measurement_move->assigned_clusters,secondary_cluster);
02564
02565
02566
02567
02568 if(find(main_cluster->assigned_measurements.begin(),main_cluster->assigned_measurements.end(),measurement_move)==main_cluster->assigned_measurements.end())
02569 {
02570
02571 main_cluster->assigned_measurements.push_back(measurement_move);
02572 }
02573
02574
02575
02576
02577 if(find(measurement_move->assigned_clusters.begin(),measurement_move->assigned_clusters.end(),main_cluster)==measurement_move->assigned_clusters.end())
02578 measurement_move->assigned_clusters.push_back(main_cluster);
02579
02580
02581 }
02582
02583 secondary_cluster->assigned_measurements.clear();
02584
02585
02586
02587
02588
02589
02590
02591
02592
02593
02594
02595
02596
02597
02598
02599
02600 }
02601
02602 measurement->assigned_clusters.clear();
02603 measurement->assigned_clusters.push_back(main_cluster);
02604
02605
02606
02607 }
02608
02609
02610
02611
02612
02613
02614
02615
02616
02617
02618 }
02619
02620 TargetPtr Mht::associate(TargetPtr& target,MeasurementPtr& measurement)
02621 {
02622 TargetPtr empty;
02623
02624
02625 if(target->getDistance(measurement)>_max_gating)
02626 {
02627
02628 if(target->_missed_associations < _miss_association_threshold)
02629 {
02630
02631 TargetPtr new_target(new Target(target));
02632 new_target->_assigned_measurements.clear();
02633
02634
02635
02636
02637
02638
02639 return new_target;
02640 }else
02641 {
02642
02643 return empty;
02644 }
02645 }else
02646 {
02647
02648 TargetPtr new_target(new Target(target,measurement));
02649
02650
02651
02652
02653
02654
02655
02656
02657
02658 return new_target;
02659 }
02660
02661 assert(NULL);
02662 return empty;
02663 }
02664
02665 bool Mht::normalize(vector<HypothesisPtr>& hypotheses,uint j)
02666 {
02667 if(hypotheses.size()==0)
02668 return false;
02669
02670 double total=hypotheses.size()>j?j:hypotheses.size();
02671 double sum=0;
02672
02673 for(uint i=0;i<total;++i)
02674 sum+=hypotheses[i]->_probability;
02675
02676 if(fpclassify(sum) != FP_ZERO)
02677 for(uint i=0;i<total;++i)
02678 hypotheses[i]->_probability/=sum;
02679 else
02680 for(uint i=0;i<total;++i)
02681 hypotheses[i]->_probability=1./total;
02682
02683 return true;
02684 }
02685
02686 void Mht::getProbability(HypothesisPtr hypothesis)
02687 {
02688 if(hypothesis->_parent_uid!=-1)
02689 {
02690 HypothesisTree::iterator it;
02691 for(it=_hypothesisTree->begin();it!=_hypothesisTree->end();it++)
02692 if((*it)->_uid==hypothesis->_parent_uid)
02693 break;
02694
02695 hypothesis->_probability=getProbability(hypothesis->_n_det,hypothesis->_n_occ,hypothesis->_n_del,hypothesis->_n_new,hypothesis->_n_fal,hypothesis->_prod,(*it)->_probability);
02696 }else
02697 hypothesis->_probability=getProbability(hypothesis->_n_det,hypothesis->_n_occ,hypothesis->_n_del,hypothesis->_n_new,hypothesis->_n_fal,hypothesis->_prod,1);
02698 }
02699
02700 double Mht::getProbability(uint n_det, uint n_occ, uint n_del, uint n_new, uint n_fal, double prod, double previous)
02701 {
02702
02703
02704
02705
02706 double p_det = 0.8;
02707 double p_occ = 0.01;
02708 double p_del = 0.2;
02709
02710 double lambda_new=0.001;
02711 double lambda_fal=0.001;
02712
02713 double p=1;
02714 p*=pow(p_det,n_det);
02715 p*=pow(p_occ,n_occ);
02716 p*=pow(p_del,n_del);
02717 p*=pow(lambda_new,n_new);
02718 p*=pow(lambda_fal,n_fal);
02719 p*=prod;
02720 p*=previous;
02721
02722
02723
02724
02725
02726
02727
02728
02729
02730 return p;
02731 }
02732
02733 HypothesisPtr Mht::getHypothesisPtr(long id,ClusterPtr& cluster)
02734 {
02735 for(uint h=0;h<cluster->assigned_hypotheses.size();h++)
02736 if(cluster->assigned_hypotheses[h]->_uid==id)
02737 return cluster->assigned_hypotheses[h];
02738 return HypothesisPtr();
02739 }
02740
02741 void Mht::updateTargetList(void)
02742 {
02743 ldb()<<"updating targets"<<endl;
02744 _current_targets.clear();
02745
02746
02747 for(uint i=0;i<_clusters.size();i++)
02748 {
02749 assert(_clusters[i]->assigned_hypotheses.size()!=0);
02750
02751
02752 sort(_clusters[i]->assigned_hypotheses.begin(),_clusters[i]->assigned_hypotheses.end(),compareHypothesesByProbabilityDescending);
02753
02754
02755 assert(_clusters[i]->assigned_hypotheses[0]!=0);
02756
02757
02758 ldb()<<"cluster: "<<_clusters[i]->id<<" nMeasurements: "<<_clusters[i]->assigned_measurements.size()<<endl;
02759
02760 for(uint h=0;h<_clusters[i]->assigned_hypotheses.size();h++)
02761 {
02762 ldb()<<"\thypothesis: "<<_clusters[i]->assigned_hypotheses[h]->_uid<<" p: "<<_clusters[i]->assigned_hypotheses[h]->_probability<<endl;
02763 for(uint t=0;t<_clusters[i]->assigned_hypotheses[h]->_targets.size();t++)
02764 ldb()<<"\t\ttarget: "<<_clusters[i]->assigned_hypotheses[h]->_targets[t]->_id<<" ("<<_clusters[i]->assigned_hypotheses[h]->_targets[t]->_uid<<")"<<endl;
02765 }
02766 ldb()<<endl;
02767
02768 _current_targets.insert(_current_targets.begin(),_clusters[i]->assigned_hypotheses[0]->_targets.begin(),_clusters[i]->assigned_hypotheses[0]->_targets.end());
02769 }
02770
02771 vector<TargetPtr> valid_targets;
02772
02773 for(uint t=0;t<_current_targets.size();t++)
02774 {
02775 bool valid=true;
02776
02777 TargetPtr target = _current_targets[t];
02778
02779
02780
02781
02782
02783
02784
02785
02786
02787 for(uint vt=0;vt<valid_targets.size();vt++)
02788 if(valid_targets[vt]->_id==target->_id)
02789 valid=false;
02790
02791 for(uint vt=0;vt<valid_targets.size();vt++)
02792 if(valid_targets[vt]->_uid==target->_uid)
02793 valid=false;
02794
02795
02796
02797
02798 if(valid)
02799 valid_targets.push_back(target);
02800 }
02801
02802 _current_targets=valid_targets;
02803
02804 sort(_current_targets.begin(),_current_targets.end(),compareTargetsById);
02805
02806 ldb()<<endl;
02807 }
02808
02809 void Mht::checkCurrentTargets(vector<MeasurementPtr>& measurements)
02810 {
02811 string report_name;
02812
02813
02814
02815 ofstream report_total_targets;
02816 report_name = getReportName("_total");
02817
02818 report_total_targets .open(report_name.c_str(),ios::app);
02819
02820 ldb(0)<<"total report file: "<<report_name<<endl;
02821
02822 report_total_targets <<"#"<<endl;
02823 report_total_targets <<"i "<<iteration<<endl;
02824 report_total_targets <<Target::_ntotal<<endl;
02825
02826 report_total_targets.close();
02827
02828
02829
02830
02831 ofstream report_distance;
02832 report_name = getReportName("_dist");
02833
02834 report_distance.open(report_name.c_str(),ios::app);
02835
02836 ldb(0)<<"distance report file: "<<report_name<<endl;
02837
02838 report_distance<<"#"<<endl;
02839 report_distance<<"i "<<iteration<<endl;
02840
02841 for(uint t=0;t<_current_targets.size();t++)
02842 {
02843 TargetPtr target = _current_targets[t];
02844
02845 bool found=false;
02846
02847 report_distance<<target->_id;
02848
02849 for(uint m=0;m<measurements.size();m++)
02850 {
02851 MeasurementPtr measurement = measurements[m];
02852
02853 if(target->_exuid==measurement->id)
02854 {
02855 double distance = target->getDistance(measurement,2);
02856
02857 report_distance<<" "<<distance<<endl;
02858
02859 found=true;
02860 break;
02861 }
02862 }
02863
02864 if(!found)
02865 {
02866 report_distance<<" -1"<<endl;
02867 }
02868 }
02869
02870 report_distance.close();
02871
02872
02873
02874
02875 ofstream report;
02876 report_name = getReportName("");
02877
02878 report.open(report_name.c_str(),ios::app);
02879
02880 ldb(0)<<"data file: "<<fileName<<endl;
02881 ldb(0)<<"report file: "<<report_name<<endl;
02882
02883 report<<"#"<<endl;
02884 report<<"i "<<iteration<<endl;
02885
02886 for(uint t=0;t<_current_targets.size();t++)
02887 {
02888 TargetPtr target = _current_targets[t];
02889
02890 report<<target->_id;
02891
02892 if(target->_assigned_measurements.size()==0)
02893 {
02894 bool found = false;
02895 for(uint m=0;m<measurements.size();m++)
02896 if(measurements[m]->id==target->_exuid)
02897 {
02898 found=true;
02899 break;
02900 }
02901
02902 if(found)
02903 {
02904 cout<<"Error!!, target: "<<target->_id<<" no assigned measurement"<<endl;
02905 report<<" nok"<<endl;
02906 }else
02907 report<<" ok"<<endl;
02908
02909 }else if(target->_exuid != target->_assigned_measurements[0]->id)
02910 {
02911 cout<<"Error!!, target("<<target->_id<<"): "<<target->_exuid<<" with measurement: "<<target->_assigned_measurements[0]->id<<endl;
02912 report<<" nok"<<endl;
02913 }else
02914 {
02915 report<<" ok"<<endl;
02916 }
02917
02918 assert(is_finite(target->_xp));
02919 assert(is_finite(target->_P));
02920 }
02921
02922 report.close();
02923 }