source: branches/MetisMQI/src/main/java/weka/filters/unsupervised/instance/Randomize.java

Last change on this file was 29, checked in by gnappo, 15 years ago

Taggata versione per la demo e aggiunto branch.

File size: 7.7 KB
Line 
1/*
2 *    This program is free software; you can redistribute it and/or modify
3 *    it under the terms of the GNU General Public License as published by
4 *    the Free Software Foundation; either version 2 of the License, or
5 *    (at your option) any later version.
6 *
7 *    This program is distributed in the hope that it will be useful,
8 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 *    GNU General Public License for more details.
11 *
12 *    You should have received a copy of the GNU General Public License
13 *    along with this program; if not, write to the Free Software
14 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 */
16
17/*
18 *    Randomize.java
19 *    Copyright (C) 1999 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23
24package weka.filters.unsupervised.instance;
25
26import weka.core.Capabilities;
27import weka.core.Instance;
28import weka.core.Instances;
29import weka.core.Option;
30import weka.core.OptionHandler;
31import weka.core.RevisionUtils;
32import weka.core.Utils;
33import weka.core.Capabilities.Capability;
34import weka.filters.Filter;
35import weka.filters.UnsupervisedFilter;
36
37import java.util.Enumeration;
38import java.util.Random;
39import java.util.Vector;
40
41/**
42 <!-- globalinfo-start -->
43 * Randomly shuffles the order of instances passed through it. The random number generator is reset with the seed value whenever a new set of instances is passed in.
44 * <p/>
45 <!-- globalinfo-end -->
46 *
47 <!-- options-start -->
48 * Valid options are: <p/>
49 *
50 * <pre> -S &lt;num&gt;
51 *  Specify the random number seed (default 42)</pre>
52 *
53 <!-- options-end -->
54 *
55 * @author Len Trigg (trigg@cs.waikato.ac.nz)
56 * @version $Revision: 5499 $
57 */
58public class Randomize 
59  extends Filter
60  implements UnsupervisedFilter, OptionHandler {
61 
62  /** for serialization */
63  static final long serialVersionUID = 8854479785121877582L;
64
65  /** The random number seed */
66  protected int m_Seed = 42;
67
68  /** The current random number generator */
69  protected Random m_Random;
70 
71  /**
72   * Returns a string describing this classifier
73   * @return a description of the classifier suitable for
74   * displaying in the explorer/experimenter gui
75   */
76  public String globalInfo() {
77    return "Randomly shuffles the order of instances passed through it. "
78      + "The random number generator is reset with the seed value whenever "
79      + "a new set of instances is passed in.";
80  }
81
82  /**
83   * Returns an enumeration describing the available options.
84   *
85   * @return an enumeration of all the available options.
86   */
87  public Enumeration listOptions() {
88
89    Vector newVector = new Vector(1);
90
91    newVector.addElement(new Option(
92              "\tSpecify the random number seed (default 42)",
93              "S", 1, "-S <num>"));
94
95    return newVector.elements();
96  }
97
98
99  /**
100   * Parses a given list of options. <p/>
101   *
102   <!-- options-start -->
103   * Valid options are: <p/>
104   *
105   * <pre> -S &lt;num&gt;
106   *  Specify the random number seed (default 42)</pre>
107   *
108   <!-- options-end -->
109   *
110   * @param options the list of options as an array of strings
111   * @throws Exception if an option is not supported
112   */
113  public void setOptions(String[] options) throws Exception {
114   
115    String seedString = Utils.getOption('S', options);
116    if (seedString.length() != 0) {
117      setRandomSeed(Integer.parseInt(seedString));
118    } else {
119      setRandomSeed(42);
120    }
121
122    if (getInputFormat() != null) {
123      setInputFormat(getInputFormat());
124    }
125  }
126
127  /**
128   * Gets the current settings of the filter.
129   *
130   * @return an array of strings suitable for passing to setOptions
131   */
132  public String [] getOptions() {
133
134    String [] options = new String [2];
135    int current = 0;
136
137    options[current++] = "-S"; options[current++] = "" + getRandomSeed();
138
139    while (current < options.length) {
140      options[current++] = "";
141    }
142    return options;
143  }
144
145  /**
146   * Returns the tip text for this property
147   * @return tip text for this property suitable for
148   * displaying in the explorer/experimenter gui
149   */
150  public String randomSeedTipText() {
151    return "Seed for the random number generator.";
152  }
153
154  /**
155   * Get the random number generator seed value.
156   *
157   * @return random number generator seed value.
158   */
159  public int getRandomSeed() {
160   
161    return m_Seed;
162  }
163 
164  /**
165   * Set the random number generator seed value.
166   *
167   * @param newRandomSeed value to use as the random number generator seed.
168   */
169  public void setRandomSeed(int newRandomSeed) {
170   
171    m_Seed = newRandomSeed;
172  }
173
174  /**
175   * Returns the Capabilities of this filter.
176   *
177   * @return            the capabilities of this object
178   * @see               Capabilities
179   */
180  public Capabilities getCapabilities() {
181    Capabilities result = super.getCapabilities();
182    result.disableAll();
183
184    // attributes
185    result.enableAllAttributes();
186    result.enable(Capability.MISSING_VALUES);
187   
188    // class
189    result.enableAllClasses();
190    result.enable(Capability.MISSING_CLASS_VALUES);
191    result.enable(Capability.NO_CLASS);
192   
193    return result;
194  }
195 
196  /**
197   * Sets the format of the input instances.
198   *
199   * @param instanceInfo an Instances object containing the input instance
200   * structure (any instances contained in the object are ignored - only the
201   * structure is required).
202   * @return true if the outputFormat may be collected immediately
203   * @throws Exception if format cannot be processed
204   */
205  public boolean setInputFormat(Instances instanceInfo) throws Exception {
206
207    super.setInputFormat(instanceInfo);
208    setOutputFormat(instanceInfo);
209    m_Random = new Random(m_Seed);
210    return true;
211  }
212
213  /**
214   * Input an instance for filtering. Filter requires all
215   * training instances be read before producing output.
216   *
217   * @param instance the input instance
218   * @return true if the filtered instance may now be
219   * collected with output().
220   * @throws IllegalStateException if no input structure has been defined
221   */
222  public boolean input(Instance instance) {
223
224    if (getInputFormat() == null) {
225      throw new IllegalStateException("No input instance format defined");
226    }
227    if (m_NewBatch) {
228      resetQueue();
229      m_NewBatch = false;
230    }
231    if (isFirstBatchDone()) {
232      push(instance);
233      return true;
234    } else {
235      bufferInput(instance);
236      return false;
237    }
238  }
239
240  /**
241   * Signify that this batch of input to the filter is finished. If
242   * the filter requires all instances prior to filtering, output()
243   * may now be called to retrieve the filtered instances. Any
244   * subsequent instances filtered should be filtered based on setting
245   * obtained from the first batch (unless the setInputFormat has been
246   * re-assigned or new options have been set). This
247   * implementation randomizes all the instances received in the batch.
248   *
249   * @return true if there are instances pending output
250   * @throws IllegalStateException if no input format has been set.
251   */
252  public boolean batchFinished() {
253
254    if (getInputFormat() == null) {
255      throw new IllegalStateException("No input instance format defined");
256    }
257
258    if (!isFirstBatchDone()) {
259      getInputFormat().randomize(m_Random);
260    }
261    for (int i = 0; i < getInputFormat().numInstances(); i++) {
262      push(getInputFormat().instance(i));
263    }
264    flushInput();
265   
266    m_NewBatch = true;
267    m_FirstBatchDone = true;
268    return (numPendingOutput() != 0);
269  }
270 
271  /**
272   * Returns the revision string.
273   *
274   * @return            the revision
275   */
276  public String getRevision() {
277    return RevisionUtils.extract("$Revision: 5499 $");
278  }
279
280  /**
281   * Main method for testing this class.
282   *
283   * @param argv should contain arguments to the filter: use -h for help
284   */
285  public static void main(String [] argv) {
286    runFilter(new Randomize(), argv);
287  }
288}
Note: See TracBrowser for help on using the repository browser.