    1   package baseCode.algorithm.learning.unsupervised.cluster;
    2
    3   import java.util.Collection;
    4   import java.util.Vector;
    5
    6   import junit.framework.TestCase;
    7   import baseCode.algorithm.learning.unsupervised.cluster.Cluster;
    8   import baseCode.algorithm.learning.unsupervised.cluster.DistanceFactory;
    9   import baseCode.algorithm.learning.unsupervised.cluster.Distancer;
    10  import baseCode.algorithm.learning.unsupervised.cluster.HierarchicalClusterer;
    11  import baseCode.common.Distanceable;
    12  import cern.colt.list.ObjectArrayList;
    13
    14  /***
    15   * <hr>
    16   * <p>
    17   * Copyright (c) 2004 Columbia University
    18   *
    19   * @author pavlidis
    20   * @version $Id: HierarchicalClustererTest.java,v 1.1 2005/03/17 13:58:43 pavlidis Exp $
    21   */
    22  public class HierarchicalClustererTest extends TestCase {
    23
    24
    25     HierarchicalClusterer foo;
    26     ObjectArrayList testData;
    27
    28     /*
    29      * @see TestCase#setUp()
    30      */
    31     protected void setUp() throws Exception {
    32        super.setUp();
    33         testData = new ObjectArrayList(new DoubleDistanceable[]{
    34        new DoubleDistanceable(5),
    35        new DoubleDistanceable(4),
    36        new DoubleDistanceable(5),
    37        new DoubleDistanceable(4),
    38        new DoubleDistanceable(5),
    39        new DoubleDistanceable(4),
    40        new DoubleDistanceable(5),
    41        new DoubleDistanceable(4),
    42        new DoubleDistanceable(8),
    43        new DoubleDistanceable(3),
    44        new DoubleDistanceable(2),
    45        new DoubleDistanceable(1)
    46         });
    47
    48        foo = new HierarchicalClusterer(testData);
    49     }
    50
    51     public void testRun() {
    52        foo.run();
    53        Cluster[] expectedResults = new Cluster[]{
    54              new DoubleDistanceable(8),
    55              new DoubleDistanceable(5),
    56              new DoubleDistanceable(5),
    57              new DoubleDistanceable(5),
    58              new DoubleDistanceable(5),
    59              new DoubleDistanceable(4),
    60              new DoubleDistanceable(4),
    61              new DoubleDistanceable(4),
    62              new DoubleDistanceable(4),
    63              new DoubleDistanceable(3),
    64              new DoubleDistanceable(2),
    65              new DoubleDistanceable(1)
    66
    67        };
    68
    69        DoubleDistanceable[] actualResults = ( DoubleDistanceable[] ) foo.getResults();
    70
    71        assertEquals(actualResults, expectedResults);
    72     }
    73
    74  }
    75
    76
    77  ///*** helpers **
    78
    79  class DoubleDistanceable extends Cluster {
    80
    81     Double value;
    82
    83     /**
    84      * @param value
    85      */
    86     public DoubleDistanceable( double value ) {
    87        this.value = new Double(value);
    88        Vector v = new Vector();
    89        v.add(this.value);
    90        this.items = v;
    91        this.distAlg = new DoubleDifferenceFactory().getDistancer();
    92     }
    93
    94     public static int compare( double d1, double d2 ) {
    95        return Double.compare( d1, d2 );
    96     }
    97
    98     public static long doubleToLongBits( double value ) {
    99        return Double.doubleToLongBits( value );
    100    }
    101
    102    public static long doubleToRawLongBits( double value ) {
    103       return Double.doubleToRawLongBits( value );
    104    }
    105
    106    public static boolean isInfinite( double v ) {
    107       return Double.isInfinite( v );
    108    }
    109
    110    public static boolean isNaN( double v ) {
    111       return Double.isNaN( v );
    112    }
    113
    114    public static double longBitsToDouble( long bits ) {
    115       return Double.longBitsToDouble( bits );
    116    }
    117
    118    public static double parseDouble( String s ) throws NumberFormatException {
    119       return Double.parseDouble( s );
    120    }
    121
    122    public static String toString( double d ) {
    123       return Double.toString( d );
    124    }
    125
    126    public static Double valueOf( String s ) throws NumberFormatException {
    127       return Double.valueOf( s );
    128    }
    129
    130    public byte byteValue() {
    131       return value.byteValue();
    132    }
    133
    134    public int compareTo( Double anotherDouble ) {
    135       return value.compareTo( anotherDouble );
    136    }
    137
    138    public int compareTo( Object o ) {
    139       return value.compareTo( o );
    140    }
    141
    142    public double doubleValue() {
    143       return value.doubleValue();
    144    }
    145
    146    public boolean equals( Object obj ) {
    147       return value.equals( obj );
    148    }
    149
    150    public float floatValue() {
    151       return value.floatValue();
    152    }
    153
    154    public int hashCode() {
    155       return value.hashCode();
    156    }
    157
    158    public int intValue() {
    159       return value.intValue();
    160    }
    161
    162    public boolean isInfinite() {
    163       return value.isInfinite();
    164    }
    165
    166    public boolean isNaN() {
    167       return value.isNaN();
    168    }
    169
    170    public long longValue() {
    171       return value.longValue();
    172    }
    173
    174    public short shortValue() {
    175       return value.shortValue();
    176    }
    177
    178    public String toString() {
    179       return value.toString();
    180    }
    181
    182    /* (non-Javadoc)
    183     * @see baseCode.algorithm.learning.unsupervised.cluster.Visitable#isVisited()
    184     */
    185
    186    private boolean visited;
    187
    188    public boolean isVisited() {
    189       return visited;
    190    }
    191
    192    /* (non-Javadoc)
    193     * @see baseCode.algorithm.learning.unsupervised.cluster.Visitable#mark()
    194     */
    195    public void mark() {
    196       visited = true;
    197    }
    198
    199    /* (non-Javadoc)
    200     * @see baseCode.algorithm.learning.unsupervised.cluster.Visitable#unMark()
    201     */
    202    public void unMark() {
    203       visited = false;
    204    }
    205
    206    /* (non-Javadoc)
    207     * @see baseCode.common.Distanceable#toCollection()
    208     */
    209    public Collection toCollection() {
    210       Collection ret =  new Vector();
    211       ret.add(this);
    212       return ret;
    213    }
    214 }
    215
    216 class DoubleDifference implements Distancer {
    217
    218    /*
    219     * (non-Javadoc)
    220     *
    221     * @see baseCode.algorithm.learning.unsupervised.cluster.Distancer#distance(java.lang.Object, java.lang.Object)
    222     */
    223    public double distance( Distanceable a, Distanceable b ) {
    224       DoubleDistanceable ad = ( DoubleDistanceable ) a;
    225       DoubleDistanceable bd = ( DoubleDistanceable ) b;
    226       return Math.abs( ad.value.doubleValue() - bd.value.doubleValue() );
    227    }
    228
    229
    230 }
    231
    232 class DoubleDifferenceFactory extends DistanceFactory {
    233
    234    private DoubleDifference d;
    235
    236    public DoubleDifferenceFactory() {
    237       d = new DoubleDifference();
    238    }
    239
    240    public Distancer getDistancer() {
    241       return d;
    242    }
    243
    244 }
    245