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.HashSet;
|
---|
7 | import java.util.LinkedHashSet;
|
---|
8 | import java.util.LinkedList;
|
---|
9 | import java.util.List;
|
---|
10 | import java.util.Random;
|
---|
11 | import java.util.Set;
|
---|
12 |
|
---|
13 | import de.ugoe.cs.eventbench.CommandHelpers;
|
---|
14 | import de.ugoe.cs.eventbench.data.Event;
|
---|
15 | import de.ugoe.cs.eventbench.data.GlobalDataContainer;
|
---|
16 | import de.ugoe.cs.eventbench.models.IStochasticProcess;
|
---|
17 | import de.ugoe.cs.util.console.Command;
|
---|
18 | import de.ugoe.cs.util.console.Console;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * <p>
|
---|
22 | * Command to generate all sequences of a given length.
|
---|
23 | * </p>
|
---|
24 | *
|
---|
25 | * @author Steffen Herbold
|
---|
26 | * @version 1.0
|
---|
27 | */
|
---|
28 | public class CMDgenerateFixedLengthSequences implements Command {
|
---|
29 |
|
---|
30 | /*
|
---|
31 | * (non-Javadoc)
|
---|
32 | *
|
---|
33 | * @see de.ugoe.cs.util.console.Command#run(java.util.List)
|
---|
34 | */
|
---|
35 | @Override
|
---|
36 | public void run(List<Object> parameters) {
|
---|
37 | String modelname;
|
---|
38 | String sequencesName;
|
---|
39 | int minLength;
|
---|
40 | int maxLength;
|
---|
41 | boolean all = true;
|
---|
42 | int numSequences = -1;
|
---|
43 | boolean validEnd = true;
|
---|
44 | try {
|
---|
45 | modelname = (String) parameters.get(0);
|
---|
46 | sequencesName = (String) parameters.get(1);
|
---|
47 | minLength = Integer.parseInt((String) parameters.get(2));
|
---|
48 | maxLength = Integer.parseInt((String) parameters.get(3));
|
---|
49 | if (parameters.size() >= 5) {
|
---|
50 | all = Boolean.parseBoolean((String) parameters.get(4));
|
---|
51 | }
|
---|
52 | if (parameters.size() >= 6) {
|
---|
53 | numSequences = Integer.parseInt((String) parameters.get(5));
|
---|
54 | }
|
---|
55 | if (parameters.size() >= 7) {
|
---|
56 | validEnd = Boolean.parseBoolean((String) parameters.get(6));
|
---|
57 | }
|
---|
58 | } catch (Exception e) {
|
---|
59 | throw new InvalidParameterException();
|
---|
60 | }
|
---|
61 |
|
---|
62 | IStochasticProcess model = null;
|
---|
63 | Object dataObject = GlobalDataContainer.getInstance()
|
---|
64 | .getData(modelname);
|
---|
65 | if (dataObject == null) {
|
---|
66 | CommandHelpers.objectNotFoundMessage(modelname);
|
---|
67 | return;
|
---|
68 | } else if (!(dataObject instanceof IStochasticProcess)) {
|
---|
69 | CommandHelpers.objectNotType(modelname, "IStochasticProcess");
|
---|
70 | return;
|
---|
71 | }
|
---|
72 | model = (IStochasticProcess) dataObject;
|
---|
73 | Collection<List<? extends Event<?>>> sequences = new LinkedHashSet<List<? extends Event<?>>>();
|
---|
74 | for (int length = minLength; length <= maxLength; length++) {
|
---|
75 | if( validEnd ) {
|
---|
76 | sequences.addAll(model.generateValidSequences(length + 2));
|
---|
77 | } else {
|
---|
78 | sequences.addAll(model.generateSequences(length + 1, true));
|
---|
79 | }
|
---|
80 | }
|
---|
81 | Console.traceln("" + sequences.size() + " possible");
|
---|
82 | if (!all && numSequences < sequences.size()) {
|
---|
83 | List<Double> probabilities = new ArrayList<Double>(sequences.size());
|
---|
84 | double probSum = 0.0;
|
---|
85 | for (List<? extends Event<?>> sequence : sequences) {
|
---|
86 | double prob = model.getProbability(sequence);
|
---|
87 | probabilities.add(prob);
|
---|
88 | probSum += prob;
|
---|
89 | }
|
---|
90 | Set<Integer> drawnSequences = new HashSet<Integer>(numSequences);
|
---|
91 | Random r = new Random();
|
---|
92 | while (drawnSequences.size() < numSequences) {
|
---|
93 | double randVal = r.nextDouble() * probSum;
|
---|
94 | double sum = 0.0d;
|
---|
95 | int index = -1;
|
---|
96 | while (sum < randVal) {
|
---|
97 | index++;
|
---|
98 | double currentProb = probabilities.get(index);
|
---|
99 | sum += currentProb;
|
---|
100 | }
|
---|
101 | if (!drawnSequences.contains(index)) {
|
---|
102 | drawnSequences.add(index);
|
---|
103 | probSum -= probabilities.get(index);
|
---|
104 | probabilities.set(index, 0.0d);
|
---|
105 | }
|
---|
106 | }
|
---|
107 | Collection<List<? extends Event<?>>> retainedSequences = new LinkedList<List<? extends Event<?>>>();
|
---|
108 | int index = 0;
|
---|
109 | for (List<? extends Event<?>> sequence : sequences) {
|
---|
110 | if (drawnSequences.contains(index)) {
|
---|
111 | retainedSequences.add(sequence);
|
---|
112 | }
|
---|
113 | index++;
|
---|
114 | }
|
---|
115 | sequences = retainedSequences;
|
---|
116 | }
|
---|
117 | if (GlobalDataContainer.getInstance().addData(sequencesName, sequences)) {
|
---|
118 | CommandHelpers.dataOverwritten(sequencesName);
|
---|
119 | }
|
---|
120 | Console.println("" + sequences.size() + " sequences generated");
|
---|
121 | }
|
---|
122 |
|
---|
123 | /*
|
---|
124 | * (non-Javadoc)
|
---|
125 | *
|
---|
126 | * @see de.ugoe.cs.util.console.Command#help()
|
---|
127 | */
|
---|
128 | @Override
|
---|
129 | public void help() {
|
---|
130 | Console.println("Usage: generateFixedLengthSequences <modelname> <sequencesName> <minlenght> <maxlength> {<all>} {<numSequences>} {<validEnd>}");
|
---|
131 | }
|
---|
132 |
|
---|
133 | }
|
---|