Changeset 100 for trunk/EventBenchCore/src/de/ugoe/cs
- Timestamp:
- 07/04/11 14:09:17 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/EventBenchCore/src/de/ugoe/cs/eventbench/models/TrieBasedModel.java
r95 r100 14 14 import edu.uci.ics.jung.graph.Tree; 15 15 16 /** 17 * <p> 18 * Implements a skeleton for stochastic processes that can calculate 19 * probabilities based on a trie. The skeleton provides all functionalities of 20 * {@link IStochasticProcess} except 21 * {@link IStochasticProcess#getProbability(List, Event)}. 22 * </p> 23 * 24 * @author Steffen Herbold 25 * @version 1.0 26 */ 16 27 public abstract class TrieBasedModel implements IStochasticProcess { 17 28 18 29 /** 30 * <p> 19 31 * Id for object serialization. 32 * </p> 20 33 */ 21 34 private static final long serialVersionUID = 1L; 22 35 36 /** 37 * <p> 38 * The order of the trie, i.e., the maximum length of subsequences stored in 39 * the trie. 40 * </p> 41 */ 23 42 protected int trieOrder; 24 43 44 /** 45 * <p> 46 * Trie on which the probability calculations are based. 47 * </p> 48 */ 25 49 protected Trie<Event<?>> trie; 50 51 /** 52 * <p> 53 * Random number generator used by probabilistic sequence generation 54 * methods. 55 * </p> 56 */ 26 57 protected final Random r; 27 58 28 59 /** 60 * <p> 61 * Constructor. Creates a new TrieBasedModel that can be used for stochastic 62 * processes with a Markov order less than or equal to {@code markovOrder}. 63 * </p> 64 * 65 * @param markovOrder 66 * Markov order of the model 67 * @param r 68 * random number generator used by probabilistic methods of the 69 * class 70 */ 29 71 public TrieBasedModel(int markovOrder, Random r) { 30 72 super(); 31 this.trieOrder = markovOrder +1;73 this.trieOrder = markovOrder + 1; 32 74 this.r = r; 33 75 } 34 76 77 /** 78 * <p> 79 * Trains the model by generating a trie from which probabilities are 80 * calculated. 81 * </p> 82 * 83 * @param sequences 84 * training data 85 */ 35 86 public void train(List<List<Event<?>>> sequences) { 36 87 trie = new Trie<Event<?>>(); 37 38 for(List<Event<?>> sequence : sequences) { 39 List<Event<?>> currentSequence = new LinkedList<Event<?>>(sequence); // defensive copy 88 89 for (List<Event<?>> sequence : sequences) { 90 List<Event<?>> currentSequence = new LinkedList<Event<?>>(sequence); // defensive 91 // copy 40 92 currentSequence.add(0, Event.STARTEVENT); 41 93 currentSequence.add(Event.ENDEVENT); 42 94 43 95 trie.train(currentSequence, trieOrder); 44 96 } 45 97 } 46 98 47 /* (non-Javadoc) 99 /* 100 * (non-Javadoc) 101 * 48 102 * @see de.ugoe.cs.eventbench.models.IStochasticProcess#randomSequence() 49 103 */ … … 51 105 public List<? extends Event<?>> randomSequence() { 52 106 List<Event<?>> sequence = new LinkedList<Event<?>>(); 53 54 IncompleteMemory<Event<?>> context = new IncompleteMemory<Event<?>>(trieOrder-1); 107 108 IncompleteMemory<Event<?>> context = new IncompleteMemory<Event<?>>( 109 trieOrder - 1); 55 110 context.add(Event.STARTEVENT); 56 111 57 112 Event<?> currentState = Event.STARTEVENT; 58 113 59 114 boolean endFound = false; 60 61 while (!endFound) {115 116 while (!endFound) { 62 117 double randVal = r.nextDouble(); 63 118 double probSum = 0.0; 64 119 List<Event<?>> currentContext = context.getLast(trieOrder); 65 for ( Event<?> symbol : trie.getKnownSymbols()) {120 for (Event<?> symbol : trie.getKnownSymbols()) { 66 121 probSum += getProbability(currentContext, symbol); 67 if( probSum>=randVal ) { 68 endFound = (symbol==Event.ENDEVENT); 69 if( !(symbol==Event.STARTEVENT || symbol==Event.ENDEVENT) ) { 70 // only add the symbol the sequence if it is not START or END 122 if (probSum >= randVal) { 123 endFound = (symbol == Event.ENDEVENT); 124 if (!(symbol == Event.STARTEVENT || symbol == Event.ENDEVENT)) { 125 // only add the symbol the sequence if it is not START 126 // or END 71 127 context.add(symbol); 72 128 currentState = symbol; … … 79 135 return sequence; 80 136 } 81 137 138 /** 139 * <p> 140 * Returns a Dot representation of the internal trie. 141 * </p> 142 * 143 * @return dot representation of the internal trie 144 */ 82 145 public String getTrieDotRepresentation() { 83 146 return trie.getDotRepresentation(); 84 147 } 85 148 149 /** 150 * <p> 151 * Returns a {@link Tree} of the internal trie that can be used for 152 * visualization. 153 * </p> 154 * 155 * @return {@link Tree} depicting the internal trie 156 */ 86 157 public Tree<TrieVertex, Edge> getTrieGraph() { 87 158 return trie.getGraph(); 88 159 } 89 160 161 /** 162 * <p> 163 * The string representation of the model is {@link Trie#toString()} of 164 * {@link #trie}. 165 * </p> 166 * 167 * @see java.lang.Object#toString() 168 */ 90 169 @Override 91 170 public String toString() { 92 171 return trie.toString(); 93 172 } 94 173 174 /* 175 * (non-Javadoc) 176 * 177 * @see de.ugoe.cs.eventbench.models.IStochasticProcess#getNumStates() 178 */ 179 @Override 95 180 public int getNumStates() { 96 181 return trie.getNumSymbols(); 97 182 } 98 183 184 /* 185 * (non-Javadoc) 186 * 187 * @see de.ugoe.cs.eventbench.models.IStochasticProcess#getStateStrings() 188 */ 189 @Override 99 190 public String[] getStateStrings() { 100 191 String[] stateStrings = new String[getNumStates()]; 101 int i =0;102 for ( Event<?> symbol : trie.getKnownSymbols()) {192 int i = 0; 193 for (Event<?> symbol : trie.getKnownSymbols()) { 103 194 stateStrings[i] = symbol.toString(); 104 195 i++; … … 106 197 return stateStrings; 107 198 } 108 199 200 /* 201 * (non-Javadoc) 202 * 203 * @see de.ugoe.cs.eventbench.models.IStochasticProcess#getEvents() 204 */ 205 @Override 109 206 public Set<? extends Event<?>> getEvents() { 110 207 return trie.getKnownSymbols(); 111 208 } 112 209 210 /* 211 * (non-Javadoc) 212 * 213 * @see 214 * de.ugoe.cs.eventbench.models.IStochasticProcess#generateSequences(int) 215 */ 216 @Override 113 217 public Set<List<? extends Event<?>>> generateSequences(int length) { 114 218 return generateSequences(length, false); 115 /*Set<List<? extends Event<?>>> sequenceSet = new LinkedHashSet<List<? extends Event<?>>>();; 116 if( length<1 ) { 117 throw new InvalidParameterException("Length of generated subsequences must be at least 1."); 118 } 119 if( length==1 ) { 120 for( Event<?> event : getEvents() ) { 219 } 220 221 /* 222 * (non-Javadoc) 223 * 224 * @see 225 * de.ugoe.cs.eventbench.models.IStochasticProcess#generateSequences(int, 226 * boolean) 227 */ 228 @Override 229 public Set<List<? extends Event<?>>> generateSequences(int length, 230 boolean fromStart) { 231 Set<List<? extends Event<?>>> sequenceSet = new LinkedHashSet<List<? extends Event<?>>>(); 232 ; 233 if (length < 1) { 234 throw new InvalidParameterException( 235 "Length of generated subsequences must be at least 1."); 236 } 237 if (length == 1) { 238 if (fromStart) { 121 239 List<Event<?>> subSeq = new LinkedList<Event<?>>(); 122 subSeq.add( event);240 subSeq.add(Event.STARTEVENT); 123 241 sequenceSet.add(subSeq); 242 } else { 243 for (Event<?> event : getEvents()) { 244 List<Event<?>> subSeq = new LinkedList<Event<?>>(); 245 subSeq.add(event); 246 sequenceSet.add(subSeq); 247 } 124 248 } 125 249 return sequenceSet; 126 250 } 127 251 Set<? extends Event<?>> events = getEvents(); 128 Set<List<? extends Event<?>>> seqsShorter = generateSequences(length-1); 129 for( Event<?> event : events ) { 130 for( List<? extends Event<?>> seqShorter : seqsShorter ) { 252 Set<List<? extends Event<?>>> seqsShorter = generateSequences( 253 length - 1, fromStart); 254 for (Event<?> event : events) { 255 for (List<? extends Event<?>> seqShorter : seqsShorter) { 131 256 Event<?> lastEvent = event; 132 if ( getProbability(seqShorter, lastEvent)>0.0) {257 if (getProbability(seqShorter, lastEvent) > 0.0) { 133 258 List<Event<?>> subSeq = new ArrayList<Event<?>>(seqShorter); 134 259 subSeq.add(lastEvent); … … 138 263 } 139 264 return sequenceSet; 140 */ 141 } 142 143 // if startValid, all sequences will start in Event.STARTEVENT 144 public Set<List<? extends Event<?>>> generateSequences(int length, boolean fromStart) { 145 Set<List<? extends Event<?>>> sequenceSet = new LinkedHashSet<List<? extends Event<?>>>();; 146 if( length<1 ) { 147 throw new InvalidParameterException("Length of generated subsequences must be at least 1."); 148 } 149 if( length==1 ) { 150 if( fromStart ) { 151 List<Event<?>> subSeq = new LinkedList<Event<?>>(); 152 subSeq.add(Event.STARTEVENT); 153 sequenceSet.add(subSeq); 154 } else { 155 for( Event<?> event : getEvents() ) { 156 List<Event<?>> subSeq = new LinkedList<Event<?>>(); 157 subSeq.add(event); 158 sequenceSet.add(subSeq); 159 } 160 } 161 return sequenceSet; 162 } 163 Set<? extends Event<?>> events = getEvents(); 164 Set<List<? extends Event<?>>> seqsShorter = generateSequences(length-1, fromStart); 165 for( Event<?> event : events ) { 166 for( List<? extends Event<?>> seqShorter : seqsShorter ) { 167 Event<?> lastEvent = event; 168 if( getProbability(seqShorter, lastEvent)>0.0 ) { 169 List<Event<?>> subSeq = new ArrayList<Event<?>>(seqShorter); 170 subSeq.add(lastEvent); 171 sequenceSet.add(subSeq); 172 } 173 } 174 } 175 return sequenceSet; 176 } 177 178 // sequences from start to end 265 } 266 267 /* 268 * (non-Javadoc) 269 * 270 * @see 271 * de.ugoe.cs.eventbench.models.IStochasticProcess#generateValidSequences 272 * (int) 273 */ 274 @Override 179 275 public Set<List<? extends Event<?>>> generateValidSequences(int length) { 180 276 // check for min-length implicitly done by generateSequences 181 Set<List<? extends Event<?>>> allSequences = generateSequences(length, true); 277 Set<List<? extends Event<?>>> allSequences = generateSequences(length, 278 true); 182 279 Set<List<? extends Event<?>>> validSequences = new LinkedHashSet<List<? extends Event<?>>>(); 183 for( List<? extends Event<?>> sequence : allSequences ) { 184 if( sequence.size()==length && Event.ENDEVENT.equals(sequence.get(sequence.size()-1)) ) { 280 for (List<? extends Event<?>> sequence : allSequences) { 281 if (sequence.size() == length 282 && Event.ENDEVENT.equals(sequence.get(sequence.size() - 1))) { 185 283 validSequences.add(sequence); 186 284 }
Note: See TracChangeset
for help on using the changeset viewer.