/// <summary> /// Construct a new nearest neighbour iterator. /// </summary> /// <param name="pRoot">The root of the tree to begin searching from.</param> /// <param name="tSearchPoint">The point in n-dimensional space to search.</param> /// <param name="kDistance">The distance function used to evaluate the points.</param> /// <param name="iMaxPoints">The max number of points which can be returned by this iterator. Capped to max in tree.</param> /// <param name="fThreshold">Threshold to apply to the search space. Negative numbers indicate that no threshold is applied.</param> public NearestNeighbour(KDNode_Rednaxela <T> pRoot, double[] tSearchPoint, IDistanceFunction kDistance, int iMaxPoints, double fThreshold) { // Check the dimensionality of the search point. if (tSearchPoint.Length != pRoot.dimensions) { throw new Exception("Dimensionality of search point and kd-tree are not the same."); } // Store the search point. this.tSearchPoint = new double[tSearchPoint.Length]; Array.Copy(tSearchPoint, this.tSearchPoint, tSearchPoint.Length); // Store the point count, distance function and tree root. this.iPointsRemaining = Math.Min(iMaxPoints, pRoot.Size); this.fThreshold = fThreshold; this.kDistanceFunction = kDistance; this.pRoot = pRoot; this.iMaxPointsReturned = iMaxPoints; _CurrentDistance = -1; // Create an interval heap for the points we check. this.pEvaluated = new IntervalHeap <T>(); // Create a min heap for the things we need to check. this.pPending = new MinHeap <KDNode_Rednaxela <T> >(); this.pPending.Insert(0, pRoot); }
protected NearestNeighborIterator(KdNode <T> treeRoot, double[] searchPoint, int maxPointsReturned, IDistanceFunction distanceFunction) { this.searchPoint = Arrays.copyOf(searchPoint, searchPoint.Length); this.pointsRemaining = Math.Min(maxPointsReturned, treeRoot.size()); this.distanceFunction = distanceFunction; this.pendingPaths = new BinaryHeap.Min <KdNode <T> >(); this.pendingPaths.offer(0, treeRoot); this.evaluatedPoints = new KDTreeRednaxela.IntervalHeap <T>(); }
/// <summary> /// Creates new instance of a function based weight provider /// </summary> /// <param name="distanceFunction">distance function to be used</param> public FunctionBasedWeightProvider(IDistanceFunction distanceFunction) { if (distanceFunction == null) { throw new ArgumentNullException("distanceFunction"); } this.DistanceFunction = distanceFunction; }
/// <summary> /// Creates new instance of a function based weight provider /// </summary> /// <param name="distanceFunction">distance function to be used</param> public FunctionBasedWeightProviderWithCaching(IDistanceFunction distanceFunction) { if (distanceFunction == null) { throw new ArgumentNullException("distanceFunction"); } this.DistanceFunction = distanceFunction; this.DistancesCache = new Dictionary<string, double>(); }
/// <summary> /// Creates new instance of a function based weight provider /// </summary> /// <param name="distanceFunction">distance function to be used</param> public FunctionBasedWeightProviderWithCaching(IDistanceFunction distanceFunction) { if (distanceFunction == null) { throw new ArgumentNullException("distanceFunction"); } this.DistanceFunction = distanceFunction; this.DistancesCache = new Dictionary <string, double>(); }
public PopulationNovelty(int size, NoveltyChromosome ancestor, IFitnessFunction fitnessFunction, IDistanceFunction distanceFunction, ISelectionMethod selectionMethod, int kNearestNeighbours) : base(size, ancestor, fitnessFunction, selectionMethod) { if (kNearestNeighbours < 2) { throw new ArgumentException("Too small nearest neighbours was specified."); } _distanceFunction = distanceFunction; _kNearestNeighbours = kNearestNeighbours; }
/// <summary> /// Construct a new nearest neighbour iterator. /// </summary> /// <param name="pRoot">The root of the tree to begin searching from.</param> /// <param name="tSearchPoint">The point in n-dimensional space to search.</param> /// <param name="kDistance">The distance function used to evaluate the points.</param> /// <param name="iMaxPoints">The max number of points which can be returned by this iterator. Capped to max in tree.</param> /// <param name="fThreshold">Threshold to apply to the search space. Negative numbers indicate that no threshold is applied.</param> public NearestNeighbour(KDNode <T> pRoot, Vector2 tSearchPoint, IDistanceFunction kDistance, int iMaxPoints, float fThreshold) { // Store the search point. this.tSearchPoint = tSearchPoint; // Store the point count, distance function and tree root. iPointsRemaining = Math.Min(iMaxPoints, pRoot.Size); this.fThreshold = fThreshold; kDistanceFunction = kDistance; this.pRoot = pRoot; iMaxPointsReturned = iMaxPoints; _CurrentDistance = -1; // Create an interval heap for the points we check. pEvaluated = new IntervalHeap <T>(); // Create a min heap for the things we need to check. pPending = new MinHeap <KDNode <T> >(); pPending.Insert(0, pRoot); }
/// <summary> /// Get the nearest neighbours to a point in the kd tree using a user defined distance function. /// </summary> /// <param Name="tSearchPoint">The point of interest.</param> /// <param Name="iMaxReturned">The maximum number of points which can be returned by the iterator.</param> /// <param Name="kDistanceFunction">The distance function to use.</param> /// <param Name="fDistance">A threshold distance to apply. Optional. Negative values mean that it is not applied.</param> /// <returns>A new nearest neighbour iterator with the given parameters.</returns> public NearestNeighbour <T> NearestNeighbors(Vector2 tSearchPoint, IDistanceFunction kDistanceFunction, int iMaxReturned, float fDistance) { return(new NearestNeighbour <T>(this, tSearchPoint, kDistanceFunction, iMaxReturned, fDistance)); }
protected static <T> nearestNeighborSearchStep( KDTreeRednaxela.MinHeap <KdNode <T> > pendingPaths, MaxHeap <T> evaluatedPoints, int desiredPoints, IDistanceFunction distanceFunction, double[] searchPoint) { // If there are pending paths possibly closer than the nearest evaluated point, check it out KdNode <T> cursor = pendingPaths.getMin(); pendingPaths.removeMin(); // Descend the tree, recording paths not taken while (!cursor.isLeaf()) { KdNode <T> pathNotTaken; if (searchPoint[cursor.splitDimension] > cursor.splitValue) { pathNotTaken = cursor.left; cursor = cursor.right; } else { pathNotTaken = cursor.right; cursor = cursor.left; } double otherDistance = distanceFunction.distanceToRect(searchPoint, pathNotTaken.minBound, pathNotTaken.maxBound); // Only add a path if we either need more points or it's closer than furthest point on list so far if (evaluatedPoints.size() < desiredPoints || otherDistance <= evaluatedPoints.getMaxKey()) { pendingPaths.offer(otherDistance, pathNotTaken); } } if (cursor.singlePoint) { double nodeDistance = distanceFunction.distance(cursor.points[0], searchPoint); // Only add a point if either need more points or it's closer than furthest on list so far if (evaluatedPoints.size() < desiredPoints || nodeDistance <= evaluatedPoints.getMaxKey()) { for (int i = 0; i < cursor.size(); i++) { T value = (T)cursor.data[i]; // If we don't need any more, replace max if (evaluatedPoints.size() == desiredPoints) { evaluatedPoints.replaceMax(nodeDistance, value); } else { evaluatedPoints.offer(nodeDistance, value); } } } } else { // Add the points at the cursor for (int i = 0; i < cursor.size(); i++) { double[] point = cursor.points[i]; T value = (T)cursor.data[i]; double distance = distanceFunction.distance(point, searchPoint); // Only add a point if either need more points or it's closer than furthest on list so far if (evaluatedPoints.size() < desiredPoints) { evaluatedPoints.offer(distance, value); } else if (distance < evaluatedPoints.getMaxKey()) { evaluatedPoints.replaceMax(distance, value); } } } }
public MaxHeap <T> findNearestNeighbors(double[] searchPoint, int maxPointsReturned, IDistanceFunction distanceFunction) { BinaryHeap.Min <KdNode <T> > pendingPaths = new BinaryHeap.Min <KdNode <T> >(); BinaryHeap.Max <T> evaluatedPoints = new BinaryHeap.Max <T>(); int pointsRemaining = Math.Min(maxPointsReturned, size()); pendingPaths.offer(0, this); while (pendingPaths.size() > 0 && (evaluatedPoints.size() < pointsRemaining || (pendingPaths.getMinKey() < evaluatedPoints.getMaxKey()))) { nearestNeighborSearchStep(pendingPaths, evaluatedPoints, pointsRemaining, distanceFunction, searchPoint); } return(evaluatedPoints); }
public NearestNeighborIterator <T> getNearestNeighborIterator(double[] searchPoint, int maxPointsReturned, IDistanceFunction distanceFunction) { return(new NearestNeighborIterator <T>(this, searchPoint, maxPointsReturned, distanceFunction)); }
/// <summary> /// Get the nearest neighbours to a point in the kd tree using a user defined distance function. /// </summary> /// <param name="tSearchPoint">The point of interest.</param> /// <param name="iMaxReturned">The maximum number of points which can be returned by the iterator.</param> /// <param name="kDistanceFunction">The distance function to use.</param> /// <param name="fDistance">A threshold distance to apply. Optional. Negative values mean that it is not applied.</param> /// <returns>A new nearest neighbour iterator with the given parameters.</returns> public NearestNeighbour <T> FindNearest(double[] tSearchPoint, IDistanceFunction kDistanceFunction, int iMaxReturned, double fDistance) { return(new NearestNeighbour <T>(this, tSearchPoint, kDistanceFunction, iMaxReturned, fDistance)); }