How to arrive at a formula from logical conditions?

43 Views Asked by At

I'm making a mobile app that needs to acquire the users location as accurately as possible. Doing so isn't as straightforward as one might think, but comes down to making a few tradeoffs. Particularly, one must ultimately face the potential conflict of deciding between using a location update (from GPS, WiFi, etc) which has great accuracy but was received a while ago, or another one that has poorer accuracy but was received more recently.

I came up with a basic algorithm for the most obvious cases:

  • A. Location fixes should be disregarded if they either

    1. Are more than 1 minute older than the current location fix.
    2. Or have more than 200 m worse accuracy than the current location fix.
  • B. Location fixes should be used if they either

    1. Are more than 1 minute newer than the current location fix.
    2. Or have more than 200 m better accuracy than the current location fix.

To sum these up, let's introduce the variables timeDelta and accuracyDelta.

timeDelta     = newLocationFixTime - currentLocationFixTime
accuracyDelta = newLocationFixAccuracy - currentLocationFixAccuracy

From this we can see that a larger timeDelta and a smaller accuracyDelta is preferable (a smaller accuracy means a smaller "circle", i.e. more exact/reliable location fix).

However, with the given basic algorithm, there is still a "window" of possible combinations of timeDelta and accuracyDelta which we have to handle, and this is where it gets a bit tricky. To make it easier for myself I decided to visualize it, and I came up with the following illustration. The red part represents conditions A1 and A2, and the green part represent conditions B1 and B2.

enter image description here

Now it became more apparent that two other cases have an obvious answer:

  • Location fixes should be used if they are newer and have better accuracy than the current location fix.
  • Location fixes should be disregarded if they are older and have worse accuracy than the current location fix.

Visualizing these two cases gives us the following illustration:

enter image description here

Now there are only two cases left:

  • The new location fix is newer than the current one but has a worse accuracy (up to 200 m difference)
  • The new location fix has better accuracy than the current one but is older (up to 60 seconds).

Just by looking at the illustration at this point, it is reasonable to assume that perhaps we should draw a diagonal line from the bottom left to the top right. And so I did:

enter image description here

Hmm, now it's beginning to look like a chart. Looking at the numbers at the edges of the straight line, one can imagine that the formula would be something like

timeDelta = accuracyDelta/(200/60) = accuracyDelta*0.3

And one can also imagine that the area under the line should be red (i.e. "disregard the new location fix") and the area above it should be green (i.e. "use the new location fix").

Or to put it into code:

boolean shouldUseNewLocationFix = accuracyDelta*0.3 <= timeDelta

Putting it into a spreadsheet with the above formula and some conditional formatting would look like this:

enter image description here

So finally to my question

What have I done? Does this sort of thing have a name? And is there a better way to do something like this?

It seems like there should be an easier and more "exact" way to arrive at that formula than making illustrations with colored fields to narrow down the possibilities and then sort of guessing the actual formula?