Example usage for org.apache.commons.math3.util Precision equals

List of usage examples for org.apache.commons.math3.util Precision equals

Introduction

In this page you can find the example usage for org.apache.commons.math3.util Precision equals.

Prototype

public static boolean equals(final double x, final double y, final int maxUlps) 

Source Link

Document

Returns true if both arguments are equal or within the range of allowed error (inclusive).

Usage

From source file:com.dlej.Main.java

public static void main(String[] args) {
    long startTime = System.currentTimeMillis();

    // initialize a new genetic algorithm
    GeneticAlgorithm ga = new GeneticAlgorithm(new OnePointCrossover<Character>(), CROSSOVER_RATE,
            new RandomCharacterMutation(), MUTATION_RATE, new TournamentSelection(TOURNAMENT_ARITY));

    // initial population
    Population initial = getInitialPopulation();

    // stopping condition
    StoppingCondition stoppingCondition = new StoppingCondition() {

        int generation = 0;

        @Override// w ww . ja  va2 s . c  o  m
        public boolean isSatisfied(Population population) {
            Chromosome fittestChromosome = population.getFittestChromosome();

            if (generation == 1 || generation % 10 == 0) {
                System.out.println("Generation " + generation + ": " + fittestChromosome.toString());
            }
            generation++;

            double fitness = fittestChromosome.fitness();
            if (Precision.equals(fitness, 0.0, 1e-6)) {
                return true;
            } else {
                return false;
            }
        }
    };

    System.out.println("Starting evolution ...");

    // run the algorithm
    Population finalPopulation = ga.evolve(initial, stoppingCondition);

    // Get the end time for the simulation.
    long endTime = System.currentTimeMillis();

    // best chromosome from the final population
    Chromosome best = finalPopulation.getFittestChromosome();
    System.out.println("Generation " + ga.getGenerationsEvolved() + ": " + best.toString());
    System.out.println("Total execution time: " + (endTime - startTime) + "ms");
}

From source file:co.turnus.analysis.util.AnalysisUtil.java

public static boolean equals(double a, double b) {
    return Precision.equals(a, b, PRECISION_EPS);
}

From source file:com.facebook.presto.operator.aggregation.AggregationTestUtils.java

public static void assertAggregation(InternalAggregationFunction function, Object expectedValue, Page page) {
    BiFunction<Object, Object, Boolean> equalAssertion;
    if (expectedValue instanceof Double && !expectedValue.equals(Double.NaN)) {
        equalAssertion = (actual, expected) -> Precision.equals((double) actual, (double) expected, 1e-10);
    } else if (expectedValue instanceof Float && !expectedValue.equals(Float.NaN)) {
        equalAssertion = (actual, expected) -> Precision.equals((float) actual, (float) expected, 1e-10f);
    } else {//from  w w w .  j  ava 2 s  . co m
        equalAssertion = Objects::equals;
    }

    assertAggregation(function, equalAssertion, null, page, expectedValue);
}

From source file:com.clust4j.optimize.TestOptimizer.java

@Test
public void testBrentSimple() {
    BrentDownhillOptimizer optimizer = new BrentDownhillOptimizer(caller);
    double res = optimizer.optimize(); // optimize
    assertTrue(Precision.equals(res, 1.5, Precision.EPSILON));
    assertTrue(Precision.equals(optimizer.getFunctionResult(), -min_val, Precision.EPSILON));

    // if we try to "reoptimize" we get the same res:
    assertTrue(res == optimizer.optimize());
}

From source file:com.clust4j.optimize.TestOptimizer.java

@Test
public void testBrentWithBracket() {
    BrentDownhillOptimizer optimizer = new BrentDownhillOptimizer(caller, -3, -2);
    double res = optimizer.optimize(); // optimize
    assertTrue(Precision.equals(res, 1.5, Precision.EPSILON));
    assertTrue(optimizer.getNumFunctionCalls() == 5);
}

From source file:com.clust4j.algo.preprocess.PreProcessorTests.java

