Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis/3.3/Symbolic/SymbolicTimeSeriesPrognosisGrammar.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: 5.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 HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
23using System.Collections.Generic;
24using System;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
30using HeuristicLab.Data;
31using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
32using HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis.Symbolic.Symbols;
33
34namespace HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis.Symbolic {
35  [StorableClass]
36  [Item("SymbolicTimeSeriesPrognosisGrammar", "Represents a grammar for time series prognosis model using all available functions.")]
37  public class SymbolicTimeSeriesPrognosisGrammar : DefaultSymbolicExpressionGrammar {
38    [Storable]
39    private int dimension;
40
41    // for persistence
42    private SymbolicTimeSeriesPrognosisGrammar() : this(1) { }
43
44    public SymbolicTimeSeriesPrognosisGrammar(int dimension)
45      : base() {
46      this.dimension = dimension;
47      Initialize();
48    }
49
50    protected SymbolicTimeSeriesPrognosisGrammar(SymbolicTimeSeriesPrognosisGrammar original)
51      : base(original) {
52      this.dimension = original.dimension;
53    }
54
55    private void Initialize() {
56      var add = new Addition();
57      var sub = new Subtraction();
58      var mul = new Multiplication();
59      var div = new Division();
60      var mean = new Average();
61      var sin = new Sine();
62      var cos = new Cosine();
63      var tan = new Tangent();
64      var log = new Logarithm();
65      var exp = new Exponential();
66      var @if = new IfThenElse();
67      var gt = new GreaterThan();
68      var lt = new LessThan();
69      var and = new And();
70      var or = new Or();
71      var not = new Not();
72      var constant = new Constant();
73      constant.MinValue = -20;
74      constant.MaxValue = 20;
75      var variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable();
76      var laggedVariableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.LaggedVariable();
77      var integralVariableSymbol = new IntegratedVariable();
78      var derivedVariableSymbol = new DerivativeVariable();
79      var movingAverageSymbol = new MovingAverage();
80
81      var allSymbols = new List<Symbol>() { add, sub, mul, div, mean, sin, cos, tan, log, exp,
82        @if, gt, lt, and, or, not,
83        constant, variableSymbol, laggedVariableSymbol, derivedVariableSymbol, integralVariableSymbol, movingAverageSymbol};
84      var unaryFunctionSymbols = new List<Symbol>() { sin, cos, tan, log, exp, not };
85      var binaryFunctionSymbols = new List<Symbol>() { gt, lt };
86      var functionSymbols = new List<Symbol>() { add, sub, mul, div, mean, and, or };
87      var terminalSymbols = new List<Symbol>() { constant, variableSymbol, laggedVariableSymbol, derivedVariableSymbol, integralVariableSymbol, movingAverageSymbol };
88      foreach (var symb in allSymbols)
89        AddSymbol(symb);
90
91      foreach (var funSymb in functionSymbols) {
92        SetMinSubtreeCount(funSymb, 1);
93        SetMaxSubtreeCount(funSymb, 3);
94      }
95      foreach (var funSymb in unaryFunctionSymbols) {
96        SetMinSubtreeCount(funSymb, 1);
97        SetMaxSubtreeCount(funSymb, 1);
98      }
99      foreach (var funSymb in binaryFunctionSymbols) {
100        SetMinSubtreeCount(funSymb, 2);
101        SetMaxSubtreeCount(funSymb, 2);
102      }
103
104      SetMinSubtreeCount(@if, 3);
105      SetMaxSubtreeCount(@if, 3);
106      foreach (var terminalSymbol in terminalSymbols) {
107        SetMinSubtreeCount(terminalSymbol, 0);
108        SetMaxSubtreeCount(terminalSymbol, 0);
109      }
110
111      SetMinSubtreeCount(StartSymbol, dimension);
112      SetMaxSubtreeCount(StartSymbol, dimension);
113
114      // allow each symbol as child of the start symbol
115      foreach (var symb in allSymbols) {
116        for (int i = 0; i < GetMaxSubtreeCount(StartSymbol); i++)
117          SetAllowedChild(StartSymbol, symb, i);
118      }
119
120      // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)
121      foreach (var parent in allSymbols) {
122        for (int i = 0; i < GetMaxSubtreeCount(parent); i++)
123          foreach (var child in allSymbols) {
124            SetAllowedChild(parent, child, i);
125          }
126      }
127    }
128
129    public void SetResultProducingBranches(int n) {
130      SetMinSubtreeCount(StartSymbol, n);
131      SetMaxSubtreeCount(StartSymbol, n);
132
133      foreach (Symbol s in Symbols) {
134        if (s != StartSymbol)
135          for (int i = 0; i < n; i++) {
136            SetAllowedChild(StartSymbol, s, i);
137          }
138      }
139    }
140
141    public override IDeepCloneable Clone(Cloner cloner) {
142      return new SymbolicTimeSeriesPrognosisGrammar(this);
143    }
144  }
145}
Note: See TracBrowser for help on using the repository browser.