Example usage for org.apache.commons.math3.distribution CauchyDistribution CauchyDistribution

List of usage examples for org.apache.commons.math3.distribution CauchyDistribution CauchyDistribution

Introduction

In this page you can find the example usage for org.apache.commons.math3.distribution CauchyDistribution CauchyDistribution.

Prototype

public CauchyDistribution(double median, double scale, double inverseCumAccuracy) 

Source Link

Document

Creates a Cauchy distribution using the given median and scale.

Usage

From source file:com.vsthost.rnd.jdeoptim.evolution.strategies.SandboxStrategy.java

@Override
public void regenerate(Population population, Problem problem, Objective objective) {
    // Get the best member of the population:
    final double[] bestMember = population.getBestMember();

    // Define the new population data and scores as we don't want to override old one within the loop:
    final double[][] newPopData = new double[population.getSize()][];
    final double[] newPopScores = new double[population.getSize()];
    final boolean[] newPopFlags = new boolean[population.getSize()];

    // Iterate over the current population:
    for (int c = 0; c < population.getSize(); c++) {
        // Are we going to adjust CR and F?
        if (this.c > 0) {
            // Yes. We will not adjust the CR first:
            this.cr = new NormalDistribution(this.randomGenerator, this.meanCR, 0.1).sample();

            // Check and reset CR:
            this.cr = this.cr > 1 ? 1 : (this.cr < 0 ? 0 : this.cr);

            // OK, now we will adjust F:
            do {//from w  w  w .  j a v  a 2s. co  m
                // Get the new F:
                this.f = new CauchyDistribution(this.randomGenerator, this.meanF, 0.1).sample();

                // Check and reset F if required:
                this.f = this.f > 1 ? 1 : this.f;
            } while (this.f <= 0);
        }

        // Get the candidate as the base of the next candidate (a.k.a. trial):
        final double[] trial = population.getMemberCopy(c);

        // Get the score of the candidate:
        final double oldScore = population.getScore(c);

        // Get 2 random member indices from the population which are distinct:
        int[] randomMembers = Utils.pickRandom(Utils.sequence(population.getSize()), 2, new int[] { c },
                this.randomGenerator);

        // Get the random members:
        final double[] randomMember1 = population.getMember(randomMembers[0]);
        final double[] randomMember2 = population.getMember(randomMembers[1]);

        // Iterate over all member elements and do the trick:
        for (int i = 0; i < population.getDimension(); i++) {
            // Any manipulation?
            if (probability.sample() < this.cr) {
                // Yes, we will proceed with a change:
                trial[i] = bestMember[i]
                        + this.f * (probability.sample() + 0.0001) * (randomMember1[i] - randomMember2[i]);
            }
        }

        // Apply limits in case that we have violated:
        for (int i = 0; i < trial.length; i++) {
            // Check lower limit:
            if (trial[i] < problem.getLower()[i]) {
                trial[i] = problem.getLower()[i];
            }
            // Check upper limit:
            else if (trial[i] > problem.getUpper()[i]) {
                trial[i] = problem.getUpper()[i];
            }
        }

        // OK, we are done with the trial. We will now check if we have a
        // better candidate. If yes, we will replace the old member with the trial,
        // if not we will just skip. Compute the score:
        final double newScore = objective.apply(trial);

        // Check the new score against the old one and act accordingly:
        if (newScore < oldScore) {
            // Yes, our trial is a better candidate. Replace:
            newPopData[c] = trial;
            newPopScores[c] = newScore;
            newPopFlags[c] = true;

            // We will now re-adjust for CR and F.
            this.goodCR += this.cr / ++this.goodNPCount;
            this.goodF += this.f;
            this.goodF2 += Math.pow(this.f, 2);
        } else {
            newPopFlags[c] = false;
        }

        // Re-compute mean CR and F if required:
        if (this.c > 0 && this.goodF != 0) {
            this.meanCR = (1 - this.c) * this.meanCR + this.c * this.goodCR;
            this.meanF = (1 - this.c) * this.meanF + this.c * this.goodF2 / this.goodF;
        }
    }

    // Now, override the population:
    for (int i = 0; i < population.getSize(); i++) {
        if (newPopFlags[i]) {
            population.setMember(i, newPopData[i], newPopScores[i]);
        }
    }
}

From source file:com.vsthost.rnd.jdeoptim.evolution.strategies.Strategy3.java

