| 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 | * ClusterGenerator.java |
|---|
| 19 | * Copyright (C) 2000 University of Waikato, Hamilton, New Zealand |
|---|
| 20 | * |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | package weka.datagenerators; |
|---|
| 24 | |
|---|
| 25 | import weka.core.Option; |
|---|
| 26 | import weka.core.Range; |
|---|
| 27 | import weka.core.Utils; |
|---|
| 28 | import weka.datagenerators.DataGenerator; |
|---|
| 29 | |
|---|
| 30 | import java.util.Enumeration; |
|---|
| 31 | import java.util.Vector; |
|---|
| 32 | |
|---|
| 33 | /** |
|---|
| 34 | * Abstract class for cluster data generators. <p/> |
|---|
| 35 | * |
|---|
| 36 | * Example usage as the main of a datagenerator called RandomGenerator: |
|---|
| 37 | * <pre> |
|---|
| 38 | * public static void main(String[] args) { |
|---|
| 39 | * try { |
|---|
| 40 | * DataGenerator.makeData(new RandomGenerator(), args); |
|---|
| 41 | * } |
|---|
| 42 | * catch (Exception e) { |
|---|
| 43 | * e.printStackTrace(); |
|---|
| 44 | * System.err.println(e.getMessage()); |
|---|
| 45 | * } |
|---|
| 46 | * } |
|---|
| 47 | * </pre> |
|---|
| 48 | * <p/> |
|---|
| 49 | * |
|---|
| 50 | * @author Gabi Schmidberger (gabi@cs.waikato.ac.nz) |
|---|
| 51 | * @author FracPete (fracpete at waikato dot ac dot nz) |
|---|
| 52 | * @version $Revision: 1.6 $ |
|---|
| 53 | */ |
|---|
| 54 | public abstract class ClusterGenerator |
|---|
| 55 | extends DataGenerator { |
|---|
| 56 | |
|---|
| 57 | /** for serialization */ |
|---|
| 58 | private static final long serialVersionUID = 6131722618472046365L; |
|---|
| 59 | |
|---|
| 60 | /** Number of attribute the dataset should have */ |
|---|
| 61 | protected int m_NumAttributes; |
|---|
| 62 | |
|---|
| 63 | /** class flag */ |
|---|
| 64 | protected boolean m_ClassFlag = false; |
|---|
| 65 | |
|---|
| 66 | /** Stores which columns are boolean (default numeric) */ |
|---|
| 67 | protected Range m_booleanCols; |
|---|
| 68 | |
|---|
| 69 | /** Stores which columns are nominal (default numeric) */ |
|---|
| 70 | protected Range m_nominalCols; |
|---|
| 71 | |
|---|
| 72 | /** |
|---|
| 73 | * initializes the generator |
|---|
| 74 | */ |
|---|
| 75 | public ClusterGenerator() { |
|---|
| 76 | super(); |
|---|
| 77 | |
|---|
| 78 | setNumAttributes(defaultNumAttributes()); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | /** |
|---|
| 82 | * Returns an enumeration describing the available options. |
|---|
| 83 | * |
|---|
| 84 | * @return an enumeration of all the available options. |
|---|
| 85 | */ |
|---|
| 86 | public Enumeration listOptions() { |
|---|
| 87 | Vector result = enumToVector(super.listOptions()); |
|---|
| 88 | |
|---|
| 89 | result.addElement(new Option( |
|---|
| 90 | "\tThe number of attributes (default " |
|---|
| 91 | + defaultNumAttributes() + ").", |
|---|
| 92 | "a", 1, "-a <num>")); |
|---|
| 93 | |
|---|
| 94 | result.addElement(new Option( |
|---|
| 95 | "\tClass Flag, if set, the cluster is listed in extra attribute.", |
|---|
| 96 | "c", 0, "-c")); |
|---|
| 97 | |
|---|
| 98 | result.addElement(new Option( |
|---|
| 99 | "\tThe indices for boolean attributes.", |
|---|
| 100 | "b", 1, "-b <range>")); |
|---|
| 101 | |
|---|
| 102 | result.addElement(new Option( |
|---|
| 103 | "\tThe indices for nominal attributes.", |
|---|
| 104 | "m", 1, "-m <range>")); |
|---|
| 105 | |
|---|
| 106 | return result.elements(); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | /** |
|---|
| 110 | * Sets the options. |
|---|
| 111 | * |
|---|
| 112 | * @param options the options |
|---|
| 113 | * @throws Exception if invalid option |
|---|
| 114 | */ |
|---|
| 115 | public void setOptions(String[] options) throws Exception { |
|---|
| 116 | String tmpStr; |
|---|
| 117 | |
|---|
| 118 | super.setOptions(options); |
|---|
| 119 | |
|---|
| 120 | tmpStr = Utils.getOption('a', options); |
|---|
| 121 | if (tmpStr.length() != 0) |
|---|
| 122 | setNumAttributes(Integer.parseInt(tmpStr)); |
|---|
| 123 | else |
|---|
| 124 | setNumAttributes(defaultNumAttributes()); |
|---|
| 125 | |
|---|
| 126 | setClassFlag(Utils.getFlag('c', options)); |
|---|
| 127 | |
|---|
| 128 | tmpStr = Utils.getOption('b', options); |
|---|
| 129 | setBooleanIndices(tmpStr); |
|---|
| 130 | m_booleanCols.setUpper(getNumAttributes()); |
|---|
| 131 | |
|---|
| 132 | tmpStr = Utils.getOption('m', options); |
|---|
| 133 | setNominalIndices(tmpStr); |
|---|
| 134 | m_nominalCols.setUpper(getNumAttributes()); |
|---|
| 135 | |
|---|
| 136 | // check indices |
|---|
| 137 | tmpStr = checkIndices(); |
|---|
| 138 | if (tmpStr.length() > 0) |
|---|
| 139 | throw new IllegalArgumentException(tmpStr); |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | /** |
|---|
| 143 | * Gets the current settings of the classifier. |
|---|
| 144 | * |
|---|
| 145 | * @return an array of strings suitable for passing to setOptions |
|---|
| 146 | */ |
|---|
| 147 | public String[] getOptions() { |
|---|
| 148 | Vector result; |
|---|
| 149 | String[] options; |
|---|
| 150 | int i; |
|---|
| 151 | |
|---|
| 152 | result = new Vector(); |
|---|
| 153 | options = super.getOptions(); |
|---|
| 154 | for (i = 0; i < options.length; i++) |
|---|
| 155 | result.add(options[i]); |
|---|
| 156 | |
|---|
| 157 | result.add("-a"); |
|---|
| 158 | result.add("" + getNumAttributes()); |
|---|
| 159 | |
|---|
| 160 | if (getClassFlag()) |
|---|
| 161 | result.add("-c"); |
|---|
| 162 | |
|---|
| 163 | if (!getBooleanCols().toString().equalsIgnoreCase("empty")) { |
|---|
| 164 | result.add("-b"); |
|---|
| 165 | result.add("" + getBooleanCols()); |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | if (!getNominalCols().toString().equalsIgnoreCase("empty")) { |
|---|
| 169 | result.add("-m"); |
|---|
| 170 | result.add("" + getNominalCols()); |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | return (String[]) result.toArray(new String[result.size()]); |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | /** |
|---|
| 177 | * returns the default number of attributes |
|---|
| 178 | * |
|---|
| 179 | * @return the default number of attributes |
|---|
| 180 | */ |
|---|
| 181 | protected int defaultNumAttributes() { |
|---|
| 182 | return 10; |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | /** |
|---|
| 186 | * Sets the number of attributes the dataset should have. |
|---|
| 187 | * @param numAttributes the new number of attributes |
|---|
| 188 | */ |
|---|
| 189 | public void setNumAttributes(int numAttributes) { |
|---|
| 190 | m_NumAttributes = numAttributes; |
|---|
| 191 | getBooleanCols().setUpper(getNumAttributes()); |
|---|
| 192 | getNominalCols().setUpper(getNumAttributes()); |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | /** |
|---|
| 196 | * Gets the number of attributes that should be produced. |
|---|
| 197 | * @return the number of attributes that should be produced |
|---|
| 198 | */ |
|---|
| 199 | public int getNumAttributes() { |
|---|
| 200 | return m_NumAttributes; |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | /** |
|---|
| 204 | * Returns the tip text for this property |
|---|
| 205 | * |
|---|
| 206 | * @return tip text for this property suitable for |
|---|
| 207 | * displaying in the explorer/experimenter gui |
|---|
| 208 | */ |
|---|
| 209 | public String numAttributesTipText() { |
|---|
| 210 | return "The number of attributes the generated data will contain."; |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | /** |
|---|
| 214 | * Sets the class flag, if class flag is set, |
|---|
| 215 | * the cluster is listed as class atrribute in an extra attribute. |
|---|
| 216 | * @param classFlag the new class flag |
|---|
| 217 | */ |
|---|
| 218 | public void setClassFlag(boolean classFlag) { |
|---|
| 219 | m_ClassFlag = classFlag; |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | /** |
|---|
| 223 | * Gets the class flag. |
|---|
| 224 | * @return the class flag |
|---|
| 225 | */ |
|---|
| 226 | public boolean getClassFlag() { |
|---|
| 227 | return m_ClassFlag; |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | /** |
|---|
| 231 | * Returns the tip text for this property |
|---|
| 232 | * |
|---|
| 233 | * @return tip text for this property suitable for |
|---|
| 234 | * displaying in the explorer/experimenter gui |
|---|
| 235 | */ |
|---|
| 236 | public String classFlagTipText() { |
|---|
| 237 | return "If set to TRUE, lists the cluster as an extra attribute."; |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | /** |
|---|
| 241 | * Sets which attributes are boolean |
|---|
| 242 | * @param rangeList a string representing the list of attributes. Since |
|---|
| 243 | * the string will typically come from a user, attributes are indexed from |
|---|
| 244 | * 1. <br/> |
|---|
| 245 | * eg: first-3,5,6-last |
|---|
| 246 | * @throws IllegalArgumentException if an invalid range list is supplied |
|---|
| 247 | */ |
|---|
| 248 | public void setBooleanIndices(String rangeList) { |
|---|
| 249 | m_booleanCols.setRanges(rangeList); |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | /** |
|---|
| 253 | * Sets which attributes are boolean. |
|---|
| 254 | * @param value the range to use |
|---|
| 255 | */ |
|---|
| 256 | public void setBooleanCols(Range value) { |
|---|
| 257 | m_booleanCols.setRanges(value.getRanges()); |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | /** |
|---|
| 261 | * returns the range of boolean attributes. |
|---|
| 262 | * |
|---|
| 263 | * @return the range of boolean attributes |
|---|
| 264 | */ |
|---|
| 265 | public Range getBooleanCols() { |
|---|
| 266 | if (m_booleanCols == null) |
|---|
| 267 | m_booleanCols = new Range(); |
|---|
| 268 | |
|---|
| 269 | return m_booleanCols; |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | /** |
|---|
| 273 | * Returns the tip text for this property |
|---|
| 274 | * |
|---|
| 275 | * @return tip text for this property suitable for |
|---|
| 276 | * displaying in the explorer/experimenter gui |
|---|
| 277 | */ |
|---|
| 278 | public String booleanColsTipText() { |
|---|
| 279 | return "The range of attributes that are generated as boolean ones."; |
|---|
| 280 | } |
|---|
| 281 | |
|---|
| 282 | /** |
|---|
| 283 | * Sets which attributes are nominal |
|---|
| 284 | * @param rangeList a string representing the list of attributes. Since |
|---|
| 285 | * the string will typically come from a user, attributes are indexed from |
|---|
| 286 | * 1. <br/> |
|---|
| 287 | * eg: first-3,5,6-last |
|---|
| 288 | * @throws IllegalArgumentException if an invalid range list is supplied |
|---|
| 289 | */ |
|---|
| 290 | public void setNominalIndices(String rangeList) { |
|---|
| 291 | m_nominalCols.setRanges(rangeList); |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | /** |
|---|
| 295 | * Sets which attributes are nominal. |
|---|
| 296 | * @param value the range to use |
|---|
| 297 | */ |
|---|
| 298 | public void setNominalCols(Range value) { |
|---|
| 299 | m_nominalCols.setRanges(value.getRanges()); |
|---|
| 300 | } |
|---|
| 301 | |
|---|
| 302 | /** |
|---|
| 303 | * returns the range of nominal attributes |
|---|
| 304 | * |
|---|
| 305 | * @return the range of nominal attributes |
|---|
| 306 | */ |
|---|
| 307 | public Range getNominalCols() { |
|---|
| 308 | if (m_nominalCols == null) |
|---|
| 309 | m_nominalCols = new Range(); |
|---|
| 310 | |
|---|
| 311 | return m_nominalCols; |
|---|
| 312 | } |
|---|
| 313 | |
|---|
| 314 | /** |
|---|
| 315 | * Returns the tip text for this property |
|---|
| 316 | * |
|---|
| 317 | * @return tip text for this property suitable for |
|---|
| 318 | * displaying in the explorer/experimenter gui |
|---|
| 319 | */ |
|---|
| 320 | public String nominalColsTipText() { |
|---|
| 321 | return "The range of attributes to generate as nominal ones."; |
|---|
| 322 | } |
|---|
| 323 | |
|---|
| 324 | /** |
|---|
| 325 | * check if attribute types are not contradicting |
|---|
| 326 | * |
|---|
| 327 | * @return empty string if no problem, otherwise error message |
|---|
| 328 | */ |
|---|
| 329 | protected String checkIndices() { |
|---|
| 330 | for (int i = 1; i < getNumAttributes() + 1; i++) { |
|---|
| 331 | m_booleanCols.isInRange(i); |
|---|
| 332 | if (m_booleanCols.isInRange(i) && m_nominalCols.isInRange(i)) { |
|---|
| 333 | return "Error in attribute type: Attribute " |
|---|
| 334 | + i + " is set boolean and nominal."; |
|---|
| 335 | } |
|---|
| 336 | } |
|---|
| 337 | return ""; |
|---|
| 338 | } |
|---|
| 339 | } |
|---|
| 340 | |
|---|
| 341 | |
|---|