parent
f35c2146eb
commit
1416213690
@ -0,0 +1,78 @@
|
|||||||
|
import matplotlib.pyplot as plt
|
||||||
|
import numpy as np
|
||||||
|
from function import *
|
||||||
|
|
||||||
|
class Nnnar:
|
||||||
|
def __init__(self, nbDimensions, minCoord, maxCoord, nbSubdivisions):
|
||||||
|
self.nbDimensions = nbDimensions
|
||||||
|
self.minCoord = minCoord
|
||||||
|
self.maxCoord = maxCoord
|
||||||
|
self.nbSubdivisions = nbSubdivisions
|
||||||
|
self.delta = maxCoord - minCoord
|
||||||
|
self.unit = self.delta / nbSubdivisions
|
||||||
|
coord = np.zeros(nbDimensions,dtype=int) + self.nbSubdivisions
|
||||||
|
self.space = np.zeros(coord)
|
||||||
|
fillSpace = np.vectorize(lambda x: [], otypes=[object])
|
||||||
|
self.space = fillSpace(self.space)
|
||||||
|
self.calculatedSpaceAroundIdx = []
|
||||||
|
|
||||||
|
def addPoint(self, coord, value):
|
||||||
|
if len(coord) != self.nbDimensions:
|
||||||
|
raise AttributeError("Error: wrong number of dimensions")
|
||||||
|
self.space[*self.getSpaceIdxFromCoord(coord)].append(Point(coord, value))
|
||||||
|
|
||||||
|
def getNNearest(self, coord, nbNearest):
|
||||||
|
idx = self.getSpaceIdxFromCoord(coord)
|
||||||
|
nbAround = 0
|
||||||
|
selected = []
|
||||||
|
while len(selected) < nbNearest:
|
||||||
|
selected = []
|
||||||
|
subSpaceIdxList = getSpaceIdxAround(idx, nbAround, self)
|
||||||
|
for subSpaceIdx in subSpaceIdxList:
|
||||||
|
selected += selectPointsInRange(self.space[subSpaceIdx],coord,nbAround*self.unit)
|
||||||
|
nbAround += 1
|
||||||
|
found = []
|
||||||
|
dist = []
|
||||||
|
i=0
|
||||||
|
while len(found) < nbNearest:
|
||||||
|
found += selected[i]
|
||||||
|
i+=1
|
||||||
|
maxDistIdx = foundMaxIndex(dist)
|
||||||
|
maxDist = max(dist)
|
||||||
|
for i in range(nbNearest, len(selected)):
|
||||||
|
if (selected[i].getDistFromCoord(coord) < maxDist):
|
||||||
|
found.pop(maxDistIdx)
|
||||||
|
dist.pop(maxDistIdx)
|
||||||
|
found.append(selected[i])
|
||||||
|
dist.append(selected[i].getDistFromCoord(coord))
|
||||||
|
maxDistIdx = foundMaxIndex(dist)
|
||||||
|
maxDist = max(dist)
|
||||||
|
return found
|
||||||
|
|
||||||
|
def getSpaceIdxFromCoord(self,coord):
|
||||||
|
coord = coord - self.minCoord
|
||||||
|
coord = coord / self.unit
|
||||||
|
coord = np.floor(coord).astype(int)
|
||||||
|
return coord
|
||||||
|
|
||||||
|
class Point:
|
||||||
|
def __init__(self, coord, value):
|
||||||
|
self.coord = coord
|
||||||
|
self.value = value
|
||||||
|
|
||||||
|
def setvalue(self, value):
|
||||||
|
self.value = value
|
||||||
|
|
||||||
|
def getDist(self, point):
|
||||||
|
return np.linalg.norm(self.coord - point.coord)
|
||||||
|
|
||||||
|
def getDistFromCoord(self, coord):
|
||||||
|
return np.linalg.norm(self.coord - coord)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
n = Nnnar(2, 0, 3, 2)
|
||||||
|
|
||||||
|
n.addPoint(np.array([1, 0.8]), 1)
|
||||||
|
print(n.space[0][0][0].coord)
|
@ -0,0 +1,12 @@
|
|||||||
|
|
||||||
|
def getSpaceIdxAround(center, nbAround, space):
|
||||||
|
if (nbAround < len(space.calculatedSpaceAroundIdx)):
|
||||||
|
return space.calculatedSpaceAroundIdx[nbAround]
|
||||||
|
# TODO: implement l'algo pour trouver les indices subSapce à nbAround autour du centre
|
||||||
|
|
||||||
|
def selectPointsInRange(points, coord, distance):
|
||||||
|
found = []
|
||||||
|
return found
|
||||||
|
|
||||||
|
def foundMaxIndex(dist):
|
||||||
|
return dist.index(max(dist))
|
Loading…
Reference in new issue