source: branches/HeuristicLab.DatastreamAnalysis/HeuristicLab.DatastreamAnalysis/3.4/Datastream.cs @ 14710

Last change on this file since 14710 was 14710, checked in by jzenisek, 5 years ago

#2719 implemented ensemble model rating by introducing the new type RatedEnsembleModel; introduced performance indicator calculation in results;

File size: 10.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Collections;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Data;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.Problems.DataAnalysis;
33
34namespace HeuristicLab.DatastreamAnalysis {
35  [StorableClass]
36  [Item("Datastream", "Represents an item containing a data source and reading options for a certain data stream.")]
37  [Creatable(CreatableAttribute.Categories.Problems)]
38  public class Datastream : ParameterizedNamedItem, IDatastream {
39
40    protected const string ProblemDataParameterName = "ProblemData";
41    protected const string InitialSlidingWindowParameterName = "InitialSlidingWindowParameter";
42    protected const string SlidingWindowSizeParameterName = "SlidingWindowSize";
43    protected const string SlidingWindowStepWidthParameterName = "SlidingWindowStepWidth";
44    protected const string SlidingWindowMovementDelayParameterName = "SlidingWindowMovementDelay";
45    protected const string FitnessPartitionParameterName = "FitnessPartition";
46    protected const string SlidingWindowSonarRatioParameterName = "SlidingWindowSonarRatio";
47
48    #region parameter properites
49
50    public IValueParameter<RegressionProblemData> ProblemDataParameter {
51      get { return (IValueParameter<RegressionProblemData>) Parameters[ProblemDataParameterName]; } 
52    }
53
54    public IValueParameter<IntRange> InitialSlidingWindowParameter {
55      get { return (IValueParameter<IntRange>) Parameters[InitialSlidingWindowParameterName]; }
56    }
57
58    public IValueParameter<IntValue> SlidingWindowSizeParameter {
59      get { return (IValueParameter<IntValue>) Parameters[SlidingWindowSizeParameterName]; }
60    }
61
62    public IValueParameter<IntValue> SlidingWindowStepWidthParameter {
63      get { return (IValueParameter<IntValue>) Parameters[SlidingWindowStepWidthParameterName]; }
64    }
65
66    public IValueParameter<IntValue> SlidingWindowMovementDelayParameter {
67      get { return (IValueParameter<IntValue>) Parameters[SlidingWindowMovementDelayParameterName]; }
68    }
69
70    public IValueParameter<DoubleValue> SlidingWindowSonarRatioParameter {
71      get { return (IValueParameter<DoubleValue>) Parameters[SlidingWindowSonarRatioParameterName]; }
72    }
73
74    //public IValueParameter<IntRange> FitnessPartitionParameter {
75    //  get { return (IValueParameter<IntRange>) Parameters[FitnessPartitionParameterName]; }
76    //}
77
78    #endregion
79
80    #region properties
81
82    public RegressionProblemData ProblemData {
83      get { return ProblemDataParameter.Value; }
84      set {
85        if(value == null) throw new ArgumentNullException("ProblemData", "The provided value for problemData is null.");
86        ProblemDataParameter.Value = value;
87        //OnProblemDataChanged();
88      }
89    }
90   
91
92    public IntRange InitialSlidingWindow {
93      get { return InitialSlidingWindowParameter.Value; }
94      set { InitialSlidingWindowParameter.Value = value; }
95    }
96
97    public IntValue SlidingWindowSize {
98      get { return SlidingWindowSizeParameter.Value; }
99      set { SlidingWindowSizeParameter.Value = value; }
100    }
101
102    public IntValue SlidingWindowStepWidth {
103      get { return SlidingWindowStepWidthParameter.Value; }
104      set { SlidingWindowStepWidthParameter.Value = value; }
105    }
106
107    public IntValue SlidingWindowMovementDelay {
108      get { return SlidingWindowMovementDelayParameter.Value; }
109      set { SlidingWindowMovementDelayParameter.Value = value; }
110    }
111
112    public DoubleValue SlidingWindowSonarRatio {
113      get { return SlidingWindowSonarRatioParameter.Value; }
114      set { SlidingWindowSonarRatioParameter.Value = value; }
115    }
116
117    #endregion
118
119    #region internal properties
120
121    public IntRange FitnessPartition { get; set; }
122
123    public bool UpdateAvailable {
124      get { return false; }
125    }
126
127    public bool SlidingWindowMovementPossible {
128      get {
129        return ProblemData != null && ProblemData.Dataset != null &&
130               (
131                (FitnessPartition.End + SlidingWindowStepWidth.Value <= ProblemData.Dataset.Rows) // shift end
132                || (FitnessPartition.Size > SlidingWindowSize.Value && (FitnessPartition.Start + SlidingWindowStepWidth.Value) < FitnessPartition.End) // shift start
133               );
134      }
135    }
136
137    public bool SlidingWindowEvaluationPossible {
138      get {
139        //return ProblemData != null && ProblemData.Dataset != null && ProblemData.Dataset.Rows > 0 && FitnessPartition.Size > 0;
140        return FitnessPartition.Size > 0;
141      }
142    }
143
144    public bool MoveSlidingWindow() {
145      if (ProblemData?.Dataset == null || ProblemData.Dataset.Rows == 0) return false;
146      if (FitnessPartition.End + SlidingWindowStepWidth.Value > ProblemData.Dataset.Rows) return false;
147
148      if (FitnessPartition.Size == SlidingWindowSize.Value) {
149        FitnessPartition.Start += SlidingWindowStepWidth.Value;
150        FitnessPartition.End += SlidingWindowStepWidth.Value;
151      } else {
152        FitnessPartition.End += SlidingWindowStepWidth.Value;
153        FitnessPartition.Start += FitnessPartition.End - SlidingWindowSize.Value;
154      }
155
156      //if (FitnessPartition.Size > SlidingWindowSize.Value) {
157      //  FitnessPartition.Start += SlidingWindowStepWidth.Value;
158      //} else if (FitnessPartition.Size == SlidingWindowSize.Value) {
159      //  FitnessPartition.Start += SlidingWindowStepWidth.Value;
160      //  FitnessPartition.End += SlidingWindowStepWidth.Value;
161      //} else {
162      //  FitnessPartition.End += SlidingWindowStepWidth.Value;
163      //  if (FitnessPartition.Size > SlidingWindowSize.Value) {
164      //    FitnessPartition.Start = FitnessPartition.End - FitnessPartition.Size;
165      //  }
166      //}   
167      return true;
168    }
169
170    #endregion
171
172    #region constructors
173    [StorableConstructor]
174    protected Datastream(bool deserializing) : base(deserializing) { }
175
176    [StorableHook(HookType.AfterDeserialization)]
177    private void AfterDeserialization() {
178      RegisterParameterEvents();
179      RecoverState();
180    }
181
182    protected Datastream(Datastream original, Cloner cloner) : base(original, cloner) {
183      RegisterParameterEvents();
184      RecoverState();
185    }
186
187    public override IDeepCloneable Clone(Cloner cloner) {
188      return new Datastream(this, cloner);
189    }
190
191    public Datastream() : base() {
192      Parameters.Add(new ValueParameter<RegressionProblemData>(ProblemDataParameterName, "ProblemData for analysis with selected ensembles.", null));
193      Parameters.Add(new FixedValueParameter<IntRange>(InitialSlidingWindowParameterName, "Initial sliding window boundaries", new IntRange(0, 100)));
194      Parameters.Add(new FixedValueParameter<IntValue>(SlidingWindowSizeParameterName, "Sliding window size", new IntValue(100)));
195      Parameters.Add(new FixedValueParameter<IntValue>(SlidingWindowStepWidthParameterName, "Sliding window step width", new IntValue(1)));
196      Parameters.Add(new FixedValueParameter<IntValue>(SlidingWindowMovementDelayParameterName, "Sliding window movement delay interval (milliseconds)", new IntValue(0)));
197      Parameters.Add(new FixedValueParameter<DoubleValue>(SlidingWindowSonarRatioParameterName, "Sliding window sonar ratio", new DoubleValue(0.25)));
198      RegisterParameterEvents();
199      InitializeState();
200    }
201    #endregion
202
203    public void InitializeState(){
204      if (ProblemData == null || ProblemData.Dataset == null || ProblemData.Dataset.Rows == 0) {
205        FitnessPartition = new IntRange(0, 0);
206      } else {
207        FitnessPartition = (IntRange)InitialSlidingWindow.Clone();
208        if (FitnessPartition.End > ProblemData.Dataset.Rows) {
209          FitnessPartition.End = ProblemData.Dataset.Rows;
210        }
211      }
212    }
213
214    public void RecoverState() {
215      if (ProblemData == null || ProblemData.Dataset == null || ProblemData.Dataset.Rows == 0) {
216        FitnessPartition = new IntRange(0, 0);
217      } else {
218        if(FitnessPartition == null) FitnessPartition = (IntRange)InitialSlidingWindow.Clone();
219        if (FitnessPartition.End > ProblemData.Dataset.Rows) {
220          FitnessPartition.End = ProblemData.Dataset.Rows;
221        }
222      }
223    }
224
225    public event EventHandler ProblemDataChanged;
226    public event EventHandler Reset;
227    public event EventHandler SlidingWindowChanged;
228
229
230    //protected virtual void OnProblemDataChanged() {
231    //  EventHandler handler = ProblemDataChanged;
232    //  if(handler != null) handler(this, EventArgs.Empty);
233    //}
234
235    //protected virtual void OnReset() {
236    //  EventHandler handler = Reset;
237    //  if (handler != null) handler(this, EventArgs.Empty);
238    //}
239
240    private void RegisterParameterEvents() {
241      ProblemDataParameter.ValueChanged += new EventHandler(ProblemData_ValueChanged);
242      //SlidingWindowSizeParameter.ValueChanged += new EventHandler(SlidingWindow_Changed);
243      InitialSlidingWindowParameter.ValueChanged += new EventHandler(SlidingWindow_Changed);
244    }
245
246    private void ProblemData_ValueChanged(object sender, EventArgs e) {
247      if (e == null) return;
248
249      InitializeState();
250      DatastreamAnalysisUtil.RaiseEvent(this, ProblemDataChanged);
251    }
252
253    private void SlidingWindow_Changed(object sender, EventArgs e) {
254      InitializeState();
255    }
256
257
258    // TODO
259    public IEnumerable<IParameterizedItem> ExecutionContextItems { get; }
260   
261  }
262}
Note: See TracBrowser for help on using the repository browser.