Free cookie consent management tool by TermsFeed Policy Generator

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

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

Merged changes from trunk to data analysis exploration branch and added fractional distance metric evaluator. #1142

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