Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2974_Constants_Optimization/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Grammars/ArithmeticExpressionGrammar.cs @ 18060

Last change on this file since 18060 was 17193, checked in by mkommend, 5 years ago

#2974: Merged trunk changes into branch.

File size: 3.5 KB
RevLine 
[3253]1#region License Information
2/* HeuristicLab
[17193]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3253]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;
[4722]23using HeuristicLab.Common;
[3253]24using HeuristicLab.Core;
[4068]25using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
[16676]26using HEAL.Attic;
[6803]27using HeuristicLab.PluginInfrastructure;
[5574]28
[3373]29namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
[6803]30  [NonDiscoverableType]
[16676]31  [StorableType("FCBA02B7-5D29-42F5-A64C-A60AD8EA475D")]
[3294]32  [Item("ArithmeticExpressionGrammar", "Represents a grammar for functional expressions using only arithmetic operations.")]
[5686]33  public class ArithmeticExpressionGrammar : SymbolicExpressionGrammar, ISymbolicDataAnalysisGrammar {
[4722]34
35    [StorableConstructor]
[16676]36    protected ArithmeticExpressionGrammar(StorableConstructorFlag _) : base(_) { }
[4722]37    protected ArithmeticExpressionGrammar(ArithmeticExpressionGrammar original, Cloner cloner) : base(original, cloner) { }
[3294]38    public ArithmeticExpressionGrammar()
[5688]39      : base(ItemAttribute.GetName(typeof(ArithmeticExpressionGrammar)), ItemAttribute.GetDescription(typeof(ArithmeticExpressionGrammar))) {
[3294]40      Initialize();
[3253]41    }
[4722]42    public override IDeepCloneable Clone(Cloner cloner) {
43      return new ArithmeticExpressionGrammar(this, cloner);
44    }
[3253]45
[3294]46    private void Initialize() {
47      var add = new Addition();
48      var sub = new Subtraction();
49      var mul = new Multiplication();
50      var div = new Division();
51      var constant = new Constant();
[3442]52      constant.MinValue = -20;
53      constant.MaxValue = 20;
[5574]54      var variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Variable();
[14826]55      var binFactorVariableSymbol = new BinaryFactorVariable();
56      var factorVariableSymbol = new FactorVariable();
[3253]57
[14826]58      var allSymbols = new List<Symbol>() { add, sub, mul, div, constant, variableSymbol, binFactorVariableSymbol, factorVariableSymbol};
[3294]59      var functionSymbols = new List<Symbol>() { add, sub, mul, div };
[3373]60
[3338]61      foreach (var symb in allSymbols)
62        AddSymbol(symb);
[3253]63
[3338]64      foreach (var funSymb in functionSymbols) {
[5686]65        SetSubtreeCount(funSymb, 1, 3);
[3338]66      }
[5686]67      SetSubtreeCount(constant, 0, 0);
68      SetSubtreeCount(variableSymbol, 0, 0);
[14826]69      SetSubtreeCount(binFactorVariableSymbol, 0, 0);
70      SetSubtreeCount(factorVariableSymbol, 0, 0);
[3253]71
[3338]72      // allow each symbol as child of the start symbol
73      foreach (var symb in allSymbols) {
[5686]74        AddAllowedChildSymbol(StartSymbol, symb);
[5726]75        AddAllowedChildSymbol(DefunSymbol, symb);
[3338]76      }
77
78      // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)
[5686]79      foreach (var parent in functionSymbols) {
80        foreach (var child in allSymbols)
81          AddAllowedChildSymbol(parent, child);
[3253]82      }
83    }
84  }
85}
Note: See TracBrowser for help on using the repository browser.