#region License Information /* HeuristicLab * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Linq; using System.Text; using HeuristicLab.Collections; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Problems.DataAnalysis; namespace HeuristicLab.DatastreamAnalysis { [StorableClass] [Item("Datastream", "Represents an item containing a data source and reading options for a certain data stream.")] [Creatable(CreatableAttribute.Categories.Problems)] public class Datastream : ParameterizedNamedItem, IDatastream { protected const string ProblemDataParameterName = "ProblemData"; protected const string DataSourceAddressParameterName = "DataSourceAdress"; protected const string InitialSlidingWindowParameterName = "InitialSlidingWindowParameter"; protected const string SlidingWindowSizeParameterName = "SlidingWindowSize"; protected const string SlidingWindowStepWidthParameterName = "SlidingWindowStepWidth"; protected const string SlidingWindowMovementDelayParameterName = "SlidingWindowMovementDelay"; protected const string FitnessPartitionParameterName = "FitnessPartition"; protected const string SlidingWindowSonarRatioParameterName = "SlidingWindowSonarRatio"; protected const string SlidingWindowEvaluationSchemeParameterName = "SlidingWindowEvaluationScheme"; #region parameter properites public enum EvaluationScheme { none, equal, linear, quadratic, cubic, exponential } public IValueParameter ProblemDataParameter { get { return (IValueParameter)Parameters[ProblemDataParameterName]; } } public IValueParameter DataSourceAddressParameter { get { return (IValueParameter)Parameters[DataSourceAddressParameterName]; } } public IValueParameter InitialSlidingWindowParameter { get { return (IValueParameter)Parameters[InitialSlidingWindowParameterName]; } } public IValueParameter SlidingWindowSizeParameter { get { return (IValueParameter)Parameters[SlidingWindowSizeParameterName]; } } public IValueParameter SlidingWindowStepWidthParameter { get { return (IValueParameter)Parameters[SlidingWindowStepWidthParameterName]; } } public IValueParameter SlidingWindowMovementDelayParameter { get { return (IValueParameter)Parameters[SlidingWindowMovementDelayParameterName]; } } public IValueParameter SlidingWindowSonarRatioParameter { get { return (IValueParameter)Parameters[SlidingWindowSonarRatioParameterName]; } } public IValueParameter> SlidingWindowEvaluationSchemeParameter { get { return (IValueParameter>)Parameters[SlidingWindowEvaluationSchemeParameterName]; } } //public IValueParameter FitnessPartitionParameter { // get { return (IValueParameter) Parameters[FitnessPartitionParameterName]; } //} #endregion #region properties public RegressionProblemData ProblemData { get { return ProblemDataParameter.Value; } set { if (value == null) throw new ArgumentNullException("ProblemData", "The provided value for problemData is null."); ProblemDataParameter.Value = value; //OnProblemDataChanged(); } } public StringValue DataSourceAddress { get { return DataSourceAddressParameter.Value; } set { DataSourceAddressParameter.Value = value; } } public IntRange InitialSlidingWindow { get { return InitialSlidingWindowParameter.Value; } set { InitialSlidingWindowParameter.Value = value; } } public IntValue SlidingWindowSize { get { return SlidingWindowSizeParameter.Value; } set { SlidingWindowSizeParameter.Value = value; } } public IntValue SlidingWindowStepWidth { get { return SlidingWindowStepWidthParameter.Value; } set { SlidingWindowStepWidthParameter.Value = value; } } public IntValue SlidingWindowMovementDelay { get { return SlidingWindowMovementDelayParameter.Value; } set { SlidingWindowMovementDelayParameter.Value = value; } } public DoubleValue SlidingWindowSonarRatio { get { return SlidingWindowSonarRatioParameter.Value; } set { SlidingWindowSonarRatioParameter.Value = value; } } public EnumValue SlidingWindowEvaluationScheme { get { return SlidingWindowEvaluationSchemeParameter.Value; } set { SlidingWindowEvaluationSchemeParameter.Value = value; } } #endregion #region internal properties public IntRange FitnessPartition { get; set; } public bool UpdateAvailable { get { return !String.IsNullOrWhiteSpace(DataSourceAddress.Value); } } public bool SlidingWindowMovementPossible { get { return ProblemData != null && ProblemData.Dataset != null && ( (FitnessPartition.End + SlidingWindowStepWidth.Value <= ProblemData.Dataset.Rows) // shift end || (FitnessPartition.Size > SlidingWindowSize.Value && (FitnessPartition.Start + SlidingWindowStepWidth.Value) < FitnessPartition.End) // shift start ); } } public bool SlidingWindowEvaluationPossible { get { //return ProblemData != null && ProblemData.Dataset != null && ProblemData.Dataset.Rows > 0 && FitnessPartition.Size > 0; return FitnessPartition.Size > 0; } } public bool MoveSlidingWindow() { if (ProblemData?.Dataset == null || ProblemData.Dataset.Rows == 0) return false; if (FitnessPartition.End + SlidingWindowStepWidth.Value > ProblemData.Dataset.Rows) return false; if (FitnessPartition.Size == SlidingWindowSize.Value) { FitnessPartition.Start += SlidingWindowStepWidth.Value; FitnessPartition.End += SlidingWindowStepWidth.Value; } else { FitnessPartition.End += SlidingWindowStepWidth.Value; FitnessPartition.Start += FitnessPartition.End - SlidingWindowSize.Value; } //if (FitnessPartition.Size > SlidingWindowSize.Value) { // FitnessPartition.Start += SlidingWindowStepWidth.Value; //} else if (FitnessPartition.Size == SlidingWindowSize.Value) { // FitnessPartition.Start += SlidingWindowStepWidth.Value; // FitnessPartition.End += SlidingWindowStepWidth.Value; //} else { // FitnessPartition.End += SlidingWindowStepWidth.Value; // if (FitnessPartition.Size > SlidingWindowSize.Value) { // FitnessPartition.Start = FitnessPartition.End - FitnessPartition.Size; // } //} return true; } #endregion #region constructors [StorableConstructor] protected Datastream(bool deserializing) : base(deserializing) { } [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { RegisterParameterEvents(); RecoverState(); } protected Datastream(Datastream original, Cloner cloner) : base(original, cloner) { RegisterParameterEvents(); RecoverState(); } public override IDeepCloneable Clone(Cloner cloner) { return new Datastream(this, cloner); } public Datastream() : base() { Parameters.Add(new ValueParameter(ProblemDataParameterName, "ProblemData for analysis with selected ensembles.", null)); Parameters.Add(new FixedValueParameter(DataSourceAddressParameterName, "Source address of data stream", new StringValue(""))); Parameters.Add(new FixedValueParameter(InitialSlidingWindowParameterName, "Initial sliding window boundaries", new IntRange(0, 100))); Parameters.Add(new FixedValueParameter(SlidingWindowSizeParameterName, "Sliding window size", new IntValue(100))); Parameters.Add(new FixedValueParameter(SlidingWindowStepWidthParameterName, "Sliding window step width", new IntValue(1))); Parameters.Add(new FixedValueParameter(SlidingWindowMovementDelayParameterName, "Sliding window movement delay interval (milliseconds)", new IntValue(0))); Parameters.Add(new FixedValueParameter(SlidingWindowSonarRatioParameterName, "Sliding window sonar ratio", new DoubleValue(0.25))); Parameters.Add(new FixedValueParameter>(SlidingWindowEvaluationSchemeParameterName, "Sliding window evaluation scheme", new EnumValue(EvaluationScheme.none))); RegisterParameterEvents(); InitializeState(); } #endregion public void InitializeState() { if (ProblemData == null || ProblemData.Dataset == null || ProblemData.Dataset.Rows == 0) { FitnessPartition = new IntRange(0, 0); } else { FitnessPartition = (IntRange)InitialSlidingWindow.Clone(); if (FitnessPartition.End > ProblemData.Dataset.Rows) { FitnessPartition.End = ProblemData.Dataset.Rows; } } } public void RecoverState() { if (ProblemData == null || ProblemData.Dataset == null || ProblemData.Dataset.Rows == 0) { FitnessPartition = new IntRange(0, 0); } else { if (FitnessPartition == null) FitnessPartition = (IntRange)InitialSlidingWindow.Clone(); if (FitnessPartition.End > ProblemData.Dataset.Rows) { FitnessPartition.End = ProblemData.Dataset.Rows; } } } public event EventHandler ProblemDataChanged; public event EventHandler Reset; public event EventHandler SlidingWindowChanged; //protected virtual void OnProblemDataChanged() { // EventHandler handler = ProblemDataChanged; // if(handler != null) handler(this, EventArgs.Empty); //} //protected virtual void OnReset() { // EventHandler handler = Reset; // if (handler != null) handler(this, EventArgs.Empty); //} private void RegisterParameterEvents() { ProblemDataParameter.ValueChanged += new EventHandler(ProblemData_ValueChanged); //SlidingWindowSizeParameter.ValueChanged += new EventHandler(SlidingWindow_Changed); InitialSlidingWindowParameter.ValueChanged += new EventHandler(SlidingWindow_Changed); } private void ProblemData_ValueChanged(object sender, EventArgs e) { if (e == null) return; InitializeState(); DatastreamAnalysisUtil.RaiseEvent(this, ProblemDataChanged); } private void SlidingWindow_Changed(object sender, EventArgs e) { InitializeState(); } // TODO public IEnumerable ExecutionContextItems { get; } } }