MOA 12.03
Real Time Analytics for Data Streams
LEDGenerator.java
Go to the documentation of this file.
00001 /*
00002  *    LEDGenerator.java
00003  *    Copyright (C) 2007 University of Waikato, Hamilton, New Zealand
00004  *    @author Richard Kirkby (rkirkby@cs.waikato.ac.nz)
00005  *
00006  *    This program is free software; you can redistribute it and/or modify
00007  *    it under the terms of the GNU General Public License as published by
00008  *    the Free Software Foundation; either version 3 of the License, or
00009  *    (at your option) any later version.
00010  *
00011  *    This program is distributed in the hope that it will be useful,
00012  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *    GNU General Public License for more details.
00015  *
00016  *    You should have received a copy of the GNU General Public License
00017  *    along with this program. If not, see <http://www.gnu.org/licenses/>.
00018  *    
00019  */
00020 package moa.streams.generators;
00021 
00022 import weka.core.Attribute;
00023 import weka.core.DenseInstance;
00024 import weka.core.FastVector;
00025 import weka.core.Instance;
00026 import weka.core.Instances;
00027 
00028 import java.util.Random;
00029 
00030 import moa.core.InstancesHeader;
00031 import moa.core.ObjectRepository;
00032 import moa.options.AbstractOptionHandler;
00033 import moa.options.FlagOption;
00034 import moa.options.IntOption;
00035 import moa.streams.InstanceStream;
00036 import moa.tasks.TaskMonitor;
00037 
00044 public class LEDGenerator extends AbstractOptionHandler implements
00045         InstanceStream {
00046 
00047     @Override
00048     public String getPurposeString() {
00049         return "Generates a problem of predicting the digit displayed on a 7-segment LED display.";
00050     }
00051 
00052     private static final long serialVersionUID = 1L;
00053 
00054     public static final int NUM_IRRELEVANT_ATTRIBUTES = 17;
00055 
00056     protected static final int originalInstances[][] = {
00057         {1, 1, 1, 0, 1, 1, 1}, {0, 0, 1, 0, 0, 1, 0},
00058         {1, 0, 1, 1, 1, 0, 1}, {1, 0, 1, 1, 0, 1, 1},
00059         {0, 1, 1, 1, 0, 1, 0}, {1, 1, 0, 1, 0, 1, 1},
00060         {1, 1, 0, 1, 1, 1, 1}, {1, 0, 1, 0, 0, 1, 0},
00061         {1, 1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 0, 1, 1}};
00062 
00063     public IntOption instanceRandomSeedOption = new IntOption(
00064             "instanceRandomSeed", 'i',
00065             "Seed for random generation of instances.", 1);
00066 
00067     public IntOption noisePercentageOption = new IntOption("noisePercentage",
00068             'n', "Percentage of noise to add to the data.", 10, 0, 100);
00069 
00070     public FlagOption suppressIrrelevantAttributesOption = new FlagOption(
00071             "suppressIrrelevantAttributes", 's',
00072             "Reduce the data to only contain 7 relevant binary attributes.");
00073 
00074     protected InstancesHeader streamHeader;
00075 
00076     protected Random instanceRandom;
00077 
00078     @Override
00079     protected void prepareForUseImpl(TaskMonitor monitor,
00080             ObjectRepository repository) {
00081         // generate header
00082         FastVector attributes = new FastVector();
00083         FastVector binaryLabels = new FastVector();
00084         binaryLabels.addElement("0");
00085         binaryLabels.addElement("1");
00086         int numAtts = 7;
00087         if (!this.suppressIrrelevantAttributesOption.isSet()) {
00088             numAtts += NUM_IRRELEVANT_ATTRIBUTES;
00089         }
00090         for (int i = 0; i < numAtts; i++) {
00091             attributes.addElement(new Attribute("att" + (i + 1), binaryLabels));
00092         }
00093         FastVector classLabels = new FastVector();
00094         for (int i = 0; i < 10; i++) {
00095             classLabels.addElement(Integer.toString(i));
00096         }
00097         attributes.addElement(new Attribute("class", classLabels));
00098         this.streamHeader = new InstancesHeader(new Instances(
00099                 getCLICreationString(InstanceStream.class), attributes, 0));
00100         this.streamHeader.setClassIndex(this.streamHeader.numAttributes() - 1);
00101         restart();
00102     }
00103 
00104     @Override
00105     public long estimatedRemainingInstances() {
00106         return -1;
00107     }
00108 
00109     @Override
00110     public InstancesHeader getHeader() {
00111         return this.streamHeader;
00112     }
00113 
00114     @Override
00115     public boolean hasMoreInstances() {
00116         return true;
00117     }
00118 
00119     @Override
00120     public boolean isRestartable() {
00121         return true;
00122     }
00123 
00124     @Override
00125     public Instance nextInstance() {
00126         InstancesHeader header = getHeader();
00127         Instance inst = new DenseInstance(header.numAttributes());
00128         inst.setDataset(header);
00129         int selected = this.instanceRandom.nextInt(10);
00130         for (int i = 0; i < 7; i++) {
00131             if ((1 + (this.instanceRandom.nextInt(100))) <= this.noisePercentageOption.getValue()) {
00132                 inst.setValue(i, originalInstances[selected][i] == 0 ? 1 : 0);
00133             } else {
00134                 inst.setValue(i, originalInstances[selected][i]);
00135             }
00136         }
00137         if (!this.suppressIrrelevantAttributesOption.isSet()) {
00138             for (int i = 0; i < NUM_IRRELEVANT_ATTRIBUTES; i++) {
00139                 inst.setValue(i + 7, this.instanceRandom.nextInt(2));
00140             }
00141         }
00142         inst.setClassValue(selected);
00143         return inst;
00144     }
00145 
00146     @Override
00147     public void restart() {
00148         this.instanceRandom = new Random(this.instanceRandomSeedOption.getValue());
00149     }
00150 
00151     @Override
00152     public void getDescription(StringBuilder sb, int indent) {
00153         // TODO Auto-generated method stub
00154     }
00155 }
 All Classes Namespaces Files Functions Variables Enumerations