@Test
public void testMeanCenter() {
    final double[][] data = new double[][] { new double[] { 0.005, 0.182751, 0.1284 },
            new double[] { 3.65816, 0.29518, 2.123316 }, new double[] { 4.1234, 0.27395, 1.8900002 } };

    final Array2DRowRealMatrix d = new Array2DRowRealMatrix(data, false);
    MeanCenterer norm = new MeanCenterer().fit(d);
    RealMatrix scaled = norm.transform(d);
    double[][] operated = scaled.getData();

    assertTrue(Precision.equals(VecUtils.mean(MatUtils.getColumn(operated, 0)), 0, Precision.EPSILON));
    assertTrue(Precision.equals(VecUtils.mean(MatUtils.getColumn(operated, 1)), 0, Precision.EPSILON));
    assertTrue(Precision.equals(VecUtils.mean(MatUtils.getColumn(operated, 2)), 0, Precision.EPSILON));

    // test copy//ww w .  ja va  2  s  .  c o m
    operated = norm.copy().transform(data);
    assertTrue(Precision.equals(VecUtils.mean(MatUtils.getColumn(operated, 0)), 0, Precision.EPSILON));
    assertTrue(Precision.equals(VecUtils.mean(MatUtils.getColumn(operated, 1)), 0, Precision.EPSILON));
    assertTrue(Precision.equals(VecUtils.mean(MatUtils.getColumn(operated, 2)), 0, Precision.EPSILON));

    // Test inverse transform.
    assertTrue(MatUtils.equalsWithTolerance(data, norm.inverseTransform(scaled).getData(), 1e-8));

    // copy coverage
    norm.copy();

    // test dim mismatch
    boolean a = false;
    try {
        norm.inverseTransform(TestSuite.getRandom(2, 2));
    } catch (DimensionMismatchException dim) {
        a = true;
    } finally {
        assertTrue(a);
    }

    // test not fit
    a = false;
    try {
        new MeanCenterer().transform(d);
    } catch (ModelNotFitException dim) {
        a = true;
    } finally {
        assertTrue(a);
    }
}

From source file:lirmm.inria.fr.math.OpenLongToDoubleHashMapTest.java

private void assertPutAndGet(OpenLongToDoubleHashMap map, int mapSize, Set<Long> keysInMap) {
    Assert.assertEquals(mapSize, map.size());
    for (Map.Entry<Long, Double> mapEntry : javaMap.entrySet()) {
        map.put(mapEntry.getKey(), mapEntry.getValue());
        if (!keysInMap.contains(mapEntry.getKey())) {
            ++mapSize;/*from www .  j  a v a2 s  .  co  m*/
        }
        Assert.assertEquals(mapSize, map.size());
        Assert.assertTrue(Precision.equals(mapEntry.getValue(), map.get(mapEntry.getKey()), 1));
    }
}

From source file:lirmm.inria.fr.math.OpenLongToDoubleHashMapTest.java

@Test
public void testPutAbsentOnExisting() {
    OpenLongToDoubleHashMap map = createFromJavaMap();
    int size = javaMap.size();
    for (Map.Entry<Long, Double> mapEntry : generateAbsent().entrySet()) {
        map.put(mapEntry.getKey(), mapEntry.getValue());
        Assert.assertEquals(++size, map.size());
        Assert.assertTrue(Precision.equals(mapEntry.getValue(), map.get(mapEntry.getKey()), 1));
    }/* w ww.j av a 2  s. co  m*/
}

From source file:com.clust4j.metrics.pairwise.PairwiseTests.java

@Test
public void testDistUpperTriangular() {
    /*/*from   www.  ja  v a 2s .co m*/
     * Test for ALL distance metrics
     */
    for (DistanceMetric metric : distances()) {
        double[][] completeDistance = Pairwise.getDistance(X, metric, true, false);
        double[][] partialDistance = Pairwise.getDistance(X, metric, true, true);

        for (int i = 0; i < X.length - 1; i++) {
            for (int j = i + 1; j < X.length; j++) {
                /*
                 * Assert partial to full == complete
                 */
                assertTrue(Precision.equals(completeDistance[i][j],
                        metric.partialDistanceToDistance(partialDistance[i][j]), 1e-8));

                /*
                 * Assert complete to partial == partial
                 */
                assertTrue(Precision.equals(partialDistance[i][j],
                        metric.distanceToPartialDistance(completeDistance[i][j]), 1e-8));

                /*
                 * Assert lower-triangular portion is all zero
                 */
                assertTrue(partialDistance[j][i] == 0.0);
                assertTrue(completeDistance[j][i] == 0.0);
            }
        }

        // Assert the diagonal is entirely zero
        for (int i = 0; i < X.length; i++) {
            assertTrue(partialDistance[i][i] == 0.0);
            assertTrue(completeDistance[i][i] == 0.0);
        }
    }
}

From source file:lirmm.inria.fr.math.OpenLongToDoubleHashMapTest.java

@Test
public void testPutOnExisting() {
    OpenLongToDoubleHashMap map = createFromJavaMap();
    for (Map.Entry<Long, Double> mapEntry : javaMap.entrySet()) {
        map.put(mapEntry.getKey(), mapEntry.getValue());
        Assert.assertEquals(javaMap.size(), map.size());
        Assert.assertTrue(Precision.equals(mapEntry.getValue(), map.get(mapEntry.getKey()), 1));
    }/*from  w  w w  .  ja  v a 2 s.c o m*/
}