All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sourceforge.cilib.util.distancemeasure.ChebyshevDistanceMeasure Maven / Gradle / Ivy

/**           __  __
 *    _____ _/ /_/ /_    Computational Intelligence Library (CIlib)
 *   / ___/ / / / __ \   (c) CIRG @ UP
 *  / /__/ / / / /_/ /   http://cilib.net
 *  \___/_/_/_/_.___/
 */
package net.sourceforge.cilib.util.distancemeasure;

import java.util.Collection;
import java.util.Iterator;
import net.sourceforge.cilib.type.types.Numeric;

/**
 * Chebyshev Distance is a special case of the {@link MinkowskiMetric} with
 * 'alpha' := infinity. It calculates the distance between to vectors as the
 * largest coordinate difference between both vectors.
 */
public class ChebyshevDistanceMeasure extends MinkowskiMetric {

    /**
     * Create an instance of the {@linkplain ChebyshevDistanceMeasure}.
     */
    public ChebyshevDistanceMeasure() {
        // alpha cannot be directly instantiated to infinity :-)
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public double distance(Collection x, Collection y) {
        /*
         * TODO: Consider re-implementing for different sized vectors, especially as everything is
         * equivalent relative to infinity
         */
        if (x.size() != y.size()) {
            throw new IllegalArgumentException("Cannot calculate Chebyshev Metric for vectors of different dimensions");
        }

        Iterator xIterator = x.iterator();
        Iterator yIterator = y.iterator();

        double maxDistance = 0.0;
        for (int i = 0; i < x.size(); ++i) {
            Numeric xElement = (Numeric) xIterator.next();
            Numeric yElement = (Numeric) yIterator.next();

            double distance = Math.abs(xElement.doubleValue() - yElement.doubleValue());
            if (distance > maxDistance) {
                maxDistance = distance;
            }
        }

        return maxDistance;
    }

    /**
     * {@inheritDoc}
     */
    public void setAlpha(int a) {
        throw new IllegalArgumentException("The 'alpha' parameter of the Chebyshev Distance Measure cannot be set directly");
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy