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

net.sourceforge.cilib.entity.topologies.HypercubeTopology Maven / Gradle / Ivy

Go to download

A library of composable components enabling simpler Computational Intelligence

There is a newer version: 0.8
Show newest version
/**           __  __
 *    _____ _/ /_/ /_    Computational Intelligence Library (CIlib)
 *   / ___/ / / / __ \   (c) CIRG @ UP
 *  / /__/ / / / /_/ /   http://cilib.net
 *  \___/_/_/_/_.___/
 */
package net.sourceforge.cilib.entity.topologies;

import com.google.common.collect.UnmodifiableIterator;
import java.util.Iterator;
import java.util.NoSuchElementException;
import net.sourceforge.cilib.controlparameter.ConstantControlParameter;
import net.sourceforge.cilib.entity.Entity;

/**
 * @param  The {@linkplain Entity} type.
 */
public class HypercubeTopology extends AbstractTopology {
    private static final long serialVersionUID = -8328600903928335004L;

    /**
     * Default constructor.
     */
    public HypercubeTopology() {
        this.neighbourhoodSize = ConstantControlParameter.of(5);
    }

    /**
     * Copy constructor.
     */
    public HypercubeTopology(HypercubeTopology copy) {
        super(copy);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public HypercubeTopology getClone() {
        return new HypercubeTopology(this);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected Iterator neighbourhoodOf(final E e) {
        return new UnmodifiableIterator() {

            private int count = 0;
            private int index = entities.indexOf(e);

            @Override
            public E next() {
                if (!hasNext()) {
                    throw new NoSuchElementException();
                }

                return entities.get(index ^ Double.valueOf(Math.pow(2, count++)).intValue());
            }

            @Override
            public boolean hasNext() {
                return count < getNeighbourhoodSize();
            }
        };
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy