diff --git a/.gitignore b/.gitignore index 7a60b85..0014dd6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ __pycache__/ *.pyc +.vscode/ diff --git a/main.py b/main.py index b61bdc0..5c52ddd 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,4 @@ - -from structure import RSSVector, Location, Cell, newCell, KNeighbors, resolve_barycenter +from structure import RSSVector, Location, Cell, newCell, KNeighbors, resolve_barycenter, MarkovModel, MarkovValue import sys Tf = [] #cells table @@ -13,7 +12,7 @@ for i in range (0, 3): for k in range (0,3): Tf[i].append([]) -#known fingerprints +#known fingerprints Tf[0][0] = newCell(-38,-27,-54,-13,2,2) Tf[0][1] = newCell(-74,-62,-48,-33,2,6) Tf[0][2] = newCell(-13,-28,-12,-40,2,10) @@ -39,6 +38,35 @@ def main(args): print(a.toString()) #### Markov #### + MM = MarkovModel(Tf) + """ + for a in range(0,3): + for b in range(0,3): + d = Location(Tf[a][b].location.x, Tf[a][b].location.y) + print(d.toString()) + print(d.getPositionInArray()) + """ + MM.moveToCellID(8) + MM.moveToCellID(7) + MM.moveToCellID(8) + MM.moveToCellID(7) + MM.moveToCellID(8) + MM.moveToCellID(7) + MM.moveToCellID(8) + MM.moveToCellID(5) + MM.moveToCellID(8) + MM.moveToCellID(2) + MM.moveToCellID(9) + MM.moveToCellID(8) + MM.moveToCellID(1) + MM.moveToCell(Tf[2][2]) + MM.moveToCellID(8) + MM.moveToCell(Tf[2][2]) + + MM.printValues() + print("") + MM.printPercentages() - return 0 + print("\r\ncurrent cell is #" + str(MM.previousCell) + " , most likely next cell is #" + str(MM.getMostLikely()) + " which is located at " + str(Location.fromID(MM.getMostLikely()).toString())) + main(sys.argv) \ No newline at end of file diff --git a/structure.py b/structure.py index 3753507..d2acb86 100644 --- a/structure.py +++ b/structure.py @@ -1,4 +1,5 @@ from operator import itemgetter as ig +from math import floor class RSSVector(): distances = [] @@ -9,20 +10,20 @@ class RSSVector(): self.n4 = n4 class Location(): - def __init(self): - self.x = self.y = self.z = 0 - + def __init__(self, x, y, z=0): self.x = x self.y = y self.z = z - + + def __mul__(self, multiplier): returnValue = Location(self.x, self.y, self.z) returnValue.x *= multiplier returnValue.y *= multiplier returnValue.z *= multiplier return returnValue + def __rmul__(self, multiplier): return self * multiplier @@ -33,16 +34,109 @@ class Location(): returnValue.z += added.z return returnValue + def __isub__(self, value): + return self + -1 * value + + def __itruediv__(self, divider): + returnValue = Location(self.x, self.y, self.z) + returnValue.x /= divider + returnValue.y /= divider + returnValue.z /= divider + return returnValue + def toString(self): return "(" + str(self.x) + " ; " + str(self.y) + " ; " + str(self.z) + ")" + def getPositionInArray(self, arraySize=3): + temp = Location(self.x, self.y) + temp /= 2 + temp -= Location(1,1) + return floor((temp.x * arraySize + temp.y)/2) + + @staticmethod + def fromID(id, arraySize=3): + id -= 1 + y=id % 3 + x=floor((id - y) / 3) + returnValue = Location(x, y) + returnValue *= 2 + returnValue += Location(1,1) + returnValue *= 2 + return returnValue class Cell(): def __init__(self, v_, loc): self.v = v_ self.location = loc - self.Likeliness = .0 # Probability of Markov model - self.pastCount = 1 + +class MarkovValue(): + def __init__(self, nb=0, percentage=0): + self.nb = nb + self.percentage = percentage # Probability of Markov model (100% <=> 1.0) + self.attachedCell = None + +class MarkovModel(): + def __init__(self,cells): + self.MarkovValues = [] #table of the coefficients of the Markov Model + self.cells = cells + self.previousCell = 0 + for i in range (0, 11): + self.MarkovValues.append([]) + for k in range (0, 10): + self.MarkovValues[i].append(MarkovValue()) + + + def moveToCellID(self, nextCell): + self.MarkovValues[self.previousCell][nextCell].nb += 1 + self.MarkovValues[10][nextCell].nb += 1 + self.MarkovValues[self.previousCell][nextCell].percentage = self.MarkovValues[self.previousCell][nextCell].nb /self.MarkovValues[10][nextCell].nb + self.previousCell = nextCell + + def moveToCell(self, nextCell): + self.moveToCellID(nextCell.location.getPositionInArray()+1) + + def printValues(self): + print("\t? \t1 \t2 \t3\t4 \t5 \t6 \t7 \t8 \t9") + print("---------------------------------------------------------------------------------", end='') + + for i in range (0, 11): + print("\r\n", end='') + if i == 10 or i == 1: + print("---------------------------------------------------------------------------------\r\n",end='') + + print(i, end='\t') + for k in range (0,10): + print(self.MarkovValues[i][k].nb, end='\t') + print("") + + def printPercentages(self): + print("\t? \t1 \t2 \t3\t4 \t5 \t6 \t7 \t8 \t9") + print("---------------------------------------------------------------------------------", end='') + + for i in range (0, 11): + print("\r\n", end='') + if i == 1: + print("---------------------------------------------------------------------------------\r\n",end='') + + print(i, end='\t') + for k in range (0,10): + print(round(self.MarkovValues[i][k].percentage,2), end='\t') + print("") + + def getMostLikely(self): + return self.getMostLikelyFromCell(self.previousCell) + + def getMostLikelyFromCell(self, currentCell): + max=0 + max_id=0 + for k in range(1,10): + if self.MarkovValues[k][currentCell].nb > max: + max = self.MarkovValues[k][currentCell].nb + max_id = k + return max_id + + def toString(self): + return "" def newCell(n1, n2, n3, n4, l1, l2): return Cell(RSSVector(n1,n2,n3,n4), Location(l1,l2)) @@ -51,11 +145,10 @@ def KNeighbors(fingerprints, sample): ''' Returns the 4 closest cells to the given sample and fills sample distance data :param Cell[3][3] fingerprints: 2D array of all the cells - :param RSSIVector sample: Mobile terminal sample + :param RSSVector sample: Mobile terminal sample :return Cell[4]: the 4 nearest cells ''' - distances = [] - neighbours = [] + distances, neighbours = [], [] for row in fingerprints: for currentItem in row: dist = abs(currentItem.v.n1 - sample.n1) \ @@ -78,7 +171,7 @@ def resolve_barycenter(nC, d): :param distance[4] d: distances of the sample of the mobile terminal :return Location: Estimated location of the mobile terminal (return None if error) ''' - return None if len(nC) != 4 else \ + return None if len(nC) != 4 or len(d) != 4 else \ 1 / (1+d[0]/d[1]+d[0]/d[2]+d[0]/d[3])*nC[0].location \ + 1 / (1+d[1]/d[0]+d[1]/d[2]+d[1]/d[3])*nC[1].location \ + 1 / (1+d[2]/d[1]+d[2]/d[0]+d[2]/d[3])*nC[2].location \ -- libgit2 0.21.4