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.
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.
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.
19
* Copyright (C) 2005 University of Waikato, Hamilton, New Zealand
23
package weka.datagenerators.classifiers.classification;
25
import weka.classifiers.bayes.net.BayesNetGenerator;
26
import weka.core.Instance;
27
import weka.core.Instances;
28
import weka.core.Option;
29
import weka.core.Utils;
30
import weka.datagenerators.ClassificationGenerator;
32
import java.util.Enumeration;
33
import java.util.Vector;
36
<!-- globalinfo-start -->
37
* Generates random instances based on a Bayes network.
39
<!-- globalinfo-end -->
41
<!-- options-start -->
42
* Valid options are: <p/>
45
* Prints this help.</pre>
47
* <pre> -o <file>
48
* The name of the output file, otherwise the generated data is
49
* printed to stdout.</pre>
51
* <pre> -r <name>
52
* The name of the relation.</pre>
55
* Whether to print debug informations.</pre>
58
* The seed for random function (default 1)</pre>
60
* <pre> -n <num>
61
* The number of examples to generate (default 100)</pre>
63
* <pre> -A <num>
64
* The number of arcs to use. (default 20)</pre>
66
* <pre> -C <num>
67
* The cardinality of the attributes and the class. (default 2)</pre>
71
* @author FracPete (fracpete at waikato dot ac dot nz)
72
* @version $Revision: 1.3 $
73
* @see BayesNetGenerator
77
extends ClassificationGenerator {
79
/** for serialization */
80
static final long serialVersionUID = -796118162379901512L;
82
/** the bayesian net generator, that produces the actual data */
83
protected BayesNetGenerator m_Generator;
86
* initializes the generator
91
setNumAttributes(defaultNumAttributes());
92
setNumArcs(defaultNumArcs());
93
setCardinality(defaultCardinality());
97
* Returns a string describing this data generator.
99
* @return a description of the data generator suitable for
100
* displaying in the explorer/experimenter gui
102
public String globalInfo() {
104
"Generates random instances based on a Bayes network.";
108
* Returns an enumeration describing the available options.
110
* @return an enumeration of all the available options
112
public Enumeration listOptions() {
113
Vector result = enumToVector(super.listOptions());
115
result.add(new Option(
116
"\tThe number of arcs to use. (default "
117
+ defaultNumArcs() + ")",
118
"A", 1, "-A <num>"));
120
result.add(new Option(
121
"\tThe cardinality of the attributes and the class. (default "
122
+ defaultCardinality() + ")",
123
"C", 1, "-C <num>"));
125
return result.elements();
129
* Parses a list of options for this object. <p/>
131
<!-- options-start -->
132
* Valid options are: <p/>
135
* Prints this help.</pre>
137
* <pre> -o <file>
138
* The name of the output file, otherwise the generated data is
139
* printed to stdout.</pre>
141
* <pre> -r <name>
142
* The name of the relation.</pre>
145
* Whether to print debug informations.</pre>
148
* The seed for random function (default 1)</pre>
150
* <pre> -n <num>
151
* The number of examples to generate (default 100)</pre>
153
* <pre> -A <num>
154
* The number of arcs to use. (default 20)</pre>
156
* <pre> -C <num>
157
* The cardinality of the attributes and the class. (default 2)</pre>
161
* @param options the list of options as an array of strings
162
* @throws Exception if an option is not supported
164
public void setOptions(String[] options) throws Exception {
168
super.setOptions(options);
173
list.add("" + getNumAttributes());
176
list.add("" + getNumExamples());
179
list.add("" + getSeed());
182
tmpStr = Utils.getOption('A', options);
183
if (tmpStr.length() != 0)
186
list.add("" + defaultNumArcs());
189
tmpStr = Utils.getOption('C', options);
190
if (tmpStr.length() != 0)
193
list.add("" + defaultCardinality());
195
setGeneratorOptions(list);
199
* Gets the current settings of the datagenerator.
201
* @return an array of strings suitable for passing to setOptions
203
public String[] getOptions() {
208
result = new Vector();
209
options = removeBlacklist(super.getOptions());
210
for (i = 0; i < options.length; i++)
211
result.add(options[i]);
213
// determine options from generator
214
options = getGenerator().getOptions();
218
result.add(Utils.getOption('A', options));
220
catch (Exception e) {
226
result.add(Utils.getOption('C', options));
228
catch (Exception e) {
232
return (String[]) result.toArray(new String[result.size()]);
236
* sets the given options of the BayesNetGenerator
238
* @param generator the generator to set the options for
239
* @param options the options to set
241
protected void setGeneratorOptions(
242
BayesNetGenerator generator, Vector options) {
245
generator.setOptions(
246
(String[]) options.toArray(new String[options.size()]));
248
catch (Exception e) {
254
* returns the actual datagenerator
256
* @return the actual datagenerator
258
protected BayesNetGenerator getGenerator() {
259
if (m_Generator == null)
260
m_Generator = new BayesNetGenerator();
266
* sets the given options of the BayesNetGenerator
268
* @param options the options to set
270
protected void setGeneratorOptions(Vector options) {
271
setGeneratorOptions(getGenerator(), options);
275
* sets a specific option/value of the generator (option must be w/o
277
* @param generator the generator to set the option for
278
* @param option the option to set
279
* @param value the new value for the option
281
protected void setGeneratorOption( BayesNetGenerator generator,
282
String option, String value ) {
289
// get options and remove specific option
290
options = generator.getOptions();
291
Utils.getOption(option, options);
293
// add option and set the new options
295
for (i = 0; i < options.length; i++) {
296
if (options[i].length() != 0)
297
list.add(options[i]);
299
list.add("-" + option);
301
setGeneratorOptions(generator, list);
303
catch (Exception e) {
309
* sets a specific option/value of the generator (option must be w/o
311
* @param option the option to set
312
* @param value the new value for the option
314
protected void setGeneratorOption(String option, String value) {
315
setGeneratorOption(getGenerator(), option, value);
319
* returns the default number of attributes
321
* @return the default number of attributes
323
protected int defaultNumAttributes() {
328
* Sets the number of attributes the dataset should have.
329
* @param numAttributes the new number of attributes
331
public void setNumAttributes(int numAttributes) {
332
setGeneratorOption("N", "" + numAttributes);
336
* Gets the number of attributes that should be produced.
337
* @return the number of attributes that should be produced
339
public int getNumAttributes() {
344
result = Integer.parseInt(
345
Utils.getOption('N', getGenerator().getOptions()));
347
catch (Exception e) {
356
* Returns the tip text for this property
358
* @return tip text for this property suitable for
359
* displaying in the explorer/experimenter gui
361
public String numAttributesTipText() {
362
return "The number of attributes the generated data will contain (including class attribute), ie the number of nodes in the bayesian net.";
366
* returns the default cardinality
368
* @return the default cardinality
370
protected int defaultCardinality() {
375
* Sets the cardinality of the attributes (incl class attribute)
376
* @param value the cardinality
378
public void setCardinality(int value) {
379
setGeneratorOption("C", "" + value);
383
* Gets the cardinality of the attributes (incl class attribute)
384
* @return the cardinality of the attributes
386
public int getCardinality() {
391
result = Integer.parseInt(
392
Utils.getOption('C', getGenerator().getOptions()));
394
catch (Exception e) {
403
* Returns the tip text for this property
405
* @return tip text for this property suitable for
406
* displaying in the explorer/experimenter gui
408
public String cardinalityTipText() {
409
return "The cardinality of the attributes, incl the class attribute.";
413
* returns the default number of arcs
415
* @return the default number of arcs
417
protected int defaultNumArcs() {
422
* Sets the number of arcs for the bayesian net
423
* @param value the number of arcs
425
public void setNumArcs(int value) {
430
nodes = getNumAttributes();
432
maxArcs = nodes * (nodes - 1) / 2;
435
throw new IllegalArgumentException(
436
"Number of arcs should be at most nodes * (nodes - 1) / 2 = "
437
+ maxArcs + " instead of " + value + " (nodes = numAttributes)!");
438
else if (value < minArcs)
439
throw new IllegalArgumentException(
440
"Number of arcs should be at least (nodes - 1) = " + minArcs
441
+ " instead of " + value + " (nodes = numAttributes)!");
443
setGeneratorOption("A", "" + value);
447
* Gets the number of arcs for the bayesian net
448
* @return the number of arcs
450
public int getNumArcs() {
455
result = Integer.parseInt(
456
Utils.getOption('A', getGenerator().getOptions()));
458
catch (Exception e) {
467
* Returns the tip text for this property
469
* @return tip text for this property suitable for
470
* displaying in the explorer/experimenter gui
472
public String numArcsTipText() {
473
return "The number of arcs in the bayesian net, at most: n * (n - 1) / 2 and at least: (n - 1); with n = numAttributes";
477
* Sets the number of examples, given by option.
478
* @param numExamples the new number of examples
480
public void setNumExamples(int numExamples) {
481
super.setNumExamples(numExamples);
482
setGeneratorOption("M", "" + numExamples);
486
* Gets the number of examples, given by option.
487
* @return the number of examples, given by option
489
public int getNumExamples() {
494
result = Integer.parseInt(
495
Utils.getOption('M', getGenerator().getOptions()));
497
catch (Exception e) {
506
* Return if single mode is set for the given data generator
507
* mode depends on option setting and or generator type.
509
* @return single mode flag
510
* @throws Exception if mode is not set yet
512
public boolean getSingleModeFlag() throws Exception {
517
* Initializes the format for the dataset produced.
518
* Must be called before the generateExample or generateExamples
520
* Re-initializes the random number generator with the given seed.
522
* @return the format for the dataset
523
* @throws Exception if the generating of the format failed
526
public Instances defineDataFormat() throws Exception {
527
BayesNetGenerator bng;
529
bng = new BayesNetGenerator();
530
bng.setOptions(getGenerator().getOptions());
531
setGeneratorOption(bng, "M", "1");
532
bng.generateRandomNetwork();
533
bng.generateInstances();
534
bng.m_Instances.renameAttribute(0, "class");
535
bng.m_Instances.setRelationName(getRelationNameToUse());
537
return bng.m_Instances;
541
* Generates one example of the dataset.
543
* @return the generated example
544
* @throws Exception if the format of the dataset is not yet defined
545
* @throws Exception if the generator only works with generateExamples
546
* which means in non single mode
548
public Instance generateExample() throws Exception {
549
throw new Exception("Cannot generate examples one-by-one!");
553
* Generates all examples of the dataset. Re-initializes the random number
554
* generator with the given seed, before generating instances.
556
* @return the generated dataset
557
* @throws Exception if the format of the dataset is not yet defined
558
* @throws Exception if the generator only works with generateExample,
559
* which means in single mode
562
public Instances generateExamples() throws Exception {
563
getGenerator().setOptions(getGenerator().getOptions());
564
getGenerator().generateRandomNetwork();
565
getGenerator().generateInstances();
566
getGenerator().m_Instances.renameAttribute(0, "class");
567
getGenerator().m_Instances.setRelationName(getRelationNameToUse());
569
return getGenerator().m_Instances;
573
* Generates a comment string that documentates the data generator.
574
* By default this string is added at the beginning of the produced output
575
* as ARFF file type, next after the options.
577
* @return string contains info about the generated rules
579
public String generateStart () {
584
* Generates a comment string that documentats the data generator.
585
* By default this string is added at the end of theproduces output
588
* @return string contains info about the generated rules
589
* @throws Exception if the generating of the documentaion fails
591
public String generateFinished() throws Exception {
596
* Main method for executing this class.
598
* @param args should contain arguments for the data producer:
600
public static void main(String[] args) {
601
runDataGenerator(new BayesNet(), args);