Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5760 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 5.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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        div.InitialFrequency = 0.0; // disable division symbol
82        var terminal = new Terminal();
83
84        var allSymbols = new List<Symbol>() { add, sub, mul, div, terminal };
85        var functionSymbols = new List<Symbol>() { add, sub, mul, div };
86        foreach (var symb in allSymbols)
87          AddSymbol(symb);
88
89        foreach (var funSymb in functionSymbols) {
90          SetMinSubtreeCount(funSymb, 1);
91          SetMaxSubtreeCount(funSymb, 3);
92        }
93        SetMinSubtreeCount(terminal, 0);
94        SetMaxSubtreeCount(terminal, 0);
95
96        // allow each symbol as child of the start symbol
97        foreach (var symb in allSymbols) {
98          SetAllowedChild(StartSymbol, symb, 0);
99        }
100
101        // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)
102        foreach (var parent in allSymbols) {
103          for (int i = 0; i < GetMaxSubtreeCount(parent); i++)
104            foreach (var child in allSymbols) {
105              SetAllowedChild(parent, child, i);
106            }
107        }
108      }
109    }
110
111    public static ISymbolicExpressionGrammar CreateSimpleArithmeticGrammar() {
112      var g = new GlobalSymbolicExpressionGrammar(new SimpleArithmeticGrammar());
113      g.MaxFunctionArguments = 0;
114      g.MinFunctionArguments = 0;
115      g.MaxFunctionDefinitions = 0;
116      g.MinFunctionDefinitions = 0;
117      return g;
118    }
119
120    public static ISymbolicExpressionGrammar CreateArithmeticAndAdfGrammar() {
121      var g = new GlobalSymbolicExpressionGrammar(new SimpleArithmeticGrammar());
122      g.MaxFunctionArguments = 3;
123      g.MinFunctionArguments = 0;
124      g.MaxFunctionDefinitions = 3;
125      g.MinFunctionDefinitions = 0;
126      return g;
127    }
128  }
129}
Note: See TracBrowser for help on using the repository browser.