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