Clusterer.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      https://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.clustering;

  22. import java.util.Collection;
  23. import java.util.List;

  24. import org.hipparchus.clustering.distance.DistanceMeasure;
  25. import org.hipparchus.exception.MathIllegalArgumentException;
  26. import org.hipparchus.exception.MathIllegalStateException;

  27. /**
  28.  * Base class for clustering algorithms.
  29.  *
  30.  * @param <T> the type of points that can be clustered
  31.  */
  32. public abstract class Clusterer<T extends Clusterable> {

  33.     /** The distance measure to use. */
  34.     private DistanceMeasure measure;

  35.     /**
  36.      * Build a new clusterer with the given {@link DistanceMeasure}.
  37.      *
  38.      * @param measure the distance measure to use
  39.      */
  40.     protected Clusterer(final DistanceMeasure measure) {
  41.         this.measure = measure;
  42.     }

  43.     /**
  44.      * Perform a cluster analysis on the given set of {@link Clusterable} instances.
  45.      *
  46.      * @param points the set of {@link Clusterable} instances
  47.      * @return a {@link List} of clusters
  48.      * @throws MathIllegalArgumentException if points are null or the number of
  49.      *   data points is not compatible with this clusterer
  50.      * @throws MathIllegalStateException if the algorithm has not yet converged after
  51.      *   the maximum number of iterations has been exceeded
  52.      */
  53.     public abstract List<? extends Cluster<T>> cluster(Collection<T> points)
  54.             throws MathIllegalArgumentException, MathIllegalStateException;

  55.     /**
  56.      * Returns the {@link DistanceMeasure} instance used by this clusterer.
  57.      *
  58.      * @return the distance measure
  59.      */
  60.     public DistanceMeasure getDistanceMeasure() {
  61.         return measure;
  62.     }

  63.     /**
  64.      * Calculates the distance between two {@link Clusterable} instances
  65.      * with the configured {@link DistanceMeasure}.
  66.      *
  67.      * @param p1 the first clusterable
  68.      * @param p2 the second clusterable
  69.      * @return the distance between the two clusterables
  70.      */
  71.     protected double distance(final Clusterable p1, final Clusterable p2) {
  72.         return measure.compute(p1.getPoint(), p2.getPoint());
  73.     }

  74. }