Example usage for java.util PriorityQueue PriorityQueue

List of usage examples for java.util PriorityQueue PriorityQueue

Introduction

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

Prototype

public PriorityQueue() 

Source Link

Document

Creates a PriorityQueue with the default initial capacity (11) that orders its elements according to their Comparable natural ordering .

Usage

From source file:com.google.uzaygezen.core.BoundedRollup.java

private BoundedRollup(int maxNodes) {
    Preconditions.checkArgument(maxNodes > 0, "maxNodes must be positive");
    this.maxNodes = maxNodes;
    this.minHeap = new PriorityQueue<ComparableTreeNode>();
}

From source file:dk.statsbiblioteket.doms.integration.summa.SummaRecordIterator.java

SummaRecordIterator(DomsWSClient domsClient, Map<String, BaseDOMSConfiguration> baseConfigurations,
        Set<String> summaBaseIDs, long timeStamp, QueryOptions options) {

    this.domsClient = domsClient;
    this.baseConfigurations = baseConfigurations;
    startTimeStamp = timeStamp;/* w  ww .j  a  v  a  2  s  . c o m*/
    queryOptions = options;
    baseRecordDescriptions = new PriorityQueue<BaseRecordDescription>();
    baseStates = createBaseStatesMap(summaBaseIDs);
}

From source file:com.ccc.crest.core.client.CrestClient.java

public CrestClient(CrestController controller, String crestUrl, String xmlUrl, String userAgent,
        ExecutorService executor) {
    log = LoggerFactory.getLogger(getClass());
    this.controller = controller;
    this.crestUrl = StrH.stripTrailingSeparator(crestUrl, '/');
    this.xmlUrl = StrH.stripTrailingSeparator(xmlUrl, '/');
    this.userAgent = userAgent;
    refreshQueue = new PriorityQueue<>();
    this.executor = executor;
    crestClients = new ArrayList<>();
    xmlClients = new ArrayList<>();
    //        crestClientIndex = new AtomicInteger(-1);
    xmlClientIndex = new AtomicInteger(-1);
    for (int i = 0; i < CrestMaxClients; i++) {
        RequestThrottle crestGeneralThrottle = new RequestThrottle(CrestGeneralMaxRequestsPerSecond);
        RequestThrottle xmlGeneralThrottle = new RequestThrottle(XmlGeneralMaxRequestsPerSecond);
        CloseableHttpClient client = HttpClients.custom().setUserAgent(userAgent).build();
        ClientElement clientElement = new ClientElement(client, crestGeneralThrottle, xmlGeneralThrottle);
        crestClients.add(clientElement);
    }//  w w  w.  ja  v a2 s  .co m
    for (int i = 0; i < CrestMaxClients; i++) {
        RequestThrottle xmlcrestGeneralThrottle = new RequestThrottle(CrestGeneralMaxRequestsPerSecond);
        RequestThrottle xmlGeneralThrottle = new RequestThrottle(XmlGeneralMaxRequestsPerSecond);
        CloseableHttpClient client = HttpClients.custom().setUserAgent(userAgent).build();
        ClientElement clientElement = new ClientElement(client, xmlGeneralThrottle, xmlGeneralThrottle);
        xmlClients.add(clientElement);
    }
}

From source file:mulavito.algorithms.shortestpath.ksp.Yen.java

@Override
protected List<List<E>> getShortestPathsIntern(final V source, final V target, int k) {
    LinkedList<List<E>> found_paths = new LinkedList<List<E>>();
    PriorityQueue<WeightedPath> prioQ = new PriorityQueue<WeightedPath>();
    DijkstraShortestPath<V, E> blockedDijkstra;

    // Check if target is reachable from source.
    if (dijkstra.getDistance(source, target) == null)
        return found_paths;

    // Add Dijkstra solution, the first shortest path.
    found_paths.add(dijkstra.getPath(source, target));

    while (found_paths.size() < k) {
        List<E> curShortestPath = found_paths.getLast();

        int maxIndex = curShortestPath.size();

        List<V> curShortestPathNodes = new LinkedList<V>();
        curShortestPathNodes.add(source);
        for (E e : found_paths.getLast()) {
            V v = graph.getEndpoints(e).getFirst();
            if (!curShortestPathNodes.contains(v))
                curShortestPathNodes.add(v);
            v = graph.getEndpoints(e).getSecond();
            if (!curShortestPathNodes.contains(v))
                curShortestPathNodes.add(v);
        }/*w ww .j  av a 2  s . co m*/
        curShortestPathNodes.remove(target);

        // Split path into Head and NextEdge
        for (int i = 0; i < maxIndex; i++) {
            List<E> head = curShortestPath.subList(0, i);
            //   V deviation = head.isEmpty() ? source : graph.getEndpoints(head.get(i - 1)).getSecond();
            V deviation = curShortestPathNodes.get(i);

            // 1. Block edges.
            Graph<V, E> blocked = blockFilter(head, deviation, curShortestPathNodes, found_paths);

            // 2. Get shortest path in graph with blocked edges.
            blockedDijkstra = new DijkstraShortestPath<V, E>(blocked, nev);

            Number dist = blockedDijkstra.getDistance(deviation, target);
            if (dist == null)
                continue;

            List<E> tail = blockedDijkstra.getPath(deviation, target);

            // 3. Combine head and tail into new path.
            List<E> candidate = new ArrayList<E>();
            candidate.addAll(head);
            candidate.addAll(tail);

            // Check if we already found this solution
            boolean duplicate = false;
            for (WeightedPath path : prioQ)
                if (ListUtils.isEqualList(path.getPath(), candidate)) {
                    duplicate = true;
                    break;
                }

            if (!duplicate)
                prioQ.add(new WeightedPath(candidate));
        }

        if (prioQ.isEmpty())
            break; // We have not found any new candidate!
        else
            found_paths.add(prioQ.poll().getPath());
    }

    return found_paths;
}

From source file:net.spfbl.core.Huffman.java

private static Huffman buildTree(int[] frequency) {
    PriorityQueue<Huffman> queue = new PriorityQueue<Huffman>();
    for (char i = 0; i < 256; i++) {
        if (frequency[i] > 0) {
            queue.add(new Huffman(i, frequency[i], null, null));
        }/*from w  ww .  j  a  va 2s  .  com*/
    }
    if (queue.size() == 1) {
        if (frequency['\0'] == 0) {
            queue.add(new Huffman('\0', 0, null, null));
        } else {
            queue.add(new Huffman('\1', 0, null, null));
        }
    }
    while (queue.size() > 1) {
        Huffman left = queue.poll();
        Huffman right = queue.poll();
        Huffman parent = new Huffman('\0', left.frequency + right.frequency, left, right);
        queue.add(parent);
    }
    return queue.poll();
}

From source file:com.joliciel.talismane.posTagger.PosTaggerImpl.java

