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

net.sourceforge.cilib.pso.dynamic.responsestrategies.ReinitialisationReactionStrategy Maven / Gradle / Ivy

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

import java.util.List;

import fj.F;

import net.sourceforge.cilib.algorithm.population.SinglePopulationBasedAlgorithm;
import net.sourceforge.cilib.entity.Entity;
import net.sourceforge.cilib.math.random.generator.Rand;
import net.sourceforge.cilib.pso.particle.Particle;

/**
 * This reaction strategy reinitialises the specified
 * {@link #setReinitialisationRatio(double) ratio} of randomly chosen entities in the given
 * {@link Topology}.
 *
 * @param  some {@link PopulationBasedAlgorithm population based algorithm}
 */
public class ReinitialisationReactionStrategy extends EnvironmentChangeResponseStrategy {
    private static final long serialVersionUID = -7283513652737895281L;

    protected double reinitialisationRatio = 0.0;

    public ReinitialisationReactionStrategy() {
        reinitialisationRatio = 0.1;
    }

    public ReinitialisationReactionStrategy(ReinitialisationReactionStrategy rhs) {
        super(rhs);
        reinitialisationRatio = rhs.reinitialisationRatio;
    }

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

    /**
     * Reinitialise the {@link Entity entities} inside the topology.
     *
     * {@inheritDoc}
     */
    @Override
	protected 

> void performReaction( A algorithm) { fj.data.List

entities = algorithm.getTopology(); int reinitialiseCount = (int) Math.floor(reinitialisationRatio * entities.length()); reinitialise(entities, reinitialiseCount); } /** * Reinitialise a specified number of the given entities. * * @param entities a {@link List} of entities that should be considered for * reinitialisation * @param reinitialiseCount anint specifying how many entities should be * reinitialised */ protected

void reinitialise(final fj.data.List

entities, int reinitialiseCount) { fj.data.List

local = entities; for (int i = 0; i < reinitialiseCount; i++) { int random = Rand.nextInt(entities.length()); final Entity entity = entities.index(random); entity.getCandidateSolution().randomise(); // remove the selected element from the all list preventing it from being selected again local = local.filter(new F() { @Override public Boolean f(P element) { return (element.equals(entity)) ? false : true; } }); } } /** * Set the ratio of entities that should be reinitialised. * * @param rr a double value in the range (0.0, 1.0) * @throws {@link IllegalArgumentException} when the ratio is not within the above * mentioned range */ public void setReinitialisationRatio(double rr) { if (rr < 0.0 || rr > 1.0) { throw new IllegalArgumentException("The reinitialisationRatio must be in the range (0.0, 1.0)"); } reinitialisationRatio = rr; } /** * Get the ratio of entities that should be reinitialised. * * @return the ratio of entities that should be reinitialised */ public double getReinitialisationRatio() { return reinitialisationRatio; } }