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 | * MinkowskiDistance.java |
---|
19 | * Copyright (C) 2009 University of Waikato, Hamilton, New Zealand |
---|
20 | * |
---|
21 | */ |
---|
22 | |
---|
23 | package weka.core; |
---|
24 | |
---|
25 | import weka.core.TechnicalInformation.Field; |
---|
26 | import weka.core.TechnicalInformation.Type; |
---|
27 | import weka.core.neighboursearch.PerformanceStats; |
---|
28 | |
---|
29 | import java.util.Enumeration; |
---|
30 | import java.util.Vector; |
---|
31 | |
---|
32 | /** |
---|
33 | <!-- globalinfo-start --> |
---|
34 | * Implementing Minkowski distance (or similarity) function.<br/> |
---|
35 | * <br/> |
---|
36 | * One object defines not one distance but the data model in which the distances between objects of that data model can be computed.<br/> |
---|
37 | * <br/> |
---|
38 | * Attention: For efficiency reasons the use of consistency checks (like are the data models of the two instances exactly the same), is low.<br/> |
---|
39 | * <br/> |
---|
40 | * For more information, see:<br/> |
---|
41 | * <br/> |
---|
42 | * Wikipedia. Minkowski distance. URL http://en.wikipedia.org/wiki/Minkowski_distance. |
---|
43 | * <p/> |
---|
44 | <!-- globalinfo-end --> |
---|
45 | * |
---|
46 | <!-- technical-bibtex-start --> |
---|
47 | * BibTeX: |
---|
48 | * <pre> |
---|
49 | * @misc{missing_id, |
---|
50 | * author = {Wikipedia}, |
---|
51 | * title = {Minkowski distance}, |
---|
52 | * URL = {http://en.wikipedia.org/wiki/Minkowski_distance} |
---|
53 | * } |
---|
54 | * </pre> |
---|
55 | * <p/> |
---|
56 | <!-- technical-bibtex-end --> |
---|
57 | * |
---|
58 | <!-- options-start --> |
---|
59 | * Valid options are: <p/> |
---|
60 | * |
---|
61 | * <pre> -P <order> |
---|
62 | * The order 'p'. With '1' being the Manhattan distance and '2' |
---|
63 | * the Euclidean distance. |
---|
64 | * (default: 2)</pre> |
---|
65 | * |
---|
66 | * <pre> -D |
---|
67 | * Turns off the normalization of attribute |
---|
68 | * values in distance calculation.</pre> |
---|
69 | * |
---|
70 | * <pre> -R <col1,col2-col4,...> |
---|
71 | * Specifies list of columns to used in the calculation of the |
---|
72 | * distance. 'first' and 'last' are valid indices. |
---|
73 | * (default: first-last)</pre> |
---|
74 | * |
---|
75 | * <pre> -V |
---|
76 | * Invert matching sense of column indices.</pre> |
---|
77 | * |
---|
78 | <!-- options-end --> |
---|
79 | * |
---|
80 | * @author FracPete (fracpete at waikato dot ac dot nz) |
---|
81 | * @version $Revision: 5953 $ |
---|
82 | */ |
---|
83 | public class MinkowskiDistance |
---|
84 | extends NormalizableDistance |
---|
85 | implements Cloneable, TechnicalInformationHandler { |
---|
86 | |
---|
87 | /** for serialization. */ |
---|
88 | private static final long serialVersionUID = -7446019339455453893L; |
---|
89 | |
---|
90 | /** the order of the minkowski distance. */ |
---|
91 | protected double m_Order = 2; |
---|
92 | |
---|
93 | /** |
---|
94 | * Constructs an Minkowski Distance object, Instances must be still set. |
---|
95 | */ |
---|
96 | public MinkowskiDistance() { |
---|
97 | super(); |
---|
98 | } |
---|
99 | |
---|
100 | /** |
---|
101 | * Constructs an Minkowski Distance object and automatically initializes the |
---|
102 | * ranges. |
---|
103 | * |
---|
104 | * @param data the instances the distance function should work on |
---|
105 | */ |
---|
106 | public MinkowskiDistance(Instances data) { |
---|
107 | super(data); |
---|
108 | } |
---|
109 | |
---|
110 | /** |
---|
111 | * Returns a string describing this object. |
---|
112 | * |
---|
113 | * @return a description of the evaluator suitable for |
---|
114 | * displaying in the explorer/experimenter gui |
---|
115 | */ |
---|
116 | public String globalInfo() { |
---|
117 | return |
---|
118 | "Implementing Minkowski distance (or similarity) function.\n\n" |
---|
119 | + "One object defines not one distance but the data model in which " |
---|
120 | + "the distances between objects of that data model can be computed.\n\n" |
---|
121 | + "Attention: For efficiency reasons the use of consistency checks " |
---|
122 | + "(like are the data models of the two instances exactly the same), " |
---|
123 | + "is low.\n\n" |
---|
124 | + "For more information, see:\n\n" |
---|
125 | + getTechnicalInformation().toString(); |
---|
126 | } |
---|
127 | |
---|
128 | /** |
---|
129 | * Returns an instance of a TechnicalInformation object, containing |
---|
130 | * detailed information about the technical background of this class, |
---|
131 | * e.g., paper reference or book this class is based on. |
---|
132 | * |
---|
133 | * @return the technical information about this class |
---|
134 | */ |
---|
135 | public TechnicalInformation getTechnicalInformation() { |
---|
136 | TechnicalInformation result; |
---|
137 | |
---|
138 | result = new TechnicalInformation(Type.MISC); |
---|
139 | result.setValue(Field.AUTHOR, "Wikipedia"); |
---|
140 | result.setValue(Field.TITLE, "Minkowski distance"); |
---|
141 | result.setValue(Field.URL, "http://en.wikipedia.org/wiki/Minkowski_distance"); |
---|
142 | |
---|
143 | return result; |
---|
144 | } |
---|
145 | |
---|
146 | /** |
---|
147 | * Returns an enumeration describing the available options. |
---|
148 | * |
---|
149 | * @return an enumeration of all the available options. |
---|
150 | */ |
---|
151 | public Enumeration listOptions() { |
---|
152 | Vector<Option> result = new Vector<Option>(); |
---|
153 | |
---|
154 | result.addElement(new Option( |
---|
155 | "\tThe order 'p'. With '1' being the Manhattan distance and '2'\n" |
---|
156 | + "\tthe Euclidean distance.\n" |
---|
157 | + "\t(default: 2)", |
---|
158 | "P", 1, "-P <order>")); |
---|
159 | |
---|
160 | Enumeration en = super.listOptions(); |
---|
161 | while (en.hasMoreElements()) |
---|
162 | result.addElement((Option)en.nextElement()); |
---|
163 | |
---|
164 | return result.elements(); |
---|
165 | } |
---|
166 | |
---|
167 | /** |
---|
168 | * Returns the tip text for this property. |
---|
169 | * |
---|
170 | * @return tip text for this property suitable for |
---|
171 | * displaying in the explorer/experimenter gui |
---|
172 | */ |
---|
173 | public String orderTipText() { |
---|
174 | return |
---|
175 | "The order of the Minkowski distance ('1' is Manhattan distance and " |
---|
176 | + "'2' the Euclidean distance)."; |
---|
177 | } |
---|
178 | |
---|
179 | /** |
---|
180 | * Sets the order. |
---|
181 | * |
---|
182 | * @param value the new order |
---|
183 | */ |
---|
184 | public void setOrder(double value) { |
---|
185 | if (m_Order != 0.0) { |
---|
186 | m_Order = value; |
---|
187 | invalidate(); |
---|
188 | } |
---|
189 | else { |
---|
190 | System.err.println("Order cannot be zero!"); |
---|
191 | } |
---|
192 | } |
---|
193 | |
---|
194 | /** |
---|
195 | * Gets the order. |
---|
196 | * |
---|
197 | * @return the order |
---|
198 | */ |
---|
199 | public double getOrder() { |
---|
200 | return m_Order; |
---|
201 | } |
---|
202 | |
---|
203 | /** |
---|
204 | * Calculates the distance between two instances. |
---|
205 | * |
---|
206 | * @param first the first instance |
---|
207 | * @param second the second instance |
---|
208 | * @return the distance between the two given instances |
---|
209 | */ |
---|
210 | public double distance(Instance first, Instance second) { |
---|
211 | return Math.pow(distance(first, second, Double.POSITIVE_INFINITY), 1/m_Order); |
---|
212 | } |
---|
213 | |
---|
214 | /** |
---|
215 | * Calculates the distance (or similarity) between two instances. Need to |
---|
216 | * pass this returned distance later on to postprocess method to set it on |
---|
217 | * correct scale. <br/> |
---|
218 | * P.S.: Please don't mix the use of this function with |
---|
219 | * distance(Instance first, Instance second), as that already does post |
---|
220 | * processing. Please consider passing Double.POSITIVE_INFINITY as the cutOffValue to |
---|
221 | * this function and then later on do the post processing on all the |
---|
222 | * distances. |
---|
223 | * |
---|
224 | * @param first the first instance |
---|
225 | * @param second the second instance |
---|
226 | * @param stats the structure for storing performance statistics. |
---|
227 | * @return the distance between the two given instances or |
---|
228 | * Double.POSITIVE_INFINITY. |
---|
229 | */ |
---|
230 | public double distance(Instance first, Instance second, PerformanceStats stats) { //debug method pls remove after use |
---|
231 | return Math.pow(distance(first, second, Double.POSITIVE_INFINITY, stats), 1/m_Order); |
---|
232 | } |
---|
233 | |
---|
234 | /** |
---|
235 | * Updates the current distance calculated so far with the new difference |
---|
236 | * between two attributes. The difference between the attributes was |
---|
237 | * calculated with the difference(int,double,double) method. |
---|
238 | * |
---|
239 | * @param currDist the current distance calculated so far |
---|
240 | * @param diff the difference between two new attributes |
---|
241 | * @return the update distance |
---|
242 | * @see #difference(int, double, double) |
---|
243 | */ |
---|
244 | protected double updateDistance(double currDist, double diff) { |
---|
245 | double result; |
---|
246 | |
---|
247 | result = currDist; |
---|
248 | result += Math.pow(Math.abs(diff), m_Order); |
---|
249 | |
---|
250 | return result; |
---|
251 | } |
---|
252 | |
---|
253 | /** |
---|
254 | * Does post processing of the distances (if necessary) returned by |
---|
255 | * distance(distance(Instance first, Instance second, double cutOffValue). It |
---|
256 | * is necessary to do so to get the correct distances if |
---|
257 | * distance(distance(Instance first, Instance second, double cutOffValue) is |
---|
258 | * used. This is because that function actually returns the squared distance |
---|
259 | * to avoid inaccuracies arising from floating point comparison. |
---|
260 | * |
---|
261 | * @param distances the distances to post-process |
---|
262 | */ |
---|
263 | public void postProcessDistances(double distances[]) { |
---|
264 | for(int i = 0; i < distances.length; i++) { |
---|
265 | distances[i] = Math.pow(distances[i], 1/m_Order); |
---|
266 | } |
---|
267 | } |
---|
268 | |
---|
269 | /** |
---|
270 | * Returns the revision string. |
---|
271 | * |
---|
272 | * @return the revision |
---|
273 | */ |
---|
274 | public String getRevision() { |
---|
275 | return RevisionUtils.extract("$Revision: 0$"); |
---|
276 | } |
---|
277 | } |
---|