@@ -75,6 +75,8 @@ @interface OneDLocalizer ()
7575@property double alphaObsModel;
7676
7777@property BOOL transiting;
78+ @property int nEvalPoint;
79+ @property double cumProba;
7880
7981@end
8082
@@ -158,6 +160,8 @@ - (void)initializeWithFile:(NSString *)path
158160 NSLog (@" %@ " , [_beaconIDs componentsJoinedByString: @" ," ]);
159161
160162
163+ _nEvalPoint = 1000 ;
164+ _cumProba = 0.99 ;
161165
162166 _localizer = new StreamParticleFilter ();
163167 _userData.localizer = self;
@@ -484,15 +488,12 @@ - (void) inputBeacons:(NSArray*) beacons
484488
485489 _result = r;
486490}
487-
488- int nEvalPoint = 1000 ;
489- double cumProba = 0.99 ;
490-
491+
491492- (void ) inputBeaconsTransit : (Beacons) beacons {
492493
493- States states = _statusInitializer->initializeStates (nEvalPoint);
494+ States states = _statusInitializer->initializeStates (self. nEvalPoint );
494495
495- State maxLLState = [self findMaximumLikelihoodLocation: beacons Given: states With: cumProba];
496+ State maxLLState = [self findMaximumLikelihoodLocation: beacons Given: states With: self . cumProba];
496497 NavLocalizeResult *r = [[NavLocalizeResult alloc ] init ];
497498
498499 r.x = Meter2Feet (maxLLState.x ());
@@ -523,20 +524,29 @@ - (State) findMaximumLikelihoodLocation: (Beacons) beacons Given: (States) state
523524 Beacons beaconsFiltered = _beaconFilter->filter (beacons);
524525
525526 _obsModel->fillsUnknownBeaconRssi (false );
526- std::vector<std::vector<double >> logLLAndMahaDists = _obsModel->computeLogLikelihoodRelatedValues (states, beaconsFiltered);
527527
528528 double countKnown = 0 , countUnknown = 0 ;
529529 double minMahaDist = std::numeric_limits<double >::max ();
530530 State stateMinMD;
531- for (int i=0 ; i<states.size (); i++){
532- std::vector<double > logLLAndMahaDist = logLLAndMahaDists.at (i);
533- double logLikelihood = _alphaObsModel * logLLAndMahaDist.at (0 );
534- double mahaDist = _alphaObsModel * logLLAndMahaDist.at (1 );
535- countKnown = logLLAndMahaDist.at (2 );
536- countUnknown = logLLAndMahaDist.at (3 );
537- if (mahaDist < minMahaDist){
538- minMahaDist = mahaDist;
539- stateMinMD = states.at (i);
531+ // Count how many beacons the observation model knows
532+ std::vector<double > logLLAndMahaDistsFor1stState = _obsModel->computeLogLikelihoodRelatedValues (states.at (0 ), beaconsFiltered);
533+ countKnown = logLLAndMahaDistsFor1stState.at (2 );
534+ countUnknown = logLLAndMahaDistsFor1stState.at (3 );
535+ // If the observation model knows no beacon, likelihood evaluation for all the states is skipped.
536+ if (countKnown==0 ){
537+ stateMinMD = states.at (0 );
538+ }else {
539+ std::vector<std::vector<double >> logLLAndMahaDists = _obsModel->computeLogLikelihoodRelatedValues (states, beaconsFiltered);
540+ for (int i=0 ; i<states.size (); i++){
541+ std::vector<double > logLLAndMahaDist = logLLAndMahaDists.at (i);
542+ double logLikelihood = _alphaObsModel * logLLAndMahaDist.at (0 );
543+ double mahaDist = _alphaObsModel * logLLAndMahaDist.at (1 );
544+ countKnown = logLLAndMahaDist.at (2 );
545+ countUnknown = logLLAndMahaDist.at (3 );
546+ if (mahaDist < minMahaDist){
547+ minMahaDist = mahaDist;
548+ stateMinMD = states.at (i);
549+ }
540550 }
541551 }
542552
@@ -585,8 +595,10 @@ - (void) sendStatusByP2P: (Status) status{
585595 if (self != activeLocalizer) {
586596 return ;
587597 }
588- NSDictionary * data = [self statusToNSData: status];
589- [[P2PManager sharedInstance ] send: data withType: @" 2d-status" ];
598+ if ([[P2PManager sharedInstance ] isActive ]){
599+ NSDictionary * data = [self statusToNSData: status];
600+ [[P2PManager sharedInstance ] send: data withType: @" 2d-status" ];
601+ }
590602}
591603
592604- (void ) sendStatesByP2P : (States) states {
@@ -686,7 +698,6 @@ - (double) computeDistanceScoreWithOptions: (NSDictionary*) options{
686698
687699- (double ) computeBeaconBasedDistanceScoreWithOptions : (NSDictionary *) options {
688700 double intervalInFeet = 1 ;
689- double cumDensity = 0.99 ;
690701 NSString * edgeID = options[@" edgeID" ];
691702 NavLightEdgeHolder* holder = [NavLightEdgeHolder sharedInstance ];
692703 NavLightEdge* edge = [holder getNavLightEdgeByEdgeID: edgeID];
@@ -698,7 +709,7 @@ - (double) computeBeaconBasedDistanceScoreWithOptions: (NSDictionary*) options{
698709 int dof = static_cast <int >(beaconsFiltered.size ());
699710
700711 if (_cbeacons.size ()>0 ){
701- v = [self computeNormalizedMahalanobisDistance: beaconsFiltered Given: states With: cumDensity ];
712+ v = [self computeNormalizedMahalanobisDistance: beaconsFiltered Given: states With: self .cumProba ];
702713 }
703714 // double v = _normalizedMahalanobisDistance;
704715 // NSLog(@"2D knnDist: eid=%@, dist=%.2f, dof=%d", edgeID, v, dof);
0 commit comments