Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2719: added proxy ensemble model

File size: 11.8 KB
RevLine 
[14536]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";
[15867]41    protected const string DataSourceAddressParameterName = "DataSourceAdress";
[14536]42    protected const string InitialSlidingWindowParameterName = "InitialSlidingWindowParameter";
43    protected const string SlidingWindowSizeParameterName = "SlidingWindowSize";
44    protected const string SlidingWindowStepWidthParameterName = "SlidingWindowStepWidth";
[14710]45    protected const string SlidingWindowMovementDelayParameterName = "SlidingWindowMovementDelay";
[14538]46    protected const string FitnessPartitionParameterName = "FitnessPartition";
[14710]47    protected const string SlidingWindowSonarRatioParameterName = "SlidingWindowSonarRatio";
[15867]48    protected const string SlidingWindowEvaluationSchemeParameterName = "SlidingWindowEvaluationScheme";
[14536]49
50    #region parameter properites
51
[15867]52    public enum EvaluationScheme { none, equal, linear, quadratic, cubic, exponential }
53
54    public IValueParameter<RegressionProblemData> ProblemDataParameter
55    {
56      get { return (IValueParameter<RegressionProblemData>)Parameters[ProblemDataParameterName]; }
[14536]57    }
58
[15867]59    public IValueParameter<StringValue> DataSourceAddressParameter
60    {
61      get { return (IValueParameter<StringValue>)Parameters[DataSourceAddressParameterName]; }
[14536]62    }
63
[15867]64    public IValueParameter<IntRange> InitialSlidingWindowParameter
65    {
66      get { return (IValueParameter<IntRange>)Parameters[InitialSlidingWindowParameterName]; }
[14536]67    }
68
[15867]69    public IValueParameter<IntValue> SlidingWindowSizeParameter
70    {
71      get { return (IValueParameter<IntValue>)Parameters[SlidingWindowSizeParameterName]; }
[14536]72    }
73
[15867]74    public IValueParameter<IntValue> SlidingWindowStepWidthParameter
75    {
76      get { return (IValueParameter<IntValue>)Parameters[SlidingWindowStepWidthParameterName]; }
[14536]77    }
78
[15867]79    public IValueParameter<IntValue> SlidingWindowMovementDelayParameter
80    {
81      get { return (IValueParameter<IntValue>)Parameters[SlidingWindowMovementDelayParameterName]; }
[14710]82    }
83
[15867]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
[14538]97    //public IValueParameter<IntRange> FitnessPartitionParameter {
98    //  get { return (IValueParameter<IntRange>) Parameters[FitnessPartitionParameterName]; }
99    //}
[14536]100
101    #endregion
102
103    #region properties
104
[15867]105    public RegressionProblemData ProblemData
106    {
[14536]107      get { return ProblemDataParameter.Value; }
[15867]108      set
109      {
110        if (value == null) throw new ArgumentNullException("ProblemData", "The provided value for problemData is null.");
[14536]111        ProblemDataParameter.Value = value;
[14538]112        //OnProblemDataChanged();
[14536]113      }
114    }
115
[15867]116    public StringValue DataSourceAddress
117    {
118      get { return DataSourceAddressParameter.Value; }
119      set { DataSourceAddressParameter.Value = value; }
120    }
121
122    public IntRange InitialSlidingWindow
123    {
[14536]124      get { return InitialSlidingWindowParameter.Value; }
[14538]125      set { InitialSlidingWindowParameter.Value = value; }
[14536]126    }
127
[15867]128    public IntValue SlidingWindowSize
129    {
[14536]130      get { return SlidingWindowSizeParameter.Value; }
[14538]131      set { SlidingWindowSizeParameter.Value = value; }
[14536]132    }
133
[15867]134    public IntValue SlidingWindowStepWidth
135    {
[14536]136      get { return SlidingWindowStepWidthParameter.Value; }
[14538]137      set { SlidingWindowStepWidthParameter.Value = value; }
[14536]138    }
139
[15867]140    public IntValue SlidingWindowMovementDelay
141    {
[14710]142      get { return SlidingWindowMovementDelayParameter.Value; }
143      set { SlidingWindowMovementDelayParameter.Value = value; }
[14536]144    }
145
[15867]146    public DoubleValue SlidingWindowSonarRatio
147    {
[14710]148      get { return SlidingWindowSonarRatioParameter.Value; }
149      set { SlidingWindowSonarRatioParameter.Value = value; }
150    }
151
[15867]152    public EnumValue<EvaluationScheme> SlidingWindowEvaluationScheme
153    {
154      get { return SlidingWindowEvaluationSchemeParameter.Value; }
155      set { SlidingWindowEvaluationSchemeParameter.Value = value; }
156    }
157
158
[14536]159    #endregion
160
161    #region internal properties
162
163    public IntRange FitnessPartition { get; set; }
164
[15867]165    public bool UpdateAvailable
166    {
167      get { return !String.IsNullOrWhiteSpace(DataSourceAddress.Value); }
[14538]168    }
169
[15867]170    public bool SlidingWindowMovementPossible
171    {
172      get
173      {
[14536]174        return ProblemData != null && ProblemData.Dataset != null &&
[14538]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               );
[14536]179      }
180    }
181
[15867]182    public bool SlidingWindowEvaluationPossible
183    {
184      get
185      {
[14538]186        //return ProblemData != null && ProblemData.Dataset != null && ProblemData.Dataset.Rows > 0 && FitnessPartition.Size > 0;
187        return FitnessPartition.Size > 0;
[14536]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;
[14538]194
195      if (FitnessPartition.Size == SlidingWindowSize.Value) {
[14536]196        FitnessPartition.Start += SlidingWindowStepWidth.Value;
197        FitnessPartition.End += SlidingWindowStepWidth.Value;
[15867]198      } else {
[14536]199        FitnessPartition.End += SlidingWindowStepWidth.Value;
[14538]200        FitnessPartition.Start += FitnessPartition.End - SlidingWindowSize.Value;
[15867]201      }
[14538]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      //}   
[14536]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();
[14538]226      RecoverState();
[14536]227    }
228
229    protected Datastream(Datastream original, Cloner cloner) : base(original, cloner) {
230      RegisterParameterEvents();
[14538]231      RecoverState();
[14536]232    }
233
234    public override IDeepCloneable Clone(Cloner cloner) {
235      return new Datastream(this, cloner);
236    }
237
[14538]238    public Datastream() : base() {
[14536]239      Parameters.Add(new ValueParameter<RegressionProblemData>(ProblemDataParameterName, "ProblemData for analysis with selected ensembles.", null));
[15867]240      Parameters.Add(new FixedValueParameter<StringValue>(DataSourceAddressParameterName, "Source address of data stream", new StringValue("")));
[14710]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)));
[14536]243      Parameters.Add(new FixedValueParameter<IntValue>(SlidingWindowStepWidthParameterName, "Sliding window step width", new IntValue(1)));
[14710]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)));
[15867]246      Parameters.Add(new FixedValueParameter<EnumValue<EvaluationScheme>>(SlidingWindowEvaluationSchemeParameterName, "Sliding window evaluation scheme", new EnumValue<EvaluationScheme>(EvaluationScheme.none)));
[14538]247      RegisterParameterEvents();
[14536]248      InitializeState();
249    }
250    #endregion
251
[15867]252    public void InitializeState() {
[14536]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
[14538]263    public void RecoverState() {
264      if (ProblemData == null || ProblemData.Dataset == null || ProblemData.Dataset.Rows == 0) {
265        FitnessPartition = new IntRange(0, 0);
266      } else {
[15867]267        if (FitnessPartition == null) FitnessPartition = (IntRange)InitialSlidingWindow.Clone();
[14538]268        if (FitnessPartition.End > ProblemData.Dataset.Rows) {
269          FitnessPartition.End = ProblemData.Dataset.Rows;
270        }
271      }
272    }
273
[14536]274    public event EventHandler ProblemDataChanged;
[14538]275    public event EventHandler Reset;
276    public event EventHandler SlidingWindowChanged;
[14536]277
278
[14538]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
[14536]289    private void RegisterParameterEvents() {
290      ProblemDataParameter.ValueChanged += new EventHandler(ProblemData_ValueChanged);
[14538]291      //SlidingWindowSizeParameter.ValueChanged += new EventHandler(SlidingWindow_Changed);
292      InitialSlidingWindowParameter.ValueChanged += new EventHandler(SlidingWindow_Changed);
[14536]293    }
294
295    private void ProblemData_ValueChanged(object sender, EventArgs e) {
296      if (e == null) return;
297
[14538]298      InitializeState();
[14536]299      DatastreamAnalysisUtil.RaiseEvent(this, ProblemDataChanged);
300    }
301
[14538]302    private void SlidingWindow_Changed(object sender, EventArgs e) {
303      InitializeState();
304    }
305
306
307    // TODO
308    public IEnumerable<IParameterizedItem> ExecutionContextItems { get; }
[15867]309
[14536]310  }
311}
Note: See TracBrowser for help on using the repository browser.