MOA 12.03
Real Time Analytics for Data Streams
WaveformGenerator.java
Go to the documentation of this file.
00001 /*
00002  *    WaveformGenerator.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 WaveformGenerator extends AbstractOptionHandler implements
00045         InstanceStream {
00046 
00047     @Override
00048     public String getPurposeString() {
00049         return "Generates a problem of predicting one of three waveform types.";
00050     }
00051 
00052     private static final long serialVersionUID = 1L;
00053 
00054     public static final int NUM_CLASSES = 3;
00055 
00056     public static final int NUM_BASE_ATTRIBUTES = 21;
00057 
00058     public static final int TOTAL_ATTRIBUTES_INCLUDING_NOISE = 40;
00059 
00060     protected static final int hFunctions[][] = {
00061         {0, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
00062         {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1, 0},
00063         {0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0}};
00064 
00065     public IntOption instanceRandomSeedOption = new IntOption(
00066             "instanceRandomSeed", 'i',
00067             "Seed for random generation of instances.", 1);
00068 
00069     public FlagOption addNoiseOption = new FlagOption("addNoise", 'n',
00070             "Adds noise, for a total of 40 attributes.");
00071 
00072     protected InstancesHeader streamHeader;
00073 
00074     protected Random instanceRandom;
00075 
00076     @Override
00077     protected void prepareForUseImpl(TaskMonitor monitor,
00078             ObjectRepository repository) {
00079         // generate header
00080         FastVector attributes = new FastVector();
00081         int numAtts = this.addNoiseOption.isSet() ? TOTAL_ATTRIBUTES_INCLUDING_NOISE
00082                 : NUM_BASE_ATTRIBUTES;
00083         for (int i = 0; i < numAtts; i++) {
00084             attributes.addElement(new Attribute("att" + (i + 1)));
00085         }
00086         FastVector classLabels = new FastVector();
00087         for (int i = 0; i < NUM_CLASSES; i++) {
00088             classLabels.addElement("class" + (i + 1));
00089         }
00090         attributes.addElement(new Attribute("class", classLabels));
00091         this.streamHeader = new InstancesHeader(new Instances(
00092                 getCLICreationString(InstanceStream.class), attributes, 0));
00093         this.streamHeader.setClassIndex(this.streamHeader.numAttributes() - 1);
00094         restart();
00095     }
00096 
00097     @Override
00098     public long estimatedRemainingInstances() {
00099         return -1;
00100     }
00101 
00102     @Override
00103     public InstancesHeader getHeader() {
00104         return this.streamHeader;
00105     }
00106 
00107     @Override
00108     public boolean hasMoreInstances() {
00109         return true;
00110     }
00111 
00112     @Override
00113     public boolean isRestartable() {
00114         return true;
00115     }
00116 
00117     @Override
00118     public Instance nextInstance() {
00119         InstancesHeader header = getHeader();
00120         Instance inst = new DenseInstance(header.numAttributes());
00121         inst.setDataset(header);
00122         int waveform = this.instanceRandom.nextInt(NUM_CLASSES);
00123         int choiceA = 0, choiceB = 0;
00124         switch (waveform) {
00125             case 0:
00126                 choiceA = 0;
00127                 choiceB = 1;
00128                 break;
00129             case 1:
00130                 choiceA = 0;
00131                 choiceB = 2;
00132                 break;
00133             case 2:
00134                 choiceA = 1;
00135                 choiceB = 2;
00136                 break;
00137 
00138         }
00139         double multiplierA = this.instanceRandom.nextDouble();
00140         double multiplierB = 1.0 - multiplierA;
00141         for (int i = 0; i < NUM_BASE_ATTRIBUTES; i++) {
00142             inst.setValue(i, (multiplierA * hFunctions[choiceA][i])
00143                     + (multiplierB * hFunctions[choiceB][i])
00144                     + this.instanceRandom.nextGaussian());
00145         }
00146         if (this.addNoiseOption.isSet()) {
00147             for (int i = NUM_BASE_ATTRIBUTES; i < TOTAL_ATTRIBUTES_INCLUDING_NOISE; i++) {
00148                 inst.setValue(i, this.instanceRandom.nextGaussian());
00149             }
00150         }
00151         inst.setClassValue(waveform);
00152         return inst;
00153     }
00154 
00155     @Override
00156     public void restart() {
00157         this.instanceRandom = new Random(this.instanceRandomSeedOption.getValue());
00158     }
00159 
00160     @Override
00161     public void getDescription(StringBuilder sb, int indent) {
00162         // TODO Auto-generated method stub
00163     }
00164 }
 All Classes Namespaces Files Functions Variables Enumerations