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 | * PredictionNode.java |
---|
19 | * Copyright (C) 2001 University of Waikato, Hamilton, New Zealand |
---|
20 | * |
---|
21 | */ |
---|
22 | |
---|
23 | package weka.classifiers.trees.adtree; |
---|
24 | |
---|
25 | import weka.classifiers.trees.ADTree; |
---|
26 | import weka.core.FastVector; |
---|
27 | import weka.core.RevisionHandler; |
---|
28 | import weka.core.RevisionUtils; |
---|
29 | |
---|
30 | import java.io.Serializable; |
---|
31 | import java.util.Enumeration; |
---|
32 | |
---|
33 | /** |
---|
34 | * Class representing a prediction node in an alternating tree. |
---|
35 | * |
---|
36 | * @author Richard Kirkby (rkirkby@cs.waikato.ac.nz) |
---|
37 | * @version $Revision: 1.7 $ |
---|
38 | */ |
---|
39 | public final class PredictionNode |
---|
40 | implements Serializable, Cloneable, RevisionHandler { |
---|
41 | |
---|
42 | /** for serialization */ |
---|
43 | private static final long serialVersionUID = 6018958856358698814L; |
---|
44 | |
---|
45 | /** The prediction value stored in this node */ |
---|
46 | private double value; |
---|
47 | |
---|
48 | /** The children of this node - any number of splitter nodes */ |
---|
49 | private FastVector children; |
---|
50 | |
---|
51 | /** |
---|
52 | * Creates a new prediction node. |
---|
53 | * |
---|
54 | * @param newValue the value that the node should store |
---|
55 | */ |
---|
56 | public PredictionNode(double newValue) { |
---|
57 | |
---|
58 | value = newValue; |
---|
59 | children = new FastVector(); |
---|
60 | } |
---|
61 | |
---|
62 | /** |
---|
63 | * Sets the prediction value of the node. |
---|
64 | * |
---|
65 | * @param newValue the value that the node should store |
---|
66 | */ |
---|
67 | public final void setValue(double newValue) { |
---|
68 | |
---|
69 | value = newValue; |
---|
70 | } |
---|
71 | |
---|
72 | /** |
---|
73 | * Gets the prediction value of the node. |
---|
74 | * |
---|
75 | * @return the value stored in the node |
---|
76 | */ |
---|
77 | public final double getValue() { |
---|
78 | |
---|
79 | return value; |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | * Gets the children of this node. |
---|
84 | * |
---|
85 | * @return a FastVector containing child Splitter object references |
---|
86 | */ |
---|
87 | public final FastVector getChildren() { |
---|
88 | |
---|
89 | return children; |
---|
90 | } |
---|
91 | |
---|
92 | /** |
---|
93 | * Enumerates the children of this node. |
---|
94 | * |
---|
95 | * @return an enumeration of child Splitter object references |
---|
96 | */ |
---|
97 | public final Enumeration children() { |
---|
98 | |
---|
99 | return children.elements(); |
---|
100 | } |
---|
101 | |
---|
102 | /** |
---|
103 | * Adds a child to this node. If possible will merge, and will perform a deep copy |
---|
104 | * of the child tree. |
---|
105 | * |
---|
106 | * @param newChild the new child to add (will be cloned) |
---|
107 | * @param addingTo the tree that this node belongs to |
---|
108 | */ |
---|
109 | public final void addChild(Splitter newChild, ADTree addingTo) { |
---|
110 | |
---|
111 | // search for an equivalent child |
---|
112 | Splitter oldEqual = null; |
---|
113 | for (Enumeration e = children(); e.hasMoreElements(); ) { |
---|
114 | Splitter split = (Splitter) e.nextElement(); |
---|
115 | if (newChild.equalTo(split)) { oldEqual = split; break; } |
---|
116 | } |
---|
117 | if (oldEqual == null) { // didn't find one so just add |
---|
118 | Splitter addChild = (Splitter) newChild.clone(); |
---|
119 | setOrderAddedSubtree(addChild, addingTo); |
---|
120 | children.addElement(addChild); |
---|
121 | } |
---|
122 | else { // found one, so do a merge |
---|
123 | for (int i=0; i<newChild.getNumOfBranches(); i++) { |
---|
124 | PredictionNode oldPred = oldEqual.getChildForBranch(i); |
---|
125 | PredictionNode newPred = newChild.getChildForBranch(i); |
---|
126 | if (oldPred != null && newPred != null) |
---|
127 | oldPred.merge(newPred, addingTo); |
---|
128 | } |
---|
129 | } |
---|
130 | } |
---|
131 | |
---|
132 | /** |
---|
133 | * Clones this node. Performs a deep copy, recursing through the tree. |
---|
134 | * |
---|
135 | * @return a clone |
---|
136 | */ |
---|
137 | public final Object clone() { |
---|
138 | |
---|
139 | PredictionNode clone = new PredictionNode(value); |
---|
140 | for (Enumeration e = children.elements(); e.hasMoreElements(); ) |
---|
141 | clone.children.addElement((Splitter)((Splitter) e.nextElement()).clone()); |
---|
142 | return clone; |
---|
143 | } |
---|
144 | |
---|
145 | /** |
---|
146 | * Merges this node with another. |
---|
147 | * |
---|
148 | * @param merger the node that is merging with this node - will not be affected, |
---|
149 | * will instead be cloned |
---|
150 | * @param mergingTo the tree that this node belongs to |
---|
151 | */ |
---|
152 | public final void merge(PredictionNode merger, ADTree mergingTo) { |
---|
153 | |
---|
154 | value += merger.value; |
---|
155 | for (Enumeration e = merger.children(); e.hasMoreElements(); ) { |
---|
156 | addChild((Splitter)e.nextElement(), mergingTo); |
---|
157 | } |
---|
158 | } |
---|
159 | |
---|
160 | /** |
---|
161 | * Sets the order added values of the subtree rooted at this splitter node. |
---|
162 | * |
---|
163 | * @param addChild the root of the subtree |
---|
164 | * @param addingTo the tree that this node will belong to |
---|
165 | */ |
---|
166 | private final void setOrderAddedSubtree(Splitter addChild, ADTree addingTo) { |
---|
167 | |
---|
168 | addChild.orderAdded = addingTo.nextSplitAddedOrder(); |
---|
169 | for (int i=0; i<addChild.getNumOfBranches(); i++) { |
---|
170 | PredictionNode node = addChild.getChildForBranch(i); |
---|
171 | if (node != null) |
---|
172 | for (Enumeration e = node.children(); e.hasMoreElements(); ) |
---|
173 | setOrderAddedSubtree((Splitter) e.nextElement(), addingTo); |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | /** |
---|
178 | * Returns the revision string. |
---|
179 | * |
---|
180 | * @return the revision |
---|
181 | */ |
---|
182 | public String getRevision() { |
---|
183 | return RevisionUtils.extract("$Revision: 1.7 $"); |
---|
184 | } |
---|
185 | } |
---|