Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2719_HeuristicLab.DatastreamAnalysis/HeuristicLab.DatastreamAnalysis/3.4/Datastream.cs @ 18242

Last change on this file since 18242 was 17980, checked in by jzenisek, 3 years ago

#2719 merged head of HeuristicLab.Problems.DataAnalysis into branch; added several minor items

File size: 12.9 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#endregion
23
24using System;
25using System.Collections.Generic;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Parameters;
30using HEAL.Attic;
31using HeuristicLab.Problems.DataAnalysis;
32
33namespace HeuristicLab.DatastreamAnalysis {
34  [Item("Datastream", "Represents an item containing a data source and reading options for a certain data stream.")]
35  [StorableType("91B9B2A6-D043-4FA0-9402-DF382B4B4D77")]
36  [Creatable(CreatableAttribute.Categories.Problems)]
37  public class Datastream : ParameterizedNamedItem, IDatastream {
38
39    protected const string ProblemDataParameterName = "ProblemData";
40    protected const string DataSourceAddressParameterName = "DataSourceAdress";
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    protected const string SlidingWindowEvaluationSchemeParameterName = "SlidingWindowEvaluationScheme";
48    protected const string EvaluationHorizonParameterName = "EvaluationHorizon";
49    protected const string RollingEvaluationHorizonParameterName = "RollingEvaluationHorizon";
50
51    #region parameter properites
52
53    public enum EvaluationScheme { none, equal, linear, quadratic, cubic, exponential }
54
55    public IValueParameter<RegressionProblemData> ProblemDataParameter
56    {
57      get { return (IValueParameter<RegressionProblemData>)Parameters[ProblemDataParameterName]; }
58    }
59
60    public IValueParameter<StringValue> DataSourceAddressParameter
61    {
62      get { return (IValueParameter<StringValue>)Parameters[DataSourceAddressParameterName]; }
63    }
64
65    public IValueParameter<IntRange> InitialSlidingWindowParameter
66    {
67      get { return (IValueParameter<IntRange>)Parameters[InitialSlidingWindowParameterName]; }
68    }
69
70    public IValueParameter<IntValue> SlidingWindowSizeParameter
71    {
72      get { return (IValueParameter<IntValue>)Parameters[SlidingWindowSizeParameterName]; }
73    }
74
75    public IValueParameter<IntValue> SlidingWindowStepWidthParameter
76    {
77      get { return (IValueParameter<IntValue>)Parameters[SlidingWindowStepWidthParameterName]; }
78    }
79
80    public IValueParameter<IntValue> SlidingWindowMovementDelayParameter
81    {
82      get { return (IValueParameter<IntValue>)Parameters[SlidingWindowMovementDelayParameterName]; }
83    }
84
85    public IValueParameter<DoubleValue> SlidingWindowSonarRatioParameter
86    {
87      get { return (IValueParameter<DoubleValue>)Parameters[SlidingWindowSonarRatioParameterName]; }
88    }
89
90    public IValueParameter<EnumValue<EvaluationScheme>> SlidingWindowEvaluationSchemeParameter
91    {
92      get
93      {
94        return (IValueParameter<EnumValue<EvaluationScheme>>)Parameters[SlidingWindowEvaluationSchemeParameterName];
95      }
96    }
97
98    public IValueParameter<IntValue> EvaluationHorizonParameter
99    {
100      get { return (IValueParameter<IntValue>)Parameters[EvaluationHorizonParameterName]; }
101    }
102
103    public IValueParameter<BoolValue> RollingEvaluationHorizonParameter
104    {
105      get { return (IValueParameter<BoolValue>)Parameters[RollingEvaluationHorizonParameterName]; }
106    }
107
108    //public IValueParameter<IntRange> FitnessPartitionParameter {
109    //  get { return (IValueParameter<IntRange>) Parameters[FitnessPartitionParameterName]; }
110    //}
111
112    #endregion
113
114    #region properties
115
116    public RegressionProblemData ProblemData
117    {
118      get { return ProblemDataParameter.Value; }
119      set
120      {
121        if (value == null) throw new ArgumentNullException("ProblemData", "The provided value for problemData is null.");
122        ProblemDataParameter.Value = value;
123        //OnProblemDataChanged();
124      }
125    }
126
127    public StringValue DataSourceAddress
128    {
129      get { return DataSourceAddressParameter.Value; }
130      set { DataSourceAddressParameter.Value = value; }
131    }
132
133    public IntRange InitialSlidingWindow
134    {
135      get { return InitialSlidingWindowParameter.Value; }
136      set { InitialSlidingWindowParameter.Value = value; }
137    }
138
139    public IntValue SlidingWindowSize
140    {
141      get { return SlidingWindowSizeParameter.Value; }
142      set { SlidingWindowSizeParameter.Value = value; }
143    }
144
145    public IntValue SlidingWindowStepWidth
146    {
147      get { return SlidingWindowStepWidthParameter.Value; }
148      set { SlidingWindowStepWidthParameter.Value = value; }
149    }
150
151    public IntValue SlidingWindowMovementDelay
152    {
153      get { return SlidingWindowMovementDelayParameter.Value; }
154      set { SlidingWindowMovementDelayParameter.Value = value; }
155    }
156
157    public DoubleValue SlidingWindowSonarRatio
158    {
159      get { return SlidingWindowSonarRatioParameter.Value; }
160      set { SlidingWindowSonarRatioParameter.Value = value; }
161    }
162
163    public EnumValue<EvaluationScheme> SlidingWindowEvaluationScheme
164    {
165      get { return SlidingWindowEvaluationSchemeParameter.Value; }
166      set { SlidingWindowEvaluationSchemeParameter.Value = value; }
167    }
168
169    public IntValue EvaluationHorizon
170    {
171      get { return EvaluationHorizonParameter.Value; }
172      set { EvaluationHorizonParameter.Value = value; }
173    }
174
175    public BoolValue RollingEvaluationHorizon
176    {
177      get { return RollingEvaluationHorizonParameter.Value; }
178      set { RollingEvaluationHorizonParameter.Value = value; }
179    }
180
181
182    #endregion
183
184    #region internal properties
185
186    public IntRange FitnessPartition { get; set; }
187
188    public bool UpdateAvailable
189    {
190      get { return !String.IsNullOrWhiteSpace(DataSourceAddress.Value); }
191    }
192
193    public bool SlidingWindowMovementPossible
194    {
195      get
196      {
197        return ProblemData != null && ProblemData.Dataset != null &&
198               (
199                (FitnessPartition.End + SlidingWindowStepWidth.Value <= ProblemData.Dataset.Rows) // shift end
200                || (FitnessPartition.Size > SlidingWindowSize.Value && (FitnessPartition.Start + SlidingWindowStepWidth.Value) < FitnessPartition.End) // shift start
201               );
202      }
203    }
204
205    public bool SlidingWindowEvaluationPossible
206    {
207      get
208      {
209        //return ProblemData != null && ProblemData.Dataset != null && ProblemData.Dataset.Rows > 0 && FitnessPartition.Size > 0;
210        return FitnessPartition.Size >= 0;
211      }
212    }
213
214    public bool MoveSlidingWindow() {
215      if (ProblemData?.Dataset == null || ProblemData.Dataset.Rows == 0) return false;
216      if (FitnessPartition.End + SlidingWindowStepWidth.Value > ProblemData.Dataset.Rows) return false;
217
218      if (FitnessPartition.Size == SlidingWindowSize.Value) {
219        FitnessPartition.Start += SlidingWindowStepWidth.Value;
220        FitnessPartition.End += SlidingWindowStepWidth.Value;
221      } else {
222        FitnessPartition.End += SlidingWindowStepWidth.Value;
223        FitnessPartition.Start += FitnessPartition.End - SlidingWindowSize.Value;
224      }
225
226      //if (FitnessPartition.Size > SlidingWindowSize.Value) {
227      //  FitnessPartition.Start += SlidingWindowStepWidth.Value;
228      //} else if (FitnessPartition.Size == SlidingWindowSize.Value) {
229      //  FitnessPartition.Start += SlidingWindowStepWidth.Value;
230      //  FitnessPartition.End += SlidingWindowStepWidth.Value;
231      //} else {
232      //  FitnessPartition.End += SlidingWindowStepWidth.Value;
233      //  if (FitnessPartition.Size > SlidingWindowSize.Value) {
234      //    FitnessPartition.Start = FitnessPartition.End - FitnessPartition.Size;
235      //  }
236      //}   
237      return true;
238    }
239
240    #endregion
241
242    #region constructors
243    [StorableConstructor]
244    protected Datastream(StorableConstructorFlag _) : base(_) { }
245
246    [StorableHook(HookType.AfterDeserialization)]
247    private void AfterDeserialization() {
248      RegisterParameterEvents();
249      RecoverState();
250    }
251
252    protected Datastream(Datastream original, Cloner cloner) : base(original, cloner) {
253      RegisterParameterEvents();
254      RecoverState();
255    }
256
257    public override IDeepCloneable Clone(Cloner cloner) {
258      return new Datastream(this, cloner);
259    }
260
261    public Datastream() : base() {
262      Parameters.Add(new ValueParameter<RegressionProblemData>(ProblemDataParameterName, "ProblemData for analysis with selected ensembles.", null));
263      Parameters.Add(new FixedValueParameter<StringValue>(DataSourceAddressParameterName, "Source address of data stream", new StringValue("")));
264      Parameters.Add(new FixedValueParameter<IntRange>(InitialSlidingWindowParameterName, "Initial sliding window boundaries", new IntRange(0, 10)));
265      Parameters.Add(new FixedValueParameter<IntValue>(SlidingWindowSizeParameterName, "Sliding window size", new IntValue(10)));
266      Parameters.Add(new FixedValueParameter<IntValue>(SlidingWindowStepWidthParameterName, "Sliding window step width", new IntValue(1)));
267      Parameters.Add(new FixedValueParameter<IntValue>(SlidingWindowMovementDelayParameterName, "Sliding window movement delay interval (milliseconds)", new IntValue(0)));
268      Parameters.Add(new FixedValueParameter<DoubleValue>(SlidingWindowSonarRatioParameterName, "Sliding window sonar ratio", new DoubleValue(0.0)));
269      Parameters.Add(new FixedValueParameter<EnumValue<EvaluationScheme>>(SlidingWindowEvaluationSchemeParameterName, "Sliding window evaluation scheme", new EnumValue<EvaluationScheme>(EvaluationScheme.none)));
270      Parameters.Add(new FixedValueParameter<IntValue>(EvaluationHorizonParameterName, "Evaluation Horizon", new IntValue(1)));
271      Parameters.Add(new ValueParameter<BoolValue>(RollingEvaluationHorizonParameterName, "Rolling Evalution Horizon", new BoolValue(false)));
272      RegisterParameterEvents();
273      InitializeState();
274    }
275    #endregion
276
277    public void InitializeState() {
278      if (ProblemData == null || ProblemData.Dataset == null || ProblemData.Dataset.Rows == 0) {
279        FitnessPartition = new IntRange(0, 0);
280      } else {
281        FitnessPartition = (IntRange)InitialSlidingWindow.Clone();
282        if (FitnessPartition.End > ProblemData.Dataset.Rows) {
283          FitnessPartition.End = ProblemData.Dataset.Rows;
284        }
285      }
286    }
287
288    public void RecoverState() {
289      if (ProblemData == null || ProblemData.Dataset == null || ProblemData.Dataset.Rows == 0) {
290        FitnessPartition = new IntRange(0, 0);
291      } else {
292        if (FitnessPartition == null) FitnessPartition = (IntRange)InitialSlidingWindow.Clone();
293        if (FitnessPartition.End > ProblemData.Dataset.Rows) {
294          FitnessPartition.End = ProblemData.Dataset.Rows;
295        }
296      }
297    }
298
299    public event EventHandler ProblemDataChanged;
300    public event EventHandler Reset;
301    public event EventHandler SlidingWindowChanged;
302
303
304    //protected virtual void OnProblemDataChanged() {
305    //  EventHandler handler = ProblemDataChanged;
306    //  if(handler != null) handler(this, EventArgs.Empty);
307    //}
308
309    //protected virtual void OnReset() {
310    //  EventHandler handler = Reset;
311    //  if (handler != null) handler(this, EventArgs.Empty);
312    //}
313
314    private void RegisterParameterEvents() {
315      ProblemDataParameter.ValueChanged += new EventHandler(ProblemData_ValueChanged);
316      //SlidingWindowSizeParameter.ValueChanged += new EventHandler(SlidingWindow_Changed);
317      InitialSlidingWindowParameter.ValueChanged += new EventHandler(SlidingWindow_Changed);
318    }
319
320    private void ProblemData_ValueChanged(object sender, EventArgs e) {
321      if (e == null) return;
322
323      InitializeState();
324      DatastreamAnalysisUtil.RaiseEvent(this, ProblemDataChanged);
325    }
326
327    private void SlidingWindow_Changed(object sender, EventArgs e) {
328      InitializeState();
329    }
330
331
332    // TODO
333    public IEnumerable<IParameterizedItem> ExecutionContextItems { get; }
334
335  }
336}
Note: See TracBrowser for help on using the repository browser.