Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5635 was 5567, checked in by gkronber, 13 years ago

#1418 improved ChangeNodeTypeManipulation operator to make unit test succeed.

File size: 6.2 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 : DefaultSymbolicExpressionGrammar {
97      protected SimpleArithmeticGrammar(SimpleArithmeticGrammar original, Cloner cloner) : base(original, cloner) { }
98      public SimpleArithmeticGrammar()
99        : base() {
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          SetMinSubtreeCount(funSymb, 1);
122          SetMaxSubtreeCount(funSymb, 3);
123        }
124        SetMinSubtreeCount(terminal, 0);
125        SetMaxSubtreeCount(terminal, 0);
126
127        // allow each symbol as child of the start symbol
128        foreach (var symb in allSymbols) {
129          SetAllowedChild(StartSymbol, symb, 0);
130        }
131
132        // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)
133        foreach (var parent in allSymbols) {
134          for (int i = 0; i < GetMaxSubtreeCount(parent); i++)
135            foreach (var child in allSymbols) {
136              SetAllowedChild(parent, child, i);
137            }
138        }
139      }
140    }
141
142    public static ISymbolicExpressionTreeGrammar CreateSimpleArithmeticGrammar() {
143      var g = new GlobalSymbolicExpressionGrammar(new SimpleArithmeticGrammar());
144      g.MaxFunctionArguments = 0;
145      g.MinFunctionArguments = 0;
146      g.MaxFunctionDefinitions = 0;
147      g.MinFunctionDefinitions = 0;
148      return g;
149    }
150
151    public static ISymbolicExpressionTreeGrammar CreateArithmeticAndAdfGrammar() {
152      var g = new GlobalSymbolicExpressionGrammar(new SimpleArithmeticGrammar());
153      g.MaxFunctionArguments = 3;
154      g.MinFunctionArguments = 0;
155      g.MaxFunctionDefinitions = 3;
156      g.MinFunctionDefinitions = 0;
157      return g;
158    }
159  }
160}
Note: See TracBrowser for help on using the repository browser.