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

net.sourceforge.cilib.measurement.entropy.DimensionalEntropyMeasurement Maven / Gradle / Ivy

Go to download

A library of composable components enabling simpler Computational Intelligence

The newest version!
/**           __  __
 *    _____ _/ /_/ /_    Computational Intelligence Library (CIlib)
 *   / ___/ / / / __ \   (c) CIRG @ UP
 *  / /__/ / / / /_/ /   http://cilib.net
 *  \___/_/_/_/_.___/
 */
package net.sourceforge.cilib.measurement.entropy;

import java.util.Iterator;
import net.sourceforge.cilib.algorithm.Algorithm;
import net.sourceforge.cilib.algorithm.population.SinglePopulationBasedAlgorithm;
import net.sourceforge.cilib.entity.Entity;
import net.sourceforge.cilib.measurement.Measurement;
import net.sourceforge.cilib.type.types.Real;
import net.sourceforge.cilib.type.types.container.TypeList;
import net.sourceforge.cilib.type.types.container.Vector;

/**
 * Calculates the entropy in each dimension of the search space, with respect
 * to the positions of the entities in the population.
 *
 */
public class DimensionalEntropyMeasurement extends EntropyMeasurement {

    public DimensionalEntropyMeasurement() {
        intervals = 5;
    }

    public DimensionalEntropyMeasurement(int i) {
        intervals = i;
    }

    public DimensionalEntropyMeasurement(DimensionalEntropyMeasurement copy) {
        this.intervals = copy.intervals;
    }

    @Override
    public Measurement getClone() {
        return new DimensionalEntropyMeasurement(this);
    }

    /**
     * Calculates an entropy value in each dimension of the search space.
     * For each dimension, the search space is divided into a number of
     * discrete intervals. Then, the probability that a given entity
     * will fall within a specific interval is calculated by counting the number
     * of entities in each interval and dividing this number by the size of
     * the population. The probabilities calculated for each interval are used
     * to determine the entropy in the specific dimension.
     *
     * @param algorithm The algorithm to perform the entropy measurement on.
     * @return An array of double values; the entropy measurements in each dimension of the search space.
     */
    @Override
    public TypeList getValue(Algorithm algorithm) {
        SinglePopulationBasedAlgorithm populationBasedAlgorithm = (SinglePopulationBasedAlgorithm) algorithm;

        int numberOfEntities = populationBasedAlgorithm.getTopology().length();
        Iterator populationIterator;

        int dimensions = populationBasedAlgorithm.getOptimisationProblem().getDomain().getDimension();

        Vector bounds = (Vector)populationBasedAlgorithm.getOptimisationProblem().getDomain().getBuiltRepresentation();
        Entity e;

        //keeps an entropy measurement for each dimension
        TypeList entropyMeasurements = new TypeList();

        for(int d = 0; d < dimensions; d++) {
            double dimensionLowerBound = bounds.get(d).getBounds().getLowerBound();
            double dimensionRange = bounds.get(d).getBounds().getRange();

            //keeps probabilities that a given particle will fall in each interval
            double[] probabilities = new double[intervals];

            for(int i = 0; i < intervals; i++) {
                int entityCount = 0; //number of entities in this interval

                double intervalLowerBound = dimensionLowerBound + i * (dimensionRange / intervals);
                double intervalUppderBound = intervalLowerBound + (dimensionRange / intervals);

                populationIterator = populationBasedAlgorithm.getTopology().iterator();

                while(populationIterator.hasNext()) {
                    e = populationIterator.next();
                    Vector entityPosition = (Vector) e.getCandidateSolution();
                    double position = entityPosition.get(d).doubleValue();

                    if(position <= intervalUppderBound && position >= intervalLowerBound) {
                        //The entity falls within the current interval in the current dimension
                        entityCount++;
                    }
                }

                //calculate probability that a particle falls within this interval
                double p = (double)entityCount / (double)numberOfEntities;
                probabilities[i] = p;
            }

            double entropy = 0.0;

            //calculate entropy for current dimension
            if(intervals == 1) {
                entropy = 0.0;
            } else {
                for(int i = 0; i < intervals; i++) {
                    double p = probabilities[i];
                    if(probabilities[i] > 0) { //equivalent to defining: 0 log 0 = 0
                        entropy += p * (Math.log(p) / Math.log(intervals));
                    }
                }
                entropy *= -1;
            }

            entropyMeasurements.append(Real.valueOf(entropy));
        }

        return entropyMeasurements;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy