Example usage for java.util.stream IntStream range

List of usage examples for java.util.stream IntStream range

Introduction

In this page you can find the example usage for java.util.stream IntStream range.

Prototype

public static IntStream range(int startInclusive, int endExclusive) 

Source Link

Document

Returns a sequential ordered IntStream from startInclusive (inclusive) to endExclusive (exclusive) by an incremental step of 1 .

Usage

From source file:eu.amidst.dynamic.inference.DynamicMAPInference.java

/**
 * Runs the inference given an input search algorithm.
 * @param searchAlgorithm a valid {@link SearchAlgorithm} value.
 *//*from w  ww .  java2 s .c  o  m*/
public void runInference(SearchAlgorithm searchAlgorithm) {

    if (MAPvariable == null || MAPvarName == null) {
        System.out.println("Error: The MAP variable has not been set");
        System.exit(-30);
    }

    if (this.mergedClassVarModels == null) {
        this.computeMergedClassVarModels();
    }

    if (this.unfoldedStaticModel == null) {
        unfoldedStaticModel = DynamicToStaticBNConverter.convertDBNtoBN(model, nTimeSteps);
    }

    //
    //        if (evidence!=null && staticEvidence==null) {
    //
    //            staticEvidence = new HashMapAssignment(staticEvenModel.getNumberOfVars());
    //
    //            evidence.stream().forEach(dynamicAssignment -> {
    //                int time = (int) dynamicAssignment.getTimeID();
    //                Set<Variable> dynAssigVariables = dynamicAssignment.getVariables();
    //                for (Variable dynVariable : dynAssigVariables) {
    //                    Variable staticVariable = staticEvenModel.getVariables().getVariableByName(dynVariable.getName() + "_t" + Integer.toString(time));
    //                    double varValue = dynamicAssignment.getValue(dynVariable);
    //                    staticEvidence.setValue(staticVariable, varValue);
    //                }
    //
    //            });
    //        }
    //

    if (evidence != null && staticEvidence == null) {

        staticEvidence = new HashMapAssignment(unfoldedStaticModel.getNumberOfVars());

        evidence.stream().forEach(dynamicAssignment -> {
            int time = (int) dynamicAssignment.getTimeID();
            Set<Variable> dynAssigVariables = dynamicAssignment.getVariables();
            for (Variable dynVariable : dynAssigVariables) {
                Variable staticVariable = unfoldedStaticModel.getVariables()
                        .getVariableByName(dynVariable.getName() + "_t" + Integer.toString(time));
                double varValue = dynamicAssignment.getValue(dynVariable);
                staticEvidence.setValue(staticVariable, varValue);
            }
        });
    }

    List<InferenceAlgorithm> staticModelsInference = new ArrayList<>(nMergedClassVars);
    IntStream.range(0, nMergedClassVars).forEachOrdered(i -> {
        InferenceAlgorithm currentModelInference;
        switch (searchAlgorithm) {
        case VMP:
            currentModelInference = new VMP();
            //((VMP)currentModelInference).setTestELBO(true);
            ((VMP) currentModelInference).setThreshold(0.0001);
            ((VMP) currentModelInference).setMaxIter(3000);
            break;

        case IS:
        default:

            currentModelInference = new ImportanceSamplingRobust();

            Random random = new Random((this.seed));
            currentModelInference.setSeed(random.nextInt());

            this.seed = random.nextInt();

            ((ImportanceSamplingRobust) currentModelInference).setSampleSize(sampleSize);

            break;
        }
        currentModelInference.setParallelMode(this.parallelMode);
        currentModelInference.setModel(mergedClassVarModels.get(i));

        if (searchAlgorithm == SearchAlgorithm.IS) {
            ((ImportanceSamplingRobust) currentModelInference)
                    .setVariablesAPosteriori(mergedClassVarModels.get(i).getVariables().getListOfVariables()
                            .stream().filter(variable -> variable.getName().contains(groupedClassName))
                            .collect(Collectors.toList()));
        }

        BayesianNetwork thisModel = mergedClassVarModels.get(i);

        if (staticEvidence != null) {
            Assignment thisEvidence = new HashMapAssignment();
            for (Variable varEvidence : staticEvidence.getVariables()) {
                thisEvidence.setValue(thisModel.getVariables().getVariableByName(varEvidence.getName()),
                        staticEvidence.getValue(varEvidence));
            }
            currentModelInference.setEvidence(thisEvidence);
        }
        currentModelInference.runInference();

        //System.out.println(currentModelInference.getLogProbabilityOfEvidence());

        staticModelsInference.add(currentModelInference);
    });
    //
    //        IntStream.range(0, 2).parallel().forEach(i -> {
    //            if (i == 0) {
    //                evenModelInference.setParallelMode(this.parallelMode);
    //                evenModelInference.setModel(staticEvenModel);
    //                if (evidence != null) {
    //                    evenModelInference.setEvidence(staticEvidence);
    //                }
    //                evenModelInference.runInference();
    //            }
    //            else {
    //                oddModelInference.setParallelMode(this.parallelMode);
    //                oddModelInference.setModel(staticOddModel);
    //                if (evidence != null) {
    //                    oddModelInference.setEvidence(staticEvidence);
    //                }
    //                oddModelInference.runInference();
    //            }
    //        });

    List<List<UnivariateDistribution>> posteriorMAPDistributions = new ArrayList<>();
    IntStream.range(0, nMergedClassVars).forEachOrdered(modelNumber -> {

        List<UnivariateDistribution> currentModelPosteriorMAPDistributions = new ArrayList<>();
        int nReplicationsMAPVariable = (modelNumber == 0 ? 0 : 1)
                + (nTimeSteps - modelNumber) / nMergedClassVars
                + ((nTimeSteps - modelNumber) % nMergedClassVars == 0 ? 0 : 1);

        IntStream.range(0, nReplicationsMAPVariable).forEachOrdered(i -> {
            currentModelPosteriorMAPDistributions.add(staticModelsInference.get(modelNumber).getPosterior(i));
            //System.out.println(staticModelsInference.get(modelNumber).getPosterior(i).toString());
        });

        posteriorMAPDistributions.add(currentModelPosteriorMAPDistributions);
    });

    //        int replicationsMAPVariableEvenModel = nTimeSteps/2 + nTimeSteps%2;
    //        IntStream.range(0,replicationsMAPVariableEvenModel).forEachOrdered(i -> posteriorMAPDistributionsEvenModel.add(evenModelInference.getPosterior(i)));
    //
    //        int replicationsMAPVariableOddModel = 1 + (nTimeSteps-1)/2 + (nTimeSteps-1)%2;
    //        IntStream.range(0,replicationsMAPVariableOddModel).forEachOrdered(i -> posteriorMAPDistributionsOddModel.add(oddModelInference.getPosterior(i)));

    posteriorMAPDistributions.forEach(list -> {

        StringBuilder stringBuilder = new StringBuilder();

        list.forEach(uniDist -> {
            stringBuilder.append(Arrays.toString(uniDist.getParameters()));
            stringBuilder.append(" ,  ");
        });
        //System.out.println("Model number " + posteriorMAPDistributions.indexOf(list) + ": " + stringBuilder.toString());
    });

    allGroupedPosteriorDistributions = posteriorMAPDistributions;

    bestSequenceEachModel = new ArrayList<>();
    IntStream.range(0, nMergedClassVars).forEachOrdered(modelNumber -> {
        int[] thisModelBestSequence = new int[nTimeSteps];
        int indexSequence = 0;

        List<UnivariateDistribution> thisModelPosteriors = posteriorMAPDistributions.get(modelNumber);

        for (int k = 0; k < thisModelPosteriors.size(); k++) {
            UnivariateDistribution thisDistribution = thisModelPosteriors.get(k);

            int indexMaxProbability = (int) argMax(thisDistribution.getParameters())[1];
            int thisDistribNumberOfMergedVars = (int) Math
                    .round(Math.log(thisDistribution.getVariable().getNumberOfStates())
                            / Math.log(MAPvariable.getNumberOfStates()));

            String m_base_nStates = Integer.toString(
                    Integer.parseInt(Integer.toString(indexMaxProbability), 10),
                    MAPvariable.getNumberOfStates());
            m_base_nStates = StringUtils.leftPad(m_base_nStates, thisDistribNumberOfMergedVars, '0');

            for (int j = 0; j < m_base_nStates.length(); j++) {
                thisModelBestSequence[indexSequence] = Integer.parseInt(m_base_nStates.substring(j, j + 1));
                indexSequence++;
            }
        }

        //System.out.println("Best sequence model " + modelNumber + ": " + Arrays.toString(thisModelBestSequence));
        bestSequenceEachModel.add(thisModelBestSequence);
    });

    List<double[]> conditionalDistributionsMAPvariable = obtainMAPVariableConditionalDistributions(
            posteriorMAPDistributions);
    //System.out.println("Cond Distributions: " + Arrays.toString(conditionalDistributionsMAPvariable));

    StringBuilder stringBuilder = new StringBuilder();
    conditionalDistributionsMAPvariable.forEach(conDistr -> {

        stringBuilder.append(Arrays.toString(conDistr));
        stringBuilder.append(" ,  ");
    });
    //System.out.println("Combined Conditional Distributions: \n" + stringBuilder.toString());

    computeMostProbableSequence(conditionalDistributionsMAPvariable);

}

From source file:com.yahoo.bullet.drpc.FilterBoltTest.java

@Test
public void testCountDistinct() {
    Map<String, Object> config = new HashMap<>();
    config.put(BulletConfig.COUNT_DISTINCT_AGGREGATION_SKETCH_ENTRIES, 512);
    bolt = ComponentUtils.prepare(config, new ExpiringFilterBolt(), collector);

    Tuple rule = makeIDTuple(TupleType.Type.RULE_TUPLE, 42L,
            makeAggregationRule(AggregationType.COUNT_DISTINCT, 1, null, Pair.of("field", "field")));
    bolt.execute(rule);/*  w w w .  ja  v a 2 s .c o m*/

    IntStream.range(0, 256).mapToObj(i -> RecordBox.get().add("field", i).getRecord())
            .map(r -> makeTuple(TupleType.Type.RECORD_TUPLE, r)).forEach(bolt::execute);

    Assert.assertEquals(collector.getEmittedCount(), 0);

    Tuple tick = TupleUtils.makeTuple(TupleType.Type.TICK_TUPLE);
    bolt.execute(tick);
    bolt.execute(tick);

    Assert.assertEquals(collector.getEmittedCount(), 1);

    byte[] rawData = getRawPayloadOfNthTuple(1);
    Assert.assertNotNull(rawData);

    Aggregation aggregation = new Aggregation();
    aggregation.setConfiguration(config);
    aggregation.setFields(singletonMap("field", "foo"));
    CountDistinct distinct = new CountDistinct(aggregation);
    distinct.combine(rawData);

    BulletRecord actual = distinct.getAggregation().getRecords().get(0);
    BulletRecord expected = RecordBox.get().add(CountDistinct.DEFAULT_NEW_NAME, 256.0).getRecord();
    Assert.assertEquals(actual, expected);

}

From source file:com.devicehive.service.UserServiceTest.java

@Test
public void should_lock_user_if_max_login_attempts_reached_when_findUser_called() throws Exception {
    UserVO newUser = new UserVO();
    newUser.setLogin(RandomStringUtils.randomAlphabetic(10));
    newUser.setStatus(UserStatus.ACTIVE);
    UserVO user = userService.createUser(newUser, "123");
    assertThat(user, notNullValue());/*from  w  w  w.  j a  v a 2s  .com*/
    assertThat(user.getId(), notNullValue());
    assertThat(user.getLogin(), notNullValue());
    assertThat(user.getPasswordHash(), notNullValue());
    assertThat(user.getPasswordSalt(), notNullValue());
    assertThat(user.getStatus(), equalTo(UserStatus.ACTIVE));

    configurationService.save(Constants.MAX_LOGIN_ATTEMPTS, 5);

    IntStream.range(1, 5).forEach(attempt -> {
        try {
            userService.findUser(user.getLogin(), "wrong_password");
            fail("should throw login exception");
        } catch (AccessDeniedException e) {
            assertThat(e.getMessage(), equalTo(String.format(Messages.INCORRECT_CREDENTIALS, user.getLogin())));
        }
        UserVO updatedUser = userDao.find(user.getId());
        assertThat(updatedUser, notNullValue());
        assertThat(updatedUser.getLoginAttempts(), equalTo(attempt));
        assertThat(updatedUser.getStatus(), equalTo(UserStatus.ACTIVE));
        assertThat(updatedUser.getLastLogin(), nullValue());
    });

    try {
        userService.findUser(user.getLogin(), "wrong_password");
        fail("should throw login exception");
    } catch (AccessDeniedException e) {
        assertThat(e.getMessage(), equalTo(String.format(Messages.INCORRECT_CREDENTIALS, user.getLogin())));
    }
    UserVO updatedUser = userDao.find(user.getId());
    assertThat(updatedUser, notNullValue());
    assertThat(updatedUser.getLoginAttempts(), equalTo(0));
    assertThat(updatedUser.getStatus(), equalTo(UserStatus.LOCKED_OUT));
    assertThat(updatedUser.getLastLogin(), nullValue());
}

From source file:org.lightjason.agentspeak.action.builtin.TestCActionMathBlasMatrix.java

/**
 * test diagonal//from   w w w .  j a  v  a  2 s. c om
 */
@Test
public final void diagonal() {
    final List<ITerm> l_return = new ArrayList<>();
    final double[] l_data = new double[] { 1, 3, 5, 11 };

    new CDiagonal().execute(false, IContext.EMPTYPLAN,
            Stream.of(new DenseDoubleMatrix1D(l_data)).map(CRawTerm::from).collect(Collectors.toList()),
            l_return);

    Assert.assertEquals(l_return.size(), 1);
    final DoubleMatrix2D l_result = l_return.get(0).raw();

    Assert.assertArrayEquals(Arrays.stream(l_data).boxed().toArray(),
            IntStream.range(0, l_result.rows()).boxed().map(i -> l_result.getQuick(i, i)).toArray());

    IntStream.range(0, l_result.rows()).forEach(i -> IntStream.range(0, l_result.columns()).filter(j -> i != j)
            .forEach(j -> Assert.assertEquals(l_result.getQuick(i, j), 0, 0)));
}

From source file:org.sahli.asciidoc.confluence.publisher.client.http.ConfluenceRestClientTest.java

private String generateJsonAttachmentResults(int numberOfAttachment) {
    return IntStream.range(1, numberOfAttachment + 1).boxed()
            .map(attachmentNumber -> "{\"id\": \"" + attachmentNumber + "\", \"title\": \"Attachment-"
                    + attachmentNumber + ".txt\", \"_links\": {\"download\": \"/download/Attachment-"
                    + attachmentNumber + ".txt\"}, \"version\": {\"number\": 1}}")
            .collect(Collectors.joining(",\n"));
}

From source file:org.apache.bookkeeper.bookie.CreateNewLogTest.java

