Skip to content

Commit 1748f38

Browse files
committed
Reduce the computation time in the 1d localizer
1 parent 4be3fb6 commit 1748f38

File tree

3 files changed

+48
-24
lines changed

3 files changed

+48
-24
lines changed

NavCog/Model/Localization/OneDLocalizer.mm

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

NavCog/NavLogging/P2PManager.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,6 @@
5757
- (void) stopBrowse;
5858
- (void) invite;
5959

60+
- (bool) isActive;
61+
6062
@end

NavCog/NavLogging/P2PManager.m

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,17 @@ -(void)session:(MCSession *)session didReceiveCertificate:(NSArray *)certificate
122122
- (void) send: (NSDictionary*) content withType: (NSString*) type{
123123
NSDictionary *dic = @{@"type":type, @"content":content};
124124
//NSData *jdata = [NSJSONSerialization dataWithJSONObject:dic options:0 error:nil];
125-
NSData *jdata = [NSKeyedArchiver archivedDataWithRootObject:dic];
125+
NSError *error = nil;
126+
NSArray *peerIDs = self.mSession.connectedPeers;
126127

128+
if(! [self isActive]){
129+
return;
130+
}
131+
132+
NSData *jdata = [NSKeyedArchiver archivedDataWithRootObject:dic];
127133
//jdata = [NavUtil compressByGzip:jdata];
128134

129135
//NSLog(@"send %@ data %ld bytes", type, (unsigned long)[jdata length]);
130-
NSError *error = nil;
131-
NSArray *peerIDs = self.mSession.connectedPeers;
132136

133137
[self.mSession sendData:jdata
134138
toPeers:peerIDs
@@ -235,7 +239,14 @@ - (void)browser:(MCNearbyServiceBrowser *)browser didNotStartBrowsingForPeers:(N
235239
NSLog(@"didNotStartBrowsingForPeers");
236240
}
237241

238-
242+
- (bool) isActive{
243+
NSArray *peerIDs = self.mSession.connectedPeers;
244+
if(peerIDs==nil || peerIDs.count==0){
245+
return false;
246+
}else{
247+
return true;
248+
}
249+
}
239250

240251
@end
241252

0 commit comments

Comments
 (0)