weka.core
Class KDTree

java.lang.Object
  extended byweka.core.KDTree
All Implemented Interfaces:
OptionHandler, java.io.Serializable

public class KDTree
extends java.lang.Object
implements OptionHandler, java.io.Serializable

This is a KD-Tree structure that stores instances using a divide and conquer method. The connection to dataset is only a reference. For the tree structure the indexes are stored in a dynamic array Building the tree: If a node has (option -L) instances no further splitting is done. Also if the split would leave one side empty, the branch is not split any further even if the instances in the resulting node are more than instances. -P
Pruning flag.

-W
minimal width of a box -L
maximal instance number in a leaf -D Distance function to be used (default = Euclidean distance) -N
Set Normalization. Used when building the tree and selecting the widest dimension, each dimension is 'normalized' to the universe range. -U
Set debuglevel.

See Also:
Serialized Form

Field Summary
static int R_MAX
           
static int R_MIN
          Index in ranges for LOW and HIGH and WIDTH
static int R_WIDTH
           
 
Constructor Summary
KDTree()
          Default Constructor
KDTree(KDTree tree)
          Constructor, copies all options from an existing KDTree.
 
Method Summary
 void addLooslyInstance(Instance instance)
          Adds one instance to KDTree loosly.
 void buildKDTree(Instances instances)
          Builds the KDTree.
 void buildKDTree(Instances instances, double[][] ranges)
          Builds the KDTree.
 void centerInstances(Instances centers, int[] assignments, double pc)
          Assigns instances to centers using KDTree.
 void deleteInstance(Instance instance)
          Deletes one instance in the KDTree.
 boolean deleteInstance(int index)
          Deletes one instance in the KDTree.
 int findKNearestNeighbour(Instance target, int kNN, int[] nearestList, double[] distanceList)
          Find k nearest neighbours to target.
 int getDebugLevel()
          Gets the debug level.
 DistanceFunction getDistanceFunction()
          Gets the distance function.
protected  java.lang.String getDistanceFunctionSpec()
          Gets the distance function specification string, which contains the class name of distance function the filter and any options to the filter
 Instances getInstances()
          Gets instances used in the tree.
 DynamicArrayOfPosInt getInstList()
          Gets instance list used in the tree.
 int getMaxInstInLeaf()
          Get the maximum number of instances in a leaf.
 double getMinBoxRelWidth()
          Gets the minimum relative box width.
 boolean getNormalize()
          Gets the normalize flag.
 java.lang.String[] getOptions()
          Gets the current settings of KDtree.
 boolean getPrune()
          Gets the pruning flag.
 boolean isValid()
          Returns true if valid flag is true.
 java.util.Enumeration listOptions()
          Returns an enumeration describing the available options.
static void main(java.lang.String[] args)
          Main method for testing this class
 int numInstances()
          Gets number of instances in KDTree.
 void setDebugLevel(int d)
          Sets the debug level.
 void setDistanceFunction(DistanceFunction distanceF)
          Sets the distance function.
 void setMaxInstInLeaf(int i)
          Sets the maximum number of instances in a leaf.
 void setMinBoxRelWidth(double i)
          Sets the minimum relative box width.
 void setNormalize(boolean n)
          Sets the flag for normalizing the widths of a KDTree Node by the width of the dimension in the universe.
 void setOptions(java.lang.String[] options)
          Parses a given list of options.
 void setPrune(boolean p)
          Sets the flag for pruning of the blacklisting algorithm.
 void setValid(boolean valid)
          Sets KDTree to be valid for dataset in m_Instances.
 java.lang.String toString()
          toString
 void updateKDTree(Instance instance)
          Adds one instance to the KDTree.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

R_MIN

public static int R_MIN
Index in ranges for LOW and HIGH and WIDTH


R_MAX

public static int R_MAX

R_WIDTH

public static int R_WIDTH
Constructor Detail

KDTree

public KDTree()
Default Constructor


KDTree

public KDTree(KDTree tree)
Constructor, copies all options from an existing KDTree.

Parameters:
tree - the KDTree to copy from
Method Detail

addLooslyInstance

public void addLooslyInstance(Instance instance)
Adds one instance to KDTree loosly. It only changes the ranges. The ranges are important for the distance function.

Parameters:
instance - the new instance

buildKDTree

public void buildKDTree(Instances instances)
                 throws java.lang.Exception
Builds the KDTree.

Parameters:
instances - instances to build the tree of
Throws:
java.lang.Exception

buildKDTree

public void buildKDTree(Instances instances,
                        double[][] ranges)
                 throws java.lang.Exception
Builds the KDTree. It is adviseable to run the replace missing attributes filter on the passed instances first.

