#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; 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; public string Filename { get; set; } [StorableConstructor] protected SymbolicTimeSeriesPrognosisSolution(bool deserializing) : base(deserializing) { } public SymbolicTimeSeriesPrognosisSolution() { horizon = 1; } public SymbolicTimeSeriesPrognosisSolution(MultiVariateDataAnalysisProblemData problemData, SymbolicTimeSeriesPrognosisModel model, int horizon, string conditionalEvaluationVariable) : this() { this.problemData = problemData; this.model = model; this.horizon = horizon; this.conditionalEvaluationVariable = conditionalEvaluationVariable; } [StorableHook(HookType.AfterDeserialization)] private void Initialize() { 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 IEnumerable GetPrognosis(int t) { return model.GetEstimatedValues(problemData, t, t + 1); } #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 model.GetEstimatedValues(problemData, 0, problemData.Dataset.Rows); } } public IEnumerable EstimatedTrainingValues { get { return model.GetEstimatedValues(problemData, problemData.TrainingSamplesStart.Value, problemData.TrainingSamplesEnd.Value); } } public IEnumerable EstimatedTestValues { get { return 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 public override IDeepCloneable Clone(Cloner cloner) { SymbolicTimeSeriesPrognosisSolution clone = (SymbolicTimeSeriesPrognosisSolution)base.Clone(cloner); clone.problemData = (MultiVariateDataAnalysisProblemData)cloner.Clone(problemData); clone.model = (SymbolicTimeSeriesPrognosisModel)cloner.Clone(model); clone.horizon = horizon; clone.conditionalEvaluationVariable = conditionalEvaluationVariable; return clone; } } }