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

Harmonizing matches

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.