Example usage for java.util Collections shuffle

List of usage examples for java.util Collections shuffle

Introduction

In this page you can find the example usage for java.util Collections shuffle.

Prototype

@SuppressWarnings({ "rawtypes", "unchecked" })
public static void shuffle(List<?> list, Random rnd) 

Source Link

Document

Randomly permute the specified list using the specified source of randomness.

Usage

From source file:cc.kave.commons.pointsto.evaluation.cv.AbstractCVFoldBuilder.java

protected void shuffleUsages(Iterable<List<Usage>> usageLists) {
    Random rnd = new Random(rndGenerator.nextLong());
    for (List<Usage> usages : usageLists) {
        Collections.shuffle(usages, rnd);
    }/* www. j ava 2s  . co  m*/
}

From source file:de.julielab.jtbd.TokenizerApplication.java

/**
 * 90-10 split evaluation//from   ww w.  jav  a  2  s.  co m
 *
 * @param orgSentencesFile
 * @param tokSentencesFile
 * @param errors
 * @param predictions
 * @return
 */
private static EvalResult do9010Evaluation(final File orgSentencesFile, final File tokSentencesFile,
        final ArrayList<String> errors, final ArrayList<String> predictions) {

    final ArrayList<String> orgSentences = readFile(orgSentencesFile);
    final ArrayList<String> tokSentences = readFile(tokSentencesFile);

    final long seed = 1;
    Collections.shuffle(orgSentences, new Random(seed));
    Collections.shuffle(tokSentences, new Random(seed));

    final int sizeAll = orgSentences.size();
    final int sizeTest = (int) (sizeAll * 0.1);
    final int sizeTrain = sizeAll - sizeTest;

    if (sizeTest == 0) {
        System.err.println("Error: no test files for this split.");
        System.exit(-1);
    }
    System.out.println("all: " + sizeAll + "\ttrain: " + sizeTrain + "\t" + "test: " + sizeTest);

    final ArrayList<String> trainOrgSentences = new ArrayList<String>();
    final ArrayList<String> trainTokSentences = new ArrayList<String>();
    final ArrayList<String> predictOrgSentences = new ArrayList<String>();
    final ArrayList<String> predictTokSentences = new ArrayList<String>();

    for (int i = 0; i < sizeTrain; i++) {

        trainOrgSentences.add(orgSentences.get(i));
        trainTokSentences.add(tokSentences.get(i));
    }

    for (int i = sizeTrain; i < sizeAll; i++) {
        predictOrgSentences.add(orgSentences.get(i));
        predictTokSentences.add(tokSentences.get(i));
    }

    // System.out.println(trainOrgSentences.toString());
    // System.out.println(trainTokSentences.toString());
    // System.out.println(predictOrgSentences.toString());
    // System.out.println(predictTokSentences.toString());
    return doEvaluation(trainOrgSentences, trainTokSentences, predictOrgSentences, predictTokSentences,
            predictions, errors);

}

From source file:cc.kave.commons.pointsto.evaluation.UsagePruning.java

/**
 * @return The number of pruned usages// w  ww .  j  a v a2 s  .c om
 */
public int prune(final int maxUsages, Map<ProjectIdentifier, List<Usage>> usages) {
    final int initialNumUsages = usages.values().stream().mapToInt(Collection::size).sum();
    int numUsages = initialNumUsages;

    if (numUsages > maxUsages) {
        Random rnd = new Random(rndGenerator.nextLong());
        List<Pair<ProjectIdentifier, Double>> projectUsageCounts = new ArrayList<>(usages.size());
        for (Map.Entry<ProjectIdentifier, List<Usage>> entry : usages.entrySet()) {
            projectUsageCounts.add(Pair.create(entry.getKey(), (double) entry.getValue().size()));
            Collections.shuffle(entry.getValue(), rnd);
        }
        EnumeratedDistribution<ProjectIdentifier> distribution = new EnumeratedDistribution<>(rndGenerator,
                projectUsageCounts);

        while (numUsages > maxUsages) {
            ProjectIdentifier project = distribution.sample();
            List<Usage> projectUsages = usages.get(project);
            if (!projectUsages.isEmpty()) {
                projectUsages.remove(projectUsages.size() - 1);
                --numUsages;
            }
        }
    }

    return initialNumUsages - numUsages;
}

From source file:org.elasticsearch.client.NodeSelectorTests.java

