Free cookie consent management tool by TermsFeed Policy Generator

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

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

#3140: merged r18091:18131 from branch to trunk

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