Changeset 7


Ignore:
Timestamp:
04/13/11 13:21:28 (13 years ago)
Author:
sherbold
Message:
  • changed PPM to work with Event<?> instead of String
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  
    44 
    55public 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         
    710        /** 
    811         * <p> 
  • trunk/EventBenchCore/src/de/ugoe/cs/eventbench/ppm/PredictionByPartialMatch.java

    r6 r7  
    11package de.ugoe.cs.eventbench.ppm; 
    22 
    3 import java.util.ArrayList; 
    43import java.util.LinkedHashSet; 
    54import java.util.LinkedList; 
     
    109import de.ugoe.cs.eventbench.data.Event; 
    1110import de.ugoe.cs.eventbench.markov.IncompleteMemory; 
    12 import de.ugoe.cs.util.console.Console; 
    1311 
    1412public class PredictionByPartialMatch { 
    1513         
    16         private String initialSymbol = "GS"; 
    17         private String endSymbol = "GE"; 
    18          
    1914        private int maxOrder = 3; 
    2015         
    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        } 
    2840         
    2941        // the training is basically the generation of the trie 
    3042        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); 
    3547                 
    3648                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); 
    4352                         
    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); 
    5162                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); 
    5666                        i++; 
    5767                        if( i>=maxOrder ) { 
     
    6575        } 
    6676         
    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) { 
    7988                        double randVal = r.nextDouble(); 
    8089                        double probSum = 0.0; 
    81                         List<String> currentContext = context.getLast(maxOrder); 
    82                         for( String symbol : knownSymbols ) { 
     90                        List<Event<?>> currentContext = context.getLast(maxOrder); 
     91                        for( Event<?> symbol : knownSymbols ) { 
    8392                                probSum += getProbability(currentContext, symbol); 
    8493                                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                                        } 
    88101                                        break; 
    89102                                } 
     
    93106        } 
    94107                 
    95         private double getProbability(List<String> context, String symbol) { 
     108        private double getProbability(List<Event<?>> context, Event<?> symbol) { 
    96109                double result = 0.0d; 
    97110                double resultCurrentContex = 0.0d; 
    98111                double resultShorterContex = 0.0d; 
    99112                 
    100                 List<String> contextCopy = new LinkedList<String>(context); // defensive copy 
    101  
    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' 
    104117                int sumCountFollowers = 0; // N(s\sigma') 
    105                 for( String follower : followers ) { 
     118                for( Event<?> follower : followers ) { 
    106119                        sumCountFollowers += trie.getCount(contextCopy, follower); 
    107120                } 
     
    130143        } 
    131144         
     145        /* 
    132146        public void testStuff() { 
    133147                // basically an inline unit test without assertions but manual observation 
     
    151165                model.trainStringTrie(list); 
    152166                model.trie.display(); 
    153                 Console.println("------------------------"); 
    154                 model.randomSequence();/* 
    155                 Console.println("------------------------"); 
    156                 model.randomSequence(); 
    157                 Console.println("------------------------"); 
    158                 model.randomSequence(); 
    159                 Console.println("------------------------");*/ 
    160167                 
    161168                List<String> context = new ArrayList<String>(); 
     
    204211                context.add("a"); 
    205212                Console.traceln(""+model.getProbability(context, "z")); 
    206         } 
     213        }*/ 
    207214} 
Note: See TracChangeset for help on using the changeset viewer.