_buffer = new Buffer(longFilterSize); _proceed = requireNonNull(proceed);
@Test public void stream() { final long seed = 0xdeadbeef; final int capacity = 10; final Random random = new Random(seed); final Buffer buffer = new Buffer(capacity); for (int i = 0; i < buffer.capacity(); ++i) { final double value = random.nextDouble(); buffer.accept(value); } random.setSeed(seed); buffer.stream().forEach(d -> Assert.assertEquals(d, random.nextDouble())); random.setSeed(seed); for (int i = 0; i < 5; ++i) random.nextDouble(); buffer.stream(5).forEach(d -> Assert.assertEquals(d, random.nextDouble())); }
@Override public boolean test(final EvolutionResult<?, N> result) { final Number fitness = result.getBestFitness(); if (fitness != null) { _buffer.accept(fitness.doubleValue()); } return !_buffer.isFull() || _proceed.test( _buffer.doubleMoments(_shortFilterSize), _buffer.doubleMoments(_longFilterSize) ); }
@Test public void bufferDoubleMoments() { final long seed = 0xdeadbeef; final int capacity = 10; final Random random = new Random(seed); final Buffer buffer = new Buffer(capacity); DoubleMomentStatistics statistics = new DoubleMomentStatistics(); for (int i = 0; i < buffer.capacity(); ++i) { final double value = random.nextDouble()*1000; buffer.accept(value); statistics.accept(value); final DoubleMoments moments = DoubleMoments.of(statistics); Assert.assertEquals(moments, buffer.doubleMoments(1000)); } final Random sr = new Random(seed); for (int i = 0; i < buffer.capacity(); ++i) { statistics = statistics(new Random(seed), i + 1, buffer.capacity() - 1); final double value = random.nextDouble()*1000; buffer.accept(value); statistics.accept(value); final DoubleMoments moments = DoubleMoments.of(statistics); Assert.assertEquals(moments, buffer.doubleMoments(1000)); } }
/** * Return the double moment statistics of the last {@code windowSize} * buffer values. * * @param windowSize the number of the last double values the statistics * consists of * @return the double moment statistics of the last {@code windowSize} * buffer values */ public DoubleMoments doubleMoments(final int windowSize) { return DoubleMoments.of( stream(windowSize).collect( DoubleMomentStatistics::new, DoubleMomentStatistics::accept, DoubleMomentStatistics::combine ) ); }
@Test public void bufferLength() { final long seed = 0xdeadbeef; final int capacity = 10; final Random random = new Random(seed); final Buffer buffer = new Buffer(capacity); for (int i = 0; i < buffer.capacity(); ++i) { buffer.accept(random.nextDouble()); Assert.assertEquals(buffer.length(), i + 1); } for (int i = 0; i < buffer.capacity(); ++i) { buffer.accept(random.nextDouble()); Assert.assertEquals(buffer.length(), buffer.capacity()); } }
/** * Return the double moment statistics of the buffer values. * * @return the double moment statistics of the buffer values */ public DoubleMoments doubleMoments() { return doubleMoments(_length); }
/** * Return the double stream of the buffer values. * * @return the double stream of the buffer values */ public DoubleStream stream() { return stream(_length); }