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

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

Introduction

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

Prototype

double nextUniform(double lower, double upper);

Source Link

Document

Generates a uniformly distributed random value from the open interval (lower,upper) (i.e., endpoints excluded).

Usage

From source file:datafu.pig.hash.lsh.RepeatingLSH.java

public RepeatingLSH(List<LSH> lshList) throws MathException {
    super(lshList.get(0).getDim(), lshList.get(0).getRandomGenerator());
    this.lshList = lshList;
    RandomGenerator rg = lshList.get(0).getRandomGenerator();
    RandomData rd = new RandomDataImpl(rg);
    /*/*  w  ww . j a  va 2  s.  com*/
     * Compute a random vector of lshList.size() with each component taken from U(0,10)
     */
    randomVec = new ArrayRealVector(lshList.size());
    for (int i = 0; i < randomVec.getDimension(); ++i) {
        randomVec.setEntry(i, rd.nextUniform(0, 10.0));
    }
}

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

private void adaptParameters(double evolFactor) {
    RandomData random = new RandomDataImpl();
    double accelerationRate = random.nextUniform(0.05, 0.1);
    double c1 = getC1();
    double c2 = getC2();
    if (currentEvolutionaryState.equals(EnumEvolutionaryState.CONVERGENCE)) {
        // C1 - increase slightly | C2 increase slightly
        c1 = c1 + (c1 * accelerationRate * 0.5);
        c2 = c2 + (c2 * accelerationRate * 0.5);
    } else if (currentEvolutionaryState.equals(EnumEvolutionaryState.EXPLOITATION)) {
        // C1 - increase | C2 decrease
        c1 = c1 + (c1 * accelerationRate);
        c2 = c2 - (c2 * accelerationRate);
    } else if (currentEvolutionaryState.equals(EnumEvolutionaryState.EXPLORATION)) {
        // C1 - increase slightly | C2 decrease slightly
        c1 = c1 + (c1 * accelerationRate * 0.5);
        c2 = c2 - (c2 * accelerationRate * 0.5);
    } else if (currentEvolutionaryState.equals(EnumEvolutionaryState.JUMPING_OUT)) {
        // C1 - decrease | C2 increase
        c1 = c1 - (c1 * accelerationRate);
        c2 = c2 + (c2 * accelerationRate);
    }/* w  w w  .ja  v  a  2  s. c  om*/
    // Normalization
    if ((c1 + c2) > (cMin + cMax)) {
        double c1Temp = c1;
        double c2Temp = c2;
        c1Temp = c1 * (cMin + cMax) / (c1 + c2);
        c2Temp = c2 * (cMin + cMax) / (c1 + c2);
        c1 = c1Temp;
        c2 = c2Temp;
    }

    // Verify and adjust C1 and C2 by bounds
    if (c1 < cMin) {
        c1 = cMin;
    }
    if (c2 < cMin) {
        c2 = cMin;
    }
    if (c1 > cMax) {
        c1 = cMax;
    }
    if (c2 > cMax) {
        c2 = cMax;
    }

    setC1(c1);
    setC2(c2);

    // Adapt inertia weight
    double inertiaWeight = 1 / (1 + 1.5 * Math.exp(-2.6 * evolFactor));
    // Verify and adjust inertia weight by bounds
    if (inertiaWeight < inertiaWeightMin) {
        inertiaWeight = inertiaWeightMin;
    }
    if (inertiaWeight > inertiaWeightMax) {
        inertiaWeight = inertiaWeightMax;
    }
    setInertiaWeight(inertiaWeight);
}

From source file:br.upe.ecomp.doss.algorithm.chargedpso.ChargedPSOParticle.java

private double getRandomInRange(double value1, double value2) {
    RandomData random = new RandomDataImpl();
    double lowerBound;
    double upperBound;
    if (value1 < value2) {
        lowerBound = value1;/*from   ww w .j  a  va2  s  . co  m*/
        upperBound = value2;
    } else {
        lowerBound = value2;
        upperBound = value1;
    }
    return random.nextUniform(lowerBound, upperBound);
}

From source file:br.upe.ecomp.doss.algorithm.fss.Fish.java

