Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis/3.3/Symbolic/SymbolicTimeSeriesPrognosisProblem.cs @ 11301

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

worked on data analysis feature exploration branch. #1142

File size: 17.4 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;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.PluginInfrastructure;
32using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
33using HeuristicLab.Problems.DataAnalysis.Symbolic;
34using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators;
35using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Manipulators;
36using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Crossovers;
37using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;
38using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Interfaces;
39using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Analyzers;
40using HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis.Symbolic.Interfaces;
41
42namespace HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis.Symbolic {
43  [Item("Symbolic Time Series Prognosis Problem", "Represents a symbolic time series prognosis problem.")]
44  [StorableClass]
45  public class SymbolicTimeSeriesPrognosisProblem : MultiVariateDataAnalysisProblem, IProblem {
46
47    #region Parameter Properties
48    public ValueParameter<SymbolicExpressionTreeCreator> SolutionCreatorParameter {
49      get { return (ValueParameter<SymbolicExpressionTreeCreator>)Parameters["SolutionCreator"]; }
50    }
51    IParameter IProblem.SolutionCreatorParameter {
52      get { return SolutionCreatorParameter; }
53    }
54    public ValueParameter<ISymbolicTimeSeriesExpressionInterpreter> SymbolicExpressionTreeInterpreterParameter {
55      get { return (ValueParameter<ISymbolicTimeSeriesExpressionInterpreter>)Parameters["SymbolicExpressionTreeInterpreter"]; }
56    }
57    public ValueParameter<ISymbolicExpressionGrammar> FunctionTreeGrammarParameter {
58      get { return (ValueParameter<ISymbolicExpressionGrammar>)Parameters["FunctionTreeGrammar"]; }
59    }
60    public ValueParameter<IntValue> MaxExpressionLengthParameter {
61      get { return (ValueParameter<IntValue>)Parameters["MaxExpressionLength"]; }
62    }
63    public ValueParameter<IntValue> MaxExpressionDepthParameter {
64      get { return (ValueParameter<IntValue>)Parameters["MaxExpressionDepth"]; }
65    }
66    public ValueParameter<IntValue> MaxFunctionDefiningBranchesParameter {
67      get { return (ValueParameter<IntValue>)Parameters["MaxFunctionDefiningBranches"]; }
68    }
69    public ValueParameter<IntValue> MaxFunctionArgumentsParameter {
70      get { return (ValueParameter<IntValue>)Parameters["MaxFunctionArguments"]; }
71    }
72    public ValueParameter<IntValue> PredictionHorizonParameter {
73      get { return (ValueParameter<IntValue>)Parameters["PredictionHorizon"]; }
74    }
75    public ValueParameter<DoubleArray> UpperEstimationLimitParameter {
76      get { return (ValueParameter<DoubleArray>)Parameters["UpperEstimationLimit"]; }
77    }
78    public ValueParameter<DoubleArray> LowerEstimationLimitParameter {
79      get { return (ValueParameter<DoubleArray>)Parameters["LowerEstimationLimit"]; }
80    }
81    #endregion
82
83    #region Properties
84    public IntValue MaxExpressionLength {
85      get { return MaxExpressionLengthParameter.Value; }
86      set { MaxExpressionLengthParameter.Value = value; }
87    }
88    public IntValue MaxExpressionDepth {
89      get { return MaxExpressionDepthParameter.Value; }
90      set { MaxExpressionDepthParameter.Value = value; }
91    }
92    public IntValue MaxFunctionDefiningBranches {
93      get { return MaxFunctionDefiningBranchesParameter.Value; }
94      set { MaxFunctionDefiningBranchesParameter.Value = value; }
95    }
96    public IntValue MaxFunctionArguments {
97      get { return MaxFunctionArgumentsParameter.Value; }
98      set { MaxFunctionArgumentsParameter.Value = value; }
99    }
100    public DoubleArray UpperEstimationLimit {
101      get { return UpperEstimationLimitParameter.Value; }
102      set { UpperEstimationLimitParameter.Value = value; }
103    }
104    public DoubleArray LowerEstimationLimit {
105      get { return LowerEstimationLimitParameter.Value; }
106      set { LowerEstimationLimitParameter.Value = value; }
107    }
108    public new SymbolicExpressionTreeCreator SolutionCreator {
109      get { return SolutionCreatorParameter.Value; }
110      set { SolutionCreatorParameter.Value = value; }
111    }
112    ISolutionCreator IProblem.SolutionCreator {
113      get { return SolutionCreatorParameter.Value; }
114    }
115    public ISymbolicTimeSeriesExpressionInterpreter SymbolicExpressionTreeInterpreter {
116      get { return SymbolicExpressionTreeInterpreterParameter.Value; }
117      set { SymbolicExpressionTreeInterpreterParameter.Value = value; }
118    }
119
120    public ISymbolicExpressionGrammar FunctionTreeGrammar {
121      get { return (ISymbolicExpressionGrammar)FunctionTreeGrammarParameter.Value; }
122      set { FunctionTreeGrammarParameter.Value = value; }
123    }
124    public override IEnumerable<IOperator> Operators {
125      get { return operators; }
126    }
127    public IEnumerable<IAnalyzer> Analyzers {
128      get { return operators.OfType<IAnalyzer>(); }
129    }
130    public IntValue TrainingSamplesStart {
131      get { return new IntValue(MultiVariateDataAnalysisProblemData.TrainingSamplesStart.Value); }
132    }
133    public IntValue TrainingSamplesEnd {
134      get {
135        return new IntValue((MultiVariateDataAnalysisProblemData.TrainingSamplesStart.Value +
136          MultiVariateDataAnalysisProblemData.TrainingSamplesEnd.Value) / 2);
137      }
138    }
139    public IntValue ValidationSamplesStart {
140      get { return TrainingSamplesEnd; }
141    }
142    public IntValue ValidationSamplesEnd {
143      get { return new IntValue(MultiVariateDataAnalysisProblemData.TrainingSamplesEnd.Value); }
144    }
145    public IntValue TestSamplesStart {
146      get { return MultiVariateDataAnalysisProblemData.TestSamplesStart; }
147    }
148    public IntValue TestSamplesEnd {
149      get { return MultiVariateDataAnalysisProblemData.TestSamplesEnd; }
150    }
151    public DoubleValue PunishmentFactor {
152      get { return new DoubleValue(10.0); }
153    }
154    #endregion
155
156    [Storable]
157    private List<IOperator> operators;
158    [Storable]
159    private SymbolicTimeSeriesPrognosisGrammar timeSeriesPrognosisGrammar;
160    [StorableConstructor]
161    protected SymbolicTimeSeriesPrognosisProblem(bool deserializing) : base(deserializing) { }
162    protected SymbolicTimeSeriesPrognosisProblem(SymbolicTimeSeriesPrognosisProblem original, Cloner cloner)
163      : base(original, cloner) {
164      operators = original.operators.Select(x => (IOperator)cloner.Clone(x)).ToList();
165      timeSeriesPrognosisGrammar = cloner.Clone(original.timeSeriesPrognosisGrammar);
166      RegisterParameterEvents();
167      RegisterParameterValueEvents();
168    }
169    public SymbolicTimeSeriesPrognosisProblem()
170      : base() {
171      SymbolicExpressionTreeCreator creator = new ProbabilisticTreeCreator();
172      timeSeriesPrognosisGrammar = new SymbolicTimeSeriesPrognosisGrammar(1);
173      var globalGrammar = new GlobalSymbolicExpressionGrammar(timeSeriesPrognosisGrammar);
174      var interpreter = new SymbolicTimeSeriesExpressionInterpreter();
175      Parameters.Add(new ValueParameter<SymbolicExpressionTreeCreator>("SolutionCreator", "The operator which should be used to create new symbolic time series prognosis solutions.", creator));
176      Parameters.Add(new ValueParameter<ISymbolicTimeSeriesExpressionInterpreter>("SymbolicExpressionTreeInterpreter", "The interpreter that should be used to evaluate the symbolic expression tree.", interpreter));
177      Parameters.Add(new ValueParameter<ISymbolicExpressionGrammar>("FunctionTreeGrammar", "The grammar that should be used for symbolic time series prognosis models.", globalGrammar));
178      Parameters.Add(new ValueParameter<IntValue>("MaxExpressionLength", "Maximal length of the symbolic expression.", new IntValue(100)));
179      Parameters.Add(new ValueParameter<IntValue>("MaxExpressionDepth", "Maximal depth of the symbolic expression.", new IntValue(10)));
180      Parameters.Add(new ValueParameter<IntValue>("MaxFunctionDefiningBranches", "Maximal number of automatically defined functions.", new IntValue(0)));
181      Parameters.Add(new ValueParameter<IntValue>("MaxFunctionArguments", "Maximal number of arguments of automatically defined functions.", new IntValue(0)));
182      Parameters.Add(new ValueParameter<IntValue>("PredictionHorizon", "The number of time steps for which to create a forecast.", new IntValue(1)));
183      Parameters.Add(new ValueParameter<DoubleArray>("UpperEstimationLimit", "The upper limit for the estimated values for each component."));
184      Parameters.Add(new ValueParameter<DoubleArray>("LowerEstimationLimit", "The lower limit for the estimated values for each component."));
185      creator.SymbolicExpressionTreeParameter.ActualName = "SymbolicTimeSeriesPrognosisModel";
186
187      ParameterizeSolutionCreator();
188
189      UpdateGrammar();
190      InitializeOperators();
191      RegisterParameterEvents();
192      RegisterParameterValueEvents();
193    }
194
195    [StorableHook(HookType.AfterDeserialization)]
196    private void AfterDeserialization() {
197      // BackwardsCompatibility3.3
198      #region Backwards compatible code (remove with 3.4)
199      if (operators == null) InitializeOperators();
200      #endregion
201      RegisterParameterEvents();
202      RegisterParameterValueEvents();
203    }
204
205    public override IDeepCloneable Clone(Cloner cloner) {
206      return new SymbolicTimeSeriesPrognosisProblem(this, cloner);
207    }
208
209    private void RegisterParameterValueEvents() {
210      MaxFunctionArgumentsParameter.ValueChanged += new EventHandler(ArchitectureParameter_ValueChanged);
211      MaxFunctionDefiningBranchesParameter.ValueChanged += new EventHandler(ArchitectureParameter_ValueChanged);
212      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
213    }
214
215    private void RegisterParameterEvents() {
216      MaxFunctionArgumentsParameter.Value.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
217      MaxFunctionDefiningBranchesParameter.Value.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
218      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
219    }
220
221    #region event handling
222    protected override void OnMultiVariateDataAnalysisProblemChanged(EventArgs e) {
223      base.OnMultiVariateDataAnalysisProblemChanged(e);
224      // input variables could have been changed
225      UpdateGrammar();
226      UpdateEstimationLimits();
227    }
228    protected virtual void OnArchitectureParameterChanged(EventArgs e) {
229      UpdateGrammar();
230    }
231    protected virtual void OnGrammarChanged(EventArgs e) { UpdateGrammar(); }
232    protected virtual void OnOperatorsChanged(EventArgs e) { RaiseOperatorsChanged(e); }
233    protected virtual void OnSolutionCreatorChanged(EventArgs e) {
234      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
235      ParameterizeSolutionCreator();
236      OnSolutionParameterNameChanged(e);
237      RaiseSolutionCreatorChanged(e);
238    }
239
240    protected virtual void OnSolutionParameterNameChanged(EventArgs e) {
241      ParameterizeOperators();
242    }
243    #endregion
244
245    #region event handlers
246    private void FunctionTreeGrammarParameter_ValueChanged(object sender, EventArgs e) {
247      if (!(FunctionTreeGrammar is GlobalSymbolicExpressionGrammar))
248        FunctionTreeGrammar = new GlobalSymbolicExpressionGrammar(FunctionTreeGrammar);
249      OnGrammarChanged(e);
250    }
251    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
252      OnSolutionCreatorChanged(e);
253    }
254    private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
255      OnSolutionParameterNameChanged(e);
256    }
257    private void ArchitectureParameter_ValueChanged(object sender, EventArgs e) {
258      MaxFunctionArgumentsParameter.Value.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
259      MaxFunctionDefiningBranchesParameter.Value.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
260      OnArchitectureParameterChanged(e);
261    }
262    private void ArchitectureParameterValue_ValueChanged(object sender, EventArgs e) {
263      OnArchitectureParameterChanged(e);
264    }
265    #endregion
266
267    #region Helpers
268    protected void AddOperator(IOperator op) {
269      operators.Add(op);
270    }
271
272    private void UpdateGrammar() {
273      var selectedTargetVariables = MultiVariateDataAnalysisProblemData.TargetVariables.CheckedItems;
274      timeSeriesPrognosisGrammar.SetResultProducingBranches(selectedTargetVariables.Count());
275
276      foreach (var varSymbol in FunctionTreeGrammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable>()) {
277        varSymbol.VariableNames = MultiVariateDataAnalysisProblemData.InputVariables.CheckedItems.Select(x => x.Value.Value);
278      }
279
280      var globalGrammar = FunctionTreeGrammar as GlobalSymbolicExpressionGrammar;
281      if (globalGrammar != null) {
282        globalGrammar.MaxFunctionArguments = MaxFunctionArguments.Value;
283        globalGrammar.MaxFunctionDefinitions = MaxFunctionDefiningBranches.Value;
284      }
285    }
286    private void UpdateEstimationLimits() {
287      IEnumerable<string> selectedTargetVariables = MultiVariateDataAnalysisProblemData.TargetVariables.CheckedItems.Select(x => x.Value.Value);
288      UpperEstimationLimit = new DoubleArray(selectedTargetVariables.Count());
289      LowerEstimationLimit = new DoubleArray(selectedTargetVariables.Count());
290      int i = 0;
291      foreach (string targetVariable in selectedTargetVariables) {
292        if (TrainingSamplesStart.Value < TrainingSamplesEnd.Value) {
293          var targetValues = MultiVariateDataAnalysisProblemData.Dataset.GetVariableValues(targetVariable, TrainingSamplesStart.Value, TrainingSamplesEnd.Value);
294          var mean = targetValues.Average();
295          var range = targetValues.Max() - targetValues.Min();
296          UpperEstimationLimit[i] = mean + PunishmentFactor.Value * range;
297          LowerEstimationLimit[i] = mean - PunishmentFactor.Value * range;
298        } else {
299          UpperEstimationLimit[i] = 0;
300          LowerEstimationLimit[i] = 0;
301        }
302        i++;
303      }
304    }
305
306    private void InitializeOperators() {
307      operators = new List<IOperator>();
308      operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>().OfType<IOperator>());
309      operators.Add(new MinAverageMaxSymbolicExpressionTreeSizeAnalyzer());
310      ParameterizeOperators();
311    }
312
313    private void ParameterizeSolutionCreator() {
314      SolutionCreator.SymbolicExpressionGrammarParameter.ActualName = FunctionTreeGrammarParameter.Name;
315      SolutionCreator.MaxTreeHeightParameter.ActualName = MaxExpressionDepthParameter.Name;
316      SolutionCreator.MaxTreeSizeParameter.ActualName = MaxExpressionLengthParameter.Name;
317      SolutionCreator.MaxFunctionArgumentsParameter.ActualName = MaxFunctionArgumentsParameter.Name;
318      SolutionCreator.MaxFunctionDefinitionsParameter.ActualName = MaxFunctionDefiningBranchesParameter.Name;
319    }
320
321    private void ParameterizeOperators() {
322      foreach (ISymbolicExpressionTreeOperator op in Operators.OfType<ISymbolicExpressionTreeOperator>()) {
323        op.MaxTreeHeightParameter.ActualName = MaxExpressionDepthParameter.Name;
324        op.MaxTreeSizeParameter.ActualName = MaxExpressionLengthParameter.Name;
325        op.SymbolicExpressionGrammarParameter.ActualName = FunctionTreeGrammarParameter.Name;
326      }
327      foreach (ISymbolicExpressionTreeCrossover op in Operators.OfType<ISymbolicExpressionTreeCrossover>()) {
328        op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
329        op.ChildParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
330      }
331      foreach (ISymbolicExpressionTreeManipulator op in Operators.OfType<ISymbolicExpressionTreeManipulator>()) {
332        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
333      }
334      foreach (ISymbolicExpressionTreeArchitectureManipulator op in Operators.OfType<ISymbolicExpressionTreeArchitectureManipulator>()) {
335        op.MaxFunctionArgumentsParameter.ActualName = MaxFunctionArgumentsParameter.Name;
336        op.MaxFunctionDefinitionsParameter.ActualName = MaxFunctionDefiningBranchesParameter.Name;
337      }
338    }
339    #endregion
340  }
341}
Note: See TracBrowser for help on using the repository browser.