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.math4.legacy.ml.clustering;

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

  20. import org.apache.commons.math4.legacy.ml.distance.DistanceMeasure;

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

  28.     /** The distance measure to use. */
  29.     private DistanceMeasure measure;

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

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

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

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