public void performIndividualMovement(double[] stepInd, Problem problem) {
    RandomData random = new RandomDataImpl();
    int dimensions = getDimensions();
    double[] currentPosition = getCurrentPosition().clone();

    double[] nextPosition = new double[getDimensions()];
    for (int i = 0; i < dimensions; i++) {
        nextPosition[i] = currentPosition[i] + random.nextUniform(-1.0, 1.0) * stepInd[i];
        if (nextPosition[i] > problem.getUpperBound(i) || nextPosition[i] < problem.getLowerBound(i)) {
            nextPosition[i] = currentPosition[i];
        }//  ww w . j  a  va  2 s.  c  o m
    }

    deltaFitness = problem.getFitness(nextPosition) - getCurrentFitness();
    if (problem.isFitnessBetterThan(getCurrentFitness(), problem.getFitness(nextPosition))
            && deltaFitness < 0) {
        deltaFitness *= -1;
    }

    if (problem.isFitnessBetterThan(getCurrentFitness(), problem.getFitness(nextPosition))) {
        previousPosition = getCurrentPosition().clone();
        updateCurrentPosition(nextPosition, problem.getFitness(nextPosition));

        if (problem.isFitnessBetterThan(getBestFitness(), getCurrentFitness())) {
            updateBestPosition(getCurrentPosition(), getCurrentFitness());
        }
    } else {
        deltaFitness = 0;
    }
}

From source file:br.upe.ecomp.doss.algorithm.chargedpso.ChargedPSOParticle.java

/**
 * Updates the current position of the particle.
 * /* ww  w .  ja  v a2 s . com*/
 * @param problem The problem that we are trying to solve.
 */
@Override
public void updateCurrentPosition(Problem problem) {
    RandomData random = new RandomDataImpl();
    double[] position = getCurrentPosition();
    double[] velocity = getVelocity();
    double newPosition;
    for (int i = 0; i < getDimensions(); i++) {
        double p = position[i];
        newPosition = position[i] + velocity[i];
        double p1 = newPosition;
        if (charge != 0) {
            double step;

            if (newPosition > problem.getUpperBound(i)) {
                step = Math.abs(random.nextUniform(0, problem.getUpperBound(i) - problem.getLowerBound(i)));
                newPosition = problem.getUpperBound(i) - step;
            } else if (newPosition < problem.getLowerBound(i)) {
                step = Math.abs(random.nextUniform(0, problem.getUpperBound(i) - problem.getLowerBound(i)));
                newPosition = problem.getLowerBound(i) + step;
            }

            if (newPosition > problem.getUpperBound(i)) {
                System.out.println("parou");
            } else if (newPosition < problem.getLowerBound(i)) {
                System.out.println("parou 1");
            }

        } else if (newPosition > problem.getUpperBound(i)) {
            newPosition = problem.getUpperBound(i);
        } else if (newPosition < problem.getLowerBound(i)) {
            newPosition = problem.getLowerBound(i);
        }
        if (newPosition > problem.getUpperBound(i)) {
            System.out.println("ops");
        }
        position[i] = newPosition;
    }
    updateCurrentPosition(position, problem.getFitness(position));

}

From source file:br.upe.ecomp.doss.algorithm.fss.FSS.java

private void performCollectiveVolitiveMovement() {
    RandomData random = new RandomDataImpl();
    double[] barycenter = calculateBarycenter();

    double actualFishSchoolWeight = getFishSchoolWeigth();

    double euclidianDistance;
    double[] position;
    double currentPosition;
    for (Fish fish : fishSchool) {

        euclidianDistance = MathUtils.distance(fish.getCurrentPosition(), barycenter);
        position = new double[dimensions];

        for (int i = 0; i < dimensions; i++) {
            currentPosition = fish.getCurrentPosition()[i];

            if (actualFishSchoolWeight > previousFishSchoolWeight) {

                position[i] = currentPosition - stepVol[i] * random.nextUniform(0.0, 1.0)
                        * ((currentPosition - barycenter[i]) / euclidianDistance);
                // * ((currentPosition - barycenter[i]) / euclidianDistance);
            } else {
                position[i] = currentPosition + stepVol[i] * random.nextUniform(0.0, 1.0)
                        * ((currentPosition - barycenter[i]) / euclidianDistance);
                // * ((currentPosition - barycenter[i]) / euclidianDistance);
            }//from w  w  w. j av a 2  s  .  c  om
            // * ((currentPosition - barycenter[i]) / euclidianDistance);
            if (position[i] > getProblem().getUpperBound(i) || position[i] < getProblem().getLowerBound(i)) {
                position[i] = currentPosition;
            }
        }

        // if (getProblem().compareFitness(fish.getCurrentFitness(),
        // getProblem().getFitness(position))) {
        fish.updateCurrentPosition(position, getProblem().getFitness(position));

        if (getProblem().isFitnessBetterThan(fish.getBestFitness(), fish.getCurrentFitness())) {
            fish.updateBestPosition(fish.getCurrentPosition(), fish.getCurrentFitness());
        }
        // }
    }

    previousFishSchoolWeight = actualFishSchoolWeight;
}

