MOA 12.03
Real Time Analytics for Data Streams
RandomRBFGenerator.java
Go to the documentation of this file.
00001 /*
00002  *    RandomRBFGenerator.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.io.Serializable;
00029 import java.util.Random;
00030 
00031 import moa.core.InstancesHeader;
00032 import moa.core.MiscUtils;
00033 import moa.core.ObjectRepository;
00034 import moa.options.AbstractOptionHandler;
00035 import moa.options.IntOption;
00036 import moa.streams.InstanceStream;
00037 import moa.tasks.TaskMonitor;
00038 
00045 public class RandomRBFGenerator extends AbstractOptionHandler implements
00046         InstanceStream {
00047 
00048     @Override
00049     public String getPurposeString() {
00050         return "Generates a random radial basis function stream.";
00051     }
00052 
00053     private static final long serialVersionUID = 1L;
00054 
00055     public IntOption modelRandomSeedOption = new IntOption("modelRandomSeed",
00056             'r', "Seed for random generation of model.", 1);
00057 
00058     public IntOption instanceRandomSeedOption = new IntOption(
00059             "instanceRandomSeed", 'i',
00060             "Seed for random generation of instances.", 1);
00061 
00062     public IntOption numClassesOption = new IntOption("numClasses", 'c',
00063             "The number of classes to generate.", 2, 2, Integer.MAX_VALUE);
00064 
00065     public IntOption numAttsOption = new IntOption("numAtts", 'a',
00066             "The number of attributes to generate.", 10, 0, Integer.MAX_VALUE);
00067 
00068     public IntOption numCentroidsOption = new IntOption("numCentroids", 'n',
00069             "The number of centroids in the model.", 50, 1, Integer.MAX_VALUE);
00070 
00071     protected static class Centroid implements Serializable {
00072 
00073         private static final long serialVersionUID = 1L;
00074 
00075         public double[] centre;
00076 
00077         public int classLabel;
00078 
00079         public double stdDev;
00080     }
00081 
00082     protected InstancesHeader streamHeader;
00083 
00084     protected Centroid[] centroids;
00085 
00086     protected double[] centroidWeights;
00087 
00088     protected Random instanceRandom;
00089 
00090     @Override
00091     public void prepareForUseImpl(TaskMonitor monitor,
00092             ObjectRepository repository) {
00093         monitor.setCurrentActivity("Preparing random RBF...", -1.0);
00094         generateHeader();
00095         generateCentroids();
00096         restart();
00097     }
00098 
00099     @Override
00100     public InstancesHeader getHeader() {
00101         return this.streamHeader;
00102     }
00103 
00104     @Override
00105     public long estimatedRemainingInstances() {
00106         return -1;
00107     }
00108 
00109     @Override
00110     public boolean hasMoreInstances() {
00111         return true;
00112     }
00113 
00114     @Override
00115     public boolean isRestartable() {
00116         return true;
00117     }
00118 
00119     @Override
00120     public void restart() {
00121         this.instanceRandom = new Random(this.instanceRandomSeedOption.getValue());
00122     }
00123 
00124     @Override
00125     public Instance nextInstance() {
00126         Centroid centroid = this.centroids[MiscUtils.chooseRandomIndexBasedOnWeights(this.centroidWeights,
00127                 this.instanceRandom)];
00128         int numAtts = this.numAttsOption.getValue();
00129         double[] attVals = new double[numAtts + 1];
00130         for (int i = 0; i < numAtts; i++) {
00131             attVals[i] = (this.instanceRandom.nextDouble() * 2.0) - 1.0;
00132         }
00133         double magnitude = 0.0;
00134         for (int i = 0; i < numAtts; i++) {
00135             magnitude += attVals[i] * attVals[i];
00136         }
00137         magnitude = Math.sqrt(magnitude);
00138         double desiredMag = this.instanceRandom.nextGaussian()
00139                 * centroid.stdDev;
00140         double scale = desiredMag / magnitude;
00141         for (int i = 0; i < numAtts; i++) {
00142             attVals[i] = centroid.centre[i] + attVals[i] * scale;
00143         }
00144         Instance inst = new DenseInstance(1.0, attVals);
00145         inst.setDataset(getHeader());
00146         inst.setClassValue(centroid.classLabel);
00147         return inst;
00148     }
00149 
00150     protected void generateHeader() {
00151         FastVector attributes = new FastVector();
00152         for (int i = 0; i < this.numAttsOption.getValue(); i++) {
00153             attributes.addElement(new Attribute("att" + (i + 1)));
00154         }
00155         FastVector classLabels = new FastVector();
00156         for (int i = 0; i < this.numClassesOption.getValue(); i++) {
00157             classLabels.addElement("class" + (i + 1));
00158         }
00159         attributes.addElement(new Attribute("class", classLabels));
00160         this.streamHeader = new InstancesHeader(new Instances(
00161                 getCLICreationString(InstanceStream.class), attributes, 0));
00162         this.streamHeader.setClassIndex(this.streamHeader.numAttributes() - 1);
00163     }
00164 
00165     protected void generateCentroids() {
00166         Random modelRand = new Random(this.modelRandomSeedOption.getValue());
00167         this.centroids = new Centroid[this.numCentroidsOption.getValue()];
00168         this.centroidWeights = new double[this.centroids.length];
00169         for (int i = 0; i < this.centroids.length; i++) {
00170             this.centroids[i] = new Centroid();
00171             double[] randCentre = new double[this.numAttsOption.getValue()];
00172             for (int j = 0; j < randCentre.length; j++) {
00173                 randCentre[j] = modelRand.nextDouble();
00174             }
00175             this.centroids[i].centre = randCentre;
00176             this.centroids[i].classLabel = modelRand.nextInt(this.numClassesOption.getValue());
00177             this.centroids[i].stdDev = modelRand.nextDouble();
00178             this.centroidWeights[i] = modelRand.nextDouble();
00179         }
00180     }
00181 
00182     @Override
00183     public void getDescription(StringBuilder sb, int indent) {
00184         // TODO Auto-generated method stub
00185     }
00186 }
 All Classes Namespaces Files Functions Variables Enumerations