source: src/main/java/weka/classifiers/RandomizableMultipleClassifiersCombiner.java @ 27

Last change on this file since 27 was 4, checked in by gnappo, 14 years ago

Import di weka.

File size: 3.8 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 *    RandomizableMultipleClassifiersCombiner.java
19 *    Copyright (C) 2004 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.classifiers;
24
25import weka.core.Option;
26import weka.core.Randomizable;
27import weka.core.Utils;
28
29import java.util.Enumeration;
30import java.util.Vector;
31
32/**
33 * Abstract utility class for handling settings common to randomizable
34 * meta classifiers that build an ensemble from multiple classifiers based
35 * on a given random number seed.
36 *
37 * @author Eibe Frank (eibe@cs.waikato.ac.nz)
38 * @version $Revision: 6041 $
39 */
40public abstract class RandomizableMultipleClassifiersCombiner
41  extends MultipleClassifiersCombiner implements Randomizable {
42
43  /** for serialization */
44  private static final long serialVersionUID = 5057936555724785679L;
45
46  /** The random number seed. */
47  protected int m_Seed = 1;
48
49  /**
50   * Returns an enumeration describing the available options.
51   *
52   * @return an enumeration of all the available options.
53   */
54  public Enumeration listOptions() {
55
56    Vector newVector = new Vector(2);
57
58    newVector.addElement(new Option(
59          "\tRandom number seed.\n"
60          + "\t(default 1)",
61          "S", 1, "-S <num>"));
62
63    Enumeration enu = super.listOptions();
64    while (enu.hasMoreElements()) {
65      newVector.addElement(enu.nextElement());
66    }
67    return newVector.elements();
68  }
69
70  /**
71   * Parses a given list of options. Valid options are:<p>
72   *
73   * -B classifierstring <br>
74   * Classifierstring should contain the full class name of a scheme
75   * included for selection followed by options to the classifier
76   * (required, option should be used once for each classifier).<p>
77   *
78   * -S num <br>
79   * Set the random number seed (default 1). <p>
80   *
81   * @param options the list of options as an array of strings
82   * @exception Exception if an option is not supported
83   */
84  public void setOptions(String[] options) throws Exception {
85
86    String seed = Utils.getOption('S', options);
87    if (seed.length() != 0) {
88      setSeed(Integer.parseInt(seed));
89    } else {
90      setSeed(1);
91    }
92
93    super.setOptions(options);
94  }
95
96  /**
97   * Gets the current settings of the classifier.
98   *
99   * @return an array of strings suitable for passing to setOptions
100   */
101  public String [] getOptions() {
102
103    String [] superOptions = super.getOptions();
104    String [] options = new String [superOptions.length + 2];
105
106    int current = 0;
107    options[current++] = "-S";
108    options[current++] = "" + getSeed();
109
110    System.arraycopy(superOptions, 0, options, current,
111        superOptions.length);
112
113    return options;
114  }
115
116  /**
117   * Returns the tip text for this property
118   * @return tip text for this property suitable for
119   * displaying in the explorer/experimenter gui
120   */
121  public String seedTipText() {
122    return "The random number seed to be used.";
123  }
124
125  /**
126   * Set the seed for random number generation.
127   *
128   * @param seed the seed
129   */
130  public void setSeed(int seed) {
131
132    m_Seed = seed;
133  }
134
135  /**
136   * Gets the seed for the random number generations
137   *
138   * @return the seed for the random number generation
139   */
140  public int getSeed() {
141
142    return m_Seed;
143  }
144}
Note: See TracBrowser for help on using the repository browser.