Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis/3.3/Symbolic/BasicExpressionGrammar.cs @ 10743

Last change on this file since 10743 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: 3.9 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.Core;
24using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
25using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
28using HeuristicLab.Common;
29namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
30  [StorableClass]
31  [Item("BasicExpressionGrammar", "Represents a grammar for functional expressions using only arithmetic operations.")]
32  public class BasicExpressionGrammar : DefaultSymbolicExpressionGrammar {
33    [Storable]
34    private HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable variableSymbol;
35
36    [StorableConstructor]
37    protected BasicExpressionGrammar(bool deserializing) : base(deserializing) { }
38    protected BasicExpressionGrammar(BasicExpressionGrammar original, Cloner cloner)
39      : base(original, cloner) {
40      foreach (var symb in Symbols) {
41        var varSym = symb as HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable;
42        if (varSym != null) this.variableSymbol = varSym;
43        break;
44      }
45    }
46    public BasicExpressionGrammar()
47      : base() {
48      Initialize();
49    }
50
51    public override IDeepCloneable Clone(Cloner cloner) {
52      return new BasicExpressionGrammar(this, cloner);
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 constant = new Constant();
61      var log = new Logarithm();
62      var exp = new Exponential();
63
64      constant.MinValue = -20;
65      constant.MaxValue = 20;
66      variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable();
67
68      var allSymbols = new List<Symbol>() { add, sub, mul, div, log, exp, constant, variableSymbol };
69      var arithSymbols = new List<Symbol>() { add, sub, mul, div };
70      var funSymbols = new List<Symbol>() { log, exp };
71
72      foreach (var symb in allSymbols)
73        AddSymbol(symb);
74
75      foreach (var funSymb in arithSymbols) {
76        SetMinSubtreeCount(funSymb, 2);
77        SetMaxSubtreeCount(funSymb, 2);
78      }
79      foreach (var funSymb in funSymbols) {
80        SetMinSubtreeCount(funSymb, 1);
81        SetMaxSubtreeCount(funSymb, 1);
82      }
83
84      SetMinSubtreeCount(constant, 0);
85      SetMaxSubtreeCount(constant, 0);
86      SetMinSubtreeCount(variableSymbol, 0);
87      SetMaxSubtreeCount(variableSymbol, 0);
88
89      // allow each symbol as child of the start symbol
90      foreach (var symb in allSymbols) {
91        SetAllowedChild(StartSymbol, symb, 0);
92      }
93
94      // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)
95      foreach (var parent in allSymbols) {
96        for (int i = 0; i < GetMaxSubtreeCount(parent); i++)
97          foreach (var child in allSymbols) {
98            SetAllowedChild(parent, child, i);
99          }
100      }
101    }
102  }
103}
Note: See TracBrowser for help on using the repository browser.