Parameters:
instances - instances to build the tree of
Throws:
java.lang.Exception

updateKDTree

public void updateKDTree(Instance instance)
                  throws java.lang.Exception
Adds one instance to the KDTree.

Parameters:
instance - the instance to be added
Throws:
java.lang.Exception

deleteInstance

public void deleteInstance(Instance instance)
                    throws java.lang.Exception
Deletes one instance in the KDTree.

Parameters:
instance - the instance to be deleted
Throws:
java.lang.Exception

deleteInstance

public boolean deleteInstance(int index)
                       throws java.lang.Exception
Deletes one instance in the KDTree.

Parameters:
index - the index of the instance to be deleted
Returns:
true if instance was deleted
Throws:
java.lang.Exception

toString

public java.lang.String toString()
toString

Returns:
string representing the tree

centerInstances

public void centerInstances(Instances centers,
                            int[] assignments,
                            double pc)
                     throws java.lang.Exception
Assigns instances to centers using KDTree.

Parameters:
centers - the current centers
assignments - the centerindex for each instance
pc - the threshold value for pruning.
Throws:
java.lang.Exception

findKNearestNeighbour

public int findKNearestNeighbour(Instance target,
                                 int kNN,
                                 int[] nearestList,
                                 double[] distanceList)
                          throws java.lang.Exception
Find k nearest neighbours to target. This is the main method.

Parameters:
target - the instance to find nearest neighbour for
Throws:
java.lang.Exception

setValid

public void setValid(boolean valid)
Sets KDTree to be valid for dataset in m_Instances.


isValid

public boolean isValid()
Returns true if valid flag is true.

Returns:
flag if KDtree is valid

numInstances

public int numInstances()
Gets number of instances in KDTree.

Returns:
number of instances

getInstances

public Instances getInstances()
Gets instances used in the tree.

Returns:
model information

getInstList

public DynamicArrayOfPosInt getInstList()
Gets instance list used in the tree.

Returns:
instance list

getDistanceFunctionSpec

protected java.lang.String getDistanceFunctionSpec()
Gets the distance function specification string, which contains the class name of distance function the filter and any options to the filter

Returns:
the filter string.

setDistanceFunction

public void setDistanceFunction(DistanceFunction distanceF)
Sets the distance function.

Parameters:
distanceF - the distance function with all options set

getDistanceFunction

public DistanceFunction getDistanceFunction()
Gets the distance function.

Returns:
the distance function

setMinBoxRelWidth

public void setMinBoxRelWidth(double i)
                       throws java.lang.Exception
Sets the minimum relative box width.

Parameters:
i - the minimum relative box width
Throws:
java.lang.Exception

getMinBoxRelWidth

public double getMinBoxRelWidth()
Gets the minimum relative box width.

Returns:
the minimum relative box width

setMaxInstInLeaf

public void setMaxInstInLeaf(int i)
                      throws java.lang.Exception
Sets the maximum number of instances in a leaf.

Parameters:
i - the maximum number of instances in a leaf
Throws:
java.lang.Exception

getMaxInstInLeaf

public int getMaxInstInLeaf()
Get the maximum number of instances in a leaf.

Returns:
the maximum number of instances in a leaf

setPrune

public void setPrune(boolean p)
Sets the flag for pruning of the blacklisting algorithm.

Parameters:
p - true to use pruning.

getPrune

public boolean getPrune()
Gets the pruning flag.

Returns:
True if pruning

setNormalize

public void setNormalize(boolean n)
Sets the flag for normalizing the widths of a KDTree Node by the width of the dimension in the universe.

Parameters:
n - true to use normalizing.

getNormalize

public boolean getNormalize()
Gets the normalize flag.

Returns:
True if normalizing

setDebugLevel

public void setDebugLevel(int d)
Sets the debug level. debug level = 0, means no output

Parameters:
d - debuglevel

getDebugLevel

public int getDebugLevel()
Gets the debug level.

Returns:
debug level

listOptions

public java.util.Enumeration listOptions()
Returns an enumeration describing the available options.

Specified by:
listOptions in interface OptionHandler
Returns:
an enumeration of all the available options.

setOptions

public void setOptions(java.lang.String[] options)
                throws java.lang.Exception
Parses a given list of options.

Specified by:
setOptions in interface OptionHandler
Parameters:
options - the list of options as an array of strings
Throws:
java.lang.Exception - if an option is not supported

getOptions

public java.lang.String[] getOptions()
Gets the current settings of KDtree.

Specified by:
getOptions in interface OptionHandler
Returns:
an array of strings suitable for passing to setOptions

main

public static void main(java.lang.String[] args)
Main method for testing this class