public Sequence[] bestSequences(int numSequences, Object[] sequence, Object[] additionalContext, double minSequenceScore) {
    int n = sequence.length;
    Heap prev = new ListHeap(size);
    Heap next = new ListHeap(size);
    Heap tmp;
    prev.add(new Sequence());
    if (additionalContext == null) {
      additionalContext = EMPTY_ADDITIONAL_CONTEXT;
    }
    for (int i = 0; i < n; i++) {
      int sz = Math.min(size, prev.size());
      int sc = 0;
      for (; prev.size() > 0 && sc < sz; sc++) {
        Sequence top = (Sequence) prev.extract();
        List tmpOutcomes = top.getOutcomes();
        String[] outcomes = (String[]) tmpOutcomes.toArray(new String[tmpOutcomes.size()]);
        String[] contexts = cg.getContext(i, sequence, outcomes, additionalContext);
        double[] scores;
        if (contextsCache != null) {
          scores = (double[]) contextsCache.get(contexts);
          if (scores == null) {
            scores = model.eval(contexts, probs);
            contextsCache.put(contexts,scores);
          }
        }
        else {
          scores = model.eval(contexts, probs);
        }
        double[] temp_scores = new double[scores.length];
        for (int c = 0; c < scores.length; c++) {
          temp_scores[c] = scores[c];
        }
        Arrays.sort(temp_scores);
        double min = temp_scores[Math.max(0,scores.length-size)];
        for (int p = 0; p < scores.length; p++) {
          if (scores[p] < min)
            continue; //only advance first "size" outcomes
          String out = model.getOutcome(p);
          if (validSequence(i, sequence, outcomes, out)) {
            Sequence ns = new Sequence(top, out, scores[p]);
            if (ns.getScore() > minSequenceScore) {
              next.add(ns);
            }
          }
        }
        if (next.size() == 0) {//if no advanced sequences, advance all valid
          for (int p = 0; p < scores.length; p++) {
            String out = model.getOutcome(p);
            if (validSequence(i, sequence, outcomes, out)) {
              Sequence ns = new Sequence(top, out, scores[p]);
              if (ns.getScore() > minSequenceScore) {
                next.add(ns);
              }
            }
          }
        }
      }
      //    make prev = next; and re-init next (we reuse existing prev set once we clear it)
      prev.clear();
      tmp = prev;
      prev = next;
      next = tmp;
    }
    int numSeq = Math.min(numSequences, prev.size());
    Sequence[] topSequences = new Sequence[numSeq];
    int seqIndex = 0;
    for (; seqIndex < numSeq; seqIndex++) {
      topSequences[seqIndex] = (Sequence) prev.extract();
    }
    return topSequences;
  }
public Sequence [ ] bestSequences ( int numSequences , Object [ ] sequence , Object [ ] additionalContext , double minSequenceScore ) { int n = sequence . length ; Heap prev = new ListHeap ( size ) ; Heap next = new ListHeap ( size ) ; Heap tmp ; prev . add ( new Sequence ( ) ) ; if ( additionalContext == null ) { additionalContext = EMPTY_ADDITIONAL_CONTEXT ; } for ( int i = 0 ; i < n ; i ++ ) { int sz = Math . min ( size , prev . size ( ) ) ; int sc = 0 ; for ( ; prev . size ( ) > 0 && sc < sz ; sc ++ ) { Sequence top = ( Sequence ) prev . extract ( ) ; List tmpOutcomes = top . getOutcomes ( ) ; String [ ] outcomes = ( String [ ] ) tmpOutcomes . toArray ( new String [ tmpOutcomes . size ( ) ] ) ; String [ ] contexts = cg . getContext ( i , sequence , outcomes , additionalContext ) ; double [ ] scores ; if ( contextsCache != null ) { scores = ( double [ ] ) contextsCache . get ( contexts ) ; if ( scores == null ) { scores = model . eval ( contexts , probs ) ; contextsCache . put ( contexts , scores ) ; } } else { scores = model . eval ( contexts , probs ) ; } double [ ] temp_scores = new double [ scores . length ] ; for ( int c = 0 ; c < scores . length ; c ++ ) { temp_scores [ c ] = scores [ c ] ; } Arrays . sort ( temp_scores ) ; double min = temp_scores [ Math . max ( 0 , scores . length - size ) ] ; for ( int p = 0 ; p < scores . length ; p ++ ) { if ( scores [ p ] < min ) continue ; String out = model . getOutcome ( p ) ; if ( validSequence ( i , sequence , outcomes , out ) ) { Sequence ns = new Sequence ( top , out , scores [ p ] ) ; if ( ns . getScore ( ) > minSequenceScore ) { next . add ( ns ) ; } } } if ( next . size ( ) == 0 ) { for ( int p = 0 ; p < scores . length ; p ++ ) { String out = model . getOutcome ( p ) ; if ( validSequence ( i , sequence , outcomes , out ) ) { Sequence ns = new Sequence ( top , out , scores [ p ] ) ; if ( ns . getScore ( ) > minSequenceScore ) { next . add ( ns ) ; } } } } } prev . clear ( ) ; tmp = prev ; prev = next ; next = tmp ; } int numSeq = Math . min ( numSequences , prev . size ( ) ) ; Sequence [ ] topSequences = new Sequence [ numSeq ] ; int seqIndex = 0 ; for ( ; seqIndex < numSeq ; seqIndex ++ ) { topSequences [ seqIndex ] = ( Sequence ) prev . extract ( ) ; } return topSequences ; }
Returns the best sequence of outcomes based on model for this object.
@param numSequences The maximum number of sequences to be returned.
@param sequence The input sequence.
@param additionalContext An Object[] of additional context.  This is passed to the context generator blindly with the assumption that the context are appropiate.
@param minSequenceScore A lower bound on the score of a returned sequence.
@return An array of the top ranked sequences of outcomes.