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