Commit a9f70deb12513f57a5403cc4c4f94a83232470e0

Authored by Anthex
1 parent 5d3499c7

Added N-Lateration function and factory dataset

Showing 2 changed files with 46 additions and 8 deletions   Show diff stats
1   -from structure import RSSVector, Location, Cell, newCell, KNeighbors, resolve_barycenter, MarkovModel
  1 +from structure import RSSVector, Location, Cell, newCell, KNeighbors, resolve_barycenter, MarkovModel, NLateration
2 2 from random import random
3 3 from math import floor
4 4  
5 5 Tf = [] #cells table
6 6  
  7 +#dataset : list of tuples representing emitters (Location, distance)
  8 +dataset = [(Location(.5,.5,.5), 3.0), (Location(4.0,.0,.0), 2.0), (Location(4.0,5.0,5.0), 4.2), (Location(3.0,3.0,3.0), 2.5)]
  9 +
7 10 testSample = RSSVector(-26, -42, -13, -46)
8 11  
9 12 #known fingerprints
... ... @@ -12,16 +15,22 @@ Tf = [[newCell(-38,-27,-54,-13,2,2),newCell(-74,-62,-48,-33,2,6),newCell(-13,-28
12 15 [newCell(-17,-50,-44,-33,10,2), newCell(-27,-28,-32,-45,10,6), newCell(-30,-20,-60,-40,10,10)]]
13 16  
14 17 def main():
  18 + #### N-Lateration ####
  19 + NLat_result = NLateration(dataset)
  20 + print("\r\nN-Lateration :\nComputed location : " + NLat_result[0].toString())
  21 + print("With distance = " + str(round(NLat_result[1], 2)) + " m")
  22 +
15 23 #### K neighbours ####
16   - print("\nk neighbors of test sample : ")
  24 + print("\nK-neighbors of test sample : ")
17 25 neighborsCells = KNeighbors(Tf, testSample)
18 26 for k in neighborsCells:
19 27 print("(", k.location.x, ";", k.location.y, ")")
20 28  
21 29 #### Distances ####
22   - print ("\ndistances : " + str(testSample.distances))
  30 + print ("\nDistances : " + str(testSample.distances))
23 31  
24 32 #### Barycenter ####
  33 + print("\r\nWeighted barycenter :")
25 34 a = resolve_barycenter(neighborsCells, testSample.distances)
26 35 print(a.toString())
27 36  
... ...
structure.py
1 1 from operator import itemgetter as ig
2   -from math import floor
3   -
  2 +from math import floor, sqrt
  3 +from numpy import arange
4 4 class RSSVector():
5 5 distances = []
6 6 def __init__(self, n1, n2, n3, n4):
... ... @@ -20,7 +20,7 @@ class RSSVector():
20 20 and v2.n3 == self.n3 and v2.n4 == self.n4 else False
21 21  
22 22 class Location():
23   - def __init__(self, x, y, z=0):
  23 + def __init__(self, x=0, y=0, z=0):
24 24 self.x = x
25 25 self.y = y
26 26 self.z = z
... ... @@ -28,7 +28,6 @@ class Location():
28 28 def __eq__(self, loc2):
29 29 return bool(self.x == loc2.x and self.y == loc2.y and self.z == loc2.z)
30 30  
31   -
32 31 def __mul__(self, multiplier):
33 32 returnValue = Location(self.x, self.y, self.z)
34 33 returnValue.x *= multiplier
... ... @@ -59,6 +58,9 @@ class Location():
59 58 def toString(self):
60 59 return "(" + str(self.x) + " ; " + str(self.y) + " ; " + str(self.z) + ")"
61 60  
  61 + def distanceTo(self, loc):
  62 + return sqrt(pow(self.x - loc.x, 2) + pow(self.y - loc.y, 2) + pow(self.z - loc.z,2))
  63 +
62 64 def getPositionInArray(self, arraySize=3):
63 65 '''
64 66 Returns the unique ID of a fingerprint given its location
... ... @@ -265,4 +267,31 @@ def resolve_barycenter(nC, d):
265 267 + 1 / (1+d[1]/d[0]+d[1]/d[2]+d[1]/d[3])*nC[1].location \
266 268 + 1 / (1+d[2]/d[1]+d[2]/d[0]+d[2]/d[3])*nC[2].location \
267 269 + 1 / (1+d[3]/d[1]+d[3]/d[2]+d[3]/d[0])*nC[3].location
268   -
269 270 \ No newline at end of file
  271 +
  272 +
  273 +def NLateration(data, step=.1, xSize=0.0, ySize=0.0, zSize=0.0):
  274 + '''
  275 + Returns a tuple containing (Computed Location, Total distance)
  276 + :param data: Array of tuples containing (Location, distance) of known emitters
  277 + :param step: Increment of grid search (lower step => better precision => slower computation)
  278 + :param xSize: The X dimension of the cuboid containing all the emitters (automatically computed if not specified)
  279 + :param ySize: The Y dimension of the cuboid containing all the emitters (automatically computed if not specified)
  280 + :param zSize: The Z dimension of the cuboid containing all the emitters (automatically computed if not specified)
  281 + '''
  282 + minLoc = Location()
  283 + minDist = xMax = yMax = zMax = 0.0
  284 + for k in data:
  285 + minDist += abs(k[0].distanceTo(Location()) - k[1])
  286 + xMax = k[0].x if k[0].x > xMax and not xSize else k[0].x
  287 + yMax = k[0].y if k[0].y > yMax and not ySize else k[0].y
  288 + zMax = k[0].z if k[0].z > zMax and not zSize else k[0].z
  289 + for k in arange(0.1,xSize,step):
  290 + for l in arange(0.1,ySize,step):
  291 + for m in arange(0.1,zSize,step):
  292 + d = .0
  293 + for n in data:
  294 + d += abs(n[0].distanceTo(Location(k,l,m)) - n[1])
  295 + if d < minDist:
  296 + minDist = d
  297 + minLoc = Location(round(k,2),round(l,2),round(m,2))
  298 + return (minLoc, minDist)
270 299 \ No newline at end of file
... ...