001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.math3.ml.clustering;
018
019import java.util.Collection;
020import java.util.List;
021
022import org.apache.commons.math3.exception.ConvergenceException;
023import org.apache.commons.math3.exception.MathIllegalArgumentException;
024import org.apache.commons.math3.ml.distance.DistanceMeasure;
025
026/**
027 * Base class for clustering algorithms.
028 *
029 * @param <T> the type of points that can be clustered
030 * @since 3.2
031 */
032public abstract class Clusterer<T extends Clusterable> {
033
034    /** The distance measure to use. */
035    private DistanceMeasure measure;
036
037    /**
038     * Build a new clusterer with the given {@link DistanceMeasure}.
039     *
040     * @param measure the distance measure to use
041     */
042    protected Clusterer(final DistanceMeasure measure) {
043        this.measure = measure;
044    }
045
046    /**
047     * Perform a cluster analysis on the given set of {@link Clusterable} instances.
048     *
049     * @param points the set of {@link Clusterable} instances
050     * @return a {@link List} of clusters
051     * @throws MathIllegalArgumentException if points are null or the number of
052     *   data points is not compatible with this clusterer
053     * @throws ConvergenceException if the algorithm has not yet converged after
054     *   the maximum number of iterations has been exceeded
055     */
056    public abstract List<? extends Cluster<T>> cluster(Collection<T> points)
057            throws MathIllegalArgumentException, ConvergenceException;
058
059    /**
060     * Returns the {@link DistanceMeasure} instance used by this clusterer.
061     *
062     * @return the distance measure
063     */
064    public DistanceMeasure getDistanceMeasure() {
065        return measure;
066    }
067
068    /**
069     * Calculates the distance between two {@link Clusterable} instances
070     * with the configured {@link DistanceMeasure}.
071     *
072     * @param p1 the first clusterable
073     * @param p2 the second clusterable
074     * @return the distance between the two clusterables
075     */
076    protected double distance(final Clusterable p1, final Clusterable p2) {
077        return measure.compute(p1.getPoint(), p2.getPoint());
078    }
079
080}