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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.Collections;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Data;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 | using HeuristicLab.Problems.DataAnalysis;
|
---|
33 |
|
---|
34 | namespace 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 |
|
---|
46 | #region parameter properites
|
---|
47 |
|
---|
48 | public IValueParameter<RegressionProblemData> ProblemDataParameter {
|
---|
49 | get { return (IValueParameter<RegressionProblemData>) Parameters[ProblemDataParameterName]; }
|
---|
50 | }
|
---|
51 |
|
---|
52 | public IFixedValueParameter<IntRange> InitialSlidingWindowParameter {
|
---|
53 | get { return (IFixedValueParameter<IntRange>) Parameters[InitialSlidingWindowParameterName]; }
|
---|
54 | }
|
---|
55 |
|
---|
56 | public IFixedValueParameter<IntValue> SlidingWindowSizeParameter {
|
---|
57 | get { return (IFixedValueParameter<IntValue>)Parameters[InitialSlidingWindowParameterName]; }
|
---|
58 | }
|
---|
59 |
|
---|
60 | public IFixedValueParameter<IntValue> SlidingWindowStepWidthParameter {
|
---|
61 | get { return (IFixedValueParameter<IntValue>)Parameters[SlidingWindowStepWidthParameterName]; }
|
---|
62 | }
|
---|
63 |
|
---|
64 | public IFixedValueParameter<IntValue> SlidingWindowMovementIntervalParameter {
|
---|
65 | get { return (IFixedValueParameter<IntValue>)Parameters[SlidingWindowMovementIntervalParameterName]; }
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 | #endregion
|
---|
70 |
|
---|
71 | #region properties
|
---|
72 |
|
---|
73 | public RegressionProblemData ProblemData {
|
---|
74 | get { return ProblemDataParameter.Value; }
|
---|
75 | set {
|
---|
76 | if(value == null) throw new ArgumentNullException("problemData", "The provided value for the problemData is null.");
|
---|
77 | if (value == ProblemData) return;
|
---|
78 | ProblemDataParameter.Value = value;
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | public IntRange InitialSlidingWindow {
|
---|
83 | get { return InitialSlidingWindowParameter.Value; }
|
---|
84 | }
|
---|
85 |
|
---|
86 | public IntValue SlidingWindowSize {
|
---|
87 | get { return SlidingWindowSizeParameter.Value; }
|
---|
88 | }
|
---|
89 |
|
---|
90 | public IntValue SlidingWindowStepWidth {
|
---|
91 | get { return SlidingWindowStepWidthParameter.Value; }
|
---|
92 | }
|
---|
93 |
|
---|
94 | public IntValue SlidingWindowMovementInterval {
|
---|
95 | get { return SlidingWindowMovementIntervalParameter.Value; }
|
---|
96 | }
|
---|
97 |
|
---|
98 | #endregion
|
---|
99 |
|
---|
100 | #region internal properties
|
---|
101 |
|
---|
102 | public IntRange FitnessPartition { get; set; }
|
---|
103 |
|
---|
104 | public bool SlidingWindowMovementPossible {
|
---|
105 | get {
|
---|
106 | return ProblemData != null && ProblemData.Dataset != null &&
|
---|
107 | FitnessPartition.End + SlidingWindowStepWidth.Value <= ProblemData.Dataset.Rows;
|
---|
108 | // TODO: add external datastream, hub, etc to condition
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | public bool SlidingWindowEvaluationPossible {
|
---|
113 | get {
|
---|
114 | return ProblemData != null && ProblemData.Dataset != null &&
|
---|
115 | FitnessPartition.Size > 0;
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | public bool MoveSlidingWindow() {
|
---|
120 | if (ProblemData?.Dataset == null || ProblemData.Dataset.Rows == 0) return false;
|
---|
121 | if (FitnessPartition.End + SlidingWindowStepWidth.Value > ProblemData.Dataset.Rows) return false;
|
---|
122 |
|
---|
123 | if (FitnessPartition.Size > SlidingWindowSize.Value) {
|
---|
124 | FitnessPartition.Start += SlidingWindowStepWidth.Value;
|
---|
125 | } else if (FitnessPartition.Size == SlidingWindowSize.Value) {
|
---|
126 | FitnessPartition.Start += SlidingWindowStepWidth.Value;
|
---|
127 | FitnessPartition.End += SlidingWindowStepWidth.Value;
|
---|
128 | } else {
|
---|
129 | FitnessPartition.End += SlidingWindowStepWidth.Value;
|
---|
130 | }
|
---|
131 | return true;
|
---|
132 | }
|
---|
133 |
|
---|
134 | #endregion
|
---|
135 |
|
---|
136 | #region constructors
|
---|
137 | [StorableConstructor]
|
---|
138 | protected Datastream(bool deserializing) : base(deserializing) { }
|
---|
139 |
|
---|
140 | [StorableHook(HookType.AfterDeserialization)]
|
---|
141 | private void AfterDeserialization() {
|
---|
142 | RegisterParameterEvents();
|
---|
143 | }
|
---|
144 |
|
---|
145 | protected Datastream(Datastream original, Cloner cloner) : base(original, cloner) {
|
---|
146 | RegisterParameterEvents();
|
---|
147 | }
|
---|
148 |
|
---|
149 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
150 | return new Datastream(this, cloner);
|
---|
151 | }
|
---|
152 |
|
---|
153 | public Datastream() {
|
---|
154 | Parameters.Add(new ValueParameter<RegressionProblemData>(ProblemDataParameterName, "ProblemData for analysis with selected ensembles.", null));
|
---|
155 | Parameters.Add(new FixedValueParameter<IntRange>(InitialSlidingWindowParameterName, "Initial sliding window boundaries", new IntRange(0, 1)));
|
---|
156 | Parameters.Add(new FixedValueParameter<IntValue>(SlidingWindowSizeParameterName, "Sliding window size", new IntValue(1)));
|
---|
157 | Parameters.Add(new FixedValueParameter<IntValue>(SlidingWindowStepWidthParameterName, "Sliding window step width", new IntValue(1)));
|
---|
158 | Parameters.Add(new FixedValueParameter<IntValue>(SlidingWindowMovementIntervalParameterName, "Sliding window movement interval (milliseconds)", new IntValue(1000)));
|
---|
159 | InitializeState();
|
---|
160 | }
|
---|
161 | #endregion
|
---|
162 |
|
---|
163 |
|
---|
164 | public void InitializeState() {
|
---|
165 | if (ProblemData == null || ProblemData.Dataset == null || ProblemData.Dataset.Rows == 0) {
|
---|
166 | FitnessPartition = new IntRange(0, 0);
|
---|
167 | } else {
|
---|
168 | FitnessPartition = (IntRange)InitialSlidingWindow.Clone();
|
---|
169 | if (FitnessPartition.End > ProblemData.Dataset.Rows) {
|
---|
170 | FitnessPartition.End = ProblemData.Dataset.Rows;
|
---|
171 | }
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | public event EventHandler ProblemDataChanged;
|
---|
176 |
|
---|
177 |
|
---|
178 | private void RegisterParameterEvents() {
|
---|
179 | ProblemDataParameter.ValueChanged += new EventHandler(ProblemData_ValueChanged);
|
---|
180 | }
|
---|
181 |
|
---|
182 | private void ProblemData_ValueChanged(object sender, EventArgs e) {
|
---|
183 | if (e == null) return;
|
---|
184 |
|
---|
185 | DatastreamAnalysisUtil.RaiseEvent(this, ProblemDataChanged);
|
---|
186 | }
|
---|
187 |
|
---|
188 |
|
---|
189 |
|
---|
190 | }
|
---|
191 | }
|
---|