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.math4.legacy.ml.clustering;
018
019import java.util.Collection;
020import java.util.List;
021
022import org.apache.commons.math4.legacy.ml.distance.DistanceMeasure;
023
024/**
025 * Base class for clustering algorithms.
026 *
027 * @param <T> the type of points that can be clustered
028 * @since 3.2
029 */
030public abstract class Clusterer<T extends Clusterable> {
031
032    /** The distance measure to use. */
033    private DistanceMeasure measure;
034
035    /**
036     * Build a new clusterer with the given {@link DistanceMeasure}.
037     *
038     * @param measure the distance measure to use
039     */
040    protected Clusterer(final DistanceMeasure measure) {
041        this.measure = measure;
042    }
043
044    /**
045     * Perform a cluster analysis on the given set of {@link Clusterable} instances.
046     *
047     * @param points the set of {@link Clusterable} instances
048     * @return a {@link List} of clusters
049     * @throws IllegalArgumentException if points are null or the number of
050     * data points is not compatible with this clusterer.
051     * @throws org.apache.commons.math4.legacy.exception.ConvergenceException
052     * if the algorithm has not yet converged after the maximum number of
053     * iterations has been exceeded.
054     */
055    public abstract List<? extends Cluster<T>> cluster(Collection<T> points);
056
057    /**
058     * Returns the {@link DistanceMeasure} instance used by this clusterer.
059     *
060     * @return the distance measure
061     */
062    public DistanceMeasure getDistanceMeasure() {
063        return measure;
064    }
065
066    /**
067     * Calculates the distance between two {@link Clusterable} instances
068     * with the configured {@link DistanceMeasure}.
069     *
070     * @param p1 the first clusterable
071     * @param p2 the second clusterable
072     * @return the distance between the two clusterables
073     */
074    protected double distance(final Clusterable p1, final Clusterable p2) {
075        return measure.compute(p1.getPoint(), p2.getPoint());
076    }
077}