| 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 | * RandomizableDistributionGenerator.java | 
|---|
| 19 | * Copyright (C) 2008 University of Waikato, Hamilton, New Zealand | 
|---|
| 20 | */ | 
|---|
| 21 |  | 
|---|
| 22 | package weka.classifiers.meta.generators; | 
|---|
| 23 |  | 
|---|
| 24 | import weka.core.Option; | 
|---|
| 25 | import weka.core.Utils; | 
|---|
| 26 |  | 
|---|
| 27 | import java.util.Enumeration; | 
|---|
| 28 | import java.util.Vector; | 
|---|
| 29 |  | 
|---|
| 30 | /** | 
|---|
| 31 | * An abstract superclass for randomizable generators that make use of | 
|---|
| 32 | * mean and standard deviation. | 
|---|
| 33 | * | 
|---|
| 34 | * @author  fracpete (fracpet at waikato dot ac dot nz) | 
|---|
| 35 | * @version $Revision: 5793 $ | 
|---|
| 36 | */ | 
|---|
| 37 | public abstract class RandomizableDistributionGenerator | 
|---|
| 38 | extends RandomizableGenerator | 
|---|
| 39 | implements Mean { | 
|---|
| 40 |  | 
|---|
| 41 | /** for serialization. */ | 
|---|
| 42 | private static final long serialVersionUID = 955762136858704289L; | 
|---|
| 43 |  | 
|---|
| 44 | /** The mean of the underlying distribution. */ | 
|---|
| 45 | protected double m_Mean = 0.0; | 
|---|
| 46 |  | 
|---|
| 47 | /** The standard deviation of the underlying distribution. */ | 
|---|
| 48 | protected double m_StandardDeviation = 1.0; | 
|---|
| 49 |  | 
|---|
| 50 | /** | 
|---|
| 51 | * Returns an enumeration describing the available options. | 
|---|
| 52 | * | 
|---|
| 53 | * @return an enumeration of all the available options. | 
|---|
| 54 | */ | 
|---|
| 55 | public Enumeration listOptions() { | 
|---|
| 56 | Vector result = new Vector(); | 
|---|
| 57 |  | 
|---|
| 58 | Enumeration enu = super.listOptions(); | 
|---|
| 59 | while (enu.hasMoreElements()) | 
|---|
| 60 | result.addElement(enu.nextElement()); | 
|---|
| 61 |  | 
|---|
| 62 | result.addElement(new Option( | 
|---|
| 63 | "\tSets the mean of the generator\n" | 
|---|
| 64 | + "\t(default: 0)", | 
|---|
| 65 | "M", 1, "-M <num>")); | 
|---|
| 66 |  | 
|---|
| 67 | result.addElement(new Option( | 
|---|
| 68 | "\tSets the standard deviation of the generator\n" | 
|---|
| 69 | + "\t(default: 1)", | 
|---|
| 70 | "SD", 1, "-SD <num>")); | 
|---|
| 71 |  | 
|---|
| 72 | return result.elements(); | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | /** | 
|---|
| 76 | * Parses a given list of options. | 
|---|
| 77 | * | 
|---|
| 78 | * @param options the list of options as an array of strings | 
|---|
| 79 | * @throws Exception if an option is not supported | 
|---|
| 80 | */ | 
|---|
| 81 | public void setOptions(String[] options) throws Exception { | 
|---|
| 82 | String      tmpStr; | 
|---|
| 83 |  | 
|---|
| 84 | super.setOptions(options); | 
|---|
| 85 |  | 
|---|
| 86 | tmpStr = Utils.getOption("M", options); | 
|---|
| 87 | if (tmpStr.length() != 0) | 
|---|
| 88 | setMean(Double.parseDouble(tmpStr)); | 
|---|
| 89 | else | 
|---|
| 90 | setMean(0.0); | 
|---|
| 91 |  | 
|---|
| 92 | tmpStr = Utils.getOption("SD", options); | 
|---|
| 93 | if (tmpStr.length() != 0) | 
|---|
| 94 | setStandardDeviation(Double.parseDouble(tmpStr)); | 
|---|
| 95 | else | 
|---|
| 96 | setStandardDeviation(1); | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | /** | 
|---|
| 100 | * Gets the current settings of the generator. | 
|---|
| 101 | * | 
|---|
| 102 | * @return an array of strings suitable for passing to setOptions | 
|---|
| 103 | */ | 
|---|
| 104 | public String[] getOptions() { | 
|---|
| 105 | Vector<String>      result; | 
|---|
| 106 | String[]            options; | 
|---|
| 107 | int                 i; | 
|---|
| 108 |  | 
|---|
| 109 | result = new Vector<String>(); | 
|---|
| 110 |  | 
|---|
| 111 | options = super.getOptions(); | 
|---|
| 112 | for (i = 0; i < options.length; i++) | 
|---|
| 113 | result.add(options[i]); | 
|---|
| 114 |  | 
|---|
| 115 | result.add("-M"); | 
|---|
| 116 | result.add("" + m_Mean); | 
|---|
| 117 |  | 
|---|
| 118 | result.add("-SD"); | 
|---|
| 119 | result.add("" + m_StandardDeviation); | 
|---|
| 120 |  | 
|---|
| 121 | return result.toArray(new String[result.size()]); | 
|---|
| 122 | } | 
|---|
| 123 |  | 
|---|
| 124 | /** | 
|---|
| 125 | * Gets the current mean of the underlying Gaussian | 
|---|
| 126 | * distribution. | 
|---|
| 127 | * | 
|---|
| 128 | * @return The current mean of the Gaussian distribution. | 
|---|
| 129 | */ | 
|---|
| 130 | public double getMean() { | 
|---|
| 131 | return m_Mean; | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|
| 134 | /** | 
|---|
| 135 | * Sets the mean of the Gaussian distribution to a new | 
|---|
| 136 | * mean. | 
|---|
| 137 | * | 
|---|
| 138 | * @param value The new mean for the distribution. | 
|---|
| 139 | */ | 
|---|
| 140 | public void setMean(double value) { | 
|---|
| 141 | m_Mean = value; | 
|---|
| 142 | } | 
|---|
| 143 |  | 
|---|
| 144 | /** | 
|---|
| 145 | * Returns the tip text for this property. | 
|---|
| 146 | * | 
|---|
| 147 | * @return            tip text for this property suitable for | 
|---|
| 148 | *                    displaying in the explorer/experimenter gui | 
|---|
| 149 | */ | 
|---|
| 150 | public String meanTipText() { | 
|---|
| 151 | return "The mean of the underlying distribution."; | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 | /** | 
|---|
| 155 | * Gets the current standard deviation of the underlying distribution. | 
|---|
| 156 | * | 
|---|
| 157 | * @return            The current standard deviation of the distribution. | 
|---|
| 158 | */ | 
|---|
| 159 | public double getStandardDeviation() { | 
|---|
| 160 | return m_StandardDeviation; | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | /** | 
|---|
| 164 | * Sets the standard deviation of the distribution to a new value. | 
|---|
| 165 | * | 
|---|
| 166 | * @param value       The new standard deviation. | 
|---|
| 167 | */ | 
|---|
| 168 | public void setStandardDeviation(double value) { | 
|---|
| 169 | if (value > 0) | 
|---|
| 170 | m_StandardDeviation = value; | 
|---|
| 171 | else | 
|---|
| 172 | m_StandardDeviation = 0.01; | 
|---|
| 173 | } | 
|---|
| 174 |  | 
|---|
| 175 | /** | 
|---|
| 176 | * Returns the tip text for this property. | 
|---|
| 177 | * | 
|---|
| 178 | * @return            tip text for this property suitable for | 
|---|
| 179 | *                    displaying in the explorer/experimenter gui | 
|---|
| 180 | */ | 
|---|
| 181 | public String standardDeviationTipText() { | 
|---|
| 182 | return "The standard deviation of the underlying distribution."; | 
|---|
| 183 | } | 
|---|
| 184 | } | 
|---|