Changeset 9 for trunk/EventBenchCore/src/de/ugoe/cs
- Timestamp:
- 04/13/11 14:13:45 (14 years ago)
- Location:
- trunk/EventBenchCore/src/de/ugoe/cs/eventbench/ppm
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/EventBenchCore/src/de/ugoe/cs/eventbench/ppm/PredictionByPartialMatch.java
r7 r9 1 1 package de.ugoe.cs.eventbench.ppm; 2 2 3 import java.util.LinkedHashSet;4 3 import java.util.LinkedList; 5 4 import java.util.List; 6 5 import java.util.Random; 7 import java.util.Set;8 6 9 7 import de.ugoe.cs.eventbench.data.Event; … … 12 10 public class PredictionByPartialMatch { 13 11 14 private int maxOrder = 3;12 private int maxOrder; 15 13 16 14 private Trie<Event<?>> trie; 17 15 18 private Set<Event<?>> knownSymbols;19 20 16 private double probEscape; 21 17 22 18 private final Random r; 23 19 24 public PredictionByPartialMatch(Random r) { 25 this(r, 0.1); 26 } 27 28 public PredictionByPartialMatch(Random r, double probEscape) { 20 public PredictionByPartialMatch(int maxOrder, Random r) { 21 this(maxOrder, r, 0.1); 22 } 23 24 public PredictionByPartialMatch(int maxOrder, Random r, double probEscape) { 25 this.maxOrder = maxOrder; 29 26 this.r = r; // TODO defensive copy instead? 30 27 this.probEscape = probEscape; … … 42 39 public void train(List<List<Event<?>>> sequences) { 43 40 trie = new Trie<Event<?>>(); 44 knownSymbols = new LinkedHashSet<Event<?>>();45 knownSymbols.add(Event.STARTEVENT);46 knownSymbols.add(Event.ENDEVENT);47 41 48 42 for(List<Event<?>> sequence : sequences) { … … 51 45 currentSequence.add(Event.ENDEVENT); 52 46 53 addToTrie(currentSequence);54 } 55 } 56 57 private void addToTrie(List<Event<?>> sequence) {47 trie.train(currentSequence, maxOrder); 48 } 49 } 50 51 /*private void addToTrie(List<Event<?>> sequence) { 58 52 if( knownSymbols==null ) { 59 53 knownSymbols = new LinkedHashSet<Event<?>>(); … … 73 67 trie.add(sequence.subList(sequenceLength-j, sequenceLength)); 74 68 } 75 } 69 }*/ 76 70 77 71 public List<? extends Event<?>> randomSequence() { … … 89 83 double probSum = 0.0; 90 84 List<Event<?>> currentContext = context.getLast(maxOrder); 91 for( Event<?> symbol : knownSymbols) {85 for( Event<?> symbol : trie.getKnownSymbols() ) { 92 86 probSum += getProbability(currentContext, symbol); 93 87 if( probSum>=randVal ) { -
trunk/EventBenchCore/src/de/ugoe/cs/eventbench/ppm/Trie.java
r6 r9 4 4 import java.awt.Rectangle; 5 5 import java.awt.Shape; 6 import java.util.LinkedHashSet; 6 7 import java.util.LinkedList; 7 8 import java.util.List; 9 import java.util.Set; 8 10 9 11 import javax.swing.JFrame; 10 12 11 13 import org.apache.commons.collections15.Transformer; 14 15 import de.ugoe.cs.eventbench.markov.IncompleteMemory; 12 16 13 17 import edu.uci.ics.jung.algorithms.layout.Layout; … … 21 25 public class Trie<T> { 22 26 27 private Set<T> knownSymbols; 28 23 29 private final TrieNode<T> rootNode; 24 30 25 31 public Trie() { 26 32 rootNode = new TrieNode<T>(); 33 knownSymbols = new LinkedHashSet<T>(); 34 } 35 36 public Set<T> getKnownSymbols() { 37 return new LinkedHashSet<T>(knownSymbols); 38 } 39 40 // trains the current Trie using the given sequence and adds all subsequence of length maxOrder 41 public void train(List<T> sequence, int maxOrder) { 42 IncompleteMemory<T> latestActions = new IncompleteMemory<T>(maxOrder); 43 int i=0; 44 for(T currentEvent : sequence) { 45 latestActions.add(currentEvent); 46 knownSymbols.add(currentEvent); 47 i++; 48 if( i>=maxOrder ) { 49 add(latestActions.getLast(maxOrder)); 50 } 51 } 52 int sequenceLength = sequence.size(); 53 for( int j=maxOrder-1 ; j>0 ; j-- ) { 54 add(sequence.subList(sequenceLength-j, sequenceLength)); 55 } 27 56 } 28 57 29 58 59 // increases the counters for each symbol in the subsequence 30 60 public void add(List<T> subsequence) { 31 if( !subsequence.isEmpty() ) { 61 if( subsequence!=null && !subsequence.isEmpty() ) { 62 knownSymbols.addAll(subsequence); 32 63 subsequence = new LinkedList<T>(subsequence); // defensive copy! 33 64 T firstSymbol = subsequence.get(0);
Note: See TracChangeset
for help on using the changeset viewer.