Ignore:
Timestamp:
07/27/14 20:28:19 (10 years ago)
Author:
rkrimmel
Message:

Harmonizing matches

Location:
branches/ralph/src/main/java/de/ugoe/cs/autoquest/tasktrees
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/ralph/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/algorithms/Match.java

    r1620 r1621  
    22 
    33import java.util.ArrayList; 
     4import java.util.Iterator; 
    45import java.util.LinkedList; 
    56 
    67public class Match { 
    78        private ArrayList<NumberSequence> matchseqs; 
    8          
    9         private LinkedList<MatchOccurence> occurences;  
    10          
    11          
    12         Match(){ 
     9 
     10        private LinkedList<MatchOccurence> occurences; 
     11 
     12        Match() { 
    1313                matchseqs = new ArrayList<NumberSequence>(2); 
    1414                occurences = new LinkedList<MatchOccurence>(); 
     
    1616                matchseqs.add(null); 
    1717        } 
    18          
     18 
    1919        public NumberSequence getFirstSequence() { 
    2020                return matchseqs.get(0); 
    2121        } 
    22          
     22 
    2323        public NumberSequence getSecondSequence() { 
    2424                return matchseqs.get(1); 
    2525        } 
    26          
     26 
    2727        public void setFirstSequence(NumberSequence seq) { 
    2828                matchseqs.set(0, seq); 
    2929        } 
    30          
     30 
    3131        public void setSecondSequence(NumberSequence seq) { 
    3232                matchseqs.set(1, seq); 
    3333        } 
    34          
     34 
    3535        public void addOccurence(MatchOccurence occurence) { 
    3636                occurences.add(occurence); 
    3737        } 
    38          
     38 
    3939        public int occurenceCount() { 
    4040                return occurences.size(); 
    41         }        
     41        } 
     42 
     43        public boolean equals(Match m) { 
     44                if ((m.getFirstSequence().equals(this.getFirstSequence()) || m 
     45                                .getFirstSequence().equals(this.getSecondSequence())) 
     46                                && (m.getSecondSequence().equals(this.getFirstSequence()) || m 
     47                                                .getSecondSequence().equals(this.getSecondSequence()))) { 
     48                        return true; 
     49                } 
     50                return false; 
     51        } 
     52 
     53        public LinkedList<MatchOccurence> getOccurences() { 
     54                return occurences; 
     55        } 
     56 
     57        public void setOccurences(LinkedList<MatchOccurence> occurences) { 
     58                this.occurences = occurences; 
     59        } 
     60 
     61         
     62        public void addOccurencesOf(Match m) { 
     63                LinkedList<MatchOccurence> occ = m.getOccurences(); 
     64                occurences.addAll(occ); 
     65        } 
    4266 
    4367} 
  • branches/ralph/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/algorithms/NumberSequence.java

    r1620 r1621  
    22 
    33import java.util.ArrayList; 
     4import java.util.LinkedList; 
    45import java.util.Random; 
    56 
     
    6162        } 
    6263 
    63  
    64         public int containsPattern(Match pattern) { 
     64        //Searching occurences of  
     65        public LinkedList<Integer> containsPattern(Match pattern) { 
     66                LinkedList<Integer> result = new LinkedList<Integer>(); 
    6567                int i = 0; 
    66                 int count = 0; 
    6768                int[] pat1 = pattern.getFirstSequence().getSequence(); 
    6869                int[] pat2 = pattern.getSecondSequence().getSequence(); 
     
    8889                                        ipat2++; 
    8990                                } 
    90                                 count++; 
     91                                result.add(i); 
    9192                        } 
    9293                        i++; 
    9394                } 
    94                 return count; 
     95                return result; 
    9596        } 
    9697         
     
    119120                this.id = id; 
    120121        } 
     122         
     123        public boolean equals(NumberSequence n) { 
     124                int[] seq = n.getSequence(); 
     125                if(n.size() !=this.size()) { 
     126                        return false;  
     127                } 
     128                for (int i=0; i<n.size();i++) { 
     129                        if(seq[i] != this.sequence[i]) { 
     130                                return false; 
     131                        } 
     132                } 
     133                return true; 
     134        } 
     135         
     136         
    121137} 
  • branches/ralph/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/SequenceForTaskDetectionRuleAlignment.java

    r1620 r1621  
    1616 
    1717import java.util.ArrayList; 
     18import java.util.Collections; 
     19import java.util.Comparator; 
    1820import java.util.HashMap; 
    1921import java.util.HashSet; 
     
    2729import de.ugoe.cs.autoquest.tasktrees.alignment.algorithms.AlignmentAlgorithmFactory; 
    2830import de.ugoe.cs.autoquest.tasktrees.alignment.algorithms.Match; 
     31import de.ugoe.cs.autoquest.tasktrees.alignment.algorithms.MatchOccurence; 
    2932import de.ugoe.cs.autoquest.tasktrees.alignment.algorithms.NumberSequence; 
    3033import de.ugoe.cs.autoquest.tasktrees.alignment.matrix.PairwiseAlignmentGenerator; 
     
    154157                SymbolMap<ITaskInstance, ITask> uniqueTasks = harmonizeEventTaskInstancesModel(appData); 
    155158 
    156                 Console.traceln(Level.INFO,"generating substitution matrix"); 
     159                // Generate a substitution matrix between all occuring events. 
     160                Console.traceln(Level.INFO, "generating substitution matrix"); 
    157161                ObjectDistanceSubstitionMatrix submat = new ObjectDistanceSubstitionMatrix( 
    158                                 uniqueTasks, 6,-3); 
     162                                uniqueTasks, 6, -3); 
    159163                submat.generate(); 
    160164 
    161                 Console.traceln(Level.INFO,"generating pairwise alignments"); 
    162                 ArrayList<Match> matchseqs = new ArrayList<Match>(); 
    163                 PairwiseAlignmentStorage alignments = PairwiseAlignmentGenerator.generate(numberseqs,submat,9); 
    164                  
    165                 Console.traceln(Level.INFO,"retrieving significant sequence pieces"); 
    166                 for (int i=0; i< numberseqs.size();i++) { 
    167                         Console.traceln(Level.FINEST,"retrieving significant sequence pieces:  " + Math.round((float)i/(float)numberseqs.size()*100) + "%"); 
    168                         for(int j=0; j< numberseqs.size();j++) { 
    169                                 if(i != j) { 
     165                // Generate pairwise alignments 
     166                Console.traceln(Level.INFO, "generating pairwise alignments"); 
     167                LinkedList<Match> matchseqs = new LinkedList<Match>(); 
     168                PairwiseAlignmentStorage alignments = PairwiseAlignmentGenerator 
     169                                .generate(numberseqs, submat, 9); 
     170 
     171                // Retrieve all matches reached a specific threshold 
     172                Console.traceln(Level.INFO, "retrieving significant sequence pieces"); 
     173                for (int i = 0; i < numberseqs.size(); i++) { 
     174                        Console.traceln( 
     175                                        Level.FINEST, 
     176                                        "retrieving significant sequence pieces:  " 
     177                                                        + Math.round((float) i / (float) numberseqs.size() 
     178                                                                        * 100) + "%"); 
     179                        for (int j = 0; j < numberseqs.size(); j++) { 
     180                                if (i != j) { 
    170181                                        matchseqs.addAll(alignments.get(i, j).getMatches()); 
    171                                 }    
    172                         } 
    173                 } 
    174                 Console.traceln(Level.FINEST,"retrieving significant sequence pieces:  100%"); 
    175                  
     182                                } 
     183                        } 
     184                } 
     185                Console.traceln(Level.FINEST, 
     186                                "retrieving significant sequence pieces:  100%"); 
    176187                Console.traceln(Level.INFO, "searching for patterns occuring most"); 
    177188 
    178                 //search this match in every other sequence 
    179                 for(int i=0; i < matchseqs.size();i++) { 
     189                // search each match in every other sequence 
     190                for (Iterator<Match> it = matchseqs.iterator(); it.hasNext();) { 
    180191                        int sequencecount = 0; 
    181192                        int totalcount = 0; 
    182                         Match pattern = matchseqs.get(i); 
     193                        Match pattern = it.next(); 
     194 
     195                        // Skip sequences with more 0 events (scrolls) than other events. 
     196                        // Both of the pattern sequences are equally long, so the added 0 
     197                        // counts 
     198                        // just need to be smaller than the length of one sequence 
     199                        if (pattern.getFirstSequence().eventCount(0) 
     200                                        + pattern.getSecondSequence().eventCount(0) + 1 > pattern 
     201                                        .getFirstSequence().size()) 
     202                                continue; 
     203         
     204                        for (int j = 0; j < numberseqs.size(); j++) { 
     205                                LinkedList<Integer> startpositions = numberseqs.get(j) 
     206                                                .containsPattern(pattern); 
     207                                if (startpositions.size() > 0) { 
     208                                        sequencecount++; 
     209                                        totalcount += startpositions.size(); 
     210                                        for (Iterator<Integer> jt = startpositions.iterator(); jt 
     211                                                        .hasNext();) { 
     212                                                pattern.addOccurence( 
     213                                                                new MatchOccurence(jt.next(), j)); 
     214                                        } 
     215 
     216                                } 
     217                        } 
     218                } 
     219 
     220                Console.traceln(Level.INFO, "sorting results"); 
     221                // Sort results to get the most occuring results 
     222                Comparator<Match> comparator = new Comparator<Match>() { 
     223                        public int compare(Match m1, Match m2) { 
     224                                return m2.occurenceCount() - m1.occurenceCount(); // use your 
     225                                                                                                                                        // logic 
     226                        } 
     227                }; 
     228                Collections.sort(matchseqs, comparator); 
     229                 
     230 
     231                 
     232                 
     233                // Harmonize matches: finding double matches and merge them 
     234                /* 
     235                Console.traceln(Level.INFO, "harmonizing matches"); 
     236                int i=0; 
     237                 
     238                while(i<matchseqs.size()) { 
     239                        int j=i; 
     240                        while(j<matchseqs.size()) { 
     241                                if(i!=j) { 
     242                                        if(matchseqs.get(i).equals(matchseqs.get(j))) { 
     243                                                matchseqs.get(i).addOccurencesOf(matchseqs.get(j)); 
     244                                                matchseqs.remove(j); 
     245                                         
     246                                                //System.out.println("Sequence " + i); 
     247                                                //matchseqs.get(i).getFirstSequence().printSequence(); 
     248                                                //matchseqs.get(i).getSecondSequence().printSequence(); 
     249                                                //System.out.println("is equal to sequence " + j); 
     250                                                //matchseqs.get(j).getFirstSequence().printSequence(); 
     251                                                //matchseqs.get(j).getSecondSequence().printSequence(); 
     252                                                continue; 
     253                                        } 
     254                                } 
     255                                j++; 
     256                        } 
     257                        i++; 
     258                } 
     259                Collections.sort(matchseqs, comparator); 
     260                */  
     261         
     262                                         
    183263                         
    184264                 
    185                          
    186                         //Skip sequences with more 0 events (scrolls) than other events. Both of the pattern sequences are equally long, so the added 0 counts  
    187                         //just need to be smaller than the length of one sequence  
    188                         if(pattern.getFirstSequence().eventCount(0) + pattern.getSecondSequence().eventCount(0) +1 > pattern.getFirstSequence().size()) 
    189                                 continue; 
    190                         Console.traceln(Level.FINEST, "searching for patterns occuring most: " + Math.round((float)i/(float)matchseqs.size()*100) + "%"); 
    191                         for(int j=0; j < numberseqs.size();j++) { 
    192                                         int tmpcount = numberseqs.get(j).containsPattern(pattern); 
    193                                         if(tmpcount > 0) { 
    194                                                 sequencecount++; 
    195                                                 totalcount+=tmpcount; 
    196                                         } 
    197                         } 
    198                          
    199                         if(totalcount > 1) { 
    200                                 matchseqs.get(i).getFirstSequence().printSequence();  
     265                 
     266                 
     267                 
     268                // Just printing the results out 
     269                for (int i = 0; i < matchseqs.size(); i++) { 
     270                        // Every pattern consists of 2 sequences, therefore the minimum 
     271                        // occurences here is 2. 
     272                        // We just need the sequences also occuring in other sequences as 
     273                        // well 
     274                        if (matchseqs.get(i).occurenceCount() > 2) { 
    201275                                matchseqs.get(i).getFirstSequence().printSequence(); 
    202                                 System.out.println("Found pattern in " + sequencecount +"/" + numberseqs.size() + " sequences, total of " + totalcount + " occurences"); 
     276                                matchseqs.get(i).getSecondSequence().printSequence(); 
     277                                System.out.println("Found pattern " 
     278                                                + matchseqs.get(i).occurenceCount() + " times"); 
    203279                                System.out.println(); 
    204280                        } 
    205281                } 
     282                 
     283 
    206284                alignments = null; 
    207                  
    208                 // 
    209                 //AlignmentAlgorithmFactory.setDefaultAlgorithm("de.ugoe.cs.autoquest.tasktrees.alignment.algorithms.NeedlemanWunsch"); 
    210                 //PairwiseAlignmentStorage matchAlignments = PairwiseAlignmentGenerator.generate(matchseqs, submat); 
    211                 //UPGMAAligningTree guidetree = new UPGMAAligningTree(matchseqs,matchAlignments,submat); 
    212                 //System.out.println(alignments.getDistanceMatrix()); 
    213                  
    214                 //for(Iterator<NumberSequence> it = guidetree.getRoot().getSequences().iterator();it.hasNext();) { 
    215                 //      NumberSequence tmp = (NumberSequence) it.next(); 
    216                 //      tmp.printSequence(); 
    217                 //} 
    218                  
    219          
     285 
    220286                /* 
    221                 do { 
    222  
    223                         // appData.getStopWatch().start("whole loop"); 
    224                         // detectAndReplaceIterations(appData); 
    225  
    226                         // appData.getStopWatch().start("task replacement"); 
    227                         // detectAndReplaceTasks(appData); 
    228                         // appData.getStopWatch().stop("task replacement"); 
    229                         // appData.getStopWatch().stop("whole loop"); 
    230  
    231                         // appData.getStopWatch().dumpStatistics(System.out); 
    232                         // appData.getStopWatch().reset(); 
    233  
    234                 } while (appData.detectedAndReplacedTasks()); */ 
     287                 * do { 
     288                 *  
     289                 * // appData.getStopWatch().start("whole loop"); // 
     290                 * detectAndReplaceIterations(appData); 
     291                 *  
     292                 * // appData.getStopWatch().start("task replacement"); // 
     293                 * detectAndReplaceTasks(appData); // 
     294                 * appData.getStopWatch().stop("task replacement"); // 
     295                 * appData.getStopWatch().stop("whole loop"); 
     296                 *  
     297                 * // appData.getStopWatch().dumpStatistics(System.out); // 
     298                 * appData.getStopWatch().reset(); 
     299                 *  
     300                 * } while (appData.detectedAndReplacedTasks()); 
     301                 */ 
    235302 
    236303                Console.println("created " 
Note: See TracChangeset for help on using the changeset viewer.