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