public void testNotMasterOnly() {
    Node masterOnly = dummyNode(true, false, false);
    Node all = dummyNode(true, true, true);
    Node masterAndData = dummyNode(true, true, false);
    Node masterAndIngest = dummyNode(true, false, true);
    Node coordinatingOnly = dummyNode(false, false, false);
    Node ingestOnly = dummyNode(false, false, true);
    Node data = dummyNode(false, true, randomBoolean());
    List<Node> nodes = new ArrayList<>();
    nodes.add(masterOnly);//from   w w w. ja v a 2  s.  c o m
    nodes.add(all);
    nodes.add(masterAndData);
    nodes.add(masterAndIngest);
    nodes.add(coordinatingOnly);
    nodes.add(ingestOnly);
    nodes.add(data);
    Collections.shuffle(nodes, getRandom());
    List<Node> expected = new ArrayList<>(nodes);
    expected.remove(masterOnly);
    NodeSelector.SKIP_DEDICATED_MASTERS.select(nodes);
    assertEquals(expected, nodes);
}

From source file:edu.byu.nlp.util.IntArrays.java

public static int[] shuffled(int[] arr, RandomGenerator rnd) {
    // int[] -> List
    List<Integer> tmp = Lists.newArrayListWithCapacity(arr.length);
    for (int i = 0; i < arr.length; i++) {
        tmp.add(arr[i]);// www  . ja  v a  2 s . co m
    }
    // shuffle
    Collections.shuffle(tmp, new RandomAdaptor(rnd));
    // List -> int[] (wish there were a better way to do this)
    int[] arr2 = new int[tmp.size()];
    for (int i = 0; i < tmp.size(); i++) {
        arr2[i] = tmp.get(i);
    }
    return arr2;
}

From source file:org.messic.server.api.randomlists.GenreRandomListPlugin.java

@Override
public RandomList getRandomList(User user) {
    List<MDOGenre> randomGenreList = daoGenre.getRandomGenre(user.getLogin(), 1);
    if (randomGenreList != null && randomGenreList.size() > 0) {
        List<MDOAlbum> albumList = daoAlbum.getAll(user.getLogin(), randomGenreList.get(0));

        RandomList rl = new RandomList("RandomListName-Genre", "RandomListTitle-Genre");
        rl.addDetail(randomGenreList.get(0).getName());

        for (int i = 0; i < albumList.size() && i < MAX_ELEMENTS; i++) {
            for (MDOSong mdoSong : albumList.get(i).getSongs()) {
                Song song = new Song(mdoSong, true, true);
                rl.addSong(song);/*from  w w  w  .  ja  v  a  2 s.c  o m*/
            }

        }

        long seed = System.nanoTime();
        if (rl.getSongs() != null) {
            Collections.shuffle(rl.getSongs(), new Random(seed));
        }
        return rl;
    }
    return null;
}

From source file:com.ibm.bluej.commonutil.PrecisionRecallThreshold.java

public SummaryScores computeSummaryScores(double limitProb) {
    if (limitProb < 0.5) {
        throw new IllegalArgumentException("Probabilities cannot be limited to below 50%");
    }/* w ww.  java2  s  .  c  o m*/
    SummaryScores sum = new SummaryScores();
    Collections.shuffle(scoredPlusGold, RANDOMNESS);
    Collections.sort(scoredPlusGold, new FirstPairComparator(null));
    Collections.reverse(scoredPlusGold);

    double tpRelative = 0;
    double fpRelative = 0;

    double cummulativeCorrect = 0;
    double auc = 0;
    double allPositive = 0;
    double averageScore = 0;
    for (Pair<Double, Boolean> p : scoredPlusGold) {
        if (p.second)
            allPositive += 1;
        averageScore += p.first;
    }
    averageScore /= scoredPlusGold.size();
    sum.relativeThreshold = averageScore;

    double maxF = 0;
    double maxFThresh = 0;
    double logLike = 0;
    double maxAcc = 0;
    double maxAccThresh = 0;
    sum.relativePrecision = Double.NaN;
    for (int i = 0; i < scoredPlusGold.size(); ++i) {
        Pair<Double, Boolean> p = scoredPlusGold.get(i);
        if (p.second) {
            ++cummulativeCorrect;
            auc += cummulativeCorrect / ((i + 1) * allPositive);
        }

        double tp = cummulativeCorrect;
        double fp = (i + 1) - cummulativeCorrect;
        double fn = allPositive - cummulativeCorrect;
        double tn = (scoredPlusGold.size() - (i + 1)) - fn;
        double precision = (double) (tp) / (tp + fp);
        double recall = (double) (tp) / (tp + fn);
        double accuracy = (tp + tn) / scoredPlusGold.size();
        double f1 = 2 * precision * recall / (precision + recall);

        if (p.second) {
            if (p.first > sum.relativeThreshold)
                tpRelative++;
        } else {
            if (p.first > sum.relativeThreshold)
                fpRelative++;
        }

        if (f1 > maxF) {
            maxF = f1;
            maxFThresh = p.first;
        }
        if (accuracy > maxAcc) {
            maxAcc = accuracy;
            maxAccThresh = p.first;
        }

        double prob = p.second ? p.first : 1 - p.first;
        if (prob < 0 || prob > 1) {
            logLike = Double.NaN;
        }
        if (prob > limitProb) {
            prob = limitProb;
        }
        if (prob < 1 - limitProb) {
            prob = 1 - limitProb;
        }
        logLike += Math.log(prob);
    }

    sum.maxFScore = maxF;
    sum.maxFScoreThreshold = maxFThresh;
    sum.auc = auc;
    sum.pearsonsR = pearsonsR();
    sum.logLikelihood = logLike;
    sum.maxAccuracy = maxAcc;
    sum.maxAccuracyThreshold = maxAccThresh;

    sum.relativePrecision = tpRelative
            / (tpRelative + (fpRelative * (allPositive / (scoredPlusGold.size() - allPositive))));
    if (Double.isNaN(sum.relativePrecision))
        sum.relativePrecision = 0;
    sum.relativeRecall = tpRelative / allPositive;
    sum.relativeFScore = 2 * sum.relativePrecision * sum.relativeRecall
            / (sum.relativePrecision + sum.relativeRecall);
    if (Double.isNaN(sum.relativeFScore))
        sum.relativeFScore = 0;

    return sum;
}

From source file:emlab.repository.AbstractRepository.java

public Iterable<T> findAllAtRandom() {
    List<T> list = Utils.asList(findAll());
    Collections.shuffle(list, new Random());
    return list;
}

From source file:org.messic.server.api.randomlists.AuthorRandomListPlugin.java

@Override
public RandomList getRandomList(User user) {
    // second list, getting all the songs of an author
    List<MDOAuthor> randomAuthorList = daoAuthor.getRandomAuthors(user.getLogin(), 1);
    if (randomAuthorList != null && randomAuthorList.size() > 0) {
        RandomList rl = new RandomList("RandomListName-Author", "RandomListTitle-Author");
        rl.addDetail(randomAuthorList.get(0).getName());
        Iterator<MDOAlbum> albumsit = randomAuthorList.get(0).getAlbums().iterator();
        while (albumsit.hasNext()) {
            MDOAlbum album = albumsit.next();
            List<MDOSong> songs = album.getSongs();
            for (int i = 0; i < songs.size() && rl.getSongs().size() < MAX_ELEMENTS; i++) {
                MDOSong mdoSong = songs.get(i);
                Song song = new Song(mdoSong, true, true);
                rl.addSong(song);//from  ww w.ja va  2 s.  co m
            }
        }
        long seed = System.nanoTime();
        if (rl.getSongs() != null) {
            Collections.shuffle(rl.getSongs(), new Random(seed));
        }
        return rl;
    }
    return null;
}

From source file:org.messic.server.api.randomlists.DateRandomListPlugin.java

@Override
public RandomList getRandomList(User user) {
    // int year = Calendar.getInstance().get( Calendar.YEAR );
    int fromYear = daoAlbum.findOldestAlbum(user.getLogin()); // Util.randInt( 1920, year );
    int toYear = fromYear + 10;// Util.randInt( fromYear, year );
    List<MDOAlbum> albums = daoAlbum.findAlbumsBasedOnDate(user.getLogin(), fromYear, toYear);

    if (albums != null && albums.size() > 0) {
        RandomList rl = new RandomList("RandomListName-Date", "RandomListTitle-Date");
        rl.addDetail(fromYear + " - " + toYear);

        for (int i = 0; i < albums.size() && rl.getSongs().size() < MAX_ELEMENTS; i++) {
            List<MDOSong> songs = albums.get(i).getSongs();
            for (int j = 0; j < songs.size() && rl.getSongs().size() < MAX_ELEMENTS; j++) {
                MDOSong mdoSong = songs.get(j);
                Song song = new Song(mdoSong, true, true);
                rl.addSong(song);//from  w w w . j  a va  2  s.  c o m
            }

        }

        long seed = System.nanoTime();
        if (rl.getSongs() != null) {
            Collections.shuffle(rl.getSongs(), new Random(seed));
        }
        return rl;
    }
    return null;
}