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 | * DecisionTableHashKey.java |
---|
19 | * Copyright (C) 2007 University of Waikato, Hamilton, New Zealand |
---|
20 | * |
---|
21 | */ |
---|
22 | package weka.classifiers.rules; |
---|
23 | |
---|
24 | import weka.core.Instance; |
---|
25 | import weka.core.Instances; |
---|
26 | import weka.core.RevisionHandler; |
---|
27 | import weka.core.RevisionUtils; |
---|
28 | |
---|
29 | import java.io.Serializable; |
---|
30 | |
---|
31 | /** |
---|
32 | * Class providing hash table keys for DecisionTable |
---|
33 | */ |
---|
34 | public class DecisionTableHashKey |
---|
35 | implements Serializable, RevisionHandler { |
---|
36 | |
---|
37 | /** for serialization */ |
---|
38 | static final long serialVersionUID = 5674163500154964602L; |
---|
39 | |
---|
40 | /** Array of attribute values for an instance */ |
---|
41 | private double [] attributes; |
---|
42 | |
---|
43 | /** True for an index if the corresponding attribute value is missing. */ |
---|
44 | private boolean [] missing; |
---|
45 | |
---|
46 | /** The key */ |
---|
47 | private int key; |
---|
48 | |
---|
49 | /** |
---|
50 | * Constructor for a hashKey |
---|
51 | * |
---|
52 | * @param t an instance from which to generate a key |
---|
53 | * @param numAtts the number of attributes |
---|
54 | * @param ignoreClass if true treat the class as a normal attribute |
---|
55 | * @throws Exception if something goes wrong |
---|
56 | */ |
---|
57 | public DecisionTableHashKey(Instance t, int numAtts, boolean ignoreClass) throws Exception { |
---|
58 | |
---|
59 | int i; |
---|
60 | int cindex = t.classIndex(); |
---|
61 | |
---|
62 | key = -999; |
---|
63 | attributes = new double [numAtts]; |
---|
64 | missing = new boolean [numAtts]; |
---|
65 | for (i=0;i<numAtts;i++) { |
---|
66 | if (i == cindex && !ignoreClass) { |
---|
67 | missing[i] = true; |
---|
68 | } else { |
---|
69 | if ((missing[i] = t.isMissing(i)) == false) { |
---|
70 | attributes[i] = t.value(i); |
---|
71 | } |
---|
72 | } |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | /** |
---|
77 | * Convert a hash entry to a string |
---|
78 | * |
---|
79 | * @param t the set of instances |
---|
80 | * @param maxColWidth width to make the fields |
---|
81 | * @return string representation of the hash entry |
---|
82 | */ |
---|
83 | public String toString(Instances t, int maxColWidth) { |
---|
84 | |
---|
85 | int i; |
---|
86 | int cindex = t.classIndex(); |
---|
87 | StringBuffer text = new StringBuffer(); |
---|
88 | |
---|
89 | for (i=0;i<attributes.length;i++) { |
---|
90 | if (i != cindex) { |
---|
91 | if (missing[i]) { |
---|
92 | text.append("?"); |
---|
93 | for (int j=0;j<maxColWidth;j++) { |
---|
94 | text.append(" "); |
---|
95 | } |
---|
96 | } else { |
---|
97 | String ss = t.attribute(i).value((int)attributes[i]); |
---|
98 | StringBuffer sb = new StringBuffer(ss); |
---|
99 | |
---|
100 | for (int j=0;j < (maxColWidth-ss.length()+1); j++) { |
---|
101 | sb.append(" "); |
---|
102 | } |
---|
103 | text.append(sb); |
---|
104 | } |
---|
105 | } |
---|
106 | } |
---|
107 | return text.toString(); |
---|
108 | } |
---|
109 | |
---|
110 | /** |
---|
111 | * Constructor for a hashKey |
---|
112 | * |
---|
113 | * @param t an array of feature values |
---|
114 | */ |
---|
115 | public DecisionTableHashKey(double [] t) { |
---|
116 | |
---|
117 | int i; |
---|
118 | int l = t.length; |
---|
119 | |
---|
120 | key = -999; |
---|
121 | attributes = new double [l]; |
---|
122 | missing = new boolean [l]; |
---|
123 | for (i=0;i<l;i++) { |
---|
124 | if (t[i] == Double.MAX_VALUE) { |
---|
125 | missing[i] = true; |
---|
126 | } else { |
---|
127 | missing[i] = false; |
---|
128 | attributes[i] = t[i]; |
---|
129 | } |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | /** |
---|
134 | * Calculates a hash code |
---|
135 | * |
---|
136 | * @return the hash code as an integer |
---|
137 | */ |
---|
138 | public int hashCode() { |
---|
139 | |
---|
140 | int hv = 0; |
---|
141 | |
---|
142 | if (key != -999) |
---|
143 | return key; |
---|
144 | for (int i=0;i<attributes.length;i++) { |
---|
145 | if (missing[i]) { |
---|
146 | hv += (i*13); |
---|
147 | } else { |
---|
148 | hv += (i * 5 * (attributes[i]+1)); |
---|
149 | } |
---|
150 | } |
---|
151 | if (key == -999) { |
---|
152 | key = hv; |
---|
153 | } |
---|
154 | return hv; |
---|
155 | } |
---|
156 | |
---|
157 | /** |
---|
158 | * Tests if two instances are equal |
---|
159 | * |
---|
160 | * @param b a key to compare with |
---|
161 | * @return true if both objects are equal |
---|
162 | */ |
---|
163 | public boolean equals(Object b) { |
---|
164 | |
---|
165 | if ((b == null) || !(b.getClass().equals(this.getClass()))) { |
---|
166 | return false; |
---|
167 | } |
---|
168 | boolean ok = true; |
---|
169 | boolean l; |
---|
170 | if (b instanceof DecisionTableHashKey) { |
---|
171 | DecisionTableHashKey n = (DecisionTableHashKey)b; |
---|
172 | for (int i=0;i<attributes.length;i++) { |
---|
173 | l = n.missing[i]; |
---|
174 | if (missing[i] || l) { |
---|
175 | if ((missing[i] && !l) || (!missing[i] && l)) { |
---|
176 | ok = false; |
---|
177 | break; |
---|
178 | } |
---|
179 | } else { |
---|
180 | if (attributes[i] != n.attributes[i]) { |
---|
181 | ok = false; |
---|
182 | break; |
---|
183 | } |
---|
184 | } |
---|
185 | } |
---|
186 | } else { |
---|
187 | return false; |
---|
188 | } |
---|
189 | return ok; |
---|
190 | } |
---|
191 | |
---|
192 | /** |
---|
193 | * Prints the hash code |
---|
194 | */ |
---|
195 | public void print_hash_code() { |
---|
196 | System.out.println("Hash val: "+hashCode()); |
---|
197 | } |
---|
198 | |
---|
199 | /** |
---|
200 | * Returns the revision string. |
---|
201 | * |
---|
202 | * @return the revision |
---|
203 | */ |
---|
204 | public String getRevision() { |
---|
205 | return RevisionUtils.extract("$Revision: 5928 $"); |
---|
206 | } |
---|
207 | } |
---|