1 | package de.ugoe.cs.eventbench.models;
|
---|
2 |
|
---|
3 | import java.util.LinkedList;
|
---|
4 | import java.util.List;
|
---|
5 | import java.util.Random;
|
---|
6 | import java.util.Set;
|
---|
7 |
|
---|
8 | import de.ugoe.cs.eventbench.data.Event;
|
---|
9 | import de.ugoe.cs.eventbench.models.Trie.Edge;
|
---|
10 | import de.ugoe.cs.eventbench.models.Trie.TrieVertex;
|
---|
11 | import edu.uci.ics.jung.graph.Tree;
|
---|
12 |
|
---|
13 | public abstract class TrieBasedModel implements IStochasticProcess {
|
---|
14 |
|
---|
15 | protected int trieOrder;
|
---|
16 |
|
---|
17 | protected Trie<Event<?>> trie;
|
---|
18 | protected final Random r;
|
---|
19 |
|
---|
20 |
|
---|
21 | public TrieBasedModel(int markovOrder, Random r) {
|
---|
22 | super();
|
---|
23 | this.trieOrder = markovOrder+1;
|
---|
24 | this.r = r;
|
---|
25 | }
|
---|
26 |
|
---|
27 | public void train(List<List<Event<?>>> sequences) {
|
---|
28 | trie = new Trie<Event<?>>();
|
---|
29 |
|
---|
30 | for(List<Event<?>> sequence : sequences) {
|
---|
31 | List<Event<?>> currentSequence = new LinkedList<Event<?>>(sequence); // defensive copy
|
---|
32 | currentSequence.add(0, Event.STARTEVENT);
|
---|
33 | currentSequence.add(Event.ENDEVENT);
|
---|
34 |
|
---|
35 | trie.train(currentSequence, trieOrder);
|
---|
36 | }
|
---|
37 | }
|
---|
38 |
|
---|
39 | /* (non-Javadoc)
|
---|
40 | * @see de.ugoe.cs.eventbench.models.IStochasticProcess#randomSequence()
|
---|
41 | */
|
---|
42 | @Override
|
---|
43 | public List<? extends Event<?>> randomSequence() {
|
---|
44 | List<Event<?>> sequence = new LinkedList<Event<?>>();
|
---|
45 |
|
---|
46 | IncompleteMemory<Event<?>> context = new IncompleteMemory<Event<?>>(trieOrder-1);
|
---|
47 | context.add(Event.STARTEVENT);
|
---|
48 |
|
---|
49 | Event<?> currentState = Event.STARTEVENT;
|
---|
50 |
|
---|
51 | boolean endFound = false;
|
---|
52 |
|
---|
53 | while(!endFound) {
|
---|
54 | double randVal = r.nextDouble();
|
---|
55 | double probSum = 0.0;
|
---|
56 | List<Event<?>> currentContext = context.getLast(trieOrder);
|
---|
57 | for( Event<?> symbol : trie.getKnownSymbols() ) {
|
---|
58 | probSum += getProbability(currentContext, symbol);
|
---|
59 | if( probSum>=randVal ) {
|
---|
60 | endFound = (symbol==Event.ENDEVENT);
|
---|
61 | if( !(symbol==Event.STARTEVENT || symbol==Event.ENDEVENT) ) {
|
---|
62 | // only add the symbol the sequence if it is not START or END
|
---|
63 | context.add(symbol);
|
---|
64 | currentState = symbol;
|
---|
65 | sequence.add(currentState);
|
---|
66 | }
|
---|
67 | break;
|
---|
68 | }
|
---|
69 | }
|
---|
70 | }
|
---|
71 | return sequence;
|
---|
72 | }
|
---|
73 |
|
---|
74 | public String getTrieDotRepresentation() {
|
---|
75 | return trie.getDotRepresentation();
|
---|
76 | }
|
---|
77 |
|
---|
78 | public Tree<TrieVertex, Edge> getTrieGraph() {
|
---|
79 | return trie.getGraph();
|
---|
80 | }
|
---|
81 |
|
---|
82 | @Override
|
---|
83 | public String toString() {
|
---|
84 | return trie.toString();
|
---|
85 | }
|
---|
86 |
|
---|
87 | public int getNumStates() {
|
---|
88 | return trie.getNumSymbols();
|
---|
89 | }
|
---|
90 |
|
---|
91 | public String[] getStateStrings() {
|
---|
92 | String[] stateStrings = new String[getNumStates()];
|
---|
93 | int i=0;
|
---|
94 | for( Event<?> symbol : trie.getKnownSymbols() ) {
|
---|
95 | stateStrings[i] = symbol.toString();
|
---|
96 | i++;
|
---|
97 | }
|
---|
98 | return stateStrings;
|
---|
99 | }
|
---|
100 |
|
---|
101 | public Set<Event<?>> getEvents() {
|
---|
102 | return trie.getKnownSymbols();
|
---|
103 | }
|
---|
104 |
|
---|
105 | } |
---|