From 4e31bfd5d5849f320e4a3906a0706787e65353d8 Mon Sep 17 00:00:00 2001 From: "ludovic.castglia" Date: Wed, 29 Jan 2025 08:54:54 +0100 Subject: [PATCH] correction algo et developpement demo2 --- demo2.py | 15 +++++++++++---- nnnar.py | 3 ++- spaceAround.py | 10 ++++++---- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/demo2.py b/demo2.py index bf0d05e..cdc5651 100644 --- a/demo2.py +++ b/demo2.py @@ -12,11 +12,10 @@ def getRandomParam(): def applyFunction(x,param): return pow(x*param[0],2) + x*param[1] + param[2] - param = getRandomParam() nbPoints = 100 minx = 0 -maxx = 10 +maxx = 1000 x = [] 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]])) nnnar.addPoint(np.array([i]),np.array([y[-1]])) - +error = 0 nbInfer = 20 for i in range(nbInfer): - xt = random()*10 + xt = random()*maxx 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,ytk[0],'xg') +print("Error: ",(error/nbInfer)/applyFunction(maxx,param)) 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() \ No newline at end of file diff --git a/nnnar.py b/nnnar.py index b279f6f..e6200f5 100644 --- a/nnnar.py +++ b/nnnar.py @@ -56,7 +56,7 @@ class Nnnar: i+=1 maxDistIdx = foundMaxIndex(dist) maxDist = max(dist) - for i in range(nbNearest, len(selected)): + while i != len(selected): if (selected[i].getDistFromCoord(coord) < maxDist): found.pop(maxDistIdx) dist.pop(maxDistIdx) @@ -64,6 +64,7 @@ class Nnnar: dist.append(selected[i].getDistFromCoord(coord)) maxDistIdx = foundMaxIndex(dist) maxDist = max(dist) + i+=1 return found, dist def getSpaceIdxFromCoord(self,coord): diff --git a/spaceAround.py b/spaceAround.py index 09a6110..05bc2b6 100644 --- a/spaceAround.py +++ b/spaceAround.py @@ -1,5 +1,6 @@ import numpy as np from sympy.utilities.iterables import multiset_permutations +import copy def findCoordAround(center, nbAround): nbDimention = len(center) @@ -69,14 +70,15 @@ def getPermutationOfCoord(center, find, coord): def applySapceAroundToCase(center,spaceAround,space): reelSpaceAround = [] + spaceAroundCopy = copy.deepcopy(spaceAround) for y in range(len(spaceAround)): estBon = True - for i in range(len(spaceAround[y])): + for i in range(len(spaceAroundCopy[y])): if not estBon: continue else : - spaceAround[y][i] += center[i] - estBon = estBon and 0 <= spaceAround[y][i] <= space.nbSubdivisions-1 + spaceAroundCopy[y][i] += center[i] + estBon = estBon and 0 <= spaceAroundCopy[y][i] <= space.nbSubdivisions-1 if estBon: - reelSpaceAround.append(spaceAround[y]) + reelSpaceAround.append(spaceAroundCopy[y]) return reelSpaceAround \ No newline at end of file