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.  *      http://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. package org.apache.commons.math3.ml.clustering;

  18. import java.util.Collection;
  19. import java.util.List;

  20. import org.apache.commons.math3.exception.ConvergenceException;
  21. import org.apache.commons.math3.exception.MathIllegalArgumentException;
  22. import org.apache.commons.math3.ml.distance.DistanceMeasure;

  23. /**
  24.  * Base class for clustering algorithms.
  25.  *
  26.  * @param <T> the type of points that can be clustered
  27.  * @since 3.2
  28.  */
  29. public abstract class Clusterer<T extends Clusterable> {

  30.     /** The distance measure to use. */
  31.     private DistanceMeasure measure;

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

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

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

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

  71. }