Example usage for org.apache.commons.math.random RandomData nextGaussian

List of usage examples for org.apache.commons.math.random RandomData nextGaussian

Introduction

In this page you can find the example usage for org.apache.commons.math.random RandomData nextGaussian.

Prototype

double nextGaussian(double mu, double sigma);

Source Link

Document

Generates a random value from the Normal (or Gaussian) distribution with the given mean and standard deviation.

Usage

From source file:br.upe.ecomp.doss.algorithm.apso.APSO.java

protected void doElitistLearningStrategy() {
    RandomData random = new RandomDataImpl();
    APSOParticle[] particles = (APSOParticle[]) getParticles();

    List<APSOParticle> apsoParticles = Arrays.asList(particles);
    APSOParticle bestParticle = Collections.max(apsoParticles, new ComparatorMaximumFitness());

    // Dimension that can be changed if the best particle fitness improves after the change
    int dimensionChange = random.nextInt(0, getProblem().getDimensionsNumber() - 1);
    double[] currentBestPosition = bestParticle.getBestPosition();

    // Standard Deviation of the Gaussian Distribution
    // TODO usar esse decaimento linear pode ser ruim para ambientes dinamicos
    double learningElitistRate = sigmaMax - (sigmaMax - sigmaMin) * (this.getIterations() / getMaxIterations());

    // Changes position in only one dimension
    // if(EnumEvolutionaryState.CONVERGENCE) TODO testar com e sem esta condicao
    double newDimensionPosition = currentBestPosition[dimensionChange]
            + (getProblem().getUpperBound(dimensionChange) - getProblem().getLowerBound(dimensionChange))
                    * random.nextGaussian(0, learningElitistRate);

    // TODO verificar se nao extrapolou o limite do espaco de busca
    double[] newPosition = currentBestPosition.clone();
    newPosition[dimensionChange] = newDimensionPosition;

    double currentBestFitness = bestParticle.getBestFitness();
    double newFitness = getProblem().getFitness(newPosition);

    // If the change improves the fitness then update the best particle with the new position,
    // else update the worst particle with the new position.
    if (getProblem().isFitnessBetterThan(currentBestFitness, newFitness)) {
        bestParticle.updateBestPosition(newPosition.clone(), newFitness);
        bestParticle.updateCurrentPosition(newPosition.clone(), newFitness);
    } else {/*from   w  ww.  j av a  2s . c  o  m*/
        PSOParticle worstParticle = Collections.min(apsoParticles, new ComparatorMaximumFitness());
        worstParticle.updateCurrentPosition(newPosition.clone(), newFitness);
        worstParticle.updatePBest(getProblem());
    }
    calculateGBest(bestParticle);
}

From source file:org.tolven.gen.model.GaussianValueGen.java

public double generate(RandomData rng) {
    return rng.nextGaussian(mean, sigma);
}

From source file:org.tolven.gen.model.RepeatScenario.java

/**
 * To apply this scenario, we will modify the start time of the scenario to repeat to
 * be the selected event time. For example, if the repeat interval is one year (in milliseconds), 
 * and apply is called with a start time around 1992, then the child scenario is called
 * with a start time of 1977, 1978, to the end-time (present day or death being most common
 * for chronic diseases or the duration of the disease for acute diseases).
 * Since most repeat scenarios represent followup activity, the start date is often offset 
 * by some interval. Thus, with a startOffset of 12 (months), and the 
 * startDate were to be 1976, then the first "repeat" will be around 1977. 
 *///from w  ww.  j av a2s  .  c om
public boolean apply(GenMedical patient, Date startTime, Date endTime, RandomData rng) {
    GregorianCalendar clock = new GregorianCalendar();
    clock.setTime(startTime);
    boolean rslt = false;
    int count = -startOffset;
    //      TolvenLogger.info( getTitle() + " of " + getScenarioToRepeat().getTitle() + " Ranging from " + clock.getTime() + " to " + endTime, RepeatScenario.class); 
    while (true) {
        if (count++ >= MAX_REPEAT)
            throw new IllegalArgumentException("Maximum repeat for scenario " + getTitle());
        if (count > maxCount)
            break;
        // Now pick a random time around that clock setting unless this is the first instance or there is no deviation
        Date instanceTime;
        if (getDeviation() == 0.0 || count <= 0) {
            instanceTime = new Date(clock.getTimeInMillis());
        } else {
            instanceTime = new Date(((long) rng.nextGaussian(clock.getTimeInMillis(), getDeviationMs())));
        }
        // Needs to be before the end time, otherwise, we're done.
        if (instanceTime.after(endTime))
            break;
        // Now apply the instance (if we're past the startOffset)
        if (count > 0) {
            getScenarioToRepeat().apply(patient, instanceTime, endTime, rng);
        }
        // Reset the clock if requested
        if (resetClock)
            clock.setTime(instanceTime);
        // Advance the clock with no randomness
        clock.add(intervalType, intervalValue);
        rslt = true;
    }
    // If there's a future count, then we keep going by calling the futureScenario
    // Note: Logic is similar but not identical to above.
    for (int x = 0; x < getFutureCount(); x++) {
        Date instanceTime;
        if (getDeviation() == 0.0) {
            instanceTime = new Date(clock.getTimeInMillis());
        } else {
            instanceTime = new Date(((long) rng.nextGaussian(clock.getTimeInMillis(), getDeviationMs())));
        }
        // 
        // In the future, start and end time are the same.
        getFutureScenario().apply(patient, instanceTime, instanceTime, rng);
        // Reset the clock if requested
        if (resetClock)
            clock.setTime(instanceTime);
        // Advance the clock with no randomness
        clock.add(intervalType, intervalValue);
    }
    return rslt;
}