[261] | 1 | package de.ugoe.cs.eventbench.commands;
|
---|
| 2 |
|
---|
| 3 | import java.security.InvalidParameterException;
|
---|
| 4 | import java.util.ArrayList;
|
---|
| 5 | import java.util.Collection;
|
---|
| 6 | import java.util.LinkedHashSet;
|
---|
| 7 | import java.util.LinkedList;
|
---|
| 8 | import java.util.List;
|
---|
| 9 | import java.util.Random;
|
---|
| 10 |
|
---|
| 11 | import de.ugoe.cs.eventbench.CommandHelpers;
|
---|
| 12 | import de.ugoe.cs.eventbench.data.Event;
|
---|
| 13 | import de.ugoe.cs.eventbench.data.GlobalDataContainer;
|
---|
| 14 | import de.ugoe.cs.eventbench.models.IStochasticProcess;
|
---|
| 15 | import de.ugoe.cs.util.console.Command;
|
---|
| 16 | import de.ugoe.cs.util.console.Console;
|
---|
| 17 |
|
---|
| 18 | /**
|
---|
| 19 | * <p>
|
---|
| 20 | * Command to generate sequences of a given length.
|
---|
| 21 | * </p>
|
---|
| 22 | *
|
---|
| 23 | * @author Steffen Herbold
|
---|
| 24 | * @version 1.0
|
---|
| 25 | */
|
---|
| 26 | public class CMDgenerateHybrid implements Command {
|
---|
| 27 |
|
---|
| 28 | /*
|
---|
| 29 | * (non-Javadoc)
|
---|
| 30 | *
|
---|
| 31 | * @see de.ugoe.cs.util.console.Command#run(java.util.List)
|
---|
| 32 | */
|
---|
| 33 | @Override
|
---|
| 34 | public void run(List<Object> parameters) {
|
---|
| 35 | String modelname;
|
---|
| 36 | String sequencesName;
|
---|
| 37 | int length;
|
---|
[394] | 38 | int maxLengthAll;
|
---|
[261] | 39 | int numSequences;
|
---|
[394] | 40 | boolean validEnd = true;
|
---|
[261] | 41 | try {
|
---|
| 42 | modelname = (String) parameters.get(0);
|
---|
| 43 | sequencesName = (String) parameters.get(1);
|
---|
| 44 | length = Integer.parseInt((String) parameters.get(2));
|
---|
[394] | 45 | maxLengthAll = Integer.parseInt((String) parameters.get(3));
|
---|
[261] | 46 | numSequences = Integer.parseInt((String) parameters.get(4));
|
---|
[394] | 47 | if (parameters.size() >= 6) {
|
---|
| 48 | validEnd = Boolean.parseBoolean((String) parameters.get(5));
|
---|
| 49 | }
|
---|
[261] | 50 | } catch (Exception e) {
|
---|
| 51 | throw new InvalidParameterException();
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[394] | 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;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[261] | 69 | IStochasticProcess model = null;
|
---|
| 70 | Object dataObject = GlobalDataContainer.getInstance()
|
---|
| 71 | .getData(modelname);
|
---|
| 72 | if (dataObject == null) {
|
---|
| 73 | CommandHelpers.objectNotFoundMessage(modelname);
|
---|
| 74 | return;
|
---|
| 75 | } else if (!(dataObject instanceof IStochasticProcess)) {
|
---|
| 76 | CommandHelpers.objectNotType(modelname, "IStochasticProcess");
|
---|
| 77 | return;
|
---|
| 78 | }
|
---|
| 79 | model = (IStochasticProcess) dataObject;
|
---|
| 80 | Collection<List<? extends Event<?>>> sequences = new LinkedHashSet<List<? extends Event<?>>>();
|
---|
[394] | 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");
|
---|
[261] | 87 | List<Double> probabilities = new ArrayList<Double>(seqsTmp.size());
|
---|
| 88 | double probSum = 0.0;
|
---|
| 89 | for (List<? extends Event<?>> sequence : seqsTmp) {
|
---|
| 90 | double prob = model.getProbability(sequence);
|
---|
| 91 | probabilities.add(prob);
|
---|
| 92 | probSum += prob;
|
---|
| 93 | }
|
---|
[394] | 94 |
|
---|
[261] | 95 | Random r = new Random();
|
---|
[394] | 96 | int j = 0;
|
---|
| 97 | while (sequences.size() < numSequences && j <= numSequences * 100) {
|
---|
[261] | 98 | j++;
|
---|
| 99 | double randVal = r.nextDouble() * probSum;
|
---|
| 100 | double sum = 0.0d;
|
---|
| 101 | int index = -1;
|
---|
| 102 | while (sum < randVal) {
|
---|
| 103 | index++;
|
---|
| 104 | double currentProb = probabilities.get(index);
|
---|
| 105 | sum += currentProb;
|
---|
| 106 | }
|
---|
| 107 | List<? extends Event<?>> seqTmp = seqsTmp.get(index);
|
---|
[401] | 108 | if (!Event.ENDEVENT.equals(seqTmp.get(seqTmp.size() - 1))) {
|
---|
[394] | 109 | List<? extends Event<?>> sequence;
|
---|
| 110 | if (validEnd) {
|
---|
| 111 | sequence = finishSequence(seqTmp, model, length + 2,
|
---|
| 112 | validEnd);
|
---|
[408] | 113 | if( sequence!= null && sequence.size()!=length+2 ) {
|
---|
| 114 | sequence = null;
|
---|
| 115 | }
|
---|
[394] | 116 | } else {
|
---|
| 117 | sequence = finishSequence(seqTmp, model, length + 1,
|
---|
| 118 | validEnd);
|
---|
[408] | 119 | if( sequence!= null && sequence.size()!=length+1 ) {
|
---|
| 120 | sequence = null;
|
---|
| 121 | }
|
---|
[261] | 122 | }
|
---|
[408] | 123 | if( sequence!=null ) {
|
---|
| 124 | sequences.add(sequence);
|
---|
| 125 | }
|
---|
[261] | 126 | }
|
---|
| 127 | }
|
---|
| 128 | if (GlobalDataContainer.getInstance().addData(sequencesName, sequences)) {
|
---|
| 129 | CommandHelpers.dataOverwritten(sequencesName);
|
---|
| 130 | }
|
---|
| 131 | Console.println("" + sequences.size() + " sequences generated");
|
---|
| 132 | }
|
---|
[394] | 133 |
|
---|
| 134 | public List<? extends Event<?>> finishSequence(
|
---|
| 135 | List<? extends Event<?>> sequence, IStochasticProcess model,
|
---|
| 136 | int maxLength, boolean validEnd) {
|
---|
[261] | 137 | Random r = new Random();
|
---|
| 138 | boolean endFound = false;
|
---|
[394] | 139 | List<Event<?>> sequenceCopy = new LinkedList<Event<?>>(sequence);
|
---|
[408] | 140 | final int maxIter = 30000;
|
---|
| 141 | int iter = 0;
|
---|
| 142 | while (!endFound && iter<maxIter) {
|
---|
| 143 | iter++;
|
---|
[394] | 144 | sequenceCopy = new LinkedList<Event<?>>(sequence);
|
---|
[401] | 145 | while (!endFound && sequenceCopy.size() <= maxLength) {
|
---|
[394] | 146 | double randVal = r.nextDouble();
|
---|
| 147 | double probSum = 0.0;
|
---|
| 148 | for (Event<?> symbol : model.getEvents()) {
|
---|
| 149 | probSum += model.getProbability(sequenceCopy, symbol);
|
---|
| 150 | if (probSum >= randVal) {
|
---|
[408] | 151 | if (!(Event.STARTEVENT.equals(symbol) || (!validEnd && Event.ENDEVENT.equals(symbol)))) {
|
---|
[394] | 152 | // only add the symbol the sequence if it is not
|
---|
| 153 | // START
|
---|
| 154 | // or END
|
---|
| 155 | sequenceCopy.add(symbol);
|
---|
| 156 | }
|
---|
[401] | 157 | endFound = Event.ENDEVENT.equals(symbol)
|
---|
[394] | 158 | || (!validEnd && sequenceCopy.size() == maxLength);
|
---|
| 159 | break;
|
---|
[261] | 160 | }
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
[408] | 164 | if( iter==maxIter ) {
|
---|
| 165 | return null;
|
---|
| 166 | }
|
---|
[261] | 167 | return sequenceCopy;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | /*
|
---|
| 171 | * (non-Javadoc)
|
---|
| 172 | *
|
---|
| 173 | * @see de.ugoe.cs.util.console.Command#help()
|
---|
| 174 | */
|
---|
| 175 | @Override
|
---|
| 176 | public void help() {
|
---|
[408] | 177 | Console.println("Usage: generateHybrid <modelname> <sequencesName> <lenght> <maxlengthAll> <numSequences> {<validEnd>}");
|
---|
[261] | 178 | }
|
---|
| 179 |
|
---|
| 180 | }
|
---|