MOA 12.03
Real Time Analytics for Data Streams
HyperplaneGenerator.java
Go to the documentation of this file.
00001 /*
00002  *    HyperplaneGenerator.java
00003  *    Copyright (C) 2008 University of Waikato, Hamilton, New Zealand
00004  *    @author Albert Bifet (abifet at cs dot waikato dot ac dot 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.FloatOption;
00034 import moa.options.IntOption;
00035 import moa.streams.InstanceStream;
00036 import moa.tasks.TaskMonitor;
00037 
00044 public class HyperplaneGenerator extends AbstractOptionHandler implements
00045         InstanceStream {
00046 
00047     @Override
00048     public String getPurposeString() {
00049         return "Generates a problem of predicting class of a rotating hyperplane.";
00050     }
00051 
00052     private static final long serialVersionUID = 1L;
00053 
00054     public IntOption instanceRandomSeedOption = new IntOption(
00055             "instanceRandomSeed", 'i',
00056             "Seed for random generation of instances.", 1);
00057 
00058     public IntOption numClassesOption = new IntOption("numClasses", 'c',
00059             "The number of classes to generate.", 2, 2, Integer.MAX_VALUE);
00060 
00061     public IntOption numAttsOption = new IntOption("numAtts", 'a',
00062             "The number of attributes to generate.", 10, 0, Integer.MAX_VALUE);
00063 
00064     public IntOption numDriftAttsOption = new IntOption("numDriftAtts", 'k',
00065             "The number of attributes with drift.", 2, 0, Integer.MAX_VALUE);
00066 
00067     public FloatOption magChangeOption = new FloatOption("magChange", 't',
00068             "Magnitude of the change for every example", 0.0, 0.0, 1.0);
00069 
00070     public IntOption noisePercentageOption = new IntOption("noisePercentage",
00071             'n', "Percentage of noise to add to the data.", 5, 0, 100);
00072 
00073     public IntOption sigmaPercentageOption = new IntOption("sigmaPercentage",
00074             's', "Percentage of probability that the direction of change is reversed.", 10, 0, 100);
00075 
00076     protected InstancesHeader streamHeader;
00077 
00078     protected Random instanceRandom;
00079 
00080     protected double[] weights;
00081 
00082     protected int[] sigma;
00083 
00084     public int numberInstance;
00085 
00086     @Override
00087     protected void prepareForUseImpl(TaskMonitor monitor,
00088             ObjectRepository repository) {
00089         monitor.setCurrentActivity("Preparing hyperplane...", -1.0);
00090         generateHeader();
00091         restart();
00092     }
00093 
00094     protected void generateHeader() {
00095         FastVector attributes = new FastVector();
00096         for (int i = 0; i < this.numAttsOption.getValue(); i++) {
00097             attributes.addElement(new Attribute("att" + (i + 1)));
00098         }
00099 
00100         FastVector classLabels = new FastVector();
00101         for (int i = 0; i < this.numClassesOption.getValue(); i++) {
00102             classLabels.addElement("class" + (i + 1));
00103         }
00104         attributes.addElement(new Attribute("class", classLabels));
00105         this.streamHeader = new InstancesHeader(new Instances(
00106                 getCLICreationString(InstanceStream.class), attributes, 0));
00107         this.streamHeader.setClassIndex(this.streamHeader.numAttributes() - 1);
00108     }
00109 
00110     @Override
00111     public long estimatedRemainingInstances() {
00112         return -1;
00113     }
00114 
00115     @Override
00116     public InstancesHeader getHeader() {
00117         return this.streamHeader;
00118     }
00119 
00120     @Override
00121     public boolean hasMoreInstances() {
00122         return true;
00123     }
00124 
00125     @Override
00126     public boolean isRestartable() {
00127         return true;
00128     }
00129 
00130     @Override
00131     public Instance nextInstance() {
00132 
00133         int numAtts = this.numAttsOption.getValue();
00134         double[] attVals = new double[numAtts + 1];
00135         double sum = 0.0;
00136         double sumWeights = 0.0;
00137         for (int i = 0; i < numAtts; i++) {
00138             attVals[i] = this.instanceRandom.nextDouble();
00139             sum += this.weights[i] * attVals[i];
00140             sumWeights += this.weights[i];
00141         }
00142         int classLabel;
00143         if (sum >= sumWeights * 0.5) {
00144             classLabel = 1;
00145         } else {
00146             classLabel = 0;
00147         }
00148         //Add Noise
00149         if ((1 + (this.instanceRandom.nextInt(100))) <= this.noisePercentageOption.getValue()) {
00150             classLabel = (classLabel == 0 ? 1 : 0);
00151         }
00152 
00153         Instance inst = new DenseInstance(1.0, attVals);
00154         inst.setDataset(getHeader());
00155         inst.setClassValue(classLabel);
00156         addDrift();
00157         return inst;
00158     }
00159 
00160     private void addDrift() {
00161         for (int i = 0; i < this.numDriftAttsOption.getValue(); i++) {
00162             this.weights[i] += (double) ((double) sigma[i]) * ((double) this.magChangeOption.getValue());
00163             if (//this.weights[i] >= 1.0 || this.weights[i] <= 0.0 ||
00164                     (1 + (this.instanceRandom.nextInt(100))) <= this.sigmaPercentageOption.getValue()) {
00165                 this.sigma[i] *= -1;
00166             }
00167         }
00168     }
00169 
00170     @Override
00171     public void restart() {
00172         this.instanceRandom = new Random(this.instanceRandomSeedOption.getValue());
00173         this.weights = new double[this.numAttsOption.getValue()];
00174         this.sigma = new int[this.numAttsOption.getValue()];
00175         for (int i = 0; i < this.numAttsOption.getValue(); i++) {
00176             this.weights[i] = this.instanceRandom.nextDouble();
00177             this.sigma[i] = (i < this.numDriftAttsOption.getValue() ? 1 : 0);
00178         }
00179     }
00180 
00181     @Override
00182     public void getDescription(StringBuilder sb, int indent) {
00183         // TODO Auto-generated method stub
00184     }
00185 }
 All Classes Namespaces Files Functions Variables Enumerations