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