correction algo et developpement demo2

demo/2
ludovic.castglia 4 months ago
parent a1473cbcd1
commit 4e31bfd5d5

@ -12,11 +12,10 @@ def getRandomParam():
def applyFunction(x,param): def applyFunction(x,param):
return pow(x*param[0],2) + x*param[1] + param[2] return pow(x*param[0],2) + x*param[1] + param[2]
param = getRandomParam() param = getRandomParam()
nbPoints = 100 nbPoints = 100
minx = 0 minx = 0
maxx = 10 maxx = 1000
x = [] x = []
y = [] y = []
@ -26,13 +25,21 @@ for i in [minx + (maxx-minx)*i/nbPoints for i in range(nbPoints)]:
knn.addPoint(np.array([i]),np.array([y[-1]])) knn.addPoint(np.array([i]),np.array([y[-1]]))
nnnar.addPoint(np.array([i]),np.array([y[-1]])) nnnar.addPoint(np.array([i]),np.array([y[-1]]))
error = 0
nbInfer = 20 nbInfer = 20
for i in range(nbInfer): for i in range(nbInfer):
xt = random()*10 xt = random()*maxx
yt = nnnar.getValueOfPoint(np.array([xt]),2) yt = nnnar.getValueOfPoint(np.array([xt]),2)
yr = applyFunction(xt,param)
error += abs(yt[0]-yr)
ytk = knn.getValueOfPoint(np.array([xt]),2)
plt.plot(xt,yt[0],'xr') plt.plot(xt,yt[0],'xr')
plt.plot(xt,ytk[0],'xg')
print("Error: ",(error/nbInfer)/applyFunction(maxx,param))
plt.plot(x,y) plt.plot(x,y)
plt.title("f(x) = "+str(param[0])+"*x^2 + "+str(param[1])+"*x + "+str(param[2]))
plt.xlabel("X")
plt.ylabel("Y")
plt.show() plt.show()

@ -56,7 +56,7 @@ class Nnnar:
i+=1 i+=1
maxDistIdx = foundMaxIndex(dist) maxDistIdx = foundMaxIndex(dist)
maxDist = max(dist) maxDist = max(dist)
for i in range(nbNearest, len(selected)): while i != len(selected):
if (selected[i].getDistFromCoord(coord) < maxDist): if (selected[i].getDistFromCoord(coord) < maxDist):
found.pop(maxDistIdx) found.pop(maxDistIdx)
dist.pop(maxDistIdx) dist.pop(maxDistIdx)
@ -64,6 +64,7 @@ class Nnnar:
dist.append(selected[i].getDistFromCoord(coord)) dist.append(selected[i].getDistFromCoord(coord))
maxDistIdx = foundMaxIndex(dist) maxDistIdx = foundMaxIndex(dist)
maxDist = max(dist) maxDist = max(dist)
i+=1
return found, dist return found, dist
def getSpaceIdxFromCoord(self,coord): def getSpaceIdxFromCoord(self,coord):

@ -1,5 +1,6 @@
import numpy as np import numpy as np
from sympy.utilities.iterables import multiset_permutations from sympy.utilities.iterables import multiset_permutations
import copy
def findCoordAround(center, nbAround): def findCoordAround(center, nbAround):
nbDimention = len(center) nbDimention = len(center)
@ -69,14 +70,15 @@ def getPermutationOfCoord(center, find, coord):
def applySapceAroundToCase(center,spaceAround,space): def applySapceAroundToCase(center,spaceAround,space):
reelSpaceAround = [] reelSpaceAround = []
spaceAroundCopy = copy.deepcopy(spaceAround)
for y in range(len(spaceAround)): for y in range(len(spaceAround)):
estBon = True estBon = True
for i in range(len(spaceAround[y])): for i in range(len(spaceAroundCopy[y])):
if not estBon: if not estBon:
continue continue
else : else :
spaceAround[y][i] += center[i] spaceAroundCopy[y][i] += center[i]
estBon = estBon and 0 <= spaceAround[y][i] <= space.nbSubdivisions-1 estBon = estBon and 0 <= spaceAroundCopy[y][i] <= space.nbSubdivisions-1
if estBon: if estBon:
reelSpaceAround.append(spaceAround[y]) reelSpaceAround.append(spaceAroundCopy[y])
return reelSpaceAround return reelSpaceAround
Loading…
Cancel
Save