~ubuntu-branches/ubuntu/precise/weka/precise

« back to all changes in this revision

Viewing changes to weka/classifiers/RandomizableClassifier.java

  • Committer: Bazaar Package Importer
  • Author(s): Soeren Sonnenburg
  • Date: 2008-02-24 09:18:45 UTC
  • Revision ID: james.westby@ubuntu.com-20080224091845-1l8zy6fm6xipbzsr
Tags: upstream-3.5.7+tut1
ImportĀ upstreamĀ versionĀ 3.5.7+tut1

Show diffs side-by-side

added added

removed removed

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