Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis/3.3/Symbolic/SymbolicTimeSeriesPrognosisGrammar.cs @ 5155

Last change on this file since 5155 was 4252, checked in by mkommend, 14 years ago

Updated grammars in DataAnalysis branch (ticket #1028).

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