Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate.Regression/3.3/Symbolic/SymbolicVectorRegressionGrammar.cs @ 9333

Last change on this file since 9333 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: 4.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.Symbols;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Problems.DataAnalysis.MultiVariate.Symbolic;
28using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
29
30namespace HeuristicLab.Problems.DataAnalysis.MultiVariate.Regression.Symbolic {
31  [StorableClass]
32  [Item("SymbolicVectorRegressionGrammar", "Represents a grammar for symbolic vector regression using all available functions.")]
33  public class SymbolicVectorRegressionGrammar : MultiVariateExpressionGrammar {
34    public SymbolicVectorRegressionGrammar() : this(1) { }
35    [StorableConstructor]
36    protected SymbolicVectorRegressionGrammar(bool deserializing) : base(deserializing) { }
37    protected SymbolicVectorRegressionGrammar(SymbolicVectorRegressionGrammar original, Cloner cloner)
38      : base(original, cloner) {
39    }
40    public SymbolicVectorRegressionGrammar(int dimension)
41      : base(dimension) {
42      Initialize();
43    }
44    public override IDeepCloneable Clone(Cloner cloner) {
45      return new SymbolicVectorRegressionGrammar(this, cloner);
46    }
47
48    private void Initialize() {
49      var add = new Addition();
50      var sub = new Subtraction();
51      var mul = new Multiplication();
52      var div = new Division();
53      var mean = new Average();
54      var sin = new Sine();
55      var cos = new Cosine();
56      var tan = new Tangent();
57      var log = new Logarithm();
58      var exp = new Exponential();
59      var @if = new IfThenElse();
60      var gt = new GreaterThan();
61      var lt = new LessThan();
62      var and = new And();
63      var or = new Or();
64      var not = new Not();
65      var constant = new Constant();
66      constant.MinValue = -20;
67      constant.MaxValue = 20;
68      var variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable();
69
70      var allSymbols = new List<Symbol>() { add, sub, mul, div, mean, sin, cos, tan, log, exp, @if, gt, lt, and, or, not, constant, variableSymbol };
71      var unaryFunctionSymbols = new List<Symbol>() { sin, cos, tan, log, exp, not };
72      var binaryFunctionSymbols = new List<Symbol>() { gt, lt };
73      var functionSymbols = new List<Symbol>() { add, sub, mul, div, mean, and, or };
74
75      foreach (var symb in allSymbols)
76        AddSymbol(symb);
77
78      foreach (var funSymb in functionSymbols) {
79        SetMinSubtreeCount(funSymb, 1);
80        SetMaxSubtreeCount(funSymb, 3);
81      }
82      foreach (var funSymb in unaryFunctionSymbols) {
83        SetMinSubtreeCount(funSymb, 1);
84        SetMaxSubtreeCount(funSymb, 1);
85      }
86      foreach (var funSymb in binaryFunctionSymbols) {
87        SetMinSubtreeCount(funSymb, 2);
88        SetMaxSubtreeCount(funSymb, 2);
89      }
90
91      SetMinSubtreeCount(@if, 3);
92      SetMaxSubtreeCount(@if, 3);
93      SetMinSubtreeCount(constant, 0);
94      SetMaxSubtreeCount(constant, 0);
95      SetMinSubtreeCount(variableSymbol, 0);
96      SetMaxSubtreeCount(variableSymbol, 0);
97
98      SetMinSubtreeCount(StartSymbol, Dimension);
99      SetMaxSubtreeCount(StartSymbol, Dimension);
100
101      SetMinSubtreeCount(constant, 0);
102      SetMaxSubtreeCount(constant, 0);
103      SetMinSubtreeCount(variableSymbol, 0);
104      SetMaxSubtreeCount(variableSymbol, 0);
105
106      // allow all symbols as children of the start-symbol
107      foreach (Symbol symb in allSymbols) {
108        for (int i = 0; i < GetMaxSubtreeCount(StartSymbol); i++)
109          SetAllowedChild(StartSymbol, symb, i);
110      }
111
112      // allow all symbols as children of all symbols
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}
Note: See TracBrowser for help on using the repository browser.