From source file:br.upe.ecomp.doss.algorithm.volitiveapso.VolitiveAPSO.java

private void performCollectiveVolitiveMovement() {
    RandomData random = new RandomDataImpl();
    double[] barycenter = calculateBarycenter();

    double actualSwarmWeight = getSwarmWeigth();

    double euclidianDistance;
    double[] position;
    double currentPosition;

    WeightedAPSOParticle[] particles = (WeightedAPSOParticle[]) getParticles();
    for (WeightedAPSOParticle particle : particles) {

        euclidianDistance = MathUtils.distance(particle.getCurrentPosition(), barycenter);
        position = new double[getDimensions()];

        for (int i = 0; i < getDimensions(); i++) {
            currentPosition = particle.getCurrentPosition()[i];

            if (actualSwarmWeight > previousSwarmWeight) {

                position[i] = currentPosition - stepVol[i] * random.nextUniform(0.0, 1.0)
                        * ((currentPosition - barycenter[i]) / euclidianDistance);
            } else {
                position[i] = currentPosition + stepVol[i] * random.nextUniform(0.0, 1.0)
                        * ((currentPosition - barycenter[i]) / euclidianDistance);
            }//from www.  ja  va 2s  .c  o m
            if (position[i] > getProblem().getUpperBound(i) || position[i] < getProblem().getLowerBound(i)) {
                position[i] = currentPosition;
            }
        }

        // if (getProblem().compareFitness(fish.getCurrentFitness(),
        // getProblem().getFitness(position))) {
        particle.updateCurrentPosition(position, getProblem().getFitness(position));

        if (getProblem().isFitnessBetterThan(particle.getBestFitness(), particle.getCurrentFitness())) {
            particle.updateBestPosition(particle.getCurrentPosition(), particle.getCurrentFitness());
        }
        // }
    }

    previousSwarmWeight = actualSwarmWeight;
}

From source file:br.upe.ecomp.doss.algorithm.volitivepso.VolitivePSO.java

private void performCollectiveVolitiveMovement() {

    RandomData random = new RandomDataImpl();
    double[] barycenter = calculateBarycenter();

    double actualSwarmWeight = getSwarmWeigth();

    double euclidianDistance;
    double[] position;
    double currentPosition;

    WeightedPSOParticle[] particles = (WeightedPSOParticle[]) getParticles();
    for (WeightedPSOParticle particle : particles) {

        euclidianDistance = MathUtils.distance(particle.getCurrentPosition(), barycenter);
        position = new double[getDimensions()];

        for (int i = 0; i < getDimensions(); i++) {
            currentPosition = particle.getCurrentPosition()[i];

            if (actualSwarmWeight > previousSwarmWeight) {

                position[i] = currentPosition - stepVol[i] * random.nextUniform(0.0, 1.0)
                        * ((currentPosition - barycenter[i]) / euclidianDistance);
            } else {
                position[i] = currentPosition + stepVol[i] * random.nextUniform(0.0, 1.0)
                        * ((currentPosition - barycenter[i]) / euclidianDistance);
            }/*  w w w  . ja  v a  2  s.c  om*/
            if (position[i] > getProblem().getUpperBound(i) || position[i] < getProblem().getLowerBound(i)) {
                position[i] = currentPosition;
            }
        }

        // if (getProblem().compareFitness(fish.getCurrentFitness(),
        // getProblem().getFitness(position))) {
        particle.updateCurrentPosition(position, getProblem().getFitness(position));

        if (getProblem().isFitnessBetterThan(particle.getBestFitness(), particle.getCurrentFitness())) {
            particle.updateBestPosition(particle.getCurrentPosition(), particle.getCurrentFitness());
        }
        // }
    }

    previousSwarmWeight = actualSwarmWeight;
}

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

/**
 * //from w  w  w  . j a  v a  2  s . c o m
 * @param patient
 * @param startTime
 * @param endTime
 * @param rng
 * @return Return true if one of the child scenarios did something (returned true), otherwise false. 
 */
public boolean apply(GenMedical patient, Date startTime, Date endTime, RandomData rng) {
    double choice = rng.nextUniform(0.0, 1.0);
    double base = 0.0;
    for (ProbabilityScenario s : getScenarios()) {
        base += s.getProbability();
        if (base > choice) {
            //            TolvenLogger.info( "Scenario Choice " + s.getTitle(), ChooseScenario.class);
            return s.apply(patient, startTime, endTime, rng);
        }
    }
    return false;
}

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

