summaryrefslogtreecommitdiff
path: root/src/de/lmu/ifi/dbs/elki/algorithm/clustering/optics/CorrelationClusterOrderEntry.java
blob: b621ac2410b9fa8476cc603359611c7a3bd77ef1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package de.lmu.ifi.dbs.elki.algorithm.clustering.optics;

/*
 This file is part of ELKI:
 Environment for Developing KDD-Applications Supported by Index-Structures

 Copyright (C) 2014
 Ludwig-Maximilians-Universität München
 Lehr- und Forschungseinheit für Datenbanksysteme
 ELKI Development Team

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import de.lmu.ifi.dbs.elki.database.ids.DBID;
import de.lmu.ifi.dbs.elki.database.ids.DBIDRef;
import de.lmu.ifi.dbs.elki.database.ids.DBIDUtil;
import de.lmu.ifi.dbs.elki.result.textwriter.TextWriteable;
import de.lmu.ifi.dbs.elki.result.textwriter.TextWriterStream;

/**
 * Cluster order entry for correlation-based OPTICS variants.
 * 
 * @author Elke Achtert
 * @author Erich Schubert
 * 
 * @param <SELF> Type self-reference
 */
public abstract class CorrelationClusterOrderEntry<SELF extends CorrelationClusterOrderEntry<SELF>> implements ClusterOrderEntry<SELF>, Comparable<SELF>, TextWriteable {
  /**
   * The component separator used by correlation distances.
   * 
   * Note: Do NOT use regular expression syntax characters!
   */
  public static final String SEPARATOR = "x";

  /**
   * The id of the entry.
   */
  protected DBID objectID;

  /**
   * The id of the entry's predecessor.
   */
  protected DBID predecessorID;

  /**
   * The correlation dimension.
   */
  protected int correlationValue;

  /**
   * The Euclidean distance in the subspace.
   */
  protected double euclideanValue;

  /**
   * Constructs a new CorrelationDistance object.
   * 
   * @param correlationValue the correlation dimension to be represented by the
   *        CorrelationDistance
   * @param euclideanValue the Euclidean distance to be represented by the
   *        CorrelationDistance
   */
  public CorrelationClusterOrderEntry(DBID objectID, DBID predecessorID, int correlationValue, double euclideanValue) {
    this.objectID = objectID;
    this.predecessorID = predecessorID;
    this.correlationValue = correlationValue;
    this.euclideanValue = euclideanValue;
  }

  @Override
  public String toString() {
    return Integer.toString(correlationValue) + SEPARATOR + Double.toString(euclideanValue);
  }

  /**
   * Indicates whether some other object is "equal to" this one.
   * 
   * NOTE: for the use in an UpdatableHeap, only the ID is used! This is
   * important, otherwise OPTICS will not work.
   * 
   * @param o the reference object with which to compare.
   * @return <code>true</code> if this object has the same attribute values as
   *         the o argument; <code>false</code> otherwise.
   */
  @Override
  public final boolean equals(Object o) {
    if(this == o) {
      return true;
    }
    if(!(o instanceof ClusterOrderEntry)) {
      return false;
    }

    final ClusterOrderEntry<?> that = (ClusterOrderEntry<?>) o;
    // Compare by ID only, for UpdatableHeap!
    return DBIDUtil.equal(objectID, that.getID());
  }

  /**
   * Returns a hash code value for the object.
   * 
   * NOTE: for the use in an UpdatableHeap, only the ID is used! This is
   * important, otherwise OPTICS will not work.
   * 
   * @return the object id if this entry
   */
  @Override
  public final int hashCode() {
    return objectID.hashCode();
  }

  @Override
  public int compareTo(SELF other) {
    if(this.correlationValue < other.correlationValue) {
      return -1;
    }
    if(this.correlationValue > other.correlationValue) {
      return +1;
    }
    return Double.compare(this.euclideanValue, other.euclideanValue);
  }

  @Override
  public DBID getID() {
    return objectID;
  }

  @Override
  public DBID getPredecessorID() {
    return predecessorID;
  }

  /**
   * Get the correlation dimensionality.
   * 
   * @return Correlation dimensionality
   */
  public int getCorrelationValue() {
    return correlationValue;
  }

  /**
   * Get the Euclidean distance in the orthogonal space.
   * 
   * @return Euclidean distance
   */
  public double getEuclideanValue() {
    return euclideanValue;
  }

  @Override
  public void writeToText(TextWriterStream out, String label) {
    out.inlinePrint("predecessor=" + DBIDUtil.toString((DBIDRef) predecessorID));
    out.inlinePrint("reach-dim=" + correlationValue);
    out.inlinePrint("reachability=" + euclideanValue);
  }
}