Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/Grammars.cs @ 5691

Last change on this file since 5691 was 5691, checked in by mkommend, 13 years ago

#1418: Fixed error in grammars.

File size: 6.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.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.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      public override ISymbolicExpressionTreeNode CreateTreeNode() {
65        return new TerminalNode(this);
66      }
67    }
68
69    private class TerminalNode : SymbolicExpressionTreeTerminalNode {
70      public override bool HasLocalParameters { get { return true; } }
71      private double value;
72      protected TerminalNode(TerminalNode original, Cloner cloner)
73        : base(original, cloner) {
74        this.value = original.value;
75      }
76      [StorableConstructor]
77      protected TerminalNode(bool deserializing) : base(deserializing) { }
78      public TerminalNode(Terminal symbol) : base(symbol) { }
79
80      public override IDeepCloneable Clone(Cloner cloner) {
81        return new TerminalNode(this, cloner);
82      }
83      public override void ResetLocalParameters(Core.IRandom random) {
84        base.ResetLocalParameters(random);
85        value = random.NextDouble();
86      }
87      public override void ShakeLocalParameters(Core.IRandom random, double shakingFactor) {
88        base.ShakeLocalParameters(random, shakingFactor);
89        value = random.NextDouble();
90      }
91      public override string ToString() {
92        return value.ToString("E4");
93      }
94    }
95
96    private class SimpleArithmeticGrammar : SymbolicExpressionGrammar {
97      protected SimpleArithmeticGrammar(SimpleArithmeticGrammar original, Cloner cloner) : base(original, cloner) { }
98      public SimpleArithmeticGrammar()
99        : base("Grammar for unit tests", "") {
100        Initialize();
101      }
102
103      public override IDeepCloneable Clone(Cloner cloner) {
104        return new SimpleArithmeticGrammar(this, cloner);
105      }
106
107      private void Initialize() {
108        var add = new Addition();
109        var sub = new Subtraction();
110        var mul = new Multiplication();
111        var div = new Division();
112        div.InitialFrequency = 0.0; // disable division symbol
113        var terminal = new Terminal();
114
115        var allSymbols = new List<Symbol>() { add, sub, mul, div, terminal };
116        var functionSymbols = new List<Symbol>() { add, sub, mul, div };
117        foreach (var symb in allSymbols)
118          AddSymbol(symb);
119
120        foreach (var funSymb in functionSymbols) {
121          SetSubtreeCount(funSymb, 1, 3);
122        }
123        SetSubtreeCount(terminal, 0, 0);
124
125        // allow each symbol as child of the start symbol
126        foreach (var symb in allSymbols) {
127          AddAllowedChildSymbol(StartSymbol, symb);
128          AddAllowedChildSymbol(DefunSymbol, symb);
129        }
130
131        // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)
132        foreach (var parent in functionSymbols) {
133          foreach (var child in allSymbols) {
134            AddAllowedChildSymbol(parent, child);
135          }
136        }
137      }
138    }
139
140    public static ISymbolicExpressionGrammar CreateSimpleArithmeticGrammar() {
141      var g = new SimpleArithmeticGrammar();
142      g.MaximumFunctionArguments = 0;
143      g.MinimumFunctionArguments = 0;
144      g.MaximumFunctionDefinitions = 0;
145      g.MinimumFunctionDefinitions = 0;
146      return g;
147    }
148
149    public static ISymbolicExpressionGrammar CreateArithmeticAndAdfGrammar() {
150      var g = new SimpleArithmeticGrammar();
151      g.MaximumFunctionArguments = 3;
152      g.MinimumFunctionArguments = 0;
153      g.MaximumFunctionDefinitions = 3;
154      g.MinimumFunctionDefinitions = 0;
155      return g;
156    }
157  }
158}
Note: See TracBrowser for help on using the repository browser.