public DiabetesGen(RandomData rng) {
    this.rng = rng;
    setDiseaseName("Diabetes Mellitus");
    //      addMatcher( new DemogMatcher( 0, 20, "M", 0.04*demoSkew));
    //      addMatcher( new DemogMatcher( 0, 20, "F", 0.06*demoSkew));
    //      addMatcher( new DemogMatcher( 20, 40, "M", 0.012*demoSkew));
    //      addMatcher( new DemogMatcher( 20, 40, "F", 0.018*demoSkew));
    addMatcher(new DemogMatcher(40, 60, "M", 0.052 * demoSkew));
    addMatcher(new DemogMatcher(40, 60, "F", 0.078 * demoSkew));
    addMatcher(new DemogMatcher(60, 200, "M", 0.08 * demoSkew));
    addMatcher(new DemogMatcher(60, 200, "F", 0.12 * demoSkew));
    // Some scenario components we'll use below
    Scenario podiatry = new RadiologyScenario("Foot Exam", "No abnormality noted on either foot.", "Normal");
    Scenario eagerLab = new GroupScenario("0.50 scenario in Diabetes",
            new Scenario[] {
                    new LabTestScenario("hemoglobin A1C test", "%", "7 (or less)", 1,
                            new GaussianValueGen(6.0, rng.nextUniform(0.5, 1.3))),
                    new LabTestScenario("Creatinine", "mg/dL", "0.9 to 1.4 mg/dL", 1,
                            new GaussianValueGen(1.2, rng.nextUniform(0.1, 0.3))),
                    new BatteryScenario("Lipid Panel",
                            new Scenario[] {
                                    new LabTestScenario("low-density lipoprotein (LDL)", "mg/dL", ">100 mg/dL",
                                            0, new GaussianValueGen(85.0, rng.nextUniform(10.0, 20.0))),
                                    new LabTestScenario("triglycerides", "mg/dL", ">150 mg/dL", 0,
                                            new GaussianValueGen(140.0, rng.nextUniform(5.0, 15.0))) }),
                    new LabTestScenario("urine albumin", "mcg", ">30 mcg", 0,
                            new GaussianValueGen(29.0, rng.nextUniform(4.0, 7.0))) });
    Scenario lazyLab = new GroupScenario("0.25 scenario in Diabetes",
            new Scenario[] {
                    new LabTestScenario("hemoglobin A1C test", "%", "7 (or less)", 1,
                            new GaussianValueGen(9.0, rng.nextUniform(1.0, 3.5))),
                    new LabTestScenario("Creatinine", "mg/dL", "0.9 to 1.4 mg/dL", 1,
                            new GaussianValueGen(1.1, rng.nextUniform(0.3, 0.7))),
                    new BatteryScenario("Lipid Panel",
                            new Scenario[] {
                                    new LabTestScenario("low-density lipoprotein (LDL)", "mg/dL", ">100 mg/dL",
                                            0, new GaussianValueGen(165.0, rng.nextUniform(10.0, 40.0))),
                                    new LabTestScenario("triglycerides", "mg/dL", ">150 mg/dL", 0,
                                            new GaussianValueGen(180.0, rng.nextUniform(15.0, 28.0))) }),
                    new LabTestScenario("urine albumin", "mcg", ">30 mcg", 0,
                            new GaussianValueGen(35.0, rng.nextUniform(5.1, 11.2))) });
    Scenario annualAppt = new AppointmentScenario("12 Month diabetes followup", null, null, null);
    //      Scenario semiAnnualAppt = new AppointmentScenario( "6 Month diabetes lab work", null, null, null);

    // Specify the scenario here.
    this.setScenario(new ChooseScenario(new ProbabilityScenario[] {
            // This says on 12 month cycle, do an appointment from which lab and podiatry exam events occur and then, repeat once at a 6 month interval a second lab scenario.
            // In addition, we'll schedule an annual appointment for one time in the future. (Ignore six-month appoitment) 
            new ProbabilityScenario(0.50,
                    new RepeatScenario(GregorianCalendar.MONTH, 12, 1.5, false, 1, 0,
                            new GroupScenario("12 month repeat",
                                    new Scenario[] { annualAppt, eagerLab, podiatry,
                                            new RepeatScenario(GregorianCalendar.MONTH, 6, 1.5, false, 1, 1,
                                                    eagerLab) }),
                            annualAppt, 1)),
            new ProbabilityScenario(0.25,
                    new RepeatScenario(GregorianCalendar.MONTH, 8, 2.1, true, 0, 0,
                            new GroupScenario("12 month repeat",
                                    new Scenario[] { annualAppt, lazyLab, podiatry, new RepeatScenario(
                                            GregorianCalendar.MONTH, 6, 1.5, false, 1, 1, lazyLab) }),
                            annualAppt, 1)) }));
}