@Override
public void regenerate(Population population, Problem problem, Objective objective) {
    // Setup a uniform integer distribution for candidate element selection:
    UniformIntegerDistribution elementSampling = new UniformIntegerDistribution(this.randomGenerator, 0,
            population.getDimension() - 1);

    // Get the best member of the population:
    final double[] bestMember = population.getBestMember();

    // Define the new population data and scores as we don't want to override old one within the loop:
    final double[][] newPopData = new double[population.getSize()][];
    final double[] newPopScores = new double[population.getSize()];
    final boolean[] newPopFlags = new boolean[population.getSize()];

    // Iterate over the current population:
    for (int c = 0; c < population.getSize(); c++) {
        // Are we going to adjust CR and F?
        if (this.c > 0) {
            // Yes. We will not adjust the CR first:
            this.cr = new NormalDistribution(this.randomGenerator, this.meanCR, 0.1).sample();

            // Check and reset CR:
            this.cr = this.cr > 1 ? 1 : (this.cr < 0 ? 0 : this.cr);

            // OK, now we will adjust F:
            do {/*w w w.  j  av a  2 s  .c  o m*/
                // Get the new F:
                this.f = new CauchyDistribution(this.randomGenerator, this.meanF, 0.1).sample();

                // Check and reset F if required:
                this.f = this.f > 1 ? 1 : this.f;
            } while (this.f <= 0.0);
        }

        // Get the candidate as the base of the next candidate (a.k.a. trial):
        final double[] trial = population.getMemberCopy(c);

        // Get the score of the candidate:
        final double oldScore = population.getScore(c);

        // Get two random candidate indices:
        final int[] randomCandidates = Utils.pickRandom(Utils.sequence(population.getSize()), 2,
                new int[] { c }, this.randomGenerator);

        // Get the index of element of candidate to start with:
        int j = elementSampling.sample();

        // Set the counter for preventing overflowing dimension:
        int k = 0;

        // Iterate and set elements:
        do {
            // Get the jitter:
            final double jitter = (probability.sample() * this.jitterFactor) + this.f;

            // Get the respective element of the best candidate:
            final double bestest = bestMember[j];

            // Get the random candidate elements:
            final double random1 = population.getMember(randomCandidates[0])[j];
            final double random2 = population.getMember(randomCandidates[1])[j];

            // Override trial:
            trial[j] = bestest + jitter * (random1 - random2);

            // Move to the next element:
            j = (j + 1) % population.getDimension();

            // Increment k:
            k++;
        } while (probability.sample() < this.cr && k < population.getDimension());

        // We have an interim trial. We will now truncate:
        for (int i = 0; i < trial.length; i++) {
            // Are we truncating?
            if (precision == 0) {
                continue;
            }

            // OK, truncate:
            trial[i] = DMatrixUtils.roundDoubleToClosest(trial[i], this.precision);
        }

        // Apply limits in case that we have violated:
        for (int i = 0; i < trial.length; i++) {
            // Check lower limit:
            if (trial[i] < problem.getLower()[i]) {
                if (this.bounceBack) {
                    trial[i] = problem.getLower()[i]
                            + probability.sample() * (problem.getUpper()[i] - problem.getLower()[i]);
                } else {
                    trial[i] = problem.getLower()[i];
                }
            }

            // Check upper limit:
            if (trial[i] > problem.getUpper()[i]) {
                if (this.bounceBack) {
                    trial[i] = problem.getUpper()[i]
                            - probability.sample() * (problem.getUpper()[i] - problem.getLower()[i]);
                } else {
                    trial[i] = problem.getUpper()[i];
                }
            }
        }

        // We will now check if we have a
        // better candidate. If yes, we will replace the old member with the trial,
        // if not we will just skip. Compute the score:
        final double newScore = objective.apply(trial);

        // Check the new score against the old one and act accordingly:
        if (newScore < oldScore) {
            // Yes, our trial is a better candidate. Replace:
            newPopData[c] = trial;
            newPopScores[c] = newScore;
            newPopFlags[c] = true;

            // We will now re-adjust for CR and F.
            this.goodCR += this.cr / ++this.goodNPCount;
            this.goodF += this.f;
            this.goodF2 += Math.pow(this.f, 2);
        } else {
            newPopFlags[c] = false;
        }
    }

    // Re-compute mean CR and F if required:
    if (this.c > 0 && this.goodF != 0) {
        this.meanCR = (1 - this.c) * this.meanCR + this.c * this.goodCR;
        this.meanF = (1 - this.c) * this.meanF + this.c * this.goodF2 / this.goodF;
    }

    // Now, override the population:
    for (int i = 0; i < population.getSize(); i++) {
        if (newPopFlags[i]) {
            population.setMember(i, newPopData[i], newPopScores[i]);
        }
    }
}

From source file:io.coala.random.impl.RandomDistributionFactoryImpl.java

@Override
public RandomNumberDistribution<Double> getCauchy(final RandomNumberStream rng, final Number median,
        final Number scale) {
    final RealDistribution dist = new CauchyDistribution(RandomNumberStream.Util.asCommonsRandomGenerator(rng),
            median.doubleValue(), scale.doubleValue());
    return new RandomNumberDistribution<Double>() {
        @Override//from  ww  w .j  a  v a 2  s .  co m
        public Double draw() {
            return dist.sample();
        }
    };
}