Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/Grammars.cs @ 5166

Last change on this file since 5166 was 4722, checked in by swagner, 14 years ago

Merged cloning refactoring branch back into trunk (#922)

File size: 4.9 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.Common;
24using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
25using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
26
27namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._3.Tests {
28  public static class Grammars {
29    private class Addition : Symbol {
30      protected Addition(Addition original, Cloner cloner) : base(original, cloner) { }
31      public Addition() : base("Addition", "") { }
32      public override IDeepCloneable Clone(Cloner cloner) {
33        return new Addition(this, cloner);
34      }
35    }
36    private class Subtraction : Symbol {
37      protected Subtraction(Subtraction original, Cloner cloner) : base(original, cloner) { }
38      public Subtraction() : base("Subtraction", "") { }
39      public override IDeepCloneable Clone(Cloner cloner) {
40        return new Subtraction(this, cloner);
41      }
42    }
43    private class Multiplication : Symbol {
44      protected Multiplication(Multiplication original, Cloner cloner) : base(original, cloner) { }
45      public Multiplication() : base("Multiplication", "") { }
46      public override IDeepCloneable Clone(Cloner cloner) {
47        return new Multiplication(this, cloner);
48      }
49    }
50    private class Division : Symbol {
51      protected Division(Division original, Cloner cloner) : base(original, cloner) { }
52      public Division() : base("Division", "") { }
53      public override IDeepCloneable Clone(Cloner cloner) {
54        return new Division(this, cloner);
55      }
56    }
57    private class Terminal : Symbol {
58      protected Terminal(Terminal original, Cloner cloner) : base(original, cloner) { }
59      public Terminal() : base("Terminal", "") { }
60      public override IDeepCloneable Clone(Cloner cloner) {
61        return new Terminal(this, cloner);
62      }
63    }
64
65    private class SimpleArithmeticGrammar : DefaultSymbolicExpressionGrammar {
66      protected SimpleArithmeticGrammar(SimpleArithmeticGrammar original, Cloner cloner) : base(original, cloner) { }
67      public SimpleArithmeticGrammar()
68        : base() {
69        Initialize();
70      }
71
72      public override IDeepCloneable Clone(Cloner cloner) {
73        return new SimpleArithmeticGrammar(this, cloner);
74      }
75
76      private void Initialize() {
77        var add = new Addition();
78        var sub = new Subtraction();
79        var mul = new Multiplication();
80        var div = new Division();
81        var terminal = new Terminal();
82
83        var allSymbols = new List<Symbol>() { add, sub, mul, div, terminal };
84        var functionSymbols = new List<Symbol>() { add, sub, mul, div };
85        foreach (var symb in allSymbols)
86          AddSymbol(symb);
87
88        foreach (var funSymb in functionSymbols) {
89          SetMinSubtreeCount(funSymb, 1);
90          SetMaxSubtreeCount(funSymb, 3);
91        }
92        SetMinSubtreeCount(terminal, 0);
93        SetMaxSubtreeCount(terminal, 0);
94
95        // allow each symbol as child of the start symbol
96        foreach (var symb in allSymbols) {
97          SetAllowedChild(StartSymbol, symb, 0);
98        }
99
100        // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)
101        foreach (var parent in allSymbols) {
102          for (int i = 0; i < GetMaxSubtreeCount(parent); i++)
103            foreach (var child in allSymbols) {
104              SetAllowedChild(parent, child, i);
105            }
106        }
107      }
108    }
109
110    public static ISymbolicExpressionGrammar CreateSimpleArithmeticGrammar() {
111      var g = new GlobalSymbolicExpressionGrammar(new SimpleArithmeticGrammar());
112      g.MaxFunctionArguments = 0;
113      g.MinFunctionArguments = 0;
114      g.MaxFunctionDefinitions = 0;
115      g.MinFunctionDefinitions = 0;
116      return g;
117    }
118
119    public static ISymbolicExpressionGrammar CreateArithmeticAndAdfGrammar() {
120      var g = new GlobalSymbolicExpressionGrammar(new SimpleArithmeticGrammar());
121      g.MaxFunctionArguments = 3;
122      g.MinFunctionArguments = 0;
123      g.MaxFunctionDefinitions = 3;
124      g.MinFunctionDefinitions = 0;
125      return g;
126    }
127  }
128}
Note: See TracBrowser for help on using the repository browser.