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 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
24 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
|
---|
25 |
|
---|
26 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._3.Tests {
|
---|
27 | public static class Grammars {
|
---|
28 | private class Addition : Symbol { }
|
---|
29 | private class Subtraction : Symbol { }
|
---|
30 | private class Multiplication : Symbol { }
|
---|
31 | private class Division : Symbol { }
|
---|
32 | private class Terminal : Symbol { }
|
---|
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 | public static void HasValidAdfGrammars(SymbolicExpressionTree tree) {
|
---|
93 | //Assert.AreEqual(tree.Root.Grammar.Symbols.Count(), 8);
|
---|
94 | //Assert.AreEqual(tree.Root.GetAllowedSymbols(0).Count(), 1); // only the start symbol is allowed
|
---|
95 | //// we allow 3 ADF branches
|
---|
96 | //Assert.AreEqual(tree.Root.GetAllowedSymbols(1).Count(), 1); // only the defun branch is allowed
|
---|
97 | //Assert.AreEqual(tree.Root.GetAllowedSymbols(2).Count(), 1); // only the defun symbol is allowed
|
---|
98 | //Assert.AreEqual(tree.Root.GetAllowedSymbols(3).Count(), 1); // only the defun symbol is allowed
|
---|
99 | //foreach (var subtree in tree.Root.SubTrees) {
|
---|
100 | // // check consistency of each sub-tree grammar independently
|
---|
101 | // var allowedSymbols = subtree.GetAllowedSymbols(0);
|
---|
102 | // int numberOfAllowedSymbols = allowedSymbols.Count();
|
---|
103 | // foreach (var parent in allowedSymbols) {
|
---|
104 | // for (int argIndex = 0; argIndex < subtree.Grammar.GetMaxSubtreeCount(parent); argIndex++) {
|
---|
105 | // var allowedChildren = from child in subtree.Grammar.Symbols
|
---|
106 | // where subtree.Grammar.IsAllowedChild(parent, child, argIndex)
|
---|
107 | // select child;
|
---|
108 | // Assert.AreEqual(numberOfAllowedSymbols, allowedChildren.Count());
|
---|
109 | // }
|
---|
110 | // }
|
---|
111 | //}
|
---|
112 | }
|
---|
113 |
|
---|
114 | }
|
---|
115 | }
|
---|