Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Symbolic/FullFunctionalExpressionGrammar.cs @ 3921

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

Extended set of available functions for symbolic regression and added test cases for the extended function set. #1013

File size: 4.1 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;
32namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
33  [StorableClass]
34  [Item("FullFunctionalExpressionGrammar", "Represents a grammar for functional expressions using all available functions.")]
35  public class FullFunctionalExpressionGrammar : DefaultSymbolicExpressionGrammar {
36    [Storable]
37    private HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable variableSymbol;
38
39    public FullFunctionalExpressionGrammar()
40      : base() {
41      Initialize();
42    }
43
44    private void Initialize() {
45      var add = new Addition();
46      var sub = new Subtraction();
47      var mul = new Multiplication();
48      var div = new Division();
49      var mean = new Average();
50      var sin = new Sine();
51      var cos = new Cosine();
52      var tan = new Tangent();
53      var log = new Logarithm();
54      var exp = new Exponential();
55      var @if = new IfThenElse();
56      var gt = new GreaterThan();
57      var lt = new LessThan();
58      var and = new And();
59      var or = new Or();
60      var not = new Not();
61      var constant = new Constant();
62      constant.MinValue = -20;
63      constant.MaxValue = 20;
64      variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable();
65
66      var allSymbols = new List<Symbol>() { add, sub, mul, div, mean, sin, cos, tan, log, exp, @if, gt, lt, and, or, not, constant, variableSymbol };
67      var unaryFunctionSymbols = new List<Symbol>() { sin, cos, tan, log, exp, not };
68      var binaryFunctionSymbols = new List<Symbol>() { gt, lt };
69      var functionSymbols = new List<Symbol>() { add, sub, mul, div, mean, and, or};
70
71      foreach (var symb in allSymbols)
72        AddSymbol(symb);
73
74      foreach (var funSymb in functionSymbols) {
75        SetMinSubtreeCount(funSymb, 1);
76        SetMaxSubtreeCount(funSymb, 3);
77      }
78      foreach (var funSymb in unaryFunctionSymbols) {
79        SetMinSubtreeCount(funSymb, 1);
80        SetMaxSubtreeCount(funSymb, 1);
81      }
82      foreach (var funSymb in binaryFunctionSymbols) {
83        SetMinSubtreeCount(funSymb, 2);
84        SetMaxSubtreeCount(funSymb, 2);
85      }
86
87      SetMinSubtreeCount(@if, 3);
88      SetMaxSubtreeCount(@if, 3);
89      SetMinSubtreeCount(constant, 0);
90      SetMaxSubtreeCount(constant, 0);
91      SetMinSubtreeCount(variableSymbol, 0);
92      SetMaxSubtreeCount(variableSymbol, 0);
93
94      // allow each symbol as child of the start symbol
95      foreach (var symb in allSymbols) {
96        SetAllowedChild(StartSymbol, symb, 0);
97      }
98
99      // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)
100      foreach (var parent in allSymbols) {
101        for (int i = 0; i < GetMaxSubtreeCount(parent); i++)
102          foreach (var child in allSymbols) {
103            SetAllowedChild(parent, child, i);
104          }
105      }
106    }
107  }
108}
Note: See TracBrowser for help on using the repository browser.