Changeset 12 for trunk/EventBenchCore/src/de/ugoe
- Timestamp:
- 04/13/11 15:53:14 (14 years ago)
- Location:
- trunk/EventBenchCore/src/de/ugoe/cs/eventbench
- Files:
-
- 2 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/EventBenchCore/src/de/ugoe/cs/eventbench/data/Event.java
r7 r12 1 1 package de.ugoe.cs.eventbench.data; 2 3 import java.security.InvalidParameterException; 2 4 3 5 … … 32 34 33 35 public Event(String type) { 36 if( type==null ) { 37 throw new InvalidParameterException("Event type must not be null"); 38 } 34 39 this.type = type; 35 40 } … … 41 46 } 42 47 if (other instanceof Event<?>) { 43 Event<?> otherToken = (Event<?>) other; 44 return otherToken.type.equals(this.type) 45 && otherToken.target.equals(this.target); 48 Event<?> otherEvent = (Event<?>) other; 49 if( target!=null ) { 50 return type.equals(otherEvent.type) 51 && target.equals(otherEvent.target); 52 } else { 53 return type.equals(otherEvent.type) 54 && target==otherEvent.target; 55 } 46 56 } else { 47 57 return false; … … 70 80 71 81 public String getStandardId() { 72 String id = target + "." + getType(); 82 String id = ""; 83 if( target!=null ) { 84 id += target + "."; 85 } 86 id += getType(); 73 87 if ( idInfo!="" ) { 74 88 id += "." + idInfo; -
trunk/EventBenchCore/src/de/ugoe/cs/eventbench/ppm/PredictionByPartialMatch.java
r9 r12 1 1 package de.ugoe.cs.eventbench.ppm; 2 2 3 import java.util.ArrayList; 3 4 import java.util.LinkedList; 4 5 import java.util.List; … … 6 7 7 8 import de.ugoe.cs.eventbench.data.Event; 8 import de.ugoe.cs. eventbench.markov.IncompleteMemory;9 import de.ugoe.cs.util.console.Console; 9 10 10 public class PredictionByPartialMatch {11 public class PredictionByPartialMatch extends TrieBasedModel { 11 12 12 private int maxOrder; 13 14 private Trie<Event<?>> trie; 15 16 private double probEscape; 17 18 private final Random r; 13 double probEscape; 19 14 20 15 public PredictionByPartialMatch(int maxOrder, Random r) { … … 23 18 24 19 public PredictionByPartialMatch(int maxOrder, Random r, double probEscape) { 25 this.maxOrder = maxOrder; 26 this.r = r; // TODO defensive copy instead? 20 super(maxOrder, r); 27 21 this.probEscape = probEscape; 28 22 } … … 36 30 } 37 31 38 // the training is basically the generation of the trie 39 public void train(List<List<Event<?>>> sequences) { 40 trie = new Trie<Event<?>>(); 41 42 for(List<Event<?>> sequence : sequences) { 43 List<Event<?>> currentSequence = new LinkedList<Event<?>>(sequence); // defensive copy 44 currentSequence.add(0, Event.STARTEVENT); 45 currentSequence.add(Event.ENDEVENT); 46 47 trie.train(currentSequence, maxOrder); 48 } 49 } 50 51 /*private void addToTrie(List<Event<?>> sequence) { 52 if( knownSymbols==null ) { 53 knownSymbols = new LinkedHashSet<Event<?>>(); 54 } 55 IncompleteMemory<Event<?>> latestActions = new IncompleteMemory<Event<?>>(maxOrder); 56 int i=0; 57 for(Event<?> currentEvent : sequence) { 58 latestActions.add(currentEvent); 59 knownSymbols.add(currentEvent); 60 i++; 61 if( i>=maxOrder ) { 62 trie.add(latestActions.getLast(maxOrder)); 63 } 64 } 65 int sequenceLength = sequence.size(); 66 for( int j=maxOrder-1 ; j>0 ; j-- ) { 67 trie.add(sequence.subList(sequenceLength-j, sequenceLength)); 68 } 69 }*/ 70 71 public List<? extends Event<?>> randomSequence() { 72 List<Event<?>> sequence = new LinkedList<Event<?>>(); 73 74 IncompleteMemory<Event<?>> context = new IncompleteMemory<Event<?>>(maxOrder-1); 75 context.add(Event.STARTEVENT); 76 77 Event<?> currentState = Event.STARTEVENT; 78 79 boolean endFound = false; 80 81 while(!endFound) { 82 double randVal = r.nextDouble(); 83 double probSum = 0.0; 84 List<Event<?>> currentContext = context.getLast(maxOrder); 85 for( Event<?> symbol : trie.getKnownSymbols() ) { 86 probSum += getProbability(currentContext, symbol); 87 if( probSum>=randVal ) { 88 endFound = (symbol==Event.ENDEVENT); 89 if( !(symbol==Event.STARTEVENT || symbol==Event.ENDEVENT) ) { 90 // only add the symbol the sequence if it is not START or END 91 context.add(symbol); 92 currentState = symbol; 93 sequence.add(currentState); 94 } 95 break; 96 } 97 } 98 } 99 return sequence; 100 } 101 102 private double getProbability(List<Event<?>> context, Event<?> symbol) { 32 @Override 33 protected double getProbability(List<Event<?>> context, Event<?> symbol) { 103 34 double result = 0.0d; 104 35 double resultCurrentContex = 0.0d; … … 132 63 } 133 64 134 @Override135 public String toString() {136 return trie.toString();137 }138 139 /*140 65 public void testStuff() { 141 66 // basically an inline unit test without assertions but manual observation 142 List<String> list = new ArrayList<String>(); 143 list.add(initialSymbol); 144 list.add("a"); 145 list.add("b"); 146 list.add("r"); 147 list.add("a"); 148 list.add("c"); 149 list.add("a"); 150 list.add("d"); 151 list.add("a"); 152 list.add("b"); 153 list.add("r"); 154 list.add("a"); 155 list.add(endSymbol); 67 List<Event<?>> list = new ArrayList<Event<?>>(); 68 list.add(new Event<Object>("a")); 69 list.add(new Event<Object>("b")); 70 list.add(new Event<Object>("r")); 71 list.add(new Event<Object>("a")); 72 list.add(new Event<Object>("c")); 73 list.add(new Event<Object>("a")); 74 list.add(new Event<Object>("d")); 75 list.add(new Event<Object>("a")); 76 list.add(new Event<Object>("b")); 77 list.add(new Event<Object>("r")); 78 list.add(new Event<Object>("a")); 156 79 157 PredictionByPartialMatch model = new PredictionByPartialMatch(); 158 model.trie = new Trie<String>(); 159 model.trainStringTrie(list); 80 int maxOrder = 3; 81 PredictionByPartialMatch model = new PredictionByPartialMatch(maxOrder, new Random()); 82 model.trie = new Trie<Event<?>>(); 83 model.trie.train(list, maxOrder); 160 84 model.trie.display(); 161 85 162 List< String> context = new ArrayList<String>();163 String symbol = "a";86 List<Event<?>> context = new ArrayList<Event<?>>(); 87 Event<Object> symbol = new Event<Object>("a"); 164 88 // expected: 5 165 89 Console.traceln(""+model.trie.getCount(context, symbol)); 166 90 167 91 // expected: 0 168 context.add( "b");92 context.add(new Event<Object>("b")); 169 93 Console.traceln(""+model.trie.getCount(context, symbol)); 170 94 171 95 // expected: 2 172 context.add( "r");96 context.add(new Event<Object>("r")); 173 97 Console.traceln(""+model.trie.getCount(context, symbol)); 174 98 175 99 // exptected: [b, r] 176 context = new ArrayList< String>();177 context.add( "a");178 context.add( "b");179 context.add( "r");100 context = new ArrayList<Event<?>>(); 101 context.add(new Event<Object>("a")); 102 context.add(new Event<Object>("b")); 103 context.add(new Event<Object>("r")); 180 104 Console.traceln(model.trie.getContextSuffix(context).toString()); 181 105 182 106 // exptected: [] 183 context = new ArrayList< String>();184 context.add( "e");107 context = new ArrayList<Event<?>>(); 108 context.add(new Event<Object>("e")); 185 109 Console.traceln(model.trie.getContextSuffix(context).toString()); 186 110 187 111 // exptected: {a, b, c, d, r} 188 context = new ArrayList< String>();112 context = new ArrayList<Event<?>>(); 189 113 Console.traceln(model.trie.getFollowingSymbols(context).toString()); 190 114 191 115 // exptected: {b, c, d} 192 context = new ArrayList< String>();193 context.add( "a");116 context = new ArrayList<Event<?>>(); 117 context.add(new Event<Object>("a")); 194 118 Console.traceln(model.trie.getFollowingSymbols(context).toString()); 195 119 196 120 // exptected: [] 197 context = new ArrayList< String>();198 context.add( "a");199 context.add( "b");200 context.add( "r");121 context = new ArrayList<Event<?>>(); 122 context.add(new Event<Object>("a")); 123 context.add(new Event<Object>("b")); 124 context.add(new Event<Object>("r")); 201 125 Console.traceln(model.trie.getFollowingSymbols(context).toString()); 202 126 203 127 // exptected: 0.0d 204 context = new ArrayList< String>();205 context.add( "a");206 Console.traceln(""+model.getProbability(context, "z"));207 } */128 context = new ArrayList<Event<?>>(); 129 context.add(new Event<Object>("a")); 130 Console.traceln(""+model.getProbability(context, new Event<Object>("z"))); 131 } 208 132 }
Note: See TracChangeset
for help on using the changeset viewer.