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

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

#2719 enhanced the optimizer's functionality

File size: 9.7 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 SlidingWindowMovementIntervalParameterName = "SlidingWindowMovementInterval";
45    protected const string FitnessPartitionParameterName = "FitnessPartition";
46
47    #region parameter properites
48
49    public IValueParameter<RegressionProblemData> ProblemDataParameter {
50      get { return (IValueParameter<RegressionProblemData>) Parameters[ProblemDataParameterName]; } 
51    }
52
53    public IValueParameter<IntRange> InitialSlidingWindowParameter {
54      get { return (IValueParameter<IntRange>) Parameters[InitialSlidingWindowParameterName]; }
55    }
56
57    public IValueParameter<IntValue> SlidingWindowSizeParameter {
58      get { return (IValueParameter<IntValue>) Parameters[SlidingWindowSizeParameterName]; }
59    }
60
61    public IValueParameter<IntValue> SlidingWindowStepWidthParameter {
62      get { return (IValueParameter<IntValue>) Parameters[SlidingWindowStepWidthParameterName]; }
63    }
64
65    public IValueParameter<IntValue> SlidingWindowMovementIntervalParameter {
66      get { return (IValueParameter<IntValue>) Parameters[SlidingWindowMovementIntervalParameterName]; }
67    }
68
69    //public IValueParameter<IntRange> FitnessPartitionParameter {
70    //  get { return (IValueParameter<IntRange>) Parameters[FitnessPartitionParameterName]; }
71    //}
72
73    #endregion
74
75    #region properties
76
77    public RegressionProblemData ProblemData {
78      get { return ProblemDataParameter.Value; }
79      set {
80        if(value == null) throw new ArgumentNullException("problemData", "The provided value for the problemData is null.");
81        ProblemDataParameter.Value = value;
82        //OnProblemDataChanged();
83      }
84    }
85   
86
87    public IntRange InitialSlidingWindow {
88      get { return InitialSlidingWindowParameter.Value; }
89      set { InitialSlidingWindowParameter.Value = value; }
90    }
91
92    public IntValue SlidingWindowSize {
93      get { return SlidingWindowSizeParameter.Value; }
94      set { SlidingWindowSizeParameter.Value = value; }
95    }
96
97    public IntValue SlidingWindowStepWidth {
98      get { return SlidingWindowStepWidthParameter.Value; }
99      set { SlidingWindowStepWidthParameter.Value = value; }
100    }
101
102    public IntValue SlidingWindowMovementInterval {
103      get { return SlidingWindowMovementIntervalParameter.Value; }
104      set { SlidingWindowMovementIntervalParameter.Value = value; }
105    }
106
107    #endregion
108
109    #region internal properties
110
111    public IntRange FitnessPartition { get; set; }
112
113    public bool UpdateAvailable {
114      get { return false; }
115    }
116
117    public bool SlidingWindowMovementPossible {
118      get {
119        return ProblemData != null && ProblemData.Dataset != null &&
120               (
121                (FitnessPartition.End + SlidingWindowStepWidth.Value <= ProblemData.Dataset.Rows) // shift end
122                || (FitnessPartition.Size > SlidingWindowSize.Value && (FitnessPartition.Start + SlidingWindowStepWidth.Value) < FitnessPartition.End) // shift start
123               );
124      }
125    }
126
127    public bool SlidingWindowEvaluationPossible {
128      get {
129        //return ProblemData != null && ProblemData.Dataset != null && ProblemData.Dataset.Rows > 0 && FitnessPartition.Size > 0;
130        return FitnessPartition.Size > 0;
131      }
132    }
133
134    public bool MoveSlidingWindow() {
135      if (ProblemData?.Dataset == null || ProblemData.Dataset.Rows == 0) return false;
136      if (FitnessPartition.End + SlidingWindowStepWidth.Value > ProblemData.Dataset.Rows) return false;
137
138      if (FitnessPartition.Size == SlidingWindowSize.Value) {
139        FitnessPartition.Start += SlidingWindowStepWidth.Value;
140        FitnessPartition.End += SlidingWindowStepWidth.Value;
141      } else { 
142        FitnessPartition.End += SlidingWindowStepWidth.Value;
143        FitnessPartition.Start += FitnessPartition.End - SlidingWindowSize.Value;
144      } 
145
146      //if (FitnessPartition.Size > SlidingWindowSize.Value) {
147      //  FitnessPartition.Start += SlidingWindowStepWidth.Value;
148      //} else if (FitnessPartition.Size == SlidingWindowSize.Value) {
149      //  FitnessPartition.Start += SlidingWindowStepWidth.Value;
150      //  FitnessPartition.End += SlidingWindowStepWidth.Value;
151      //} else {
152      //  FitnessPartition.End += SlidingWindowStepWidth.Value;
153      //  if (FitnessPartition.Size > SlidingWindowSize.Value) {
154      //    FitnessPartition.Start = FitnessPartition.End - FitnessPartition.Size;
155      //  }
156      //}   
157      return true;
158    }
159
160    #endregion
161
162    #region constructors
163    [StorableConstructor]
164    protected Datastream(bool deserializing) : base(deserializing) { }
165
166    [StorableHook(HookType.AfterDeserialization)]
167    private void AfterDeserialization() {
168      RegisterParameterEvents();
169      RecoverState();
170    }
171
172    protected Datastream(Datastream original, Cloner cloner) : base(original, cloner) {
173      RegisterParameterEvents();
174      RecoverState();
175    }
176
177    public override IDeepCloneable Clone(Cloner cloner) {
178      return new Datastream(this, cloner);
179    }
180
181    public Datastream() : base() {
182      Parameters.Add(new ValueParameter<RegressionProblemData>(ProblemDataParameterName, "ProblemData for analysis with selected ensembles.", null));
183      Parameters.Add(new FixedValueParameter<IntRange>(InitialSlidingWindowParameterName, "Initial sliding window boundaries", new IntRange(0, 5)));
184      Parameters.Add(new FixedValueParameter<IntValue>(SlidingWindowSizeParameterName, "Sliding window size", new IntValue(5)));
185      Parameters.Add(new FixedValueParameter<IntValue>(SlidingWindowStepWidthParameterName, "Sliding window step width", new IntValue(1)));
186      Parameters.Add(new FixedValueParameter<IntValue>(SlidingWindowMovementIntervalParameterName, "Sliding window movement interval (milliseconds)", new IntValue(2000)));
187      RegisterParameterEvents();
188      InitializeState();
189    }
190    #endregion
191
192    public void InitializeState(){
193      if (ProblemData == null || ProblemData.Dataset == null || ProblemData.Dataset.Rows == 0) {
194        FitnessPartition = new IntRange(0, 0);
195      } else {
196        FitnessPartition = (IntRange)InitialSlidingWindow.Clone();
197        if (FitnessPartition.End > ProblemData.Dataset.Rows) {
198          FitnessPartition.End = ProblemData.Dataset.Rows;
199        }
200      }
201    }
202
203    public void RecoverState() {
204      if (ProblemData == null || ProblemData.Dataset == null || ProblemData.Dataset.Rows == 0) {
205        FitnessPartition = new IntRange(0, 0);
206      } else {
207        if(FitnessPartition == null) FitnessPartition = (IntRange)InitialSlidingWindow.Clone();
208        if (FitnessPartition.End > ProblemData.Dataset.Rows) {
209          FitnessPartition.End = ProblemData.Dataset.Rows;
210        }
211      }
212    }
213
214    public event EventHandler ProblemDataChanged;
215    public event EventHandler Reset;
216    public event EventHandler SlidingWindowChanged;
217
218
219    //protected virtual void OnProblemDataChanged() {
220    //  EventHandler handler = ProblemDataChanged;
221    //  if(handler != null) handler(this, EventArgs.Empty);
222    //}
223
224    //protected virtual void OnReset() {
225    //  EventHandler handler = Reset;
226    //  if (handler != null) handler(this, EventArgs.Empty);
227    //}
228
229    private void RegisterParameterEvents() {
230      ProblemDataParameter.ValueChanged += new EventHandler(ProblemData_ValueChanged);
231      //SlidingWindowSizeParameter.ValueChanged += new EventHandler(SlidingWindow_Changed);
232      InitialSlidingWindowParameter.ValueChanged += new EventHandler(SlidingWindow_Changed);
233    }
234
235    private void ProblemData_ValueChanged(object sender, EventArgs e) {
236      if (e == null) return;
237
238      InitializeState();
239      DatastreamAnalysisUtil.RaiseEvent(this, ProblemDataChanged);
240    }
241
242    private void SlidingWindow_Changed(object sender, EventArgs e) {
243      InitializeState();
244    }
245
246
247    // TODO
248    public IEnumerable<IParameterizedItem> ExecutionContextItems { get; }
249   
250  }
251}
Note: See TracBrowser for help on using the repository browser.