Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.ExternalEvaluation.GP/3.3/Grammar/FullFunctionalExpressionGrammar.cs @ 4722

Last change on this file since 4722 was 4722, checked in by swagner, 13 years ago

Merged cloning refactoring branch back into trunk (#922)

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