Changeset 395
- Timestamp:
- 03/08/12 09:38:19 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/EventBenchConsole/src/de/ugoe/cs/eventbench/commands/CMDgenerateGreedy.java
r311 r395 2 2 3 3 import java.security.InvalidParameterException; 4 import java.util.ArrayList; 4 import java.util.Collection; 5 import java.util.Iterator; 5 6 import java.util.LinkedHashSet; 6 7 import java.util.LinkedList; … … 10 11 11 12 import de.ugoe.cs.eventbench.CommandHelpers; 12 import de.ugoe.cs.eventbench.coverage.CoverageCalculatorProcess;13 13 import de.ugoe.cs.eventbench.coverage.SequenceTools; 14 14 import de.ugoe.cs.eventbench.data.Event; … … 50 50 int coverageDepth; 51 51 float desiredCoverage; 52 boolean validEnd = true; 52 53 try { 53 54 modelname = (String) parameters.get(0); … … 57 58 coverageDepth = Integer.parseInt((String) parameters.get(4)); 58 59 desiredCoverage = Float.parseFloat((String) parameters.get(5)); 60 if (parameters.size() >= 7) { 61 validEnd = Boolean.parseBoolean((String) parameters.get(6)); 62 } 59 63 } catch (Exception e) { 60 64 throw new InvalidParameterException(); … … 74 78 75 79 // set up everything 76 List<List<? extends Event<?>>> allSequences = new ArrayList<List<? extends Event<?>>>();80 List<List<? extends Event<?>>> allSequences = new LinkedList<List<? extends Event<?>>>(); 77 81 for (int length = minLength; length <= maxLength; length++) { 78 allSequences.addAll(model.generateValidSequences(length + 2)); 82 if (validEnd) { 83 allSequences.addAll(model.generateValidSequences(length + 2)); 84 } else { 85 allSequences.addAll(model.generateSequences(length + 1, true)); 86 } 79 87 } 80 88 Console.traceln("" + allSequences.size() + " possible"); 81 89 82 Set<List<? extends Event<?>>> allSubSeqs = SequenceTools83 . containedSubSequences(allSequences,coverageDepth);90 Collection<List<? extends Event<?>>> allSubSeqs = model 91 .generateSequences(coverageDepth); 84 92 Map<List<? extends Event<?>>, Double> weightMap = SequenceTools 85 93 .generateWeights(model, allSubSeqs); 86 94 Set<List<? extends Event<?>>> coveredSubSeqs = new LinkedHashSet<List<? extends Event<?>>>(); 87 95 88 List<Set<List<? extends Event<?>>>> containedSubSeqs = new ArrayList<Set<List<? extends Event<?>>>>( 89 allSequences.size()); 96 List<Set<List<? extends Event<?>>>> containedSubSeqs = new LinkedList<Set<List<? extends Event<?>>>>(); 90 97 for (List<? extends Event<?>> sequence : allSequences) { 91 98 List<List<? extends Event<?>>> wrapper = new LinkedList<List<? extends Event<?>>>(); … … 96 103 } 97 104 98 Double[] sequenceGain = new Double[allSequences.size()];99 105 List<List<? extends Event<?>>> testSuite = new LinkedList<List<? extends Event<?>>>(); 100 CoverageCalculatorProcess coverageCalculator = new CoverageCalculatorProcess(101 model, testSuite, coverageDepth);102 106 double currentCoverage = 0.0d; 103 107 104 108 // Build test suite 109 double prevGain = 1.0d; 110 boolean gainEqual = false; 105 111 while (currentCoverage < desiredCoverage) { 106 for (int i = 0; i < allSequences.size(); i++) { 112 Double[] sequenceGain = new Double[allSequences.size()]; 113 int i = 0; 114 for (Set<List<? extends Event<?>>> containedSubSeq : containedSubSeqs) { 107 115 double gain = 0.0d; 108 for (List<? extends Event<?>> subSeq : containedSubSeqs.get(i)) { 116 Iterator<List<? extends Event<?>>> subSeqIter = containedSubSeq 117 .iterator(); 118 while (subSeqIter.hasNext()) { 119 List<? extends Event<?>> subSeq = subSeqIter.next(); 109 120 if (!coveredSubSeqs.contains(subSeq)) { 110 121 gain += weightMap.get(subSeq); 122 } else { 123 subSeqIter.remove(); 111 124 } 112 125 } 113 126 sequenceGain[i] = gain; 127 // optimization using that the gain is monotonically decreasing 128 if (Math.abs(gain - prevGain) <= eps) { 129 gainEqual = true; 130 break; 131 } 132 i++; 114 133 } 115 int maxIndex = ArrayTools.findMax(sequenceGain); 116 if (sequenceGain[maxIndex] <= 0.0 + eps) { 134 int maxIndex; 135 if (gainEqual) { 136 maxIndex = i; 137 } else { 138 maxIndex = ArrayTools.findMax(sequenceGain); 139 } 140 if (maxIndex < 0 || sequenceGain[maxIndex] <= 0.0 + eps) { 117 141 Console.traceln("No gain anymore! Desired coverage cannot be satisfied!"); 118 142 break; 119 143 } 144 prevGain = sequenceGain[maxIndex]; 120 145 testSuite.add(allSequences.get(maxIndex)); 121 146 coveredSubSeqs.addAll(containedSubSeqs.get(maxIndex)); 122 coverageCalculator.setSequences(testSuite); 123 currentCoverage = coverageCalculator.getCoveragePossibleWeight(); 147 currentCoverage += sequenceGain[maxIndex]; 148 if (gainEqual) { 149 allSequences.remove(maxIndex); 150 containedSubSeqs.remove(maxIndex); 151 gainEqual = false; 152 } else { 153 for (int j = sequenceGain.length - 1; j >= 0; j--) { 154 if (j == maxIndex || sequenceGain[j] <= 0.0 + eps) { 155 allSequences.remove(j); 156 containedSubSeqs.remove(j); 157 } 158 } 159 } 124 160 } 125 161 … … 138 174 @Override 139 175 public void help() { 140 Console.println("generateGreedy <modelname> <sequencesName> <minLength> <maxLength> <coverageDepth> <desiredCoverage> ");176 Console.println("generateGreedy <modelname> <sequencesName> <minLength> <maxLength> <coverageDepth> <desiredCoverage> {<validEnd>}"); 141 177 } 142 178
Note: See TracChangeset
for help on using the changeset viewer.