Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.ExternalEvaluation.GP/3.5/ExternalEvaluationExpressionGrammar.cs @ 18132

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

#3140: merged r18091:18131 from branch to trunk

File size: 4.3 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 System.Linq;
24using HEAL.Attic;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Problems.DataAnalysis.Symbolic;
29
30namespace HeuristicLab.Problems.ExternalEvaluation.GP {
31  [StorableType("747A7784-EF15-4CEF-A621-79A9071A69F5")]
32  [Item("ExternalEvaluationExpressionGrammar", "Represents a grammar for functional expressions using all available functions.")]
33  public class ExternalEvaluationExpressionGrammar : DataAnalysisGrammar {
34    [Storable]
35    private HeuristicLab.Problems.DataAnalysis.Symbolic.Variable variableSymbol;
36    [StorableConstructor]
37    protected ExternalEvaluationExpressionGrammar(StorableConstructorFlag _) : base(_) { }
38    protected ExternalEvaluationExpressionGrammar(ExternalEvaluationExpressionGrammar original, Cloner cloner) : base(original, cloner) { }
39    public override IDeepCloneable Clone(Cloner cloner) {
40      return new ExternalEvaluationExpressionGrammar(this, cloner);
41    }
42
43    public ExternalEvaluationExpressionGrammar()
44      : base("ExternalEvaluationExpressionGrammar", "Represents a grammar for functional expressions using all available functions.") {
45      Initialize();
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 number = new Number();
66      number.MinValue = -20;
67      number.MaxValue = 20;
68      var constant = new Constant();
69      variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Variable();
70
71      var allSymbols = new List<Symbol>() { add, sub, mul, div, mean, sin, cos, tan, log, exp, @if, gt, lt, and, or, not, number, constant, variableSymbol };
72      var unaryFunctionSymbols = new List<Symbol>() { sin, cos, tan, log, exp, not };
73      var binaryFunctionSymbols = new List<Symbol>() { gt, lt };
74      var functionSymbols = new List<Symbol>() { add, sub, mul, div, mean, and, or };
75
76      foreach (var symb in allSymbols)
77        AddSymbol(symb);
78
79      foreach (var funSymb in functionSymbols) {
80        SetSubtreeCount(funSymb, 1, 3);
81      }
82      foreach (var funSymb in unaryFunctionSymbols) {
83        SetSubtreeCount(funSymb, 1, 1);
84      }
85      foreach (var funSymb in binaryFunctionSymbols) {
86        SetSubtreeCount(funSymb, 2, 2);
87      }
88
89      SetSubtreeCount(@if, 3, 3);
90      SetSubtreeCount(number, 0, 0);
91      SetSubtreeCount(constant,0, 0);
92      SetSubtreeCount(variableSymbol, 0, 0);
93
94      // allow each symbol as child of the start symbol
95      foreach (var symb in allSymbols) {
96        AddAllowedChildSymbol(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 < GetMaximumSubtreeCount(parent); i++)
102          foreach (var child in allSymbols) {
103            AddAllowedChildSymbol(parent, child, i);
104          }
105      }
106
107      Symbols.First(s => s is Constant).Enabled = false;
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.