Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.1/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/Grammars.cs @ 15592

Last change on this file since 15592 was 4106, checked in by gkronber, 14 years ago

Fixed bugs in SubtreeCrossover, ArgumentCreater and ArgumentDuplicater and updated unit tests for symbolic expression tree operators. #1103

File size: 3.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
24using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
25
26namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._3.Tests {
27  public static class Grammars {
28    private class Addition : Symbol { public Addition() : base("Addition", "") { } }
29    private class Subtraction : Symbol { public Subtraction() : base("Subtraction", "") { } }
30    private class Multiplication : Symbol { public Multiplication() : base("Multiplication", "") { } }
31    private class Division : Symbol { public Division() : base("Division", "") { } }
32    private class Terminal : Symbol { public Terminal() : base("Terminal", "") { } }
33
34    private class SimpleArithmeticGrammar : DefaultSymbolicExpressionGrammar {
35      public SimpleArithmeticGrammar()
36        : base() {
37        Initialize();
38      }
39
40      private void Initialize() {
41        var add = new Addition();
42        var sub = new Subtraction();
43        var mul = new Multiplication();
44        var div = new Division();
45        var terminal = new Terminal();
46
47        var allSymbols = new List<Symbol>() { add, sub, mul, div, terminal };
48        var functionSymbols = new List<Symbol>() { add, sub, mul, div };
49        foreach (var symb in allSymbols)
50          AddSymbol(symb);
51
52        foreach (var funSymb in functionSymbols) {
53          SetMinSubtreeCount(funSymb, 1);
54          SetMaxSubtreeCount(funSymb, 3);
55        }
56        SetMinSubtreeCount(terminal, 0);
57        SetMaxSubtreeCount(terminal, 0);
58
59        // allow each symbol as child of the start symbol
60        foreach (var symb in allSymbols) {
61          SetAllowedChild(StartSymbol, symb, 0);
62        }
63
64        // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)
65        foreach (var parent in allSymbols) {
66          for (int i = 0; i < GetMaxSubtreeCount(parent); i++)
67            foreach (var child in allSymbols) {
68              SetAllowedChild(parent, child, i);
69            }
70        }
71      }
72    }
73
74    public static ISymbolicExpressionGrammar CreateSimpleArithmeticGrammar() {
75      var g = new GlobalSymbolicExpressionGrammar(new SimpleArithmeticGrammar());
76      g.MaxFunctionArguments = 0;
77      g.MinFunctionArguments = 0;
78      g.MaxFunctionDefinitions = 0;
79      g.MinFunctionDefinitions = 0;
80      return g;
81    }
82
83    public static ISymbolicExpressionGrammar CreateArithmeticAndAdfGrammar() {
84      var g = new GlobalSymbolicExpressionGrammar(new SimpleArithmeticGrammar());
85      g.MaxFunctionArguments = 3;
86      g.MinFunctionArguments = 0;
87      g.MaxFunctionDefinitions = 3;
88      g.MinFunctionDefinitions = 0;
89      return g;
90    }
91  }
92}
Note: See TracBrowser for help on using the repository browser.