Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis/3.3/Symbolic/SymbolicTimeSeriesPrognosisSolution.cs @ 4401

Last change on this file since 4401 was 4401, checked in by gkronber, 14 years ago

Added model and solution classes for time series prognosis and added views for time series prognosis solutions. #1142

File size: 6.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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
22using System.Collections.Generic;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis.Symbolic.Symbols;
29using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
30using System.Linq;
31using System.Drawing;
32using System;
33
34namespace HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis.Symbolic {
35  [StorableClass]
36  [Item("SymbolicTimeSeriesPrognosisSolution", "Represents a solution for time series prognosis.")]
37  public class SymbolicTimeSeriesPrognosisSolution : NamedItem, IMultiVariateDataAnalysisSolution {
38    [Storable]
39    private MultiVariateDataAnalysisProblemData problemData;
40    [Storable]
41    private SymbolicTimeSeriesPrognosisModel model;
42    [Storable]
43    private int horizon;
44
45    [StorableConstructor]
46    protected SymbolicTimeSeriesPrognosisSolution(bool deserializing) : base(deserializing) { }
47
48    public SymbolicTimeSeriesPrognosisSolution() {
49      horizon = 1;
50    }
51
52    public SymbolicTimeSeriesPrognosisSolution(MultiVariateDataAnalysisProblemData problemData, SymbolicTimeSeriesPrognosisModel model, int horizon)
53      : this() {
54      this.problemData = problemData;
55      this.model = model;
56      this.horizon = horizon;
57    }
58
59    [StorableHook(HookType.AfterDeserialization)]
60    private void Initialize() {
61      if (problemData != null)
62        RegisterProblemDataEvents();
63    }
64
65    public override Image ItemImage {
66      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Function; }
67    }
68
69    public int Horizon {
70      get { return horizon; }
71      set {
72        if (value <= 0) throw new ArgumentException();
73        horizon = value;
74      }
75    }
76
77    public SymbolicTimeSeriesPrognosisModel Model {
78      get { return model; }
79      set {
80        if (model != value) {
81          if (value == null) throw new ArgumentNullException();
82          model = value;
83          RaiseModelChanged();
84        }
85      }
86    }
87
88
89    public IEnumerable<double[]> GetPrognosis(int t) {
90      return model.GetEstimatedValues(problemData, t, t + 1);
91    }
92
93    #region IMultiVariateDataAnalysisSolution Members
94
95    public MultiVariateDataAnalysisProblemData ProblemData {
96      get { return problemData; }
97      set {
98        if (problemData != value) {
99          if (value == null) throw new ArgumentNullException();
100          if (model != null && problemData != null &&
101            !(problemData.InputVariables
102              .Select(c => c.Value)
103              .SequenceEqual(value.InputVariables
104                             .Select(c => c.Value)) &&
105             problemData.TargetVariables
106             .Select(c => c.Value)
107             .SequenceEqual(value.TargetVariables
108                            .Select(c => c.Value)))) {
109            throw new ArgumentException("Could not set new problem data with different structure");
110          }
111
112          if (problemData != null) DeregisterProblemDataEvents();
113          problemData = value;
114          RaiseProblemDataChanged();
115          RegisterProblemDataEvents();
116        }
117      }
118    }
119
120    IMultiVariateDataAnalysisModel IMultiVariateDataAnalysisSolution.Model {
121      get { return model; }
122    }
123
124    public IEnumerable<double[]> EstimatedValues {
125      get {
126        return model.GetEstimatedValues(problemData, 0, problemData.Dataset.Rows);
127      }
128    }
129
130    public IEnumerable<double[]> EstimatedTrainingValues {
131      get {
132        return model.GetEstimatedValues(problemData, problemData.TrainingSamplesStart.Value, problemData.TrainingSamplesEnd.Value);
133      }
134    }
135
136    public IEnumerable<double[]> EstimatedTestValues {
137      get {
138        return model.GetEstimatedValues(problemData, problemData.TestSamplesStart.Value, problemData.TestSamplesEnd.Value);
139      }
140    }
141
142    #endregion
143
144
145    #region Events
146    protected virtual void RegisterProblemDataEvents() {
147      ProblemData.ProblemDataChanged += new EventHandler(ProblemData_Changed);
148    }
149    protected virtual void DeregisterProblemDataEvents() {
150      ProblemData.ProblemDataChanged += new EventHandler(ProblemData_Changed);
151    }
152    private void ProblemData_Changed(object sender, EventArgs e) {
153      RaiseProblemDataChanged();
154    }
155
156    public event EventHandler ProblemDataChanged;
157    protected virtual void RaiseProblemDataChanged() {
158      var listeners = ProblemDataChanged;
159      if (listeners != null)
160        listeners(this, EventArgs.Empty);
161    }
162
163    public event EventHandler ModelChanged;
164    protected virtual void RaiseModelChanged() {
165      EventHandler handler = ModelChanged;
166      if (handler != null)
167        handler(this, EventArgs.Empty);
168    }
169
170    public event EventHandler EstimatedValuesChanged;
171    protected virtual void RaiseEstimatedValuesChanged() {
172      var listeners = EstimatedValuesChanged;
173      if (listeners != null)
174        listeners(this, EventArgs.Empty);
175    }
176    #endregion
177
178
179    public override IDeepCloneable Clone(Cloner cloner) {
180      SymbolicTimeSeriesPrognosisSolution clone = (SymbolicTimeSeriesPrognosisSolution)base.Clone(cloner);
181      clone.problemData = (MultiVariateDataAnalysisProblemData)cloner.Clone(problemData);
182      clone.model = (SymbolicTimeSeriesPrognosisModel)cloner.Clone(model);
183      clone.horizon = horizon;
184      return clone;
185    }
186  }
187}
Note: See TracBrowser for help on using the repository browser.