1 | /* |
---|
2 | * This program is free software; you can redistribute it and/or modify |
---|
3 | * it under the terms of the GNU General Public License as published by |
---|
4 | * the Free Software Foundation; either version 2 of the License, or |
---|
5 | * (at your option) any later version. |
---|
6 | * |
---|
7 | * This program is distributed in the hope that it will be useful, |
---|
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
10 | * GNU General Public License for more details. |
---|
11 | * |
---|
12 | * You should have received a copy of the GNU General Public License |
---|
13 | * along with this program; if not, write to the Free Software |
---|
14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
15 | */ |
---|
16 | |
---|
17 | /* |
---|
18 | * BallSplitter.java |
---|
19 | * Copyright (C) 2007 University of Waikato |
---|
20 | */ |
---|
21 | |
---|
22 | package weka.core.neighboursearch.balltrees; |
---|
23 | |
---|
24 | import weka.core.EuclideanDistance; |
---|
25 | import weka.core.Instances; |
---|
26 | import weka.core.OptionHandler; |
---|
27 | import weka.core.RevisionHandler; |
---|
28 | import weka.core.RevisionUtils; |
---|
29 | |
---|
30 | import java.io.Serializable; |
---|
31 | import java.util.Enumeration; |
---|
32 | import java.util.Vector; |
---|
33 | |
---|
34 | /** |
---|
35 | * Abstract class for splitting a ball tree's BallNode. |
---|
36 | * |
---|
37 | * @author Ashraf M. Kibriya (amk14[at-the-rate]cs[dot]waikato[dot]ac[dot]nz) |
---|
38 | * @version $Revision: 5953 $ |
---|
39 | */ |
---|
40 | public abstract class BallSplitter |
---|
41 | implements Serializable, OptionHandler, RevisionHandler { |
---|
42 | |
---|
43 | /** The instance on which the tree is built. */ |
---|
44 | protected Instances m_Instances; |
---|
45 | |
---|
46 | /** The distance function (metric) from which |
---|
47 | * the tree is (OR is to be) built. */ |
---|
48 | protected EuclideanDistance m_DistanceFunction; |
---|
49 | |
---|
50 | /** |
---|
51 | * The master index array that'll be reshuffled as nodes |
---|
52 | * are split (and the tree is constructed). |
---|
53 | */ |
---|
54 | protected int[] m_Instlist; |
---|
55 | |
---|
56 | /** |
---|
57 | * default constructor. |
---|
58 | */ |
---|
59 | public BallSplitter() { |
---|
60 | } |
---|
61 | |
---|
62 | /** |
---|
63 | * Creates a new instance of BallSplitter. |
---|
64 | * @param instList The master index array. |
---|
65 | * @param insts The instances on which the tree |
---|
66 | * is (or is to be) built. |
---|
67 | * @param e The Euclidean distance function to |
---|
68 | * use for splitting. |
---|
69 | */ |
---|
70 | public BallSplitter(int[] instList, Instances insts, EuclideanDistance e) { |
---|
71 | m_Instlist = instList; |
---|
72 | m_Instances = insts; |
---|
73 | m_DistanceFunction = e; |
---|
74 | } |
---|
75 | |
---|
76 | /** |
---|
77 | * Checks whether if this ball splitter is |
---|
78 | * correctly intialized or not (i.e. master index |
---|
79 | * array, instances, and distance function is |
---|
80 | * supplied or not) |
---|
81 | * @throws Exception If the object is not correctly |
---|
82 | * initialized. |
---|
83 | */ |
---|
84 | protected void correctlyInitialized() throws Exception { |
---|
85 | if(m_Instances==null) |
---|
86 | throw new Exception("No instances supplied."); |
---|
87 | else if(m_Instlist==null) |
---|
88 | throw new Exception("No instance list supplied."); |
---|
89 | else if(m_DistanceFunction==null) |
---|
90 | throw new Exception("No Euclidean distance function supplied."); |
---|
91 | else if(m_Instances.numInstances() != m_Instlist.length) |
---|
92 | throw new Exception("The supplied instance list doesn't seem to match " + |
---|
93 | "the supplied instances"); |
---|
94 | } |
---|
95 | |
---|
96 | /** |
---|
97 | * Returns an enumeration describing the available options. |
---|
98 | * |
---|
99 | * @return an enumeration of all the available options. |
---|
100 | */ |
---|
101 | public Enumeration listOptions() { |
---|
102 | return new Vector().elements(); |
---|
103 | } |
---|
104 | |
---|
105 | /** |
---|
106 | * Parses a given list of options. |
---|
107 | * |
---|
108 | * @param options the list of options as an array of strings |
---|
109 | * @throws Exception if an option is not supported |
---|
110 | */ |
---|
111 | public void setOptions(String[] options) throws Exception { |
---|
112 | } |
---|
113 | |
---|
114 | /** |
---|
115 | * Gets the current settings of the object. |
---|
116 | * |
---|
117 | * @return an array of strings suitable for passing to setOptions |
---|
118 | */ |
---|
119 | public String[] getOptions() { |
---|
120 | return new String[0]; |
---|
121 | } |
---|
122 | |
---|
123 | /** |
---|
124 | * Splits a node into two. |
---|
125 | * @param node The node to split. |
---|
126 | * @param numNodesCreated The number of nodes that so far have been |
---|
127 | * created for the tree, so that the newly created nodes are |
---|
128 | * assigned correct/meaningful node numbers/ids. |
---|
129 | * @throws Exception If there is some problem in splitting the |
---|
130 | * given node. |
---|
131 | */ |
---|
132 | public abstract void splitNode(BallNode node, int numNodesCreated) |
---|
133 | throws Exception; |
---|
134 | |
---|
135 | /** |
---|
136 | * Sets the training instances on which the tree is |
---|
137 | * (or is to be) built. |
---|
138 | * @param inst The training instances. |
---|
139 | */ |
---|
140 | public void setInstances(Instances inst) { |
---|
141 | m_Instances = inst; |
---|
142 | } |
---|
143 | |
---|
144 | /** |
---|
145 | * Sets the master index array containing indices of the |
---|
146 | * training instances. This array will be rearranged as |
---|
147 | * the tree is built (or a node is split_), so that each |
---|
148 | * node is assigned a portion in this array which |
---|
149 | * contain the instances insides the node's region. |
---|
150 | * @param instList The master index array. |
---|
151 | */ |
---|
152 | public void setInstanceList(int[] instList) { |
---|
153 | m_Instlist = instList; |
---|
154 | } |
---|
155 | |
---|
156 | /** |
---|
157 | * Sets the distance function used to (or to be used |
---|
158 | * to) build the tree. |
---|
159 | * @param func The distance function. |
---|
160 | */ |
---|
161 | public void setEuclideanDistanceFunction(EuclideanDistance func) { |
---|
162 | m_DistanceFunction = func; |
---|
163 | } |
---|
164 | |
---|
165 | /** |
---|
166 | * Returns the revision string. |
---|
167 | * |
---|
168 | * @return the revision |
---|
169 | */ |
---|
170 | public String getRevision() { |
---|
171 | return RevisionUtils.extract("$Revision: 5953 $"); |
---|
172 | } |
---|
173 | } |
---|