Changeset 171 for trunk/EventBenchConsole/src/de/ugoe/cs/eventbench/commands/CMDgenerateFixedLengthSequences.java
- Timestamp:
- 09/09/11 06:23:36 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/EventBenchConsole/src/de/ugoe/cs/eventbench/commands/CMDgenerateFixedLengthSequences.java
r128 r171 17 17 import de.ugoe.cs.util.console.Console; 18 18 19 /** 20 * <p> 21 * Command to generate all sequences of a given length. 22 * </p> 23 * 24 * @author Steffen Herbold 25 * @version 1.0 26 */ 19 27 public class CMDgenerateFixedLengthSequences implements Command { 20 28 29 /* 30 * (non-Javadoc) 31 * 32 * @see de.ugoe.cs.util.console.Command#run(java.util.List) 33 */ 21 34 @Override 22 35 public void run(List<Object> parameters) { … … 32 45 minLength = Integer.parseInt((String) parameters.get(2)); 33 46 maxLength = Integer.parseInt((String) parameters.get(3)); 34 if ( parameters.size()>=5) {47 if (parameters.size() >= 5) { 35 48 all = Boolean.parseBoolean((String) parameters.get(4)); 36 49 } 37 if ( parameters.size()==6) {38 numSequences = Integer.parseInt((String) parameters.get(5)); 50 if (parameters.size() == 6) { 51 numSequences = Integer.parseInt((String) parameters.get(5)); 39 52 } 40 } 41 catch (Exception e) { 53 } catch (Exception e) { 42 54 throw new InvalidParameterException(); 43 55 } 44 45 IStochasticProcess model = null; 46 Object dataObject = GlobalDataContainer.getInstance().getData(modelname); 47 if( dataObject==null ) { 56 57 IStochasticProcess model = null; 58 Object dataObject = GlobalDataContainer.getInstance() 59 .getData(modelname); 60 if (dataObject == null) { 48 61 Console.println("Model " + modelname + " not found in storage."); 49 } 50 else if( !(dataObject instanceof IStochasticProcess) ) { 62 } else if (!(dataObject instanceof IStochasticProcess)) { 51 63 Console.println("Object " + modelname + " not of type MarkovModel!"); 52 64 } else { 53 65 model = (IStochasticProcess) dataObject; 54 66 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));67 for (int length = minLength; length <= maxLength; length++) { 68 sequences.addAll(model.generateValidSequences(length + 2)); 57 69 } 58 70 Console.traceln("" + sequences.size() + " possible"); 59 if( !all && numSequences<sequences.size() ) { 60 List<Double> probabilities = new ArrayList<Double>(sequences.size()); 71 if (!all && numSequences < sequences.size()) { 72 List<Double> probabilities = new ArrayList<Double>( 73 sequences.size()); 61 74 double probSum = 0.0; 62 for ( List<? extends Event<?>> sequence : sequences) {75 for (List<? extends Event<?>> sequence : sequences) { 63 76 double prob = model.getProbability(sequence); 64 77 probabilities.add(prob); … … 67 80 Set<Integer> drawnSequences = new HashSet<Integer>(numSequences); 68 81 Random r = new Random(); 69 while ( drawnSequences.size()<numSequences) {70 double randVal = r.nextDouble() *probSum;82 while (drawnSequences.size() < numSequences) { 83 double randVal = r.nextDouble() * probSum; 71 84 double sum = 0.0d; 72 85 int index = -1; 73 while ( sum<randVal) {86 while (sum < randVal) { 74 87 index++; 75 88 double currentProb = probabilities.get(index); 76 89 sum += currentProb; 77 90 } 78 if ( !drawnSequences.contains(index)) {91 if (!drawnSequences.contains(index)) { 79 92 drawnSequences.add(index); 80 93 probSum -= probabilities.get(index); … … 84 97 Collection<List<? extends Event<?>>> retainedSequences = new LinkedList<List<? extends Event<?>>>(); 85 98 int index = 0; 86 for (List<? extends Event<?>> sequence : sequences) {87 if ( drawnSequences.contains(index)) {99 for (List<? extends Event<?>> sequence : sequences) { 100 if (drawnSequences.contains(index)) { 88 101 retainedSequences.add(sequence); 89 102 } … … 92 105 sequences = retainedSequences; 93 106 } 94 if( GlobalDataContainer.getInstance().addData(sequencesName, sequences) ) { 95 Console.traceln("Old data \"" + sequencesName + "\" overwritten"); 107 if (GlobalDataContainer.getInstance().addData(sequencesName, 108 sequences)) { 109 Console.traceln("Old data \"" + sequencesName 110 + "\" overwritten"); 96 111 } 97 112 Console.println("" + sequences.size() + " sequences generated"); … … 99 114 } 100 115 116 /* 117 * (non-Javadoc) 118 * 119 * @see de.ugoe.cs.util.console.Command#help() 120 */ 101 121 @Override 102 122 public void help() {
Note: See TracChangeset
for help on using the changeset viewer.