public KdTreeNearestNeighbor( KdTreeDistance<P> distance ) { this( new KdTreeSearch1Standard<>(distance), new KdTreeSearchNStandard<>(distance), new AxisSplitterMedian<>(distance)); }
public KdTreeNearestNeighbor( KdTreeDistance<P> distance ) { this( new KdTreeSearch1Standard<>(distance), new KdTreeSearchNStandard<>(distance), new AxisSplitterMedian<>(distance)); }
/** * Approximate {@link NearestNeighbor} search which uses a set of randomly generated K-D trees and a Best-Bin-First * search. Designed to work in high dimensional space. Distance measure is Euclidean squared. * * @see KdForestBbfSearch * @see AxisSplitterMedian * * @param distance Specifies how distance is computed between two points. * @param maxNodesSearched Maximum number of nodes it will search. Controls speed and accuracy. * @param numTrees Number of trees that are considered. Try 10 and tune. * @param numConsiderSplit Number of nodes that are considered when generating a tree. Must be less than the * point's dimension. Try 5 * @param randomSeed Seed used by random number generator * @param <P> Point type. * @return {@link NearestNeighbor} implementation */ public static <P> NearestNeighbor<P> kdRandomForest( KdTreeDistance<P> distance , int maxNodesSearched , int numTrees , int numConsiderSplit , long randomSeed ) { Random rand = new Random(randomSeed); return new KdForestBbfSearch<>(numTrees,maxNodesSearched,distance, new AxisSplitterMedian<>(distance,new AxisSplitRuleRandomK(rand,numConsiderSplit))); }
/** * Approximate {@link NearestNeighbor} search which uses a set of randomly generated K-D trees and a Best-Bin-First * search. Designed to work in high dimensional space. Distance measure is Euclidean squared. * * @see KdForestBbfSearch * @see AxisSplitterMedian * * @param distance Specifies how distance is computed between two points. * @param maxNodesSearched Maximum number of nodes it will search. Controls speed and accuracy. * @param numTrees Number of trees that are considered. Try 10 and tune. * @param numConsiderSplit Number of nodes that are considered when generating a tree. Must be less than the * point's dimension. Try 5 * @param randomSeed Seed used by random number generator * @param <P> Point type. * @return {@link NearestNeighbor} implementation */ public static <P> NearestNeighbor<P> kdRandomForest( KdTreeDistance<P> distance , int maxNodesSearched , int numTrees , int numConsiderSplit , long randomSeed ) { Random rand = new Random(randomSeed); return new KdForestBbfSearch<>(numTrees,maxNodesSearched,distance, new AxisSplitterMedian<>(distance,new AxisSplitRuleRandomK(rand,numConsiderSplit))); }
/** * Creates canonical K-D Tree by selecting the maximum variance axis and splitting the points at the median. * */ public KdTreeConstructor( KdTreeDistance<P> distance ) { this(new KdTreeMemory<>(), new AxisSplitterMedian<>(distance, new AxisSplitRuleMax())); }
/** * Performs an approximate {@link NearestNeighbor} search using K-D tree. Node are searched in Best-Bin-First * order. Distance measure is Euclidean squared. * * @see KdTreeNearestNeighbor * @see KdTreeSearch1Bbf * @see AxisSplitterMedian * * @param maxNodesSearched Maximum number of nodes it will search. Controls speed and accuracy. * @param <P> Point type. * @param distance Specifies how distance is computed between two points. * @return {@link NearestNeighbor} implementation */ public static <P> NearestNeighbor<P> kdtree( KdTreeDistance<P> distance , int maxNodesSearched ) { return new KdTreeNearestNeighbor<P>(new KdTreeSearch1Bbf<>(distance,maxNodesSearched), new KdTreeSearchNBbf<>(distance,maxNodesSearched),new AxisSplitterMedian<>(distance)); }
/** * Creates canonical K-D Tree by selecting the maximum variance axis and splitting the points at the median. * */ public KdTreeConstructor( KdTreeDistance<P> distance ) { this(new KdTreeMemory<>(), new AxisSplitterMedian<>(distance, new AxisSplitRuleMax())); }
/** * Performs an approximate {@link NearestNeighbor} search using K-D tree. Node are searched in Best-Bin-First * order. Distance measure is Euclidean squared. * * @see KdTreeNearestNeighbor * @see KdTreeSearch1Bbf * @see AxisSplitterMedian * * @param maxNodesSearched Maximum number of nodes it will search. Controls speed and accuracy. * @param <P> Point type. * @param distance Specifies how distance is computed between two points. * @return {@link NearestNeighbor} implementation */ public static <P> NearestNeighbor<P> kdtree( KdTreeDistance<P> distance , int maxNodesSearched ) { return new KdTreeNearestNeighbor<P>(new KdTreeSearch1Bbf<>(distance,maxNodesSearched), new KdTreeSearchNBbf<>(distance,maxNodesSearched),new AxisSplitterMedian<>(distance)); }
public TestKdForestBbfSearch() { // set the max nodes so it that it will produce perfect results AxisSplitRule rule = new AxisSplitRuleRandomK(new Random(234),1); KdTreeEuclideanSq_F64 distance = new KdTreeEuclideanSq_F64(N); setAlg(new KdForestBbfSearch<>(5,10000,distance, new AxisSplitterMedian<>(distance,rule))); } }
@Test public void checkRuleSetCalled() { DummyRule rule = new DummyRule(2); AxisSplitterMedian<double[]> alg = new AxisSplitterMedian<>(distance,rule); assertTrue(rule.calledSetDimension); }
@Test public void splitData_three() { List<double[]> points = createPoints(2, 1,2 , 3,5 , -3,4); AxisSplitterMedian<double[]> alg = new AxisSplitterMedian<>(distance,new DummyRule(0)); alg.splitData(points,null,left,null,right,null); // The first point is selected to be the median assertEquals(1,left.size()); assertEquals(1,right.size()); assertEquals(-3,left.get(0)[0],1e-8); assertEquals(3,right.get(0)[0],1e-8); }
@Test public void splitData_two() { List<double[]> points = createPoints(2, 1,2 , 3,5); AxisSplitterMedian<double[]> alg = new AxisSplitterMedian<>(distance,new DummyRule(0)); alg.splitData(points,null,left,null,right,null); // The second point is selected to be the median assertEquals(1,left.size()); assertEquals(0, right.size()); assertEquals(1,left.get(0)[0],1e-8); }
@Test public void splitData_one() { List<double[]> points = createPoints(2, 1,2); AxisSplitterMedian<double[]> alg = new AxisSplitterMedian<>(distance,new DummyRule(0)); alg.splitData(points,null,left,null,right,null); // the median point is not included and will become the node's point assertEquals(0,left.size()); assertEquals(0,right.size()); }
/** * Make two of the points identical and see if things blow up */ @Test public void identical_points() { List<double[]> points = createPoints(2, 1,2, 1.1,4 , 1,2); AxisSplitterMedian<double[]> alg = new AxisSplitterMedian<>(distance,new DummyRule(0)); alg.splitData(points,null,left,null,right,null); // sorted order should be (1,2) (1,2) (1.1,4) assertEquals(1, left.size()); assertEquals(1, right.size()); assertEquals(1,alg.getSplitPoint()[0],1e-8); assertEquals(1,left.get(0)[0],1e-8); assertEquals(1.1,right.get(0)[0],1e-8); }
/** * Make sure the split point is returned */ @Test public void splitData_split_point() { List<double[]> points = createPoints(2, 1,2 , 3,5 , -3,4); AxisSplitterMedian<double[]> alg = new AxisSplitterMedian<>(distance,new DummyRule(1)); alg.splitData(points,null,left,null,right,null); assertEquals(1,alg.getSplitAxis()); assertEquals(-3,alg.getSplitPoint()[0],1e-8); // assertEquals(2, alg.getSplitIndex()); }
@Test public void splitData_withData() { List<double[]> points = createPoints(2, 1,2 , 3,5 , -3,4); GrowQueue_I32 data = new GrowQueue_I32(); for( int i = 0; i < points.size(); i++ ) data.add(i); AxisSplitterMedian<double[]> alg = new AxisSplitterMedian<>(distance,new DummyRule(1)); alg.splitData(points,data,left,leftData,right,rightData); assertEquals(1,left.size()); assertEquals(1,right.size()); assertEquals(1,leftData.size()); assertEquals(1,rightData.size()); assertEquals(1,alg.getSplitAxis(),1e-8); assertEquals(-3,alg.getSplitPoint()[0],1e-8); assertTrue(data.get(2) == alg.getSplitIndex()); assertTrue(data.get(0) == leftData.get(0)); assertTrue(data.get(1) == rightData.get(0)); }