01: /*
02: * This program is free software; you can redistribute it and/or modify
03: * it under the terms of the GNU General Public License as published by
04: * the Free Software Foundation; either version 2 of the License, or
05: * (at your option) any later version.
06: *
07: * This program is distributed in the hope that it will be useful,
08: * but WITHOUT ANY WARRANTY; without even the implied warranty of
09: * 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: * InstancesComparator.java
19: * Copyright (C) 2004 Stijn Lievens
20: *
21: */
22:
23: package weka.classifiers.misc.monotone;
24:
25: import weka.core.Instance;
26:
27: import java.util.Comparator;
28:
29: /**
30: * Class to compare instances with respect to a given attribute, indicated
31: * by its index. The ordering of the attribute values is determined
32: * by the internal values of WEKA. There is also the possibility of
33: * reversing this order.
34: * <p>
35: * This implementation is part of the master's thesis: "Studie
36: * en implementatie van instantie-gebaseerde algoritmen voor gesuperviseerd
37: * rangschikken", Stijn Lievens, Ghent University, 2004.
38: * </p>
39: *
40: * @author Stijn Lievens (stijn.lievens@ugent.be)
41: * @version $Revision: 1.1 $
42: */
43: public class InstancesComparator implements Comparator {
44:
45: /** index of the attribute */
46: private int m_Index;
47:
48: /** If 1 then the order is not reversed, when -1, the order is reversed */
49: private int m_Reverse = 1;
50:
51: /**
52: * Construct an <code> InstancesComparator </code> that compares
53: * the attributes with the given index.
54: *
55: * @param index the index on which to compare instances
56: */
57: public InstancesComparator(int index) {
58: m_Index = index;
59: }
60:
61: /**
62: * Construct an <code> InstancesComparator </code> that compares
63: * the attributes with the given index, with the possibility of
64: * reversing the order.
65: *
66: * @param index the index on which to compare instances
67: * @param reverse if <code> true </code> the order is reversed, if
68: * <code> false </code> the order is not reversed
69: */
70: public InstancesComparator(int index, boolean reverse) {
71: m_Index = index;
72: m_Reverse = (reverse == true) ? -1 : 1;
73: }
74:
75: /**
76: * Compares two objects (instances) with respect to the attribute
77: * this comparator is constructed on.
78: *
79: * @param o1 the first object to be compared
80: * @param o2 the second object to be compared
81: * @return -1 if <code> o1 < o2 </code> (wrt to the given attribute),
82: * 1 if <code> o1 > o2 </code>, and 0 if <code> o1 </code> and
83: * <code> o2 </code> are equal (wrt to the given attribute)
84: */
85: public int compare(Object o1, Object o2) {
86: Instance i1 = (Instance) o1;
87: Instance i2 = (Instance) o2;
88:
89: if (i1.value(m_Index) < i2.value(m_Index)) {
90: return -1 * m_Reverse;
91: } else if (i1.value(m_Index) > i2.value(m_Index)) {
92: return 1 * m_Reverse;
93: }
94: return 0;
95: }
96: }
|