Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17516 was 15867, checked in by jzenisek, 7 years ago

#2719: added proxy ensemble model

File size: 11.8 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 DataSourceAddressParameterName = "DataSourceAdress";
42    protected const string InitialSlidingWindowParameterName = "InitialSlidingWindowParameter";
43    protected const string SlidingWindowSizeParameterName = "SlidingWindowSize";
44    protected const string SlidingWindowStepWidthParameterName = "SlidingWindowStepWidth";
45    protected const string SlidingWindowMovementDelayParameterName = "SlidingWindowMovementDelay";
46    protected const string FitnessPartitionParameterName = "FitnessPartition";
47    protected const string SlidingWindowSonarRatioParameterName = "SlidingWindowSonarRatio";
48    protected const string SlidingWindowEvaluationSchemeParameterName = "SlidingWindowEvaluationScheme";
49
50    #region parameter properites
51
52    public enum EvaluationScheme { none, equal, linear, quadratic, cubic, exponential }
53
54    public IValueParameter<RegressionProblemData> ProblemDataParameter
55    {
56      get { return (IValueParameter<RegressionProblemData>)Parameters[ProblemDataParameterName]; }
57    }
58
59    public IValueParameter<StringValue> DataSourceAddressParameter
60    {
61      get { return (IValueParameter<StringValue>)Parameters[DataSourceAddressParameterName]; }
62    }
63
64    public IValueParameter<IntRange> InitialSlidingWindowParameter
65    {
66      get { return (IValueParameter<IntRange>)Parameters[InitialSlidingWindowParameterName]; }
67    }
68
69    public IValueParameter<IntValue> SlidingWindowSizeParameter
70    {
71      get { return (IValueParameter<IntValue>)Parameters[SlidingWindowSizeParameterName]; }
72    }
73
74    public IValueParameter<IntValue> SlidingWindowStepWidthParameter
75    {
76      get { return (IValueParameter<IntValue>)Parameters[SlidingWindowStepWidthParameterName]; }
77    }
78
79    public IValueParameter<IntValue> SlidingWindowMovementDelayParameter
80    {
81      get { return (IValueParameter<IntValue>)Parameters[SlidingWindowMovementDelayParameterName]; }
82    }
83
84    public IValueParameter<DoubleValue> SlidingWindowSonarRatioParameter
85    {
86      get { return (IValueParameter<DoubleValue>)Parameters[SlidingWindowSonarRatioParameterName]; }
87    }
88
89    public IValueParameter<EnumValue<EvaluationScheme>> SlidingWindowEvaluationSchemeParameter
90    {
91      get
92      {
93        return (IValueParameter<EnumValue<EvaluationScheme>>)Parameters[SlidingWindowEvaluationSchemeParameterName];
94      }
95    }
96
97    //public IValueParameter<IntRange> FitnessPartitionParameter {
98    //  get { return (IValueParameter<IntRange>) Parameters[FitnessPartitionParameterName]; }
99    //}
100
101    #endregion
102
103    #region properties
104
105    public RegressionProblemData ProblemData
106    {
107      get { return ProblemDataParameter.Value; }
108      set
109      {
110        if (value == null) throw new ArgumentNullException("ProblemData", "The provided value for problemData is null.");
111        ProblemDataParameter.Value = value;
112        //OnProblemDataChanged();
113      }
114    }
115
116    public StringValue DataSourceAddress
117    {
118      get { return DataSourceAddressParameter.Value; }
119      set { DataSourceAddressParameter.Value = value; }
120    }
121
122    public IntRange InitialSlidingWindow
123    {
124      get { return InitialSlidingWindowParameter.Value; }
125      set { InitialSlidingWindowParameter.Value = value; }
126    }
127
128    public IntValue SlidingWindowSize
129    {
130      get { return SlidingWindowSizeParameter.Value; }
131      set { SlidingWindowSizeParameter.Value = value; }
132    }
133
134    public IntValue SlidingWindowStepWidth
135    {
136      get { return SlidingWindowStepWidthParameter.Value; }
137      set { SlidingWindowStepWidthParameter.Value = value; }
138    }
139
140    public IntValue SlidingWindowMovementDelay
141    {
142      get { return SlidingWindowMovementDelayParameter.Value; }
143      set { SlidingWindowMovementDelayParameter.Value = value; }
144    }
145
146    public DoubleValue SlidingWindowSonarRatio
147    {
148      get { return SlidingWindowSonarRatioParameter.Value; }
149      set { SlidingWindowSonarRatioParameter.Value = value; }
150    }
151
152    public EnumValue<EvaluationScheme> SlidingWindowEvaluationScheme
153    {
154      get { return SlidingWindowEvaluationSchemeParameter.Value; }
155      set { SlidingWindowEvaluationSchemeParameter.Value = value; }
156    }
157
158
159    #endregion
160
161    #region internal properties
162
163    public IntRange FitnessPartition { get; set; }
164
165    public bool UpdateAvailable
166    {
167      get { return !String.IsNullOrWhiteSpace(DataSourceAddress.Value); }
168    }
169
170    public bool SlidingWindowMovementPossible
171    {
172      get
173      {
174        return ProblemData != null && ProblemData.Dataset != null &&
175               (
176                (FitnessPartition.End + SlidingWindowStepWidth.Value <= ProblemData.Dataset.Rows) // shift end
177                || (FitnessPartition.Size > SlidingWindowSize.Value && (FitnessPartition.Start + SlidingWindowStepWidth.Value) < FitnessPartition.End) // shift start
178               );
179      }
180    }
181
182    public bool SlidingWindowEvaluationPossible
183    {
184      get
185      {
186        //return ProblemData != null && ProblemData.Dataset != null && ProblemData.Dataset.Rows > 0 && FitnessPartition.Size > 0;
187        return FitnessPartition.Size > 0;
188      }
189    }
190
191    public bool MoveSlidingWindow() {
192      if (ProblemData?.Dataset == null || ProblemData.Dataset.Rows == 0) return false;
193      if (FitnessPartition.End + SlidingWindowStepWidth.Value > ProblemData.Dataset.Rows) return false;
194
195      if (FitnessPartition.Size == SlidingWindowSize.Value) {
196        FitnessPartition.Start += SlidingWindowStepWidth.Value;
197        FitnessPartition.End += SlidingWindowStepWidth.Value;
198      } else {
199        FitnessPartition.End += SlidingWindowStepWidth.Value;
200        FitnessPartition.Start += FitnessPartition.End - SlidingWindowSize.Value;
201      }
202
203      //if (FitnessPartition.Size > SlidingWindowSize.Value) {
204      //  FitnessPartition.Start += SlidingWindowStepWidth.Value;
205      //} else if (FitnessPartition.Size == SlidingWindowSize.Value) {
206      //  FitnessPartition.Start += SlidingWindowStepWidth.Value;
207      //  FitnessPartition.End += SlidingWindowStepWidth.Value;
208      //} else {
209      //  FitnessPartition.End += SlidingWindowStepWidth.Value;
210      //  if (FitnessPartition.Size > SlidingWindowSize.Value) {
211      //    FitnessPartition.Start = FitnessPartition.End - FitnessPartition.Size;
212      //  }
213      //}   
214      return true;
215    }
216
217    #endregion
218
219    #region constructors
220    [StorableConstructor]
221    protected Datastream(bool deserializing) : base(deserializing) { }
222
223    [StorableHook(HookType.AfterDeserialization)]
224    private void AfterDeserialization() {
225      RegisterParameterEvents();
226      RecoverState();
227    }
228
229    protected Datastream(Datastream original, Cloner cloner) : base(original, cloner) {
230      RegisterParameterEvents();
231      RecoverState();
232    }
233
234    public override IDeepCloneable Clone(Cloner cloner) {
235      return new Datastream(this, cloner);
236    }
237
238    public Datastream() : base() {
239      Parameters.Add(new ValueParameter<RegressionProblemData>(ProblemDataParameterName, "ProblemData for analysis with selected ensembles.", null));
240      Parameters.Add(new FixedValueParameter<StringValue>(DataSourceAddressParameterName, "Source address of data stream", new StringValue("")));
241      Parameters.Add(new FixedValueParameter<IntRange>(InitialSlidingWindowParameterName, "Initial sliding window boundaries", new IntRange(0, 100)));
242      Parameters.Add(new FixedValueParameter<IntValue>(SlidingWindowSizeParameterName, "Sliding window size", new IntValue(100)));
243      Parameters.Add(new FixedValueParameter<IntValue>(SlidingWindowStepWidthParameterName, "Sliding window step width", new IntValue(1)));
244      Parameters.Add(new FixedValueParameter<IntValue>(SlidingWindowMovementDelayParameterName, "Sliding window movement delay interval (milliseconds)", new IntValue(0)));
245      Parameters.Add(new FixedValueParameter<DoubleValue>(SlidingWindowSonarRatioParameterName, "Sliding window sonar ratio", new DoubleValue(0.25)));
246      Parameters.Add(new FixedValueParameter<EnumValue<EvaluationScheme>>(SlidingWindowEvaluationSchemeParameterName, "Sliding window evaluation scheme", new EnumValue<EvaluationScheme>(EvaluationScheme.none)));
247      RegisterParameterEvents();
248      InitializeState();
249    }
250    #endregion
251
252    public void InitializeState() {
253      if (ProblemData == null || ProblemData.Dataset == null || ProblemData.Dataset.Rows == 0) {
254        FitnessPartition = new IntRange(0, 0);
255      } else {
256        FitnessPartition = (IntRange)InitialSlidingWindow.Clone();
257        if (FitnessPartition.End > ProblemData.Dataset.Rows) {
258          FitnessPartition.End = ProblemData.Dataset.Rows;
259        }
260      }
261    }
262
263    public void RecoverState() {
264      if (ProblemData == null || ProblemData.Dataset == null || ProblemData.Dataset.Rows == 0) {
265        FitnessPartition = new IntRange(0, 0);
266      } else {
267        if (FitnessPartition == null) FitnessPartition = (IntRange)InitialSlidingWindow.Clone();
268        if (FitnessPartition.End > ProblemData.Dataset.Rows) {
269          FitnessPartition.End = ProblemData.Dataset.Rows;
270        }
271      }
272    }
273
274    public event EventHandler ProblemDataChanged;
275    public event EventHandler Reset;
276    public event EventHandler SlidingWindowChanged;
277
278
279    //protected virtual void OnProblemDataChanged() {
280    //  EventHandler handler = ProblemDataChanged;
281    //  if(handler != null) handler(this, EventArgs.Empty);
282    //}
283
284    //protected virtual void OnReset() {
285    //  EventHandler handler = Reset;
286    //  if (handler != null) handler(this, EventArgs.Empty);
287    //}
288
289    private void RegisterParameterEvents() {
290      ProblemDataParameter.ValueChanged += new EventHandler(ProblemData_ValueChanged);
291      //SlidingWindowSizeParameter.ValueChanged += new EventHandler(SlidingWindow_Changed);
292      InitialSlidingWindowParameter.ValueChanged += new EventHandler(SlidingWindow_Changed);
293    }
294
295    private void ProblemData_ValueChanged(object sender, EventArgs e) {
296      if (e == null) return;
297
298      InitializeState();
299      DatastreamAnalysisUtil.RaiseEvent(this, ProblemDataChanged);
300    }
301
302    private void SlidingWindow_Changed(object sender, EventArgs e) {
303      InitializeState();
304    }
305
306
307    // TODO
308    public IEnumerable<IParameterizedItem> ExecutionContextItems { get; }
309
310  }
311}
Note: See TracBrowser for help on using the repository browser.