#region License Information /* HeuristicLab * Copyright (C) 2002-2010 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.Collections.Generic; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis.Symbolic.Symbols; using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols; using System.Linq; using System.Drawing; using System; using HeuristicLab.Data; namespace HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis.Symbolic { [StorableClass] [Item("SymbolicTimeSeriesPrognosisSolution", "Represents a solution for time series prognosis.")] public class SymbolicTimeSeriesPrognosisSolution : NamedItem, IMultiVariateDataAnalysisSolution, IStorableContent { [Storable] private MultiVariateDataAnalysisProblemData problemData; [Storable] private SymbolicTimeSeriesPrognosisModel model; [Storable] private int horizon; [Storable] private string conditionalEvaluationVariable; [Storable] private double[] lowerEstimationLimit; [Storable] private double[] upperEstimationLimit; public string Filename { get; set; } [StorableConstructor] protected SymbolicTimeSeriesPrognosisSolution(bool deserializing) : base(deserializing) { } protected SymbolicTimeSeriesPrognosisSolution(SymbolicTimeSeriesPrognosisSolution original, Cloner cloner) : base(original, cloner) { problemData = (MultiVariateDataAnalysisProblemData)cloner.Clone(original.problemData); model = (SymbolicTimeSeriesPrognosisModel)cloner.Clone(original.model); horizon = original.horizon; conditionalEvaluationVariable = original.conditionalEvaluationVariable; lowerEstimationLimit = (double[])original.lowerEstimationLimit.Clone(); upperEstimationLimit = (double[])original.upperEstimationLimit.Clone(); } public SymbolicTimeSeriesPrognosisSolution() { horizon = 1; } public SymbolicTimeSeriesPrognosisSolution(MultiVariateDataAnalysisProblemData problemData, SymbolicTimeSeriesPrognosisModel model, int horizon, string conditionalEvaluationVariable, double[] lowerEstimationLimit, double[] upperEstimationLimit) : this() { this.problemData = problemData; this.model = model; this.horizon = horizon; this.conditionalEvaluationVariable = conditionalEvaluationVariable; this.lowerEstimationLimit = (double[])lowerEstimationLimit.Clone(); this.upperEstimationLimit = (double[])upperEstimationLimit.Clone(); } public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicTimeSeriesPrognosisSolution(this, cloner); } [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { if (problemData != null) RegisterProblemDataEvents(); } public override Image ItemImage { get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Function; } } public int Horizon { get { return horizon; } set { if (value <= 0) throw new ArgumentException(); horizon = value; } } public SymbolicTimeSeriesPrognosisModel Model { get { return model; } set { if (model != value) { if (value == null) throw new ArgumentNullException(); model = value; RaiseModelChanged(); } } } public string ConditionalEvaluationVariable { get { return conditionalEvaluationVariable; } set { if (conditionalEvaluationVariable != value) { conditionalEvaluationVariable = value; RaiseEstimatedValuesChanged(); } } } public double GetLowerEstimationLimit(int i) { return lowerEstimationLimit[i]; } public double GetUpperEstimationLimit(int i) { return upperEstimationLimit[i]; } public IEnumerable GetPrognosis(int t) { return model.GetEstimatedValues(problemData, t, t + 1, horizon); } #region IMultiVariateDataAnalysisSolution Members public MultiVariateDataAnalysisProblemData ProblemData { get { return problemData; } set { if (problemData != value) { if (value == null) throw new ArgumentNullException(); if (model != null && problemData != null && !(problemData.InputVariables .Select(c => c.Value) .SequenceEqual(value.InputVariables .Select(c => c.Value)) && problemData.TargetVariables .Select(c => c.Value) .SequenceEqual(value.TargetVariables .Select(c => c.Value)))) { throw new ArgumentException("Could not set new problem data with different structure"); } if (problemData != null) DeregisterProblemDataEvents(); problemData = value; RaiseProblemDataChanged(); RegisterProblemDataEvents(); } } } IMultiVariateDataAnalysisModel IMultiVariateDataAnalysisSolution.Model { get { return model; } } public IEnumerable EstimatedValues { get { return ApplyEstimationLimits(model.GetEstimatedValues(problemData, 0, problemData.Dataset.Rows)); } } public IEnumerable EstimatedTrainingValues { get { return ApplyEstimationLimits(model.GetEstimatedValues(problemData, problemData.TrainingSamplesStart.Value, problemData.TrainingSamplesEnd.Value)); } } public IEnumerable EstimatedTestValues { get { return ApplyEstimationLimits(model.GetEstimatedValues(problemData, problemData.TestSamplesStart.Value, problemData.TestSamplesEnd.Value)); } } #endregion #region Events protected virtual void RegisterProblemDataEvents() { ProblemData.ProblemDataChanged += new EventHandler(ProblemData_Changed); } protected virtual void DeregisterProblemDataEvents() { ProblemData.ProblemDataChanged += new EventHandler(ProblemData_Changed); } private void ProblemData_Changed(object sender, EventArgs e) { RaiseProblemDataChanged(); } public event EventHandler ProblemDataChanged; protected virtual void RaiseProblemDataChanged() { var listeners = ProblemDataChanged; if (listeners != null) listeners(this, EventArgs.Empty); } public event EventHandler ModelChanged; protected virtual void RaiseModelChanged() { EventHandler handler = ModelChanged; if (handler != null) handler(this, EventArgs.Empty); } public event EventHandler EstimatedValuesChanged; protected virtual void RaiseEstimatedValuesChanged() { var listeners = EstimatedValuesChanged; if (listeners != null) listeners(this, EventArgs.Empty); } #endregion private IEnumerable ApplyEstimationLimits(IEnumerable values) { foreach (var xs in values) { for (int i = 0; i < xs.Length; i++) { if (double.IsNaN(xs[i])) { xs[i] = (upperEstimationLimit[i] - lowerEstimationLimit[i]) / 2.0 + lowerEstimationLimit[i]; } else { xs[i] = Math.Max(lowerEstimationLimit[i], Math.Min(upperEstimationLimit[i], xs[i])); } } yield return xs; } } } }