Changeset 9


Ignore:
Timestamp:
04/13/11 14:13:45 (13 years ago)
Author:
sherbold
Message:
  • moved training logic for single sequences from PPM to Trie
Location:
trunk/EventBenchCore/src/de/ugoe/cs/eventbench/ppm
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/EventBenchCore/src/de/ugoe/cs/eventbench/ppm/PredictionByPartialMatch.java

    r7 r9  
    11package de.ugoe.cs.eventbench.ppm; 
    22 
    3 import java.util.LinkedHashSet; 
    43import java.util.LinkedList; 
    54import java.util.List; 
    65import java.util.Random; 
    7 import java.util.Set; 
    86 
    97import de.ugoe.cs.eventbench.data.Event; 
     
    1210public class PredictionByPartialMatch { 
    1311         
    14         private int maxOrder = 3; 
     12        private int maxOrder; 
    1513         
    1614        private Trie<Event<?>> trie; 
    1715         
    18         private Set<Event<?>> knownSymbols; 
    19          
    2016        private double probEscape; 
    2117         
    2218        private final Random r; 
    2319         
    24         public PredictionByPartialMatch(Random r) { 
    25                 this(r, 0.1); 
    26         } 
    27          
    28         public PredictionByPartialMatch(Random r, double probEscape) { 
     20        public PredictionByPartialMatch(int maxOrder, Random r) { 
     21                this(maxOrder, r, 0.1); 
     22        } 
     23         
     24        public PredictionByPartialMatch(int maxOrder, Random r, double probEscape) { 
     25                this.maxOrder = maxOrder; 
    2926                this.r = r; // TODO defensive copy instead? 
    3027                this.probEscape = probEscape; 
     
    4239        public void train(List<List<Event<?>>> sequences) { 
    4340                trie = new Trie<Event<?>>(); 
    44                 knownSymbols = new LinkedHashSet<Event<?>>(); 
    45                 knownSymbols.add(Event.STARTEVENT); 
    46                 knownSymbols.add(Event.ENDEVENT); 
    4741                 
    4842                for(List<Event<?>> sequence : sequences) { 
     
    5145                        currentSequence.add(Event.ENDEVENT); 
    5246                         
    53                         addToTrie(currentSequence); 
    54                 } 
    55         } 
    56          
    57         private void addToTrie(List<Event<?>> sequence) { 
     47                        trie.train(currentSequence, maxOrder); 
     48                } 
     49        } 
     50         
     51        /*private void addToTrie(List<Event<?>> sequence) { 
    5852                if( knownSymbols==null ) { 
    5953                        knownSymbols = new LinkedHashSet<Event<?>>(); 
     
    7367                        trie.add(sequence.subList(sequenceLength-j, sequenceLength)); 
    7468                } 
    75         } 
     69        }*/ 
    7670         
    7771        public List<? extends Event<?>> randomSequence() { 
     
    8983                        double probSum = 0.0; 
    9084                        List<Event<?>> currentContext = context.getLast(maxOrder); 
    91                         for( Event<?> symbol : knownSymbols ) { 
     85                        for( Event<?> symbol : trie.getKnownSymbols() ) { 
    9286                                probSum += getProbability(currentContext, symbol); 
    9387                                if( probSum>=randVal ) { 
  • trunk/EventBenchCore/src/de/ugoe/cs/eventbench/ppm/Trie.java

    r6 r9  
    44import java.awt.Rectangle; 
    55import java.awt.Shape; 
     6import java.util.LinkedHashSet; 
    67import java.util.LinkedList; 
    78import java.util.List; 
     9import java.util.Set; 
    810 
    911import javax.swing.JFrame; 
    1012 
    1113import org.apache.commons.collections15.Transformer; 
     14 
     15import de.ugoe.cs.eventbench.markov.IncompleteMemory; 
    1216 
    1317import edu.uci.ics.jung.algorithms.layout.Layout; 
     
    2125public class Trie<T> { 
    2226         
     27        private Set<T> knownSymbols; 
     28         
    2329        private final TrieNode<T> rootNode; 
    2430         
    2531        public Trie() { 
    2632                rootNode = new TrieNode<T>(); 
     33                knownSymbols = new LinkedHashSet<T>(); 
     34        } 
     35         
     36        public Set<T> getKnownSymbols() { 
     37                return new LinkedHashSet<T>(knownSymbols); 
     38        } 
     39         
     40        // trains the current Trie using the given sequence and adds all subsequence of length maxOrder 
     41        public void train(List<T> sequence, int maxOrder) { 
     42                IncompleteMemory<T> latestActions = new IncompleteMemory<T>(maxOrder); 
     43                int i=0; 
     44                for(T currentEvent : sequence) { 
     45                        latestActions.add(currentEvent); 
     46                        knownSymbols.add(currentEvent); 
     47                        i++; 
     48                        if( i>=maxOrder ) { 
     49                                add(latestActions.getLast(maxOrder)); 
     50                        } 
     51                } 
     52                int sequenceLength = sequence.size(); 
     53                for( int j=maxOrder-1 ; j>0 ; j-- ) { 
     54                        add(sequence.subList(sequenceLength-j, sequenceLength)); 
     55                } 
    2756        } 
    2857         
    2958 
     59        // increases the counters for each symbol in the subsequence 
    3060        public void add(List<T> subsequence) { 
    31                 if( !subsequence.isEmpty() ) { 
     61                if( subsequence!=null && !subsequence.isEmpty() ) { 
     62                        knownSymbols.addAll(subsequence); 
    3263                        subsequence = new LinkedList<T>(subsequence);  // defensive copy! 
    3364                        T firstSymbol = subsequence.get(0); 
Note: See TracChangeset for help on using the changeset viewer.