@Override
public List<PosTagSequence> tagSentence(List<TokenSequence> tokenSequences) {
    MONITOR.startTask("tagSentence");
    try {//from   w  w w  .  j a  va2s.  c  o m
        MONITOR.startTask("apply filters");
        try {
            for (TokenSequence tokenSequence : tokenSequences) {
                for (TokenSequenceFilter tokenFilter : this.preProcessingFilters) {
                    tokenFilter.apply(tokenSequence);
                }
            }
        } finally {
            MONITOR.endTask("apply filters");
        }
        int sentenceLength = tokenSequences.get(0).getText().length();

        TreeMap<Double, PriorityQueue<PosTagSequence>> heaps = new TreeMap<Double, PriorityQueue<PosTagSequence>>();

        PriorityQueue<PosTagSequence> heap0 = new PriorityQueue<PosTagSequence>();
        for (TokenSequence tokenSequence : tokenSequences) {
            // add an empty PosTagSequence for each token sequence
            PosTagSequence emptySequence = this.getPosTaggerService().getPosTagSequence(tokenSequence, 0);
            emptySequence.setScoringStrategy(decisionMaker.getDefaultScoringStrategy());
            heap0.add(emptySequence);
        }
        heaps.put(0.0, heap0);

        PriorityQueue<PosTagSequence> finalHeap = null;
        while (heaps.size() > 0) {
            Entry<Double, PriorityQueue<PosTagSequence>> heapEntry = heaps.pollFirstEntry();
            if (LOG.isTraceEnabled()) {
                LOG.trace("heap key: " + heapEntry.getKey() + ", sentence length: " + sentenceLength);
            }
            if (heapEntry.getKey() == sentenceLength) {
                finalHeap = heapEntry.getValue();
                break;
            }
            PriorityQueue<PosTagSequence> previousHeap = heapEntry.getValue();

            // limit the breadth to K
            int maxSequences = previousHeap.size() > this.beamWidth ? this.beamWidth : previousHeap.size();

            for (int j = 0; j < maxSequences; j++) {
                PosTagSequence history = previousHeap.poll();
                Token token = history.getNextToken();
                if (LOG.isTraceEnabled()) {
                    LOG.trace("#### Next history ( " + heapEntry.getKey() + "): " + history.toString());
                    LOG.trace("Prob: " + df.format(history.getScore()));
                    LOG.trace("Token: " + token.getText());

                    StringBuilder sb = new StringBuilder();
                    for (Token oneToken : history.getTokenSequence().listWithWhiteSpace()) {
                        if (oneToken.equals(token))
                            sb.append("[" + oneToken + "]");
                        else
                            sb.append(oneToken);
                    }
                    LOG.trace(sb.toString());
                }

                PosTaggerContext context = this.getPosTaggerFeatureService().getContext(token, history);
                List<Decision<PosTag>> decisions = new ArrayList<Decision<PosTag>>();

                // test the positive rules on the current token
                boolean ruleApplied = false;
                if (posTaggerPositiveRules != null) {
                    MONITOR.startTask("check rules");
                    try {
                        for (PosTaggerRule rule : posTaggerPositiveRules) {
                            if (LOG.isTraceEnabled()) {
                                LOG.trace("Checking rule: " + rule.getCondition().getName());
                            }
                            RuntimeEnvironment env = this.featureService.getRuntimeEnvironment();
                            FeatureResult<Boolean> ruleResult = rule.getCondition().check(context, env);
                            if (ruleResult != null && ruleResult.getOutcome()) {
                                Decision<PosTag> positiveRuleDecision = TalismaneSession.getPosTagSet()
                                        .createDefaultDecision(rule.getTag());
                                decisions.add(positiveRuleDecision);
                                positiveRuleDecision.addAuthority(rule.getCondition().getName());
                                ruleApplied = true;
                                if (LOG.isTraceEnabled()) {
                                    LOG.trace("Rule applies. Setting posTag to: " + rule.getTag().getCode());
                                }
                                break;
                            }
                        }
                    } finally {
                        MONITOR.endTask("check rules");
                    }
                }

                if (!ruleApplied) {
                    // test the features on the current token
                    List<FeatureResult<?>> featureResults = new ArrayList<FeatureResult<?>>();
                    MONITOR.startTask("analyse features");
                    try {
                        for (PosTaggerFeature<?> posTaggerFeature : posTaggerFeatures) {
                            MONITOR.startTask(posTaggerFeature.getCollectionName());
                            try {
                                RuntimeEnvironment env = this.featureService.getRuntimeEnvironment();
                                FeatureResult<?> featureResult = posTaggerFeature.check(context, env);
                                if (featureResult != null)
                                    featureResults.add(featureResult);
                            } finally {
                                MONITOR.endTask(posTaggerFeature.getCollectionName());
                            }
                        }
                        if (LOG.isTraceEnabled()) {
                            for (FeatureResult<?> result : featureResults) {
                                LOG.trace(result.toString());
                            }
                        }
                    } finally {
                        MONITOR.endTask("analyse features");
                    }

                    // evaluate the feature results using the maxent model
                    MONITOR.startTask("make decision");
                    decisions = this.decisionMaker.decide(featureResults);
                    MONITOR.endTask("make decision");

                    for (ClassificationObserver<PosTag> observer : this.observers) {
                        observer.onAnalyse(token, featureResults, decisions);
                    }

                    // apply the negative rules
                    Set<PosTag> eliminatedPosTags = new TreeSet<PosTag>();
                    if (posTaggerNegativeRules != null) {
                        MONITOR.startTask("check negative rules");
                        try {
                            for (PosTaggerRule rule : posTaggerNegativeRules) {
                                if (LOG.isTraceEnabled()) {
                                    LOG.trace("Checking negative rule: " + rule.getCondition().getName());
                                }
                                RuntimeEnvironment env = this.featureService.getRuntimeEnvironment();
                                FeatureResult<Boolean> ruleResult = rule.getCondition().check(context, env);
                                if (ruleResult != null && ruleResult.getOutcome()) {
                                    eliminatedPosTags.add(rule.getTag());
                                    if (LOG.isTraceEnabled()) {
                                        LOG.trace(
                                                "Rule applies. Eliminating posTag: " + rule.getTag().getCode());
                                    }
                                }
                            }

                            if (eliminatedPosTags.size() > 0) {
                                List<Decision<PosTag>> decisionShortList = new ArrayList<Decision<PosTag>>();
                                for (Decision<PosTag> decision : decisions) {
                                    if (!eliminatedPosTags.contains(decision.getOutcome())) {
                                        decisionShortList.add(decision);
                                    } else {
                                        LOG.trace("Eliminating decision: " + decision.toString());
                                    }
                                }
                                if (decisionShortList.size() > 0) {
                                    decisions = decisionShortList;
                                } else {
                                    LOG.debug("All decisions eliminated! Restoring original decisions.");
                                }
                            }
                        } finally {
                            MONITOR.endTask("check negative rules");
                        }
                    }

                    // is this a known word in the lexicon?
                    MONITOR.startTask("apply constraints");
                    try {
                        if (LOG.isTraceEnabled()) {
                            String posTags = "";
                            for (PosTag onePosTag : token.getPossiblePosTags()) {
                                posTags += onePosTag.getCode() + ",";
                            }
                            LOG.trace("Token: " + token.getText() + ". PosTags: " + posTags);
                        }

                        List<Decision<PosTag>> decisionShortList = new ArrayList<Decision<PosTag>>();

                        for (Decision<PosTag> decision : decisions) {
                            if (decision.getProbability() >= MIN_PROB_TO_STORE) {
                                decisionShortList.add(decision);
                            }
                        }
                        if (decisionShortList.size() > 0) {
                            decisions = decisionShortList;
                        }
                    } finally {
                        MONITOR.endTask("apply constraints");
                    }
                } // has a rule been applied?

                // add new TaggedTokenSequences to the heap, one for each outcome provided by MaxEnt
                MONITOR.startTask("heap sort");
                for (Decision<PosTag> decision : decisions) {
                    if (LOG.isTraceEnabled())
                        LOG.trace("Outcome: " + decision.getOutcome() + ", " + decision.getProbability());

                    PosTaggedToken posTaggedToken = this.getPosTaggerService().getPosTaggedToken(token,
                            decision);
                    PosTagSequence sequence = this.getPosTaggerService().getPosTagSequence(history);
                    sequence.addPosTaggedToken(posTaggedToken);
                    if (decision.isStatistical())
                        sequence.addDecision(decision);

                    double heapIndex = token.getEndIndex();
                    // add another half for an empty token, to differentiate it from regular ones
                    if (token.getStartIndex() == token.getEndIndex())
                        heapIndex += 0.5;

                    // if it's the last token, make sure we end
                    if (token.getIndex() == sequence.getTokenSequence().size() - 1)
                        heapIndex = sentenceLength;

                    if (LOG.isTraceEnabled())
                        LOG.trace("Heap index: " + heapIndex);

                    PriorityQueue<PosTagSequence> heap = heaps.get(heapIndex);
                    if (heap == null) {
                        heap = new PriorityQueue<PosTagSequence>();
                        heaps.put(heapIndex, heap);
                    }
                    heap.add(sequence);
                } // next outcome for this token
                MONITOR.endTask("heap sort");
            } // next history      
        } // next atomic index
          // return the best sequence on the heap
        List<PosTagSequence> sequences = new ArrayList<PosTagSequence>();
        int i = 0;
        while (!finalHeap.isEmpty()) {
            sequences.add(finalHeap.poll());
            i++;
            if (i >= this.getBeamWidth())
                break;
        }

        // apply post-processing filters
        LOG.debug("####Final postag sequences:");
        int j = 1;
        for (PosTagSequence sequence : sequences) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Sequence " + (j++) + ", score=" + df.format(sequence.getScore()));
                LOG.debug("Sequence before filters: " + sequence);
            }
            for (PosTagSequenceFilter filter : this.postProcessingFilters)
                filter.apply(sequence);

            if (LOG.isDebugEnabled()) {
                LOG.debug("Sequence after filters: " + sequence);
            }
        }

        return sequences;
    } finally {
        MONITOR.endTask("tagSentence");
    }
}