@Test
public void testCreateNewLogAndCompactionLog() throws Exception {
    ServerConfiguration conf = TestBKConfiguration.newServerConfiguration();

    // Creating a new configuration with a number of
    // ledger directories.
    conf.setLedgerDirNames(ledgerDirs);//w ww. ja v  a2s  .  c  om
    conf.setEntryLogFilePreAllocationEnabled(true);
    LedgerDirsManager ledgerDirsManager = new LedgerDirsManager(conf, conf.getLedgerDirs(),
            new DiskChecker(conf.getDiskUsageThreshold(), conf.getDiskUsageWarnThreshold()));
    EntryLogger el = new EntryLogger(conf, ledgerDirsManager);
    // set same thread executor for entryLoggerAllocator's allocatorExecutor
    setSameThreadExecutorForEntryLoggerAllocator(el.getEntryLoggerAllocator());
    AtomicBoolean receivedException = new AtomicBoolean(false);

    IntStream.range(0, 2).parallel().forEach((i) -> {
        try {
            if (i % 2 == 0) {
                ((EntryLogManagerBase) el.getEntryLogManager()).createNewLog((long) i);
            } else {
                el.createNewCompactionLog();
            }
        } catch (IOException e) {
            LOG.error("Received exception while creating newLog", e);
            receivedException.set(true);
        }
    });

    Assert.assertFalse("There shouldn't be any exceptions while creating newlog", receivedException.get());
    Assert.assertEquals("previousAllocatedEntryLogId after 2 times createNewLog is called", 2,
            el.getPreviousAllocatedEntryLogId());
}

From source file:org.apache.tinkerpop.gremlin.groovy.engine.GremlinExecutorTest.java

@Test
public void shouldAllowVariableReuseAcrossThreads() throws Exception {
    final ExecutorService service = Executors.newFixedThreadPool(8, testingThreadFactory);
    final GremlinExecutor gremlinExecutor = GremlinExecutor.build().create();

    final AtomicBoolean failed = new AtomicBoolean(false);
    final int max = 512;
    final List<Pair<Integer, List<Integer>>> futures = Collections.synchronizedList(new ArrayList<>(max));
    IntStream.range(0, max).forEach(i -> {
        final int yValue = i * 2;
        final Bindings b = new SimpleBindings();
        b.put("x", i);
        b.put("y", yValue);
        final int zValue = i * -1;

        final String script = "z=" + zValue + ";[x,y,z]";
        try {/*w w  w .  j a v a2s .  c o  m*/
            service.submit(() -> {
                try {
                    final List<Integer> result = (List<Integer>) gremlinExecutor.eval(script, b).get();
                    futures.add(Pair.with(i, result));
                } catch (Exception ex) {
                    failed.set(true);
                }
            });
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    });

    service.shutdown();
    assertThat(service.awaitTermination(60000, TimeUnit.MILLISECONDS), is(true));

    // likely a concurrency exception if it occurs - and if it does then we've messed up because that's what this
    // test is partially designed to protected against.
    assertThat(failed.get(), is(false));

    assertEquals(max, futures.size());
    futures.forEach(t -> {
        assertEquals(t.getValue0(), t.getValue1().get(0));
        assertEquals(t.getValue0() * 2, t.getValue1().get(1).intValue());
        assertEquals(t.getValue0() * -1, t.getValue1().get(2).intValue());
    });
}

From source file:com.yahoo.bullet.drpc.JoinBoltTest.java

@Test
public void testCounting() {
    bolt = ComponentUtils.prepare(new ExpiringJoinBolt(), collector);

    Tuple rule = TupleUtils.makeIDTuple(TupleType.Type.RULE_TUPLE, 42L, makeGroupFilterRule("timestamp",
            asList("1", "2"), EQUALS, GROUP, 1, singletonList(new GroupOperation(COUNT, null, "cnt"))));
    bolt.execute(rule);/*from  w w  w  . jav a  2s.c om*/

    Tuple returnInfo = TupleUtils.makeIDTuple(TupleType.Type.RETURN_TUPLE, 42L, "");
    bolt.execute(returnInfo);

    // Send 5 GroupData with counts 1, 2, 3, 4, 5 to the JoinBolt
    IntStream.range(1, 6)
            .forEach(i -> sendRawByteTuplesTo(bolt, 42L, singletonList(getGroupDataWithCount("cnt", i))));

    // 1 + 2 + 3 + 4 + 5
    List<BulletRecord> result = singletonList(RecordBox.get().add("cnt", 15L).getRecord());
    Tuple expected = TupleUtils.makeTuple(TupleType.Type.JOIN_TUPLE, Clip.of(result).asJSON(), "");

    Tuple tick = TupleUtils.makeTuple(TupleType.Type.TICK_TUPLE);
    bolt.execute(tick);
    // Should cause an expiry and starts buffering the rule for the rule tickout
    bolt.execute(tick);
    // We need to tick the default rule tickout to make the rule emit
    for (int i = 0; i < JoinBolt.DEFAULT_RULE_TICKOUT - 1; ++i) {
        bolt.execute(tick);
        Assert.assertFalse(collector.wasTupleEmitted(expected));
    }
    bolt.execute(tick);

    Assert.assertTrue(collector.wasNthEmitted(expected, 1));
    Assert.assertEquals(collector.getAllEmitted().count(), 1);

}

From source file:org.sahli.asciidoc.confluence.publisher.client.http.ConfluenceRestClientTest.java

private static String generateJsonPageResults(int numberOfPages) {
    return IntStream
            .range(1, numberOfPages + 1).boxed().map(pageNumber -> "{\"id\": \"" + pageNumber
                    + "\", \"title\": \"Page " + pageNumber + "\", \"version\": {\"number\": 1}}")
            .collect(Collectors.joining(",\n"));
}

From source file:eu.amidst.core.inference.ImportanceSamplingRobust.java

/**
 * {@inheritDoc}// w w  w. j  a  v  a 2s. c om
 */
@Override
public void runInference() {

    LocalRandomGenerator randomGenerator = new LocalRandomGenerator(seed);

    IntStream weightedSampleStream = IntStream.range(0, sampleSize).parallel();

    if (!parallelMode) {
        weightedSampleStream = weightedSampleStream.sequential();
    }

    //            weightedSampleStream = IntStream.range(0, sampleSize).parallel()
    //                    .mapToObj(i -> generateSample(randomGenerator.current()));
    //        } else {
    //            weightedSampleStream = IntStream.range(0, sampleSize).sequential()
    //                    .mapToObj(i -> generateSample(randomGenerator.current()));
    //        }

    double logSumWeights = weightedSampleStream.mapToDouble(i -> {
        WeightedAssignment weightedSample = generateSample(randomGenerator.current());
        updatePosteriorDistributions(weightedSample.assignment, weightedSample.logWeight);
        //updateLogProbabilityOfEvidence(weightedSample.logWeight);

        //logProbOfEvidence = robustSumOfLogarithms(logProbOfEvidence,weightedSample.logWeight-Math.log(sampleSize));

        //System.out.println(weightedSample.logWeight);

        //System.out.println(logProbOfEvidence);

        return weightedSample.logWeight;

    }).reduce(this::robustSumOfLogarithms).getAsDouble();

    //        return weightedSampleStream.mapToDouble(ws -> ws.logWeight - Math.log(sampleSize)).reduce(this::robustSumOfLogarithms).getAsDouble();

    //logProbOfEvidence = robustSumOfLogarithms(logProbOfEvidence,-Math.log(sampleSize));

    if (evidence != null) {
        logProbOfEvidence = logSumWeights - Math.log(sampleSize);
    } else {
        logProbOfEvidence = 0;
    }

    //        if(saveDataOnMemory_) {
    //            weightedSampleList = weightedSampleStream.collect(Collectors.toList());
    //            //weightedSampleList.forEach(weightedAssignment -> System.out.println("Weight: " + weightedAssignment.logWeight + " for assignment " + weightedAssignment.assignment.getValue(this.model.getVariables().getListOfVariables().get(0)) + " prob " + model.getLogProbabiltyOf(weightedAssignment.assignment)));
    //        }

}