MOA 12.03
Real Time Analytics for Data Streams
ConceptDriftStream.java
Go to the documentation of this file.
00001 /*
00002  *    ConceptDriftStream.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;
00021 
00022 import java.util.Random;
00023 
00024 import moa.core.InstancesHeader;
00025 import moa.core.ObjectRepository;
00026 import moa.options.AbstractOptionHandler;
00027 import moa.options.ClassOption;
00028 import moa.options.FloatOption;
00029 import moa.options.IntOption;
00030 import moa.tasks.TaskMonitor;
00031 import weka.core.Instance;
00032 
00049 public class ConceptDriftStream extends AbstractOptionHandler implements
00050         InstanceStream {
00051 
00052     @Override
00053     public String getPurposeString() {
00054         return "Adds Concept Drift to examples in a stream.";
00055     }
00056 
00057     private static final long serialVersionUID = 1L;
00058 
00059     public ClassOption streamOption = new ClassOption("stream", 's',
00060             "Stream to add concept drift.", InstanceStream.class,
00061             "generators.RandomTreeGenerator");
00062 
00063     public ClassOption driftstreamOption = new ClassOption("driftstream", 'd',
00064             "Concept drift Stream.", InstanceStream.class,
00065             "generators.RandomTreeGenerator");
00066 
00067     public FloatOption alphaOption = new FloatOption("alpha",
00068             'a', "Angle alpha of change grade.", 0.0, 0.0, 90.0);
00069 
00070     public IntOption positionOption = new IntOption("position",
00071             'p', "Central position of concept drift change.", 0);
00072 
00073     public IntOption widthOption = new IntOption("width",
00074             'w', "Width of concept drift change.", 1000);
00075 
00076     public IntOption randomSeedOption = new IntOption("randomSeed", 'r',
00077             "Seed for random noise.", 1);
00078 
00079     protected InstanceStream inputStream;
00080 
00081     protected InstanceStream driftStream;
00082 
00083     protected Random random;
00084 
00085     protected int numberInstanceStream;
00086 
00087     @Override
00088     public void prepareForUseImpl(TaskMonitor monitor,
00089             ObjectRepository repository) {
00090 
00091         this.inputStream = (InstanceStream) getPreparedClassOption(this.streamOption);
00092         this.driftStream = (InstanceStream) getPreparedClassOption(this.driftstreamOption);
00093         this.random = new Random(this.randomSeedOption.getValue());
00094         numberInstanceStream = 0;
00095         if (this.alphaOption.getValue() != 0.0) {
00096             this.widthOption.setValue((int) (1 / Math.tan(this.alphaOption.getValue() * Math.PI / 180)));
00097         }
00098     }
00099 
00100     @Override
00101     public long estimatedRemainingInstances() {
00102         return this.inputStream.estimatedRemainingInstances() + this.driftStream.estimatedRemainingInstances();
00103     }
00104 
00105     @Override
00106     public InstancesHeader getHeader() {
00107         return this.inputStream.getHeader();
00108     }
00109 
00110     @Override
00111     public boolean hasMoreInstances() {
00112         return (this.inputStream.hasMoreInstances() || this.driftStream.hasMoreInstances());
00113     }
00114 
00115     @Override
00116     public boolean isRestartable() {
00117         return (this.inputStream.isRestartable() && this.driftStream.isRestartable());
00118     }
00119 
00120     @Override
00121     public Instance nextInstance() {
00122         numberInstanceStream++;
00123         double x = -4.0 * (double) (numberInstanceStream - this.positionOption.getValue()) / (double) this.widthOption.getValue();
00124         double probabilityDrift = 1.0 / (1.0 + Math.exp(x));
00125         if (this.random.nextDouble() > probabilityDrift) {
00126             return this.inputStream.nextInstance();
00127         } else {
00128             return this.driftStream.nextInstance();
00129         }
00130 
00131     }
00132 
00133     @Override
00134     public void restart() {
00135         this.inputStream.restart();
00136         this.driftStream.restart();
00137         numberInstanceStream = 0;
00138     }
00139 
00140     @Override
00141     public void getDescription(StringBuilder sb, int indent) {
00142         // TODO Auto-generated method stub
00143     }
00144 }
 All Classes Namespaces Files Functions Variables Enumerations