/** * returns the first element and removes it from the heap. * * @return the first element * @throws Exception if no elements in heap */ public MyHeapElement get() throws Exception { if (m_heap[0].index == 0) { throw new Exception("No elements present in the heap"); } MyHeapElement r = m_heap[1]; m_heap[1] = m_heap[m_heap[0].index]; m_heap[0].index--; downheap(); return r; }
/** * Puts an element by substituting it in place of the top most element. * * @param i the index * @param d the distance * @throws Exception if distance is smaller than that of the head element */ public void putBySubstitute(int i, double d) throws Exception { MyHeapElement head = get(); put(i, d); // System.out.println("previous: "+head.distance+" current: "+m_heap[1].distance); if (head.distance == m_heap[1].distance) { // Utils.eq(head.distance, // m_heap[1].distance)) { putKthNearest(head.index, head.distance); } else if (head.distance > m_heap[1].distance) { // Utils.gr(head.distance, // m_heap[1].distance)) { m_KthNearest = null; m_KthNearestSize = 0; initSize = 10; } else if (head.distance < m_heap[1].distance) { throw new Exception("The substituted element is smaller than the " + "head element. put() should have been called " + "in place of putBySubstitute()"); } }
if (heap.size() < k) { distance = m_EuclideanDistance.distance(target, m_Instances .instance(m_InstList[idx]), Double.POSITIVE_INFINITY, m_Stats); heap.put(m_InstList[idx], distance); } else { MyHeapElement temp = heap.peek(); distance = m_EuclideanDistance.distance(target, m_Instances .instance(m_InstList[idx]), temp.distance, m_Stats); if (distance < temp.distance) { heap.putBySubstitute(m_InstList[idx], distance); } else if (distance == temp.distance) { heap.putKthNearest(m_InstList[idx], distance); if (heap.size() < k) { // if haven't found the first k double distanceToSplitPlane = distanceToParents + m_EuclideanDistance.sqDifference(node.m_SplitDim, target + m_EuclideanDistance.sqDifference(node.m_SplitDim, target .value(node.m_SplitDim), node.m_SplitValue); if (heap.peek().distance >= distanceToSplitPlane) { findNearestNeighbours(target, further, k, heap, distanceToSplitPlane);
double distance = Double.NEGATIVE_INFINITY; if (heap.totalSize() >= k) distance = m_DistanceFunction.distance(target, node.getPivot()); && Math.sqrt(heap.peek().distance) < distance - node.getRadius()) { return; } else if (node.m_Left != null && node.m_Right != null) { // if node is not if (target == m_Instances.instance(m_InstList[i])) //for hold-one-out cross-validation continue; if (heap.totalSize() < k) { distance = m_DistanceFunction.distance(target, m_Instances .instance(m_InstList[i]), Double.POSITIVE_INFINITY, m_Stats); heap.put(m_InstList[i], distance); } else { MyHeapElement head = heap.peek(); distance = m_DistanceFunction.distance(target, m_Instances.instance(m_InstList[i]), head.distance, m_Stats); if (distance < head.distance) { heap.putBySubstitute(m_InstList[i], distance); } else if (distance == head.distance) { heap.putKthNearest(m_InstList[i], distance);
if (heap.size() < k) { distance = m_EuclideanDistance.distance(target, m_Instances .instance(m_InstList[idx]), Double.POSITIVE_INFINITY, m_Stats); heap.put(m_InstList[idx], distance); } else { MyHeapElement temp = heap.peek(); distance = m_EuclideanDistance.distance(target, m_Instances .instance(m_InstList[idx]), temp.distance, m_Stats); if (distance < temp.distance) { heap.putBySubstitute(m_InstList[idx], distance); } else if (distance == temp.distance) { heap.putKthNearest(m_InstList[idx], distance); if (heap.size() < k) { // if haven't found the first k double distanceToSplitPlane = distanceToParents + m_EuclideanDistance.sqDifference(node.m_SplitDim, target + m_EuclideanDistance.sqDifference(node.m_SplitDim, target .value(node.m_SplitDim), node.m_SplitValue); if (heap.peek().distance >= distanceToSplitPlane) { findNearestNeighbours(target, further, k, heap, distanceToSplitPlane);
MyHeap heap = new MyHeap(kNN); double distance; int firstkNN=0; for(int i=0; i<m_Instances.numInstances(); i++) { if(firstkNN<kNN) { if(print) System.out.println("K(a): "+(heap.size()+heap.noOfKthNearest())); distance = m_DistanceFunction.distance(target, m_Instances.instance(i), Double.POSITIVE_INFINITY, m_Stats); if(distance == 0.0 && m_SkipIdentical) continue; else heap.put(i, distance); heap.put(i, distance); firstkNN++; MyHeapElement temp = heap.peek(); if(print) System.out.println("K(b): "+(heap.size()+heap.noOfKthNearest())); distance = m_DistanceFunction.distance(target, m_Instances.instance(i), temp.distance, m_Stats); if(distance == 0.0 && m_SkipIdentical) continue; if(distance < temp.distance) { heap.putBySubstitute(i, distance); heap.putKthNearest(i, distance); Instances neighbours = new Instances(m_Instances, (heap.size()+heap.noOfKthNearest())); m_Distances = new double[heap.size()+heap.noOfKthNearest()];
double distance = Double.NEGATIVE_INFINITY; if (heap.totalSize() >= k) distance = m_DistanceFunction.distance(target, node.getPivot()); && Math.sqrt(heap.peek().distance) < distance - node.getRadius()) { return; } else if (node.m_Left != null && node.m_Right != null) { // if node is not if (target == m_Instances.instance(m_InstList[i])) //for hold-one-out cross-validation continue; if (heap.totalSize() < k) { distance = m_DistanceFunction.distance(target, m_Instances .instance(m_InstList[i]), Double.POSITIVE_INFINITY, m_Stats); heap.put(m_InstList[i], distance); } else { MyHeapElement head = heap.peek(); distance = m_DistanceFunction.distance(target, m_Instances.instance(m_InstList[i]), head.distance, m_Stats); if (distance < head.distance) { heap.putBySubstitute(m_InstList[i], distance); } else if (distance == head.distance) { heap.putKthNearest(m_InstList[i], distance);
MyHeap heap = new MyHeap(kNN); double distance; int firstkNN=0; for(int i=0; i<m_Instances.numInstances(); i++) { if(firstkNN<kNN) { if(print) System.out.println("K(a): "+(heap.size()+heap.noOfKthNearest())); distance = m_DistanceFunction.distance(target, m_Instances.instance(i), Double.POSITIVE_INFINITY, m_Stats); heap.put(i, distance); firstkNN++; MyHeapElement temp = heap.peek(); if(print) System.out.println("K(b): "+(heap.size()+heap.noOfKthNearest())); distance = m_DistanceFunction.distance(target, m_Instances.instance(i), temp.distance, m_Stats); if(distance == 0.0 && m_SkipIdentical) continue; if(distance < temp.distance) { heap.putBySubstitute(i, distance); heap.putKthNearest(i, distance); Instances neighbours = new Instances(m_Instances, (heap.size()+heap.noOfKthNearest())); m_Distances = new double[heap.size()+heap.noOfKthNearest()]; int [] indices = new int[heap.size()+heap.noOfKthNearest()]; int i=1; MyHeapElement h; while(heap.noOfKthNearest()>0) { h = heap.getKthNearest();
m_Stats.searchStart(); MyHeap heap = new MyHeap(k); findNearestNeighbours(target, m_Root, k, heap, 0.0); m_Stats.searchFinish(); Instances neighbours = new Instances(m_Instances, (heap.size() + heap .noOfKthNearest())); m_DistanceList = new double[heap.size() + heap.noOfKthNearest()]; int[] indices = new int[heap.size() + heap.noOfKthNearest()]; int i = indices.length - 1; MyHeapElement h; while (heap.noOfKthNearest() > 0) { h = heap.getKthNearest(); indices[i] = h.index; m_DistanceList[i] = h.distance; i--; while (heap.size() > 0) { h = heap.get(); indices[i] = h.index; m_DistanceList[i] = h.distance;
m_Stats.searchStart(); MyHeap heap = new MyHeap(k); findNearestNeighbours(target, m_Root, k, heap, 0.0); m_Stats.searchFinish(); Instances neighbours = new Instances(m_Instances, (heap.size() + heap .noOfKthNearest())); m_DistanceList = new double[heap.size() + heap.noOfKthNearest()]; int[] indices = new int[heap.size() + heap.noOfKthNearest()]; int i = indices.length - 1; MyHeapElement h; while (heap.noOfKthNearest() > 0) { h = heap.getKthNearest(); indices[i] = h.index; m_DistanceList[i] = h.distance; i--; while (heap.size() > 0) { h = heap.get(); indices[i] = h.index; m_DistanceList[i] = h.distance;
MyHeap heap = new MyHeap(k); m_Stats.searchFinish(); Instances neighbours = new Instances(m_Instances, heap.totalSize()); m_Distances = new double[heap.totalSize()]; int [] indices = new int[heap.totalSize()]; int i=1; MyHeapElement h; while(heap.noOfKthNearest()>0) { h = heap.getKthNearest(); indices[indices.length-i] = h.index; m_Distances[indices.length-i] = h.distance; i++; while(heap.size()>0) { h = heap.get(); indices[indices.length-i] = h.index; m_Distances[indices.length-i] = h.distance;
MyHeap heap = new MyHeap(k); m_Stats.searchFinish(); Instances neighbours = new Instances(m_Instances, heap.totalSize()); m_Distances = new double[heap.totalSize()]; int [] indices = new int[heap.totalSize()]; int i=1; MyHeapElement h; while(heap.noOfKthNearest()>0) { h = heap.getKthNearest(); indices[indices.length-i] = h.index; m_Distances[indices.length-i] = h.distance; i++; while(heap.size()>0) { h = heap.get(); indices[indices.length-i] = h.index; m_Distances[indices.length-i] = h.distance;
/** * Puts an element by substituting it in place of the top most element. * * @param i the index * @param d the distance * @throws Exception if distance is smaller than that of the head element */ public void putBySubstitute(int i, double d) throws Exception { MyHeapElement head = get(); put(i, d); // System.out.println("previous: "+head.distance+" current: "+m_heap[1].distance); if (head.distance == m_heap[1].distance) { // Utils.eq(head.distance, // m_heap[1].distance)) { putKthNearest(head.index, head.distance); } else if (head.distance > m_heap[1].distance) { // Utils.gr(head.distance, // m_heap[1].distance)) { m_KthNearest = null; m_KthNearestSize = 0; initSize = 10; } else if (head.distance < m_heap[1].distance) { throw new Exception("The substituted element is smaller than the " + "head element. put() should have been called " + "in place of putBySubstitute()"); } }
/** * returns the first element and removes it from the heap. * * @return the first element * @throws Exception if no elements in heap */ public MyHeapElement get() throws Exception { if (m_heap[0].index == 0) { throw new Exception("No elements present in the heap"); } MyHeapElement r = m_heap[1]; m_heap[1] = m_heap[m_heap[0].index]; m_heap[0].index--; downheap(); return r; }
/** * returns the total size. * * @return the total size */ public int totalSize() { return size() + noOfKthNearest(); }
/** * returns the total size. * * @return the total size */ public int totalSize() { return size() + noOfKthNearest(); }