Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/Grammars.cs @ 3360

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

Fixed bugs in ADF operators and added test classes for ADF operators. #290 (Implement ADFs)

File size: 4.8 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;
23using System.Text;
24using System.Collections.Generic;
25using System.Linq;
26using Microsoft.VisualStudio.TestTools.UnitTesting;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Random;
29using System.Diagnostics;
30using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.GeneralSymbols;
31
32namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._3.Tests {
33  public static class Grammars {
34    private class Addition : Symbol { }
35    private class Subtraction : Symbol { }
36    private class Multiplication : Symbol { }
37    private class Division : Symbol { }
38    private class Terminal : Symbol { }
39
40    private class SimpleArithmeticGrammar : DefaultSymbolicExpressionGrammar {
41      public SimpleArithmeticGrammar()
42        : base() {
43        Initialize();
44      }
45
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 terminal = new Terminal();
52
53        var allSymbols = new List<Symbol>() { add, sub, mul, div, terminal };
54        var functionSymbols = new List<Symbol>() { add, sub, mul, div };
55        foreach (var symb in allSymbols)
56          AddSymbol(symb);
57
58        foreach (var funSymb in functionSymbols) {
59          SetMinSubtreeCount(funSymb, 1);
60          SetMaxSubtreeCount(funSymb, 3);
61        }
62        SetMinSubtreeCount(terminal, 0);
63        SetMaxSubtreeCount(terminal, 0);
64
65        // allow each symbol as child of the start symbol
66        foreach (var symb in allSymbols) {
67          SetAllowedChild(StartSymbol, symb, 0);
68        }
69
70        // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)
71        foreach (var parent in allSymbols) {
72          for (int i = 0; i < GetMaxSubtreeCount(parent); i++)
73            foreach (var child in allSymbols) {
74              SetAllowedChild(parent, child, i);
75            }
76        }
77      }
78    }
79
80    public static ISymbolicExpressionGrammar CreateSimpleArithmeticGrammar() {
81      var g = new GlobalSymbolicExpressionGrammar(new SimpleArithmeticGrammar());
82      g.MaxFunctionArguments = 0;
83      g.MinFunctionArguments = 0;
84      g.MaxFunctionDefinitions = 0;
85      g.MinFunctionDefinitions = 0;
86      return g;
87    }
88
89    public static ISymbolicExpressionGrammar CreateArithmeticAndAdfGrammar() {
90      var g = new GlobalSymbolicExpressionGrammar(new SimpleArithmeticGrammar());
91      g.MaxFunctionArguments = 3;
92      g.MinFunctionArguments = 0;
93      g.MaxFunctionDefinitions = 3;
94      g.MinFunctionDefinitions = 0;
95      return g;
96    }
97
98    public static void HasValidAdfGrammars(SymbolicExpressionTree tree) {
99      Assert.AreEqual(tree.Root.Grammar.Symbols.Count(), 8);
100      Assert.AreEqual(tree.Root.GetAllowedSymbols(0).Count(), 1); // only the start symbol is allowed
101      // we allow 3 ADF branches
102      Assert.AreEqual(tree.Root.GetAllowedSymbols(1).Count(), 1); // only the defun branch is allowed
103      Assert.AreEqual(tree.Root.GetAllowedSymbols(2).Count(), 1); // only the defun symbol is allowed
104      Assert.AreEqual(tree.Root.GetAllowedSymbols(3).Count(), 1); // only the defun symbol is allowed
105      foreach (var subtree in tree.Root.SubTrees) {
106        // check consistency of each sub-tree grammar independently
107        var allowedSymbols = subtree.GetAllowedSymbols(0);
108        int numberOfAllowedSymbols = allowedSymbols.Count();
109        foreach (var parent in allowedSymbols) {
110          for (int argIndex = 0; argIndex < subtree.Grammar.GetMaxSubtreeCount(parent); argIndex++) {
111            var allowedChildren = from child in subtree.Grammar.Symbols
112                                  where subtree.Grammar.IsAllowedChild(parent, child, argIndex)
113                                  select child;
114            Assert.AreEqual(numberOfAllowedSymbols, allowedChildren.Count());
115          }
116        }
117      }
118    }
119
120  }
121}
Note: See TracBrowser for help on using the repository browser.