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