Changeset 120
- Timestamp:
- 07/13/11 11:32:29 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/EventBenchConsole/src/de/ugoe/cs/eventbench/commands/CMDgenerateFixedLengthSequences.java
r103 r120 2 2 3 3 import java.security.InvalidParameterException; 4 import java.util.ArrayList; 4 5 import java.util.Collection; 6 import java.util.HashSet; 7 import java.util.LinkedHashSet; 8 import java.util.LinkedList; 5 9 import java.util.List; 10 import java.util.Random; 11 import java.util.Set; 6 12 7 13 import de.ugoe.cs.eventbench.data.Event; … … 17 23 String modelname; 18 24 String sequencesName; 19 int length; 25 int minLength; 26 int maxLength; 27 boolean all = true; 28 int numSequences = -1; 20 29 try { 21 30 modelname = (String) parameters.get(0); 22 31 sequencesName = (String) parameters.get(1); 23 length = Integer.parseInt((String) parameters.get(2)); 32 minLength = Integer.parseInt((String) parameters.get(2)); 33 maxLength = Integer.parseInt((String) parameters.get(3)); 34 if( parameters.size()>=5 ) { 35 all = Boolean.parseBoolean((String) parameters.get(4)); 36 } 37 if( parameters.size()==6 ) { 38 numSequences = Integer.parseInt((String) parameters.get(5)); 39 } 24 40 } 25 41 catch (Exception e) { … … 36 52 } else { 37 53 model = (IStochasticProcess) dataObject; 38 Collection<List<? extends Event<?>>> sequences = model.generateValidSequences(length+2); 54 Collection<List<? extends Event<?>>> sequences = new LinkedHashSet<List<? extends Event<?>>>(); 55 for( int length=minLength; length<maxLength; length++ ) { 56 sequences.addAll(model.generateValidSequences(length+2)); 57 } 58 Console.traceln("" + sequences.size() + " possible"); 59 if( !all && numSequences<sequences.size() ) { 60 List<Double> probabilities = new ArrayList<Double>(sequences.size()); 61 double probSum = 0.0; 62 for( List<? extends Event<?>> sequence : sequences ) { 63 double prob = model.getProbability(sequence); 64 probabilities.add(prob); 65 probSum += prob; 66 } 67 Set<Integer> drawnSequences = new HashSet<Integer>(numSequences); 68 Random r = new Random(); 69 while( drawnSequences.size()<numSequences ) { 70 double randVal = r.nextDouble()*probSum; 71 double sum = 0.0d; 72 int index = -1; 73 while( sum<randVal ) { 74 index++; 75 double currentProb = probabilities.get(index); 76 sum += currentProb; 77 } 78 if( !drawnSequences.contains(index) ) { 79 drawnSequences.add(index); 80 probSum -= probabilities.get(index); 81 probabilities.set(index, 0.0d); 82 } 83 } 84 Collection<List<? extends Event<?>>> retainedSequences = new LinkedList<List<? extends Event<?>>>(); 85 int index = 0; 86 for( List<? extends Event<?>> sequence : sequences) { 87 if( drawnSequences.contains(index) ) { 88 retainedSequences.add(sequence); 89 } 90 index++; 91 } 92 sequences = retainedSequences; 93 } 39 94 if( GlobalDataContainer.getInstance().addData(sequencesName, sequences) ) { 40 95 Console.traceln("Old data \"" + sequencesName + "\" overwritten"); 41 96 } 97 Console.println("" + sequences.size() + " sequences generated"); 42 98 } 43 99 } … … 45 101 @Override 46 102 public void help() { 47 Console.println("Usage: generateFixedLengthSequences <modelname> <sequencesName> <length> ");103 Console.println("Usage: generateFixedLengthSequences <modelname> <sequencesName> <length> {<all>} {<numSequences>}"); 48 104 } 49 105
Note: See TracChangeset
for help on using the changeset viewer.