Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.ExternalEvaluation.GP/3.5/ExternalEvaluationExpressionGrammar.cs @ 17717

Last change on this file since 17717 was 17498, checked in by mkommend, 5 years ago

#3052: Merged r17413 into stable.

File size: 4.2 KB
RevLine 
[4089]1#region License Information
2/* HeuristicLab
[17181]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[4089]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;
[17498]23using HEAL.Attic;
[4722]24using HeuristicLab.Common;
[4089]25using HeuristicLab.Core;
[17498]26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
[5750]27using HeuristicLab.Problems.DataAnalysis.Symbolic;
[4089]28
29namespace HeuristicLab.Problems.ExternalEvaluation.GP {
[17097]30  [StorableType("747A7784-EF15-4CEF-A621-79A9071A69F5")]
[5750]31  [Item("ExternalEvaluationExpressionGrammar", "Represents a grammar for functional expressions using all available functions.")]
[17498]32  public class ExternalEvaluationExpressionGrammar : DataAnalysisGrammar {
[4089]33    [Storable]
[5750]34    private HeuristicLab.Problems.DataAnalysis.Symbolic.Variable variableSymbol;
[4722]35    [StorableConstructor]
[17097]36    protected ExternalEvaluationExpressionGrammar(StorableConstructorFlag _) : base(_) { }
[5750]37    protected ExternalEvaluationExpressionGrammar(ExternalEvaluationExpressionGrammar original, Cloner cloner) : base(original, cloner) { }
[4722]38    public override IDeepCloneable Clone(Cloner cloner) {
[5750]39      return new ExternalEvaluationExpressionGrammar(this, cloner);
[4722]40    }
[4089]41
[5750]42    public ExternalEvaluationExpressionGrammar()
43      : base("ExternalEvaluationExpressionGrammar", "Represents a grammar for functional expressions using all available functions.") {
[4089]44      Initialize();
45    }
46
[17498]47    private void Initialize() {
[4089]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 constant = new Constant();
65      constant.MinValue = -20;
66      constant.MaxValue = 20;
[5750]67      variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Variable();
[4089]68
69      var allSymbols = new List<Symbol>() { add, sub, mul, div, mean, sin, cos, tan, log, exp, @if, gt, lt, and, or, not, constant, variableSymbol };
70      var unaryFunctionSymbols = new List<Symbol>() { sin, cos, tan, log, exp, not };
71      var binaryFunctionSymbols = new List<Symbol>() { gt, lt };
[4722]72      var functionSymbols = new List<Symbol>() { add, sub, mul, div, mean, and, or };
[4089]73
74      foreach (var symb in allSymbols)
75        AddSymbol(symb);
76
77      foreach (var funSymb in functionSymbols) {
[5750]78        SetSubtreeCount(funSymb, 1, 3);
[4089]79      }
80      foreach (var funSymb in unaryFunctionSymbols) {
[5750]81        SetSubtreeCount(funSymb, 1, 1);
[4089]82      }
83      foreach (var funSymb in binaryFunctionSymbols) {
[5750]84        SetSubtreeCount(funSymb, 2, 2);
[4089]85      }
86
[5750]87      SetSubtreeCount(@if, 3, 3);
88      SetSubtreeCount(constant, 0, 0);
89      SetSubtreeCount(variableSymbol, 0, 0);
[4089]90
91      // allow each symbol as child of the start symbol
92      foreach (var symb in allSymbols) {
[5750]93        AddAllowedChildSymbol(StartSymbol, symb, 0);
[4089]94      }
95
96      // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)
97      foreach (var parent in allSymbols) {
[5750]98        for (int i = 0; i < GetMaximumSubtreeCount(parent); i++)
[4089]99          foreach (var child in allSymbols) {
[5750]100            AddAllowedChildSymbol(parent, child, i);
[4089]101          }
102      }
103    }
104  }
105}
Note: See TracBrowser for help on using the repository browser.