Changeset 394
- Timestamp:
- 03/08/12 09:32:59 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/EventBenchConsole/src/de/ugoe/cs/eventbench/commands/CMDgenerateHybrid.java
r261 r394 36 36 String sequencesName; 37 37 int length; 38 int maxLength all;38 int maxLengthAll; 39 39 int numSequences; 40 int maxIterOuter = 100; 41 int maxIterInner = 1000; 40 boolean validEnd = true; 42 41 try { 43 42 modelname = (String) parameters.get(0); 44 43 sequencesName = (String) parameters.get(1); 45 44 length = Integer.parseInt((String) parameters.get(2)); 46 maxLength all = Integer.parseInt((String) parameters.get(3));45 maxLengthAll = Integer.parseInt((String) parameters.get(3)); 47 46 numSequences = Integer.parseInt((String) parameters.get(4)); 47 if (parameters.size() >= 6) { 48 validEnd = Boolean.parseBoolean((String) parameters.get(5)); 49 } 48 50 } catch (Exception e) { 49 51 throw new InvalidParameterException(); 52 } 53 54 if (length <= maxLengthAll) { 55 // indirectly call command generateFixedLengthSequences 56 List<Object> parameters2 = new LinkedList<Object>(); 57 parameters2.add(modelname); 58 parameters2.add(sequencesName); 59 parameters2.add(Integer.toString(length)); 60 parameters2.add(Integer.toString(length)); 61 parameters2.add(Boolean.toString(false)); 62 parameters2.add(Integer.toString(numSequences)); 63 parameters2.add(Boolean.toString(validEnd)); 64 CMDgenerateFixedLengthSequences cmd = new CMDgenerateFixedLengthSequences(); 65 cmd.run(parameters2); 66 return; 50 67 } 51 68 … … 62 79 model = (IStochasticProcess) dataObject; 63 80 Collection<List<? extends Event<?>>> sequences = new LinkedHashSet<List<? extends Event<?>>>(); 64 65 List<List<? extends Event<?>>> seqsTmp = new ArrayList<List<? extends Event<?>>>(model.generateSequences(maxLengthall+1, true)); 66 67 Console.traceln("" + seqsTmp.size() + " of length " + maxLengthall + " possible"); 81 82 List<List<? extends Event<?>>> seqsTmp = new ArrayList<List<? extends Event<?>>>( 83 model.generateSequences(maxLengthAll + 1, true)); 84 85 Console.traceln("" + seqsTmp.size() + " of length " + maxLengthAll 86 + " possible"); 68 87 List<Double> probabilities = new ArrayList<Double>(seqsTmp.size()); 69 88 double probSum = 0.0; … … 73 92 probSum += prob; 74 93 } 75 94 76 95 Random r = new Random(); 77 int j =0;78 while (sequences.size() < numSequences && j <=maxIterOuter) {96 int j = 0; 97 while (sequences.size() < numSequences && j <= numSequences * 100) { 79 98 j++; 80 99 double randVal = r.nextDouble() * probSum; … … 87 106 } 88 107 List<? extends Event<?>> seqTmp = seqsTmp.get(index); 89 if ( seqTmp.get(seqTmp.size()-1)!=Event.ENDEVENT) {90 for( int i=0 ; i<maxIterInner ; i++ ) {91 List<? extends Event<?>> sequence = finishSequence(seqTmp, model);92 if( sequence.size() == length+2 ) {93 j=0;94 sequences.add(sequence);95 break;96 }108 if (seqTmp.get(seqTmp.size() - 1) != Event.ENDEVENT) { 109 List<? extends Event<?>> sequence; 110 if (validEnd) { 111 sequence = finishSequence(seqTmp, model, length + 2, 112 validEnd); 113 } else { 114 sequence = finishSequence(seqTmp, model, length + 1, 115 validEnd); 97 116 } 117 sequences.add(sequence); 98 118 } 99 119 } … … 103 123 Console.println("" + sequences.size() + " sequences generated"); 104 124 } 105 106 public List<? extends Event<?>> finishSequence(List<? extends Event<?>> sequence, IStochasticProcess model) { 125 126 public List<? extends Event<?>> finishSequence( 127 List<? extends Event<?>> sequence, IStochasticProcess model, 128 int maxLength, boolean validEnd) { 107 129 Random r = new Random(); 108 List<Event<?>> sequenceCopy = new LinkedList<Event<?>>(sequence);109 110 130 boolean endFound = false; 131 List<Event<?>> sequenceCopy = new LinkedList<Event<?>>(sequence); 111 132 112 133 while (!endFound) { 113 double randVal = r.nextDouble(); 114 double probSum = 0.0; 115 for (Event<?> symbol : model.getEvents()) { 116 probSum += model.getProbability(sequenceCopy, symbol); 117 if (probSum >= randVal) { 118 endFound = (symbol == Event.ENDEVENT); 119 if (!(symbol == Event.STARTEVENT )) { 120 // only add the symbol the sequence if it is not START 121 // or END 122 sequenceCopy.add(symbol); 134 sequenceCopy = new LinkedList<Event<?>>(sequence); 135 while (!endFound && sequenceCopy.size() < maxLength) { 136 double randVal = r.nextDouble(); 137 double probSum = 0.0; 138 for (Event<?> symbol : model.getEvents()) { 139 probSum += model.getProbability(sequenceCopy, symbol); 140 if (probSum >= randVal) { 141 if (!(symbol == Event.STARTEVENT || symbol == Event.ENDEVENT)) { 142 // only add the symbol the sequence if it is not 143 // START 144 // or END 145 sequenceCopy.add(symbol); 146 } 147 endFound = (symbol == Event.ENDEVENT) 148 || (!validEnd && sequenceCopy.size() == maxLength); 149 break; 123 150 } 124 break;125 151 } 126 152 } … … 136 162 @Override 137 163 public void help() { 138 Console.println("Usage: generateFixedLengthSequences <modelname> <sequencesName> <lenght> <maxlengthAll> <numSequences> ");164 Console.println("Usage: generateFixedLengthSequences <modelname> <sequencesName> <lenght> <maxlengthAll> <numSequences> {<validEnd>}"); 139 165 } 140 166
Note: See TracChangeset
for help on using the changeset viewer.