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 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 | using HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis.Symbolic.Symbols;
|
---|
29 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
|
---|
30 | using System.Linq;
|
---|
31 | using System.Drawing;
|
---|
32 | using System;
|
---|
33 | using HeuristicLab.Data;
|
---|
34 |
|
---|
35 | namespace 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 |
|
---|
57 | public SymbolicTimeSeriesPrognosisSolution() {
|
---|
58 | horizon = 1;
|
---|
59 | }
|
---|
60 |
|
---|
61 | public SymbolicTimeSeriesPrognosisSolution(MultiVariateDataAnalysisProblemData problemData, SymbolicTimeSeriesPrognosisModel model, int horizon, string conditionalEvaluationVariable, double[] lowerEstimationLimit, double[] upperEstimationLimit)
|
---|
62 | : this() {
|
---|
63 | this.problemData = problemData;
|
---|
64 | this.model = model;
|
---|
65 | this.horizon = horizon;
|
---|
66 | this.conditionalEvaluationVariable = conditionalEvaluationVariable;
|
---|
67 | this.lowerEstimationLimit = (double[])lowerEstimationLimit.Clone();
|
---|
68 | this.upperEstimationLimit = (double[])upperEstimationLimit.Clone();
|
---|
69 | }
|
---|
70 |
|
---|
71 | [StorableHook(HookType.AfterDeserialization)]
|
---|
72 | private void Initialize() {
|
---|
73 | if (problemData != null)
|
---|
74 | RegisterProblemDataEvents();
|
---|
75 | }
|
---|
76 |
|
---|
77 | public override Image ItemImage {
|
---|
78 | get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Function; }
|
---|
79 | }
|
---|
80 |
|
---|
81 | public int Horizon {
|
---|
82 | get { return horizon; }
|
---|
83 | set {
|
---|
84 | if (value <= 0) throw new ArgumentException();
|
---|
85 | horizon = value;
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | public SymbolicTimeSeriesPrognosisModel Model {
|
---|
90 | get { return model; }
|
---|
91 | set {
|
---|
92 | if (model != value) {
|
---|
93 | if (value == null) throw new ArgumentNullException();
|
---|
94 | model = value;
|
---|
95 | RaiseModelChanged();
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | public string ConditionalEvaluationVariable {
|
---|
101 | get { return conditionalEvaluationVariable; }
|
---|
102 | set {
|
---|
103 | if (conditionalEvaluationVariable != value) {
|
---|
104 | conditionalEvaluationVariable = value;
|
---|
105 | RaiseEstimatedValuesChanged();
|
---|
106 | }
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | public double GetLowerEstimationLimit(int i) {
|
---|
111 | return lowerEstimationLimit[i];
|
---|
112 | }
|
---|
113 | public double GetUpperEstimationLimit(int i) {
|
---|
114 | return upperEstimationLimit[i];
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | public IEnumerable<double[]> GetPrognosis(int t) {
|
---|
119 | return model.GetEstimatedValues(problemData, t, t + 1, horizon);
|
---|
120 | }
|
---|
121 |
|
---|
122 | #region IMultiVariateDataAnalysisSolution Members
|
---|
123 |
|
---|
124 | public MultiVariateDataAnalysisProblemData ProblemData {
|
---|
125 | get { return problemData; }
|
---|
126 | set {
|
---|
127 | if (problemData != value) {
|
---|
128 | if (value == null) throw new ArgumentNullException();
|
---|
129 | if (model != null && problemData != null &&
|
---|
130 | !(problemData.InputVariables
|
---|
131 | .Select(c => c.Value)
|
---|
132 | .SequenceEqual(value.InputVariables
|
---|
133 | .Select(c => c.Value)) &&
|
---|
134 | problemData.TargetVariables
|
---|
135 | .Select(c => c.Value)
|
---|
136 | .SequenceEqual(value.TargetVariables
|
---|
137 | .Select(c => c.Value)))) {
|
---|
138 | throw new ArgumentException("Could not set new problem data with different structure");
|
---|
139 | }
|
---|
140 |
|
---|
141 | if (problemData != null) DeregisterProblemDataEvents();
|
---|
142 | problemData = value;
|
---|
143 | RaiseProblemDataChanged();
|
---|
144 | RegisterProblemDataEvents();
|
---|
145 | }
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | IMultiVariateDataAnalysisModel IMultiVariateDataAnalysisSolution.Model {
|
---|
150 | get { return model; }
|
---|
151 | }
|
---|
152 |
|
---|
153 | public IEnumerable<double[]> EstimatedValues {
|
---|
154 | get {
|
---|
155 | return ApplyEstimationLimits(model.GetEstimatedValues(problemData, 0, problemData.Dataset.Rows));
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | public IEnumerable<double[]> EstimatedTrainingValues {
|
---|
160 | get {
|
---|
161 | return ApplyEstimationLimits(model.GetEstimatedValues(problemData, problemData.TrainingSamplesStart.Value, problemData.TrainingSamplesEnd.Value));
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 | public IEnumerable<double[]> EstimatedTestValues {
|
---|
166 | get {
|
---|
167 | return ApplyEstimationLimits(model.GetEstimatedValues(problemData, problemData.TestSamplesStart.Value, problemData.TestSamplesEnd.Value));
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | #endregion
|
---|
172 |
|
---|
173 |
|
---|
174 | #region Events
|
---|
175 | protected virtual void RegisterProblemDataEvents() {
|
---|
176 | ProblemData.ProblemDataChanged += new EventHandler(ProblemData_Changed);
|
---|
177 | }
|
---|
178 | protected virtual void DeregisterProblemDataEvents() {
|
---|
179 | ProblemData.ProblemDataChanged += new EventHandler(ProblemData_Changed);
|
---|
180 | }
|
---|
181 | private void ProblemData_Changed(object sender, EventArgs e) {
|
---|
182 | RaiseProblemDataChanged();
|
---|
183 | }
|
---|
184 |
|
---|
185 | public event EventHandler ProblemDataChanged;
|
---|
186 | protected virtual void RaiseProblemDataChanged() {
|
---|
187 | var listeners = ProblemDataChanged;
|
---|
188 | if (listeners != null)
|
---|
189 | listeners(this, EventArgs.Empty);
|
---|
190 | }
|
---|
191 |
|
---|
192 | public event EventHandler ModelChanged;
|
---|
193 | protected virtual void RaiseModelChanged() {
|
---|
194 | EventHandler handler = ModelChanged;
|
---|
195 | if (handler != null)
|
---|
196 | handler(this, EventArgs.Empty);
|
---|
197 | }
|
---|
198 |
|
---|
199 | public event EventHandler EstimatedValuesChanged;
|
---|
200 | protected virtual void RaiseEstimatedValuesChanged() {
|
---|
201 | var listeners = EstimatedValuesChanged;
|
---|
202 | if (listeners != null)
|
---|
203 | listeners(this, EventArgs.Empty);
|
---|
204 | }
|
---|
205 | #endregion
|
---|
206 |
|
---|
207 |
|
---|
208 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
209 | SymbolicTimeSeriesPrognosisSolution clone = (SymbolicTimeSeriesPrognosisSolution)base.Clone(cloner);
|
---|
210 | clone.problemData = (MultiVariateDataAnalysisProblemData)cloner.Clone(problemData);
|
---|
211 | clone.model = (SymbolicTimeSeriesPrognosisModel)cloner.Clone(model);
|
---|
212 | clone.horizon = horizon;
|
---|
213 | clone.conditionalEvaluationVariable = conditionalEvaluationVariable;
|
---|
214 | clone.lowerEstimationLimit = (double[])lowerEstimationLimit.Clone();
|
---|
215 | clone.upperEstimationLimit = (double[])upperEstimationLimit.Clone();
|
---|
216 | return clone;
|
---|
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 | xs[i] = Math.Max(lowerEstimationLimit[i], Math.Min(upperEstimationLimit[i], xs[i]));
|
---|
223 | }
|
---|
224 | yield return xs;
|
---|
225 | }
|
---|
226 | }
|
---|
227 | }
|
---|
228 | } |
---|