Changeset 7 for trunk/EventBenchCore
- Timestamp:
- 04/13/11 13:21:28 (14 years ago)
- Location:
- trunk/EventBenchCore/src/de/ugoe/cs/eventbench
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/EventBenchCore/src/de/ugoe/cs/eventbench/data/Event.java
r5 r7 4 4 5 5 public class Event<T> { 6 6 7 public static final Event<Object> STARTEVENT = new Event<Object>("START"); 8 public static final Event<Object> ENDEVENT = new Event<Object>("END"); 9 7 10 /** 8 11 * <p> -
trunk/EventBenchCore/src/de/ugoe/cs/eventbench/ppm/PredictionByPartialMatch.java
r6 r7 1 1 package de.ugoe.cs.eventbench.ppm; 2 2 3 import java.util.ArrayList;4 3 import java.util.LinkedHashSet; 5 4 import java.util.LinkedList; … … 10 9 import de.ugoe.cs.eventbench.data.Event; 11 10 import de.ugoe.cs.eventbench.markov.IncompleteMemory; 12 import de.ugoe.cs.util.console.Console;13 11 14 12 public class PredictionByPartialMatch { 15 13 16 private String initialSymbol = "GS";17 private String endSymbol = "GE";18 19 14 private int maxOrder = 3; 20 15 21 private Trie<String> trie; 22 23 private Set<String> knownSymbols; 24 25 private double probEscape = 0.2d; // TODO getter/setter - steering parameter! 26 27 private Random r = new Random(); // TODO should be defined in the constructor 16 private Trie<Event<?>> trie; 17 18 private Set<Event<?>> knownSymbols; 19 20 private double probEscape; 21 22 private final Random r; 23 24 public PredictionByPartialMatch(Random r) { 25 this(r, 0.1); 26 } 27 28 public PredictionByPartialMatch(Random r, double probEscape) { 29 this.r = r; // TODO defensive copy instead? 30 this.probEscape = probEscape; 31 } 32 33 public void setProbEscape(double probEscape) { 34 this.probEscape = probEscape; 35 } 36 37 public double getProbEscape() { 38 return probEscape; 39 } 28 40 29 41 // the training is basically the generation of the trie 30 42 public void train(List<List<Event<?>>> sequences) { 31 trie = new Trie< String>();32 knownSymbols = new LinkedHashSet< String>();33 knownSymbols.add( initialSymbol);34 knownSymbols.add( endSymbol);43 trie = new Trie<Event<?>>(); 44 knownSymbols = new LinkedHashSet<Event<?>>(); 45 knownSymbols.add(Event.STARTEVENT); 46 knownSymbols.add(Event.ENDEVENT); 35 47 36 48 for(List<Event<?>> sequence : sequences) { 37 List<String> stringSequence = new LinkedList<String>(); 38 stringSequence.add(initialSymbol); 39 for( Event<?> event : sequence ) { 40 stringSequence.add(event.getStandardId()); 41 } 42 stringSequence.add(endSymbol); 49 List<Event<?>> currentSequence = new LinkedList<Event<?>>(sequence); // defensive copy 50 currentSequence.add(0, Event.STARTEVENT); 51 currentSequence.add(Event.ENDEVENT); 43 52 44 trainStringTrie(stringSequence); 45 } 46 } 47 48 private void trainStringTrie(List<String> sequence) { 49 knownSymbols = new LinkedHashSet<String>(); 50 IncompleteMemory<String> latestActions = new IncompleteMemory<String>(maxOrder); 53 addToTrie(currentSequence); 54 } 55 } 56 57 private void addToTrie(List<Event<?>> sequence) { 58 if( knownSymbols==null ) { 59 knownSymbols = new LinkedHashSet<Event<?>>(); 60 } 61 IncompleteMemory<Event<?>> latestActions = new IncompleteMemory<Event<?>>(maxOrder); 51 62 int i=0; 52 for(String currentAction : sequence) { 53 String currentId = currentAction; 54 latestActions.add(currentId); 55 knownSymbols.add(currentId); 63 for(Event<?> currentEvent : sequence) { 64 latestActions.add(currentEvent); 65 knownSymbols.add(currentEvent); 56 66 i++; 57 67 if( i>=maxOrder ) { … … 65 75 } 66 76 67 // TODO needs to be changed from String to <? extends Event> 68 public List<String> randomSequence() { 69 List<String> sequence = new LinkedList<String>(); 70 71 IncompleteMemory<String> context = new IncompleteMemory<String>(maxOrder-1); 72 context.add(initialSymbol); 73 sequence.add(initialSymbol); 74 75 String currentState = initialSymbol; 76 77 Console.println(currentState); 78 while(!endSymbol.equals(currentState)) { 77 public List<? extends Event<?>> randomSequence() { 78 List<Event<?>> sequence = new LinkedList<Event<?>>(); 79 80 IncompleteMemory<Event<?>> context = new IncompleteMemory<Event<?>>(maxOrder-1); 81 context.add(Event.STARTEVENT); 82 83 Event<?> currentState = Event.STARTEVENT; 84 85 boolean endFound = false; 86 87 while(!endFound) { 79 88 double randVal = r.nextDouble(); 80 89 double probSum = 0.0; 81 List< String> currentContext = context.getLast(maxOrder);82 for( Stringsymbol : knownSymbols ) {90 List<Event<?>> currentContext = context.getLast(maxOrder); 91 for( Event<?> symbol : knownSymbols ) { 83 92 probSum += getProbability(currentContext, symbol); 84 93 if( probSum>=randVal ) { 85 context.add(symbol); 86 currentState = symbol; 87 sequence.add(currentState); 94 endFound = (symbol==Event.ENDEVENT); 95 if( !(symbol==Event.STARTEVENT || symbol==Event.ENDEVENT) ) { 96 // only add the symbol the sequence if it is not START or END 97 context.add(symbol); 98 currentState = symbol; 99 sequence.add(currentState); 100 } 88 101 break; 89 102 } … … 93 106 } 94 107 95 private double getProbability(List< String> context, Stringsymbol) {108 private double getProbability(List<Event<?>> context, Event<?> symbol) { 96 109 double result = 0.0d; 97 110 double resultCurrentContex = 0.0d; 98 111 double resultShorterContex = 0.0d; 99 112 100 List< String> contextCopy = new LinkedList<String>(context); // defensive copy101 102 103 List< String> followers = trie.getFollowingSymbols(contextCopy); // \Sigma'113 List<Event<?>> contextCopy = new LinkedList<Event<?>>(context); // defensive copy 114 115 116 List<Event<?>> followers = trie.getFollowingSymbols(contextCopy); // \Sigma' 104 117 int sumCountFollowers = 0; // N(s\sigma') 105 for( Stringfollower : followers ) {118 for( Event<?> follower : followers ) { 106 119 sumCountFollowers += trie.getCount(contextCopy, follower); 107 120 } … … 130 143 } 131 144 145 /* 132 146 public void testStuff() { 133 147 // basically an inline unit test without assertions but manual observation … … 151 165 model.trainStringTrie(list); 152 166 model.trie.display(); 153 Console.println("------------------------");154 model.randomSequence();/*155 Console.println("------------------------");156 model.randomSequence();157 Console.println("------------------------");158 model.randomSequence();159 Console.println("------------------------");*/160 167 161 168 List<String> context = new ArrayList<String>(); … … 204 211 context.add("a"); 205 212 Console.traceln(""+model.getProbability(context, "z")); 206 } 213 }*/ 207 214 }
Note: See TracChangeset
for help on using the changeset viewer.