Commit 23328613d26d61d3a9f20ef54f1c75f4677e97d6
1 parent
8e9d77cf
fix return value of resolve_barycenter
Showing
2 changed files
with
22 additions
and
16 deletions
Show diff stats
main.py
... | ... | @@ -3,8 +3,13 @@ from structure import * |
3 | 3 | import sys |
4 | 4 | |
5 | 5 | Tf = [] #cells table |
6 | + | |
6 | 7 | testSample = RSSVector(-26, -42, -13, -46) |
8 | +testSample2 = RSSVector(-26, -42, -13, -46) | |
9 | +testSample3 = RSSVector(-26, -42, -13, -46) | |
7 | 10 | |
11 | +testSamples = [] | |
12 | +testSamples.extend([testSample, testSample2, testSample3]) | |
8 | 13 | |
9 | 14 | #cells Table initialization |
10 | 15 | for i in range (0, 3): |
... | ... | @@ -12,6 +17,7 @@ for i in range (0, 3): |
12 | 17 | for k in range (0,3): |
13 | 18 | Tf[i].append([]) |
14 | 19 | |
20 | +#known fingerprints | |
15 | 21 | Tf[0][0] = Cell(RSSVector(-38,-27,-54,-13), Location(2,2)) |
16 | 22 | Tf[0][1] = Cell(RSSVector(-74,-62,-48,-33), Location(2,6)) |
17 | 23 | Tf[0][2] = Cell(RSSVector(-13,-28,-12,-40), Location(2,10)) |
... | ... | @@ -23,24 +29,20 @@ Tf[2][1] = Cell(RSSVector(-27,-28,-32,-45), Location(10,6)) |
23 | 29 | Tf[2][2] = Cell(RSSVector(-30,-20,-60,-40), Location(10,10)) |
24 | 30 | |
25 | 31 | def main(args): |
26 | - | |
27 | - print(Tf[1][1].v.n2) | |
28 | - | |
29 | - | |
32 | + #### K neighbours #### | |
30 | 33 | print("\nk neighbors of test sample : ") |
31 | 34 | neighborsCells = KNeighbors(Tf, testSample) |
32 | 35 | for k in neighborsCells: |
33 | 36 | print("(", k.location.x, ";", k.location.y, ")") |
34 | 37 | |
38 | + #### Distances #### | |
35 | 39 | print ("\ndistances : " + str(testSample.distances)) |
36 | 40 | |
37 | - testLoc = Location(3,4,5) | |
38 | - loc2 = Location(1,2,3) | |
39 | - print((testLoc+loc2).toString()) | |
40 | - | |
41 | + #### Barycenter #### | |
41 | 42 | a = resolve_barycenter(neighborsCells, testSample) |
42 | - print(a.toString) | |
43 | - return 0; | |
43 | + print(a.toString()) | |
44 | 44 | |
45 | + #### Markov #### | |
45 | 46 | |
47 | + return 0; | |
46 | 48 | main(sys.argv) |
47 | 49 | \ No newline at end of file | ... | ... |
structure.py
1 | -from operator import itemgetter | |
1 | +from operator import itemgetter as ig | |
2 | 2 | |
3 | 3 | class RSSVector(): |
4 | 4 | distances = [] |
... | ... | @@ -41,7 +41,8 @@ class Cell(): |
41 | 41 | def __init__(self, v_, loc): |
42 | 42 | self.v = v_ |
43 | 43 | self.location = loc |
44 | - | |
44 | + self.Likeliness = .0 # Probability of Markov model | |
45 | + self.pastCount = 1 | |
45 | 46 | |
46 | 47 | def KNeighbors(fingerprints, sample): |
47 | 48 | ''' |
... | ... | @@ -58,7 +59,7 @@ def KNeighbors(fingerprints, sample): |
58 | 59 | + abs(currentItem.v.n3 - sample.n3) \ |
59 | 60 | + abs(currentItem.v.n4 - sample.n4) |
60 | 61 | distances.append((dist, currentItem)) |
61 | - distances = sorted(distances, key=itemgetter(0)) | |
62 | + distances = sorted(distances, key=ig(0)) | |
62 | 63 | neighbours = [] |
63 | 64 | for k in range (0,4): |
64 | 65 | neighbours.append(distances[k][1]) |
... | ... | @@ -72,9 +73,12 @@ def resolve_barycenter(neighbourCells, sample): |
72 | 73 | Returns the weighted barycenter of the 4 neighbouring cells |
73 | 74 | :param Cell[4] neighbourCells: Array containing the 4 closest cells |
74 | 75 | :param RSSIVector sample: Sample of the mobile terminal |
75 | - :return Location: Estimated location of the mobile terminal | |
76 | + :return Location: Estimated location of the mobile terminal (return None if error) | |
76 | 77 | ''' |
77 | 78 | d = sample.distances #shorter notation |
78 | - a1,a2,a3,a4 = 1 / (1+d[0]/d[1]+d[0]/d[2]+d[0]/d[3]), 1 / (1+d[1]/d[0]+d[1]/d[2]+d[1]/d[3]), 1 / (1+d[2]/d[1]+d[2]/d[0]+d[2]/d[3]), 1 / (1+d[3]/d[1]+d[3]/d[2]+d[3]/d[0]) | |
79 | - return a1*neighbourCells[0].location + a2*neighbourCells[1].location + a3*neighbourCells[1].location + a4*neighbourCells[1].location | |
79 | + a1,a2,a3,a4 = 1 / (1+d[0]/d[1]+d[0]/d[2]+d[0]/d[3]),\ | |
80 | + 1 / (1+d[1]/d[0]+d[1]/d[2]+d[1]/d[3]),\ | |
81 | + 1 / (1+d[2]/d[1]+d[2]/d[0]+d[2]/d[3]),\ | |
82 | + 1 / (1+d[3]/d[1]+d[3]/d[2]+d[3]/d[0]) | |
83 | + return None if a1+a2+a3+a4 != 1.0 else a1*neighbourCells[0].location + a2*neighbourCells[1].location + a3*neighbourCells[2].location + a4*neighbourCells[3].location | |
80 | 84 | |
81 | 85 | \ No newline at end of file | ... | ... |