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;
|
---|
38 | int maxLengthall;
|
---|
39 | int numSequences;
|
---|
40 | int maxIterOuter = 100;
|
---|
41 | int maxIterInner = 1000;
|
---|
42 | try {
|
---|
43 | modelname = (String) parameters.get(0);
|
---|
44 | sequencesName = (String) parameters.get(1);
|
---|
45 | length = Integer.parseInt((String) parameters.get(2));
|
---|
46 | maxLengthall = Integer.parseInt((String) parameters.get(3));
|
---|
47 | numSequences = Integer.parseInt((String) parameters.get(4));
|
---|
48 | } catch (Exception e) {
|
---|
49 | throw new InvalidParameterException();
|
---|
50 | }
|
---|
51 |
|
---|
52 | IStochasticProcess model = null;
|
---|
53 | Object dataObject = GlobalDataContainer.getInstance()
|
---|
54 | .getData(modelname);
|
---|
55 | if (dataObject == null) {
|
---|
56 | CommandHelpers.objectNotFoundMessage(modelname);
|
---|
57 | return;
|
---|
58 | } else if (!(dataObject instanceof IStochasticProcess)) {
|
---|
59 | CommandHelpers.objectNotType(modelname, "IStochasticProcess");
|
---|
60 | return;
|
---|
61 | }
|
---|
62 | model = (IStochasticProcess) dataObject;
|
---|
63 | 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");
|
---|
68 | List<Double> probabilities = new ArrayList<Double>(seqsTmp.size());
|
---|
69 | double probSum = 0.0;
|
---|
70 | for (List<? extends Event<?>> sequence : seqsTmp) {
|
---|
71 | double prob = model.getProbability(sequence);
|
---|
72 | probabilities.add(prob);
|
---|
73 | probSum += prob;
|
---|
74 | }
|
---|
75 |
|
---|
76 | Random r = new Random();
|
---|
77 | int j=0;
|
---|
78 | while (sequences.size() < numSequences && j<=maxIterOuter) {
|
---|
79 | j++;
|
---|
80 | double randVal = r.nextDouble() * probSum;
|
---|
81 | double sum = 0.0d;
|
---|
82 | int index = -1;
|
---|
83 | while (sum < randVal) {
|
---|
84 | index++;
|
---|
85 | double currentProb = probabilities.get(index);
|
---|
86 | sum += currentProb;
|
---|
87 | }
|
---|
88 | 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 | }
|
---|
97 | }
|
---|
98 | }
|
---|
99 | }
|
---|
100 | if (GlobalDataContainer.getInstance().addData(sequencesName, sequences)) {
|
---|
101 | CommandHelpers.dataOverwritten(sequencesName);
|
---|
102 | }
|
---|
103 | Console.println("" + sequences.size() + " sequences generated");
|
---|
104 | }
|
---|
105 |
|
---|
106 | public List<? extends Event<?>> finishSequence(List<? extends Event<?>> sequence, IStochasticProcess model) {
|
---|
107 | Random r = new Random();
|
---|
108 | List<Event<?>> sequenceCopy = new LinkedList<Event<?>>(sequence);
|
---|
109 |
|
---|
110 | boolean endFound = false;
|
---|
111 |
|
---|
112 | 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);
|
---|
123 | }
|
---|
124 | break;
|
---|
125 | }
|
---|
126 | }
|
---|
127 | }
|
---|
128 | return sequenceCopy;
|
---|
129 | }
|
---|
130 |
|
---|
131 | /*
|
---|
132 | * (non-Javadoc)
|
---|
133 | *
|
---|
134 | * @see de.ugoe.cs.util.console.Command#help()
|
---|
135 | */
|
---|
136 | @Override
|
---|
137 | public void help() {
|
---|
138 | Console.println("Usage: generateFixedLengthSequences <modelname> <sequencesName> <lenght> <maxlengthAll> <numSequences>");
|
---|
139 | }
|
---|
140 |
|
---|
141 | }
|
---|