Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4461 was 4461, checked in by gkronber, 12 years ago

made DataAnalysisProblem and DataAnalysisProblemData and SymbolicTimeSeriesPrognosisSolution savable and added field for conditional evaluation to Symbolic/SymbolicTimeSeriesPrognosisSolution (fix of changeset r4457). #1142

File size: 6.9 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, IStorableContent {
38    [Storable]
39    private MultiVariateDataAnalysisProblemData problemData;
40    [Storable]
41    private SymbolicTimeSeriesPrognosisModel model;
42    [Storable]
43    private int horizon;
44    [Storable]
45    private string conditionalEvaluationVariable;
46
47    public string Filename { get; set; }
48
49    [StorableConstructor]
50    protected SymbolicTimeSeriesPrognosisSolution(bool deserializing) : base(deserializing) { }
51
52    public SymbolicTimeSeriesPrognosisSolution() {
53      horizon = 1;
54    }
55
56    public SymbolicTimeSeriesPrognosisSolution(MultiVariateDataAnalysisProblemData problemData, SymbolicTimeSeriesPrognosisModel model, int horizon, string conditionalEvaluationVariable)
57      : this() {
58      this.problemData = problemData;
59      this.model = model;
60      this.horizon = horizon;
61      this.conditionalEvaluationVariable = conditionalEvaluationVariable;
62    }
63
64    [StorableHook(HookType.AfterDeserialization)]
65    private void Initialize() {
66      if (problemData != null)
67        RegisterProblemDataEvents();
68    }
69
70    public override Image ItemImage {
71      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Function; }
72    }
73
74    public int Horizon {
75      get { return horizon; }
76      set {
77        if (value <= 0) throw new ArgumentException();
78        horizon = value;
79      }
80    }
81
82    public SymbolicTimeSeriesPrognosisModel Model {
83      get { return model; }
84      set {
85        if (model != value) {
86          if (value == null) throw new ArgumentNullException();
87          model = value;
88          RaiseModelChanged();
89        }
90      }
91    }
92
93    public string ConditionalEvaluationVariable {
94      get { return conditionalEvaluationVariable; }
95      set {
96        if (conditionalEvaluationVariable != value) {
97          conditionalEvaluationVariable = value;
98          RaiseEstimatedValuesChanged();
99        }
100      }
101    }
102
103
104    public IEnumerable<double[]> GetPrognosis(int t) {
105      return model.GetEstimatedValues(problemData, t, t + 1);
106    }
107
108    #region IMultiVariateDataAnalysisSolution Members
109
110    public MultiVariateDataAnalysisProblemData ProblemData {
111      get { return problemData; }
112      set {
113        if (problemData != value) {
114          if (value == null) throw new ArgumentNullException();
115          if (model != null && problemData != null &&
116            !(problemData.InputVariables
117              .Select(c => c.Value)
118              .SequenceEqual(value.InputVariables
119                             .Select(c => c.Value)) &&
120             problemData.TargetVariables
121             .Select(c => c.Value)
122             .SequenceEqual(value.TargetVariables
123                            .Select(c => c.Value)))) {
124            throw new ArgumentException("Could not set new problem data with different structure");
125          }
126
127          if (problemData != null) DeregisterProblemDataEvents();
128          problemData = value;
129          RaiseProblemDataChanged();
130          RegisterProblemDataEvents();
131        }
132      }
133    }
134
135    IMultiVariateDataAnalysisModel IMultiVariateDataAnalysisSolution.Model {
136      get { return model; }
137    }
138
139    public IEnumerable<double[]> EstimatedValues {
140      get {
141        return model.GetEstimatedValues(problemData, 0, problemData.Dataset.Rows);
142      }
143    }
144
145    public IEnumerable<double[]> EstimatedTrainingValues {
146      get {
147        return model.GetEstimatedValues(problemData, problemData.TrainingSamplesStart.Value, problemData.TrainingSamplesEnd.Value);
148      }
149    }
150
151    public IEnumerable<double[]> EstimatedTestValues {
152      get {
153        return model.GetEstimatedValues(problemData, problemData.TestSamplesStart.Value, problemData.TestSamplesEnd.Value);
154      }
155    }
156
157    #endregion
158
159
160    #region Events
161    protected virtual void RegisterProblemDataEvents() {
162      ProblemData.ProblemDataChanged += new EventHandler(ProblemData_Changed);
163    }
164    protected virtual void DeregisterProblemDataEvents() {
165      ProblemData.ProblemDataChanged += new EventHandler(ProblemData_Changed);
166    }
167    private void ProblemData_Changed(object sender, EventArgs e) {
168      RaiseProblemDataChanged();
169    }
170
171    public event EventHandler ProblemDataChanged;
172    protected virtual void RaiseProblemDataChanged() {
173      var listeners = ProblemDataChanged;
174      if (listeners != null)
175        listeners(this, EventArgs.Empty);
176    }
177
178    public event EventHandler ModelChanged;
179    protected virtual void RaiseModelChanged() {
180      EventHandler handler = ModelChanged;
181      if (handler != null)
182        handler(this, EventArgs.Empty);
183    }
184
185    public event EventHandler EstimatedValuesChanged;
186    protected virtual void RaiseEstimatedValuesChanged() {
187      var listeners = EstimatedValuesChanged;
188      if (listeners != null)
189        listeners(this, EventArgs.Empty);
190    }
191    #endregion
192
193
194    public override IDeepCloneable Clone(Cloner cloner) {
195      SymbolicTimeSeriesPrognosisSolution clone = (SymbolicTimeSeriesPrognosisSolution)base.Clone(cloner);
196      clone.problemData = (MultiVariateDataAnalysisProblemData)cloner.Clone(problemData);
197      clone.model = (SymbolicTimeSeriesPrognosisModel)cloner.Clone(model);
198      clone.horizon = horizon;
199      clone.conditionalEvaluationVariable = conditionalEvaluationVariable;
200      return clone;
201    }
202  }
203}
Note: See TracBrowser for help on using the repository browser.