From source file:edu.snu.leader.hierarchy.simple.test.TestIndividual.java

/**
 * Initialize the individual//w w  w.  j av  a  2  s  .  c  o m
 *
 * @param allIndividuals
 */
public void initialize(List<TestIndividual> allIndividuals) {
    // Basically, we just need to find our neighbors
    // Build a priority queue to sort things for us
    PriorityQueue<TestNeighbor> sortedNeighbors = new PriorityQueue<TestNeighbor>();

    // Iterate through all the individuals
    Iterator<TestIndividual> indIter = allIndividuals.iterator();
    while (indIter.hasNext()) {
        // Get the individual
        TestIndividual ind = indIter.next();

        // If it is us, continue on
        if (_id.equals(ind._id)) {
            continue;
        }

        // Build a neighbor out of it and put it in the queue
        TestNeighbor neighbor = new TestNeighbor((float) _location.distance(ind._location), ind);
        sortedNeighbors.add(neighbor);
    }

    // Get the "nearest" neighbors
    int count = Math.min(sortedNeighbors.size(), _nearestNeighborCount);
    for (int i = 0; i < count; i++) {
        _nearestNeighbors.add(sortedNeighbors.poll());
    }
}

From source file:com.joliciel.talismane.parser.TransitionBasedParserImpl.java

@Override
public List<ParseConfiguration> parseSentence(List<PosTagSequence> posTagSequences) {
    MONITOR.startTask("parseSentence");
    try {//  w ww  .  j  av  a 2 s  .c  om
        long startTime = (new Date()).getTime();
        int maxAnalysisTimeMilliseconds = maxAnalysisTimePerSentence * 1000;
        int minFreeMemoryBytes = minFreeMemory * KILOBYTE;

        TokenSequence tokenSequence = posTagSequences.get(0).getTokenSequence();

        TreeMap<Integer, PriorityQueue<ParseConfiguration>> heaps = new TreeMap<Integer, PriorityQueue<ParseConfiguration>>();

        PriorityQueue<ParseConfiguration> heap0 = new PriorityQueue<ParseConfiguration>();
        for (PosTagSequence posTagSequence : posTagSequences) {
            // add an initial ParseConfiguration for each postag sequence
            ParseConfiguration initialConfiguration = this.getParserServiceInternal()
                    .getInitialConfiguration(posTagSequence);
            initialConfiguration.setScoringStrategy(decisionMaker.getDefaultScoringStrategy());
            heap0.add(initialConfiguration);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Adding initial posTagSequence: " + posTagSequence);
            }
        }
        heaps.put(0, heap0);
        PriorityQueue<ParseConfiguration> backupHeap = null;

        PriorityQueue<ParseConfiguration> finalHeap = null;
        PriorityQueue<ParseConfiguration> terminalHeap = new PriorityQueue<ParseConfiguration>();
        while (heaps.size() > 0) {
            Entry<Integer, PriorityQueue<ParseConfiguration>> heapEntry = heaps.pollFirstEntry();
            PriorityQueue<ParseConfiguration> currentHeap = heapEntry.getValue();
            int currentHeapIndex = heapEntry.getKey();
            if (LOG.isTraceEnabled()) {
                LOG.trace("##### Polling next heap: " + heapEntry.getKey() + ", size: "
                        + heapEntry.getValue().size());
            }

            boolean finished = false;
            // systematically set the final heap here, just in case we exit "naturally" with no more heaps
            finalHeap = heapEntry.getValue();
            backupHeap = new PriorityQueue<ParseConfiguration>();

            // we jump out when either (a) all tokens have been attached or (b) we go over the max alloted time
            ParseConfiguration topConf = currentHeap.peek();
            if (topConf.isTerminal()) {
                LOG.trace("Exiting with terminal heap: " + heapEntry.getKey() + ", size: "
                        + heapEntry.getValue().size());
                finished = true;
            }

            if (earlyStop && terminalHeap.size() >= beamWidth) {
                LOG.debug(
                        "Early stop activated and terminal heap contains " + beamWidth + " entries. Exiting.");
                finalHeap = terminalHeap;
                finished = true;
            }

            long analysisTime = (new Date()).getTime() - startTime;
            if (maxAnalysisTimePerSentence > 0 && analysisTime > maxAnalysisTimeMilliseconds) {
                LOG.info("Parse tree analysis took too long for sentence: " + tokenSequence.getText());
                LOG.info("Breaking out after " + maxAnalysisTimePerSentence + " seconds.");
                finished = true;
            }

            if (minFreeMemory > 0) {
                long freeMemory = Runtime.getRuntime().freeMemory();
                if (freeMemory < minFreeMemoryBytes) {
                    LOG.info("Not enough memory left to parse sentence: " + tokenSequence.getText());
                    LOG.info("Min free memory (bytes):" + minFreeMemoryBytes);
                    LOG.info("Current free memory (bytes): " + freeMemory);
                    finished = true;
                }
            }

            if (finished) {
                break;
            }

            // limit the breadth to K
            int maxSequences = currentHeap.size() > this.beamWidth ? this.beamWidth : currentHeap.size();

            int j = 0;
            while (currentHeap.size() > 0) {
                ParseConfiguration history = currentHeap.poll();
                if (LOG.isTraceEnabled()) {
                    LOG.trace("### Next configuration on heap " + heapEntry.getKey() + ":");
                    LOG.trace(history.toString());
                    LOG.trace("Score: " + df.format(history.getScore()));
                    LOG.trace(history.getPosTagSequence());
                }

                List<Decision<Transition>> decisions = new ArrayList<Decision<Transition>>();

                // test the positive rules on the current configuration
                boolean ruleApplied = false;
                if (parserPositiveRules != null) {
                    MONITOR.startTask("check rules");
                    try {
                        for (ParserRule rule : parserPositiveRules) {
                            if (LOG.isTraceEnabled()) {
                                LOG.trace("Checking rule: " + rule.toString());
                            }
                            RuntimeEnvironment env = this.featureService.getRuntimeEnvironment();
                            FeatureResult<Boolean> ruleResult = rule.getCondition().check(history, env);
                            if (ruleResult != null && ruleResult.getOutcome()) {
                                Decision<Transition> positiveRuleDecision = TalismaneSession
                                        .getTransitionSystem().createDefaultDecision(rule.getTransition());
                                decisions.add(positiveRuleDecision);
                                positiveRuleDecision.addAuthority(rule.getCondition().getName());
                                ruleApplied = true;
                                if (LOG.isTraceEnabled()) {
                                    LOG.trace("Rule applies. Setting transition to: "
                                            + rule.getTransition().getCode());
                                }
                                break;
                            }
                        }
                    } finally {
                        MONITOR.endTask("check rules");
                    }
                }

                if (!ruleApplied) {
                    // test the features on the current configuration
                    List<FeatureResult<?>> parseFeatureResults = new ArrayList<FeatureResult<?>>();
                    MONITOR.startTask("feature analyse");
                    try {
                        for (ParseConfigurationFeature<?> feature : this.parseFeatures) {
                            MONITOR.startTask(feature.getName());
                            try {
                                RuntimeEnvironment env = this.featureService.getRuntimeEnvironment();
                                FeatureResult<?> featureResult = feature.check(history, env);
                                if (featureResult != null)
                                    parseFeatureResults.add(featureResult);
                            } finally {
                                MONITOR.endTask(feature.getName());
                            }
                        }
                        if (LOG_FEATURES.isTraceEnabled()) {
                            for (FeatureResult<?> featureResult : parseFeatureResults) {
                                LOG_FEATURES.trace(featureResult.toString());
                            }
                        }
                    } finally {
                        MONITOR.endTask("feature analyse");
                    }

                    // evaluate the feature results using the decision maker
                    MONITOR.startTask("make decision");
                    try {
                        decisions = this.decisionMaker.decide(parseFeatureResults);

                        for (ClassificationObserver<Transition> observer : this.observers) {
                            observer.onAnalyse(history, parseFeatureResults, decisions);
                        }

                        List<Decision<Transition>> decisionShortList = new ArrayList<Decision<Transition>>(
                                decisions.size());
                        for (Decision<Transition> decision : decisions) {
                            if (decision.getProbability() > MIN_PROB_TO_STORE)
                                decisionShortList.add(decision);
                        }
                        decisions = decisionShortList;
                    } finally {
                        MONITOR.endTask("make decision");
                    }

                    // apply the negative rules
                    Set<Transition> eliminatedTransitions = new HashSet<Transition>();
                    if (parserNegativeRules != null) {
                        MONITOR.startTask("check negative rules");
                        try {
                            for (ParserRule rule : parserNegativeRules) {
                                if (LOG.isTraceEnabled()) {
                                    LOG.trace("Checking negative rule: " + rule.toString());
                                }
                                RuntimeEnvironment env = this.featureService.getRuntimeEnvironment();
                                FeatureResult<Boolean> ruleResult = rule.getCondition().check(history, env);
                                if (ruleResult != null && ruleResult.getOutcome()) {
                                    eliminatedTransitions.addAll(rule.getTransitions());
                                    if (LOG.isTraceEnabled()) {
                                        for (Transition eliminatedTransition : rule.getTransitions())
                                            LOG.trace("Rule applies. Eliminating transition: "
                                                    + eliminatedTransition.getCode());
                                    }
                                }
                            }

                            if (eliminatedTransitions.size() > 0) {
                                List<Decision<Transition>> decisionShortList = new ArrayList<Decision<Transition>>();
                                for (Decision<Transition> decision : decisions) {
                                    if (!eliminatedTransitions.contains(decision.getOutcome())) {
                                        decisionShortList.add(decision);
                                    } else {
                                        LOG.trace("Eliminating decision: " + decision.toString());
                                    }
                                }
                                if (decisionShortList.size() > 0) {
                                    decisions = decisionShortList;
                                } else {
                                    LOG.debug("All decisions eliminated! Restoring original decisions.");
                                }
                            }
                        } finally {
                            MONITOR.endTask("check negative rules");
                        }
                    }
                } // has a positive rule been applied?

                boolean transitionApplied = false;
                // add new configuration to the heap, one for each valid transition
                MONITOR.startTask("heap sort");
                try {
                    // Why apply all decisions here? Why not just the top N (where N = beamwidth)?
                    // Answer: because we're not always adding solutions to the same heap
                    // And yet: a decision here can only do one of two things: process a token (heap+1000), or add a non-processing transition (heap+1)
                    // So, if we've already applied N decisions of each type, we should be able to stop
                    for (Decision<Transition> decision : decisions) {
                        Transition transition = decision.getOutcome();
                        if (LOG.isTraceEnabled())
                            LOG.trace("Outcome: " + transition.getCode() + ", " + decision.getProbability());

                        if (transition.checkPreconditions(history)) {
                            transitionApplied = true;
                            ParseConfiguration configuration = this.parserServiceInternal
                                    .getConfiguration(history);
                            if (decision.isStatistical())
                                configuration.addDecision(decision);
                            transition.apply(configuration);

                            int nextHeapIndex = parseComparisonStrategy.getComparisonIndex(configuration)
                                    * 1000;
                            if (configuration.isTerminal()) {
                                nextHeapIndex = Integer.MAX_VALUE;
                            } else {
                                while (nextHeapIndex <= currentHeapIndex)
                                    nextHeapIndex++;
                            }

                            PriorityQueue<ParseConfiguration> nextHeap = heaps.get(nextHeapIndex);
                            if (nextHeap == null) {
                                if (configuration.isTerminal())
                                    nextHeap = terminalHeap;
                                else
                                    nextHeap = new PriorityQueue<ParseConfiguration>();
                                heaps.put(nextHeapIndex, nextHeap);
                                if (LOG.isTraceEnabled())
                                    LOG.trace("Created heap with index: " + nextHeapIndex);
                            }
                            nextHeap.add(configuration);
                            if (LOG.isTraceEnabled()) {
                                LOG.trace("Added configuration with score " + configuration.getScore()
                                        + " to heap: " + nextHeapIndex + ", total size: " + nextHeap.size());
                            }

                            configuration.clearMemory();
                        } else {
                            if (LOG.isTraceEnabled())
                                LOG.trace("Cannot apply transition: doesn't meet pre-conditions");
                            // just in case the we run out of both heaps and analyses, we build this backup heap
                            backupHeap.add(history);
                        } // does transition meet pre-conditions?
                    } // next transition
                } finally {
                    MONITOR.endTask("heap sort");
                }

                if (transitionApplied) {
                    j++;
                } else {
                    LOG.trace("No transitions could be applied: not counting this history as part of the beam");
                }

                // beam width test
                if (j == maxSequences)
                    break;
            } // next history   
        } // next atomic index

        // return the best sequences on the heap
        List<ParseConfiguration> bestConfigurations = new ArrayList<ParseConfiguration>();
        int i = 0;

        if (finalHeap.isEmpty())
            finalHeap = backupHeap;

        while (!finalHeap.isEmpty()) {
            bestConfigurations.add(finalHeap.poll());
            i++;
            if (i >= this.getBeamWidth())
                break;
        }
        if (LOG.isDebugEnabled()) {
            for (ParseConfiguration finalConfiguration : bestConfigurations) {
                LOG.debug(df.format(finalConfiguration.getScore()) + ": " + finalConfiguration.toString());
                LOG.debug("Pos tag sequence: " + finalConfiguration.getPosTagSequence());
                LOG.debug("Transitions: " + finalConfiguration.getTransitions());
                LOG.debug("Decisions: " + finalConfiguration.getDecisions());
                if (LOG.isTraceEnabled()) {
                    StringBuilder sb = new StringBuilder();
                    for (Decision<Transition> decision : finalConfiguration.getDecisions()) {
                        sb.append(" * ");
                        sb.append(df.format(decision.getProbability()));
                    }
                    sb.append(" root ");
                    sb.append(finalConfiguration.getTransitions().size());
                    LOG.trace(sb.toString());

                    sb = new StringBuilder();
                    sb.append(" * PosTag sequence score ");
                    sb.append(df.format(finalConfiguration.getPosTagSequence().getScore()));
                    sb.append(" = ");
                    for (PosTaggedToken posTaggedToken : finalConfiguration.getPosTagSequence()) {
                        sb.append(" * ");
                        sb.append(df.format(posTaggedToken.getDecision().getProbability()));
                    }
                    sb.append(" root ");
                    sb.append(finalConfiguration.getPosTagSequence().size());
                    LOG.trace(sb.toString());

                    sb = new StringBuilder();
                    sb.append(" * Token sequence score = ");
                    sb.append(df.format(finalConfiguration.getPosTagSequence().getTokenSequence().getScore()));
                    LOG.trace(sb.toString());

                }
            }
        }
        return bestConfigurations;
    } finally {
        MONITOR.endTask("parseSentence");
    }
}

From source file:hivemall.knn.lsh.MinHashUDTF.java

private void computeAndForwardSignatures(List<FeatureValue> features, Object[] forwardObjs)
        throws HiveException {
    final PriorityQueue<Integer> minhashes = new PriorityQueue<Integer>();
    // Compute N sets K minhash values
    for (int i = 0; i < num_hashes; i++) {
        float weightedMinHashValues = Float.MAX_VALUE;

        for (FeatureValue fv : features) {
            Object f = fv.getFeature();
            int hashIndex = Math.abs(hashFuncs[i].hash(f));
            float w = fv.getValueAsFloat();
            float hashValue = calcWeightedHashValue(hashIndex, w);
            if (hashValue < weightedMinHashValues) {
                weightedMinHashValues = hashValue;
                minhashes.offer(hashIndex);
            }/*  w w  w  .  j av  a  2s .  co  m*/
        }

        forwardObjs[0] = getSignature(minhashes, num_keygroups);
        forward(forwardObjs);

        minhashes.clear();
    }
}

From source file:com.mentor.questa.vrm.jenkins.QuestaVrmHostAction.java

private CategoryDataset buildDataSet(StaplerRequest req) {
    boolean showAction = Boolean.valueOf(req.getParameter("showActions")) || getActionCookie(req);
    DataSetBuilder<String, Long> dsb = new DataSetBuilder<String, Long>();

    PriorityQueue<Pair> pq = new PriorityQueue<Pair>();
    HashMap<String, Integer> hostCount = new HashMap<String, Integer>();
    for (TestResult temp : getRegressionResult().getActions()) {
        QuestaVrmAbstractResult action = (QuestaVrmAbstractResult) temp;
        if (showAction || action instanceof QuestaVrmTestResult) {
            if (action.getStartTime() == -1 || action.getDoneTime() == -1) {
                continue;
            }//from   w ww.j a v a2 s  . c o  m
            pq.add(new Pair(action.getStartTimeDate(), action.getHost(), 1));
            pq.add(new Pair(action.getDoneTimeDate(), action.getHost(), -1));
            hostCount.put(action.getHost(), 0);
        }
    }

    if (pq.isEmpty()) {
        return dsb.build();
    }

    long offset = getRegressionResult().getRegressionBegin().getTime();
    int noOfTests;
    HashSet<String> visited = new HashSet<String>();

    while (!pq.isEmpty()) {
        long currentKey = pq.peek().date.getTime();

        while (!pq.isEmpty() && pq.peek().date.getTime() == currentKey) {
            Pair current = pq.peek();
            noOfTests = hostCount.get(current.host);
            while (!pq.isEmpty() && pq.peek().compareTo(current) == 0) {
                noOfTests += pq.poll().diff;
            }
            dsb.add(noOfTests, current.host, (current.date.getTime() - offset) / 1000);
            hostCount.put(current.host, noOfTests);
            visited.add(current.host);

        }
        for (String host : hostCount.keySet()) {
            if (!visited.contains(host)) {
                dsb.add(hostCount.get(host), host, (currentKey - offset) / 1000);
            }
        }
        visited.clear();

    }
    return dsb.build();

}