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 * @version $Id: Clusterer.html 885258 2013-11-03 02:46:49Z tn $
031 * @since 3.2
032 */
033public abstract class Clusterer<T extends Clusterable> {
034
035    /** The distance measure to use. */
036    private DistanceMeasure measure;
037
038    /**
039     * Build a new clusterer with the given {@link DistanceMeasure}.
040     *
041     * @param measure the distance measure to use
042     */
043    protected Clusterer(final DistanceMeasure measure) {
044        this.measure = measure;
045    }
046
047    /**
048     * Perform a cluster analysis on the given set of {@link Clusterable} instances.
049     *
050     * @param points the set of {@link Clusterable} instances
051     * @return a {@link List} of clusters
052     * @throws MathIllegalArgumentException if points are null or the number of
053     *   data points is not compatible with this clusterer
054     * @throws ConvergenceException if the algorithm has not yet converged after
055     *   the maximum number of iterations has been exceeded
056     */
057    public abstract List<? extends Cluster<T>> cluster(Collection<T> points)
058            throws MathIllegalArgumentException, ConvergenceException;
059
060    /**
061     * Returns the {@link DistanceMeasure} instance used by this clusterer.
062     *
063     * @return the distance measure
064     */
065    public DistanceMeasure getDistanceMeasure() {
066        return measure;
067    }
068
069    /**
070     * Calculates the distance between two {@link Clusterable} instances
071     * with the configured {@link DistanceMeasure}.
072     *
073     * @param p1 the first clusterable
074     * @param p2 the second clusterable
075     * @return the distance between the two clusterables
076     */
077    protected double distance(final Clusterable p1, final Clusterable p2) {
078        return measure.compute(p1.getPoint(), p2.getPoint());
079    }
080
081}