source: trunk/EventBenchConsole/src/de/ugoe/cs/eventbench/commands/CMDgenerateFixedLengthSequences.java @ 171

Last change on this file since 171 was 171, checked in by sherbold, 13 years ago
  • code documentation and formatting
  • Property svn:mime-type set to text/plain
File size: 3.9 KB
Line 
1package de.ugoe.cs.eventbench.commands;
2
3import java.security.InvalidParameterException;
4import java.util.ArrayList;
5import java.util.Collection;
6import java.util.HashSet;
7import java.util.LinkedHashSet;
8import java.util.LinkedList;
9import java.util.List;
10import java.util.Random;
11import java.util.Set;
12
13import de.ugoe.cs.eventbench.data.Event;
14import de.ugoe.cs.eventbench.data.GlobalDataContainer;
15import de.ugoe.cs.eventbench.models.IStochasticProcess;
16import de.ugoe.cs.util.console.Command;
17import 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 */
27public 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                } else if (!(dataObject instanceof IStochasticProcess)) {
63                        Console.println("Object " + modelname + " not of type MarkovModel!");
64                } else {
65                        model = (IStochasticProcess) dataObject;
66                        Collection<List<? extends Event<?>>> sequences = new LinkedHashSet<List<? extends Event<?>>>();
67                        for (int length = minLength; length <= maxLength; length++) {
68                                sequences.addAll(model.generateValidSequences(length + 2));
69                        }
70                        Console.traceln("" + sequences.size() + " possible");
71                        if (!all && numSequences < sequences.size()) {
72                                List<Double> probabilities = new ArrayList<Double>(
73                                                sequences.size());
74                                double probSum = 0.0;
75                                for (List<? extends Event<?>> sequence : sequences) {
76                                        double prob = model.getProbability(sequence);
77                                        probabilities.add(prob);
78                                        probSum += prob;
79                                }
80                                Set<Integer> drawnSequences = new HashSet<Integer>(numSequences);
81                                Random r = new Random();
82                                while (drawnSequences.size() < numSequences) {
83                                        double randVal = r.nextDouble() * probSum;
84                                        double sum = 0.0d;
85                                        int index = -1;
86                                        while (sum < randVal) {
87                                                index++;
88                                                double currentProb = probabilities.get(index);
89                                                sum += currentProb;
90                                        }
91                                        if (!drawnSequences.contains(index)) {
92                                                drawnSequences.add(index);
93                                                probSum -= probabilities.get(index);
94                                                probabilities.set(index, 0.0d);
95                                        }
96                                }
97                                Collection<List<? extends Event<?>>> retainedSequences = new LinkedList<List<? extends Event<?>>>();
98                                int index = 0;
99                                for (List<? extends Event<?>> sequence : sequences) {
100                                        if (drawnSequences.contains(index)) {
101                                                retainedSequences.add(sequence);
102                                        }
103                                        index++;
104                                }
105                                sequences = retainedSequences;
106                        }
107                        if (GlobalDataContainer.getInstance().addData(sequencesName,
108                                        sequences)) {
109                                Console.traceln("Old data \"" + sequencesName
110                                                + "\" overwritten");
111                        }
112                        Console.println("" + sequences.size() + " sequences generated");
113                }
114        }
115
116        /*
117         * (non-Javadoc)
118         *
119         * @see de.ugoe.cs.util.console.Command#help()
120         */
121        @Override
122        public void help() {
123                Console.println("Usage: generateFixedLengthSequences <modelname> <sequencesName> <length> {<all>} {<numSequences>}");
124        }
125
126}
Note: See TracBrowser for help on using the repository browser.