Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis/3.3/Symbolic/SymbolicTimeSeriesPrognosisProblem.cs @ 4113

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

Added plugin for time series prognosis. #1081

File size: 16.6 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    private List<IOperator> operators;
125    public override IEnumerable<IOperator> Operators {
126      get { return operators; }
127    }
128    public IEnumerable<IAnalyzer> Analyzers {
129      get { return operators.OfType<IAnalyzer>(); }
130    }
131    public IntValue TrainingSamplesStart {
132      get { return new IntValue(MultiVariateDataAnalysisProblemData.TrainingSamplesStart.Value); }
133    }
134    public IntValue TrainingSamplesEnd {
135      get {
136        return new IntValue((MultiVariateDataAnalysisProblemData.TrainingSamplesStart.Value +
137          MultiVariateDataAnalysisProblemData.TrainingSamplesEnd.Value) / 2);
138      }
139    }
140    public IntValue ValidationSamplesStart {
141      get { return TrainingSamplesEnd; }
142    }
143    public IntValue ValidationSamplesEnd {
144      get { return new IntValue(MultiVariateDataAnalysisProblemData.TrainingSamplesEnd.Value); }
145    }
146    public IntValue TestSamplesStart {
147      get { return MultiVariateDataAnalysisProblemData.TestSamplesStart; }
148    }
149    public IntValue TestSamplesEnd {
150      get { return MultiVariateDataAnalysisProblemData.TestSamplesEnd; }
151    }
152    public DoubleValue PunishmentFactor {
153      get { return new DoubleValue(10.0); }
154    }
155    #endregion
156
157    [Storable]
158    private SymbolicTimeSeriesPrognosisGrammar grammar;
159
160    public SymbolicTimeSeriesPrognosisProblem()
161      : base() {
162      SymbolicExpressionTreeCreator creator = new ProbabilisticTreeCreator();
163      grammar = new SymbolicTimeSeriesPrognosisGrammar(1);
164      var globalGrammar = new GlobalSymbolicExpressionGrammar(grammar);
165      var interpreter = new SymbolicTimeSeriesExpressionInterpreter();
166      Parameters.Add(new ValueParameter<SymbolicExpressionTreeCreator>("SolutionCreator", "The operator which should be used to create new symbolic time series prognosis solutions.", creator));
167      Parameters.Add(new ValueParameter<ISymbolicTimeSeriesExpressionInterpreter>("SymbolicExpressionTreeInterpreter", "The interpreter that should be used to evaluate the symbolic expression tree.", interpreter));
168      Parameters.Add(new ValueParameter<ISymbolicExpressionGrammar>("FunctionTreeGrammar", "The grammar that should be used for symbolic time series prognosis models.", globalGrammar));
169      Parameters.Add(new ValueParameter<IntValue>("MaxExpressionLength", "Maximal length of the symbolic expression.", new IntValue(100)));
170      Parameters.Add(new ValueParameter<IntValue>("MaxExpressionDepth", "Maximal depth of the symbolic expression.", new IntValue(10)));
171      Parameters.Add(new ValueParameter<IntValue>("MaxFunctionDefiningBranches", "Maximal number of automatically defined functions.", (IntValue)new IntValue(0).AsReadOnly()));
172      Parameters.Add(new ValueParameter<IntValue>("MaxFunctionArguments", "Maximal number of arguments of automatically defined functions.", (IntValue)new IntValue(0).AsReadOnly()));
173      Parameters.Add(new ValueParameter<IntValue>("PredictionHorizon", "The number of time steps for which to create a forecast.", new IntValue(1)));
174      Parameters.Add(new ValueParameter<DoubleArray>("UpperEstimationLimit", "The upper limit for the estimated values for each component."));
175      Parameters.Add(new ValueParameter<DoubleArray>("LowerEstimationLimit", "The lower limit for the estimated values for each component."));
176      creator.SymbolicExpressionTreeParameter.ActualName = "SymbolicTimeSeriesPrognosisModel";
177
178      ParameterizeSolutionCreator();
179
180      UpdateGrammar();
181      Initialize();
182    }
183
184    [StorableConstructor]
185    protected SymbolicTimeSeriesPrognosisProblem(bool deserializing) : base(deserializing) { }
186
187    [StorableHook(HookType.AfterDeserialization)]
188    private void AfterDeserializationHook() {
189      Initialize();
190    }
191
192    public override IDeepCloneable Clone(Cloner cloner) {
193      SymbolicTimeSeriesPrognosisProblem clone = (SymbolicTimeSeriesPrognosisProblem)base.Clone(cloner);
194      clone.grammar = (SymbolicTimeSeriesPrognosisGrammar)cloner.Clone(grammar);
195      clone.Initialize();
196      return clone;
197    }
198
199    private void RegisterParameterValueEvents() {
200      MaxFunctionArgumentsParameter.ValueChanged += new EventHandler(ArchitectureParameter_ValueChanged);
201      MaxFunctionDefiningBranchesParameter.ValueChanged += new EventHandler(ArchitectureParameter_ValueChanged);
202      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
203    }
204
205    private void RegisterParameterEvents() {
206      MaxFunctionArgumentsParameter.Value.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
207      MaxFunctionDefiningBranchesParameter.Value.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
208      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
209    }
210
211    #region event handling
212    protected override void OnMultiVariateDataAnalysisProblemChanged(EventArgs e) {
213      base.OnMultiVariateDataAnalysisProblemChanged(e);
214      // input variables could have been changed
215      UpdateGrammar();
216      UpdateEstimationLimits();
217    }
218    protected virtual void OnArchitectureParameterChanged(EventArgs e) {
219      UpdateGrammar();
220    }
221    protected virtual void OnGrammarChanged(EventArgs e) { }
222    protected virtual void OnOperatorsChanged(EventArgs e) { RaiseOperatorsChanged(e); }
223    protected virtual void OnSolutionCreatorChanged(EventArgs e) {
224      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
225      ParameterizeSolutionCreator();
226      OnSolutionParameterNameChanged(e);
227      RaiseSolutionCreatorChanged(e);
228    }
229
230    protected virtual void OnSolutionParameterNameChanged(EventArgs e) {
231      ParameterizeOperators();
232    }
233    #endregion
234
235    #region event handlers
236    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
237      OnSolutionCreatorChanged(e);
238    }
239    private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
240      OnSolutionParameterNameChanged(e);
241    }
242    private void ArchitectureParameter_ValueChanged(object sender, EventArgs e) {
243      MaxFunctionArgumentsParameter.Value.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
244      MaxFunctionDefiningBranchesParameter.Value.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
245      OnArchitectureParameterChanged(e);
246    }
247    private void ArchitectureParameterValue_ValueChanged(object sender, EventArgs e) {
248      OnArchitectureParameterChanged(e);
249    }
250    #endregion
251
252    #region Helpers
253    protected void AddOperator(IOperator op) {
254      operators.Add(op);
255    }
256
257    private void Initialize() {
258      InitializeOperators();
259      RegisterParameterEvents();
260      RegisterParameterValueEvents();
261    }
262
263    private void UpdateGrammar() {
264      var selectedTargetVariables = MultiVariateDataAnalysisProblemData.TargetVariables.CheckedItems;
265      grammar.SetResultProducingBranches(selectedTargetVariables.Count());
266
267      foreach (var varSymbol in grammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable>()) {
268        varSymbol.VariableNames = MultiVariateDataAnalysisProblemData.InputVariables.CheckedItems.Select(x => x.Value.Value);
269      }
270
271      var globalGrammar = new GlobalSymbolicExpressionGrammar(grammar);
272      globalGrammar.MaxFunctionArguments = MaxFunctionArguments.Value;
273      globalGrammar.MaxFunctionDefinitions = MaxFunctionDefiningBranches.Value;
274      FunctionTreeGrammar = globalGrammar;
275    }
276    private void UpdateEstimationLimits() {
277      IEnumerable<string> selectedTargetVariables = MultiVariateDataAnalysisProblemData.TargetVariables.CheckedItems.Select(x => x.Value.Value);
278      UpperEstimationLimit = new DoubleArray(selectedTargetVariables.Count());
279      LowerEstimationLimit = new DoubleArray(selectedTargetVariables.Count());
280      int i = 0;
281      foreach (string targetVariable in selectedTargetVariables) {
282        if (TrainingSamplesStart.Value < TrainingSamplesEnd.Value) {
283          var targetValues = MultiVariateDataAnalysisProblemData.Dataset.GetVariableValues(targetVariable, TrainingSamplesStart.Value, TrainingSamplesEnd.Value);
284          var mean = targetValues.Average();
285          var range = targetValues.Max() - targetValues.Min();
286          UpperEstimationLimit[i] = mean + PunishmentFactor.Value * range;
287          LowerEstimationLimit[i] = mean - PunishmentFactor.Value * range;
288        } else {
289          UpperEstimationLimit[i] = 0;
290          LowerEstimationLimit[i] = 0;
291        }
292        i++;
293      }
294    }
295
296    private void InitializeOperators() {
297      operators = new List<IOperator>();
298      operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>().OfType<IOperator>());
299      operators.Add(new MinAverageMaxSymbolicExpressionTreeSizeAnalyzer());
300      ParameterizeOperators();
301    }
302
303    private void ParameterizeSolutionCreator() {
304      SolutionCreator.SymbolicExpressionGrammarParameter.ActualName = FunctionTreeGrammarParameter.Name;
305      SolutionCreator.MaxTreeHeightParameter.ActualName = MaxExpressionDepthParameter.Name;
306      SolutionCreator.MaxTreeSizeParameter.ActualName = MaxExpressionLengthParameter.Name;
307      SolutionCreator.MaxFunctionArgumentsParameter.ActualName = MaxFunctionArgumentsParameter.Name;
308      SolutionCreator.MaxFunctionDefinitionsParameter.ActualName = MaxFunctionDefiningBranchesParameter.Name;
309    }
310
311    private void ParameterizeOperators() {
312      foreach (ISymbolicExpressionTreeOperator op in Operators.OfType<ISymbolicExpressionTreeOperator>()) {
313        op.MaxTreeHeightParameter.ActualName = MaxExpressionDepthParameter.Name;
314        op.MaxTreeSizeParameter.ActualName = MaxExpressionLengthParameter.Name;
315        op.SymbolicExpressionGrammarParameter.ActualName = FunctionTreeGrammarParameter.Name;
316      }
317      foreach (ISymbolicExpressionTreeCrossover op in Operators.OfType<ISymbolicExpressionTreeCrossover>()) {
318        op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
319        op.ChildParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
320      }
321      foreach (ISymbolicExpressionTreeManipulator op in Operators.OfType<ISymbolicExpressionTreeManipulator>()) {
322        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
323      }
324      foreach (ISymbolicExpressionTreeArchitectureManipulator op in Operators.OfType<ISymbolicExpressionTreeArchitectureManipulator>()) {
325        op.MaxFunctionArgumentsParameter.ActualName = MaxFunctionArgumentsParameter.Name;
326        op.MaxFunctionDefinitionsParameter.ActualName = MaxFunctionDefiningBranchesParameter.Name;
327      }
328    }
329    #endregion
330  }
331}
Note: See TracBrowser for help on using the repository browser.