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

net.sourceforge.cilib.util.selection.recipes.DistanceBasedElitistSelector Maven / Gradle / Ivy

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

import java.util.Comparator;
import net.sourceforge.cilib.util.selection.PartialSelection;
import net.sourceforge.cilib.util.selection.Selection;
import net.sourceforge.cilib.util.selection.arrangement.DistanceComparator;
import net.sourceforge.cilib.util.selection.arrangement.ReverseArrangement;
import net.sourceforge.cilib.util.selection.arrangement.SortedArrangement;

/**
 * This class is similar to {@link ElitistSelector}, but where a
 * {@link DistanceComparator} is used. This class is necessary to select a
 * different comparator than the default one, due to the way in which the
 * archive has been implemented.
 *
 * @param  The selection type.
 */
public class DistanceBasedElitistSelector> implements Selector {

    private static final long serialVersionUID = -5432603299031620114L;
    private Comparator comparator;

    /**
     * Create a new instance with a defined comparator being
     * {@link DistanceComparator}.
     */
    public DistanceBasedElitistSelector() {
        this.comparator = new DistanceComparator();
    }

    /**
     * Create a new instance with the provided {@link Comparator}.
     *
     * @param comparator The comparator to set.
     */
    public DistanceBasedElitistSelector(Comparator comparator) {
        this.comparator = comparator;
    }

    /**
     * Create a copy of the provided instance.
     *
     * @param copy The instance to copy.
     */
    public DistanceBasedElitistSelector(ElitistSelector copy) {
        this.comparator = copy.getComparator();
    }

    /**
     * Set the comparator to be used.
     *
     * @param comparator The value to set.
     */
    public void setComparator(Comparator comparator) {
        this.comparator = comparator;
    }

    /**
     * Get the current comparator.
     *
     * @return The current comparator instance.
     */
    public Comparator getComparator() {
        return this.comparator;
    }

    @Override
    public PartialSelection on(Iterable iterable) {
        return Selection.copyOf(iterable).orderBy(new SortedArrangement())
                .orderBy(new ReverseArrangement());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy