Free cookie consent management tool by TermsFeed Policy Generator

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

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

Merged changes from trunk to data analysis exploration branch and added fractional distance metric evaluator. #1142

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