Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3140_NumberSymbol/HeuristicLab.Problems.ExternalEvaluation.GP/3.5/ExternalEvaluationExpressionGrammar.cs @ 18115

Last change on this file since 18115 was 18115, checked in by gkronber, 2 years ago

#3140: made several more changes for the constant -> number branch

File size: 4.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 HEAL.Attic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27using HeuristicLab.Problems.DataAnalysis.Symbolic;
28
29namespace HeuristicLab.Problems.ExternalEvaluation.GP {
30  [StorableType("747A7784-EF15-4CEF-A621-79A9071A69F5")]
31  [Item("ExternalEvaluationExpressionGrammar", "Represents a grammar for functional expressions using all available functions.")]
32  public class ExternalEvaluationExpressionGrammar : DataAnalysisGrammar {
33    [Storable]
34    private HeuristicLab.Problems.DataAnalysis.Symbolic.Variable variableSymbol;
35    [StorableConstructor]
36    protected ExternalEvaluationExpressionGrammar(StorableConstructorFlag _) : base(_) { }
37    protected ExternalEvaluationExpressionGrammar(ExternalEvaluationExpressionGrammar original, Cloner cloner) : base(original, cloner) { }
38    public override IDeepCloneable Clone(Cloner cloner) {
39      return new ExternalEvaluationExpressionGrammar(this, cloner);
40    }
41
42    public ExternalEvaluationExpressionGrammar()
43      : base("ExternalEvaluationExpressionGrammar", "Represents a grammar for functional expressions using all available functions.") {
44      Initialize();
45    }
46
47    private void Initialize() {
48      var add = new Addition();
49      var sub = new Subtraction();
50      var mul = new Multiplication();
51      var div = new Division();
52      var mean = new Average();
53      var sin = new Sine();
54      var cos = new Cosine();
55      var tan = new Tangent();
56      var log = new Logarithm();
57      var exp = new Exponential();
58      var @if = new IfThenElse();
59      var gt = new GreaterThan();
60      var lt = new LessThan();
61      var and = new And();
62      var or = new Or();
63      var not = new Not();
64      var number = new Number();
65      number.MinValue = -20;
66      number.MaxValue = 20;
67      var constant = new Constant();
68      variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Variable();
69
70      var allSymbols = new List<Symbol>() { add, sub, mul, div, mean, sin, cos, tan, log, exp, @if, gt, lt, and, or, not, number, 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        SetSubtreeCount(funSymb, 1, 3);
80      }
81      foreach (var funSymb in unaryFunctionSymbols) {
82        SetSubtreeCount(funSymb, 1, 1);
83      }
84      foreach (var funSymb in binaryFunctionSymbols) {
85        SetSubtreeCount(funSymb, 2, 2);
86      }
87
88      SetSubtreeCount(@if, 3, 3);
89      SetSubtreeCount(number, 0, 0);
90      SetSubtreeCount(constant,0, 0);
91      SetSubtreeCount(variableSymbol, 0, 0);
92
93      // allow each symbol as child of the start symbol
94      foreach (var symb in allSymbols) {
95        AddAllowedChildSymbol(StartSymbol, symb, 0);
96      }
97
98      // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)
99      foreach (var parent in allSymbols) {
100        for (int i = 0; i < GetMaximumSubtreeCount(parent); i++)
101          foreach (var child in allSymbols) {
102            AddAllowedChildSymbol(parent, child, i);
103          }
104      }
105    }
106  }
107}
Note: See TracBrowser for help on using the repository browser.