source: branches/MetisMQI/src/main/java/weka/classifiers/misc/monotone/InstancesComparator.java

Last change on this file was 29, checked in by gnappo, 14 years ago

Taggata versione per la demo e aggiunto branch.

File size: 3.3 KB
Line 
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 *    InstancesComparator.java
19 *    Copyright (C) 2004 Stijn Lievens
20 *
21 */
22
23package weka.classifiers.misc.monotone;
24
25import weka.core.Instance;
26import weka.core.RevisionHandler;
27import weka.core.RevisionUtils;
28
29import java.util.Comparator;
30
31/**
32 * Class to compare instances with respect to a given attribute, indicated
33 * by its index.  The ordering of the attribute values is determined
34 * by the internal values of WEKA.  There is also the possibility of
35 * reversing this order.
36 * <p>
37 * This implementation is part of the master's thesis: "Studie
38 * en implementatie van instantie-gebaseerde algoritmen voor gesuperviseerd
39 * rangschikken", Stijn Lievens, Ghent University, 2004.
40 * </p>
41 *
42 * @author Stijn Lievens (stijn.lievens@ugent.be)
43 * @version $Revision: 5922 $
44 */
45public class InstancesComparator
46  implements Comparator, RevisionHandler {
47
48  /** index of the attribute  */
49  private int m_Index;
50 
51  /** If 1 then the order is not reversed, when -1, the order is reversed */
52  private int m_Reverse = 1;
53
54  /**
55   * Construct an <code> InstancesComparator </code> that compares
56   * the attributes with the given index.
57   *
58   * @param index the index on which to compare instances
59   */
60  public InstancesComparator(int index) {
61    m_Index = index;
62  }
63
64  /**
65   * Construct an <code> InstancesComparator </code> that compares
66   * the attributes with the given index, with the possibility of
67   * reversing the order.
68   *
69   * @param index the index on which to compare instances
70   * @param reverse if <code> true </code> the order is reversed, if
71   * <code> false </code> the order is not reversed
72   */
73  public InstancesComparator(int index, boolean reverse) {
74    m_Index = index;
75    m_Reverse = (reverse == true) ? -1 : 1;
76  }
77
78  /**
79   * Compares two objects (instances) with respect to the attribute
80   * this comparator is constructed on.
81   *
82   * @param o1 the first object to be compared
83   * @param o2 the second object to be compared
84   * @return -1 if <code> o1 &lt; o2 </code> (wrt to the given attribute),
85   * 1 if <code> o1 &gt; o2 </code>, and 0 if <code> o1 </code> and
86   * <code> o2 </code> are equal (wrt to the given attribute)
87   */
88  public int compare(Object o1, Object o2) {
89    Instance i1 = (Instance) o1;
90    Instance i2 = (Instance) o2;
91
92    if (i1.value(m_Index) < i2.value(m_Index)) {
93      return -1 * m_Reverse;
94    } else if (i1.value(m_Index) > i2.value(m_Index)) {
95      return 1 * m_Reverse;
96    }
97    return 0;
98  }
99 
100  /**
101   * Returns the revision string.
102   *
103   * @return            the revision
104   */
105  public String getRevision() {
106    return RevisionUtils.extract("$Revision: 5922 $");
107  }
108}
Note: See TracBrowser for help on using the repository browser.