Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Tests/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4/Grammars.cs @ 9596

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

#1777: Extracted unit tests into a separate solution.

File size: 8.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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 System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Problems.DataAnalysis.Symbolic;
28
29namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_34.Tests {
30  public static class Grammars {
31    [StorableClass]
32    private class Addition : Symbol {
33      private const int minimumArity = 1;
34      private const int maximumArity = byte.MaxValue;
35
36      public override int MinimumArity {
37        get { return minimumArity; }
38      }
39      public override int MaximumArity {
40        get { return maximumArity; }
41      }
42
43      [StorableConstructor]
44      protected Addition(bool deserializing) : base(deserializing) { }
45      protected Addition(Addition original, Cloner cloner) : base(original, cloner) { }
46      public Addition() : base("Addition", "") { }
47      public override IDeepCloneable Clone(Cloner cloner) {
48        return new Addition(this, cloner);
49      }
50    }
51
52    [StorableClass]
53    private class Subtraction : Symbol {
54      private const int minimumArity = 1;
55      private const int maximumArity = byte.MaxValue;
56
57      public override int MinimumArity {
58        get { return minimumArity; }
59      }
60      public override int MaximumArity {
61        get { return maximumArity; }
62      }
63
64      [StorableConstructor]
65      protected Subtraction(bool deserializing) : base(deserializing) { }
66      protected Subtraction(Subtraction original, Cloner cloner) : base(original, cloner) { }
67      public Subtraction() : base("Subtraction", "") { }
68      public override IDeepCloneable Clone(Cloner cloner) {
69        return new Subtraction(this, cloner);
70      }
71    }
72
73    [StorableClass]
74    private class Multiplication : Symbol {
75      private const int minimumArity = 1;
76      private const int maximumArity = byte.MaxValue;
77
78      public override int MinimumArity {
79        get { return minimumArity; }
80      }
81      public override int MaximumArity {
82        get { return maximumArity; }
83      }
84
85      [StorableConstructor]
86      protected Multiplication(bool deserializing) : base(deserializing) { }
87      protected Multiplication(Multiplication original, Cloner cloner) : base(original, cloner) { }
88      public Multiplication() : base("Multiplication", "") { }
89      public override IDeepCloneable Clone(Cloner cloner) {
90        return new Multiplication(this, cloner);
91      }
92    }
93
94    [StorableClass]
95    private class Division : Symbol {
96      private const int minimumArity = 1;
97      private const int maximumArity = byte.MaxValue;
98
99      public override int MinimumArity {
100        get { return minimumArity; }
101      }
102      public override int MaximumArity {
103        get { return maximumArity; }
104      }
105
106      [StorableConstructor]
107      protected Division(bool deserializing) : base(deserializing) { }
108      protected Division(Division original, Cloner cloner) : base(original, cloner) { }
109      public Division() : base("Division", "") { }
110      public override IDeepCloneable Clone(Cloner cloner) {
111        return new Division(this, cloner);
112      }
113    }
114
115    [StorableClass]
116    private class Terminal : Symbol {
117      private const int minimumArity = 0;
118      private const int maximumArity = 0;
119
120      public override int MinimumArity {
121        get { return minimumArity; }
122      }
123      public override int MaximumArity {
124        get { return maximumArity; }
125      }
126
127      [StorableConstructor]
128      protected Terminal(bool deserializing) : base(deserializing) { }
129      protected Terminal(Terminal original, Cloner cloner) : base(original, cloner) { }
130      public Terminal() : base("Terminal", "") { }
131      public override IDeepCloneable Clone(Cloner cloner) {
132        return new Terminal(this, cloner);
133      }
134
135      public override ISymbolicExpressionTreeNode CreateTreeNode() {
136        return new TerminalNode(this);
137      }
138    }
139
140    [StorableClass]
141    private class TerminalNode : SymbolicExpressionTreeTerminalNode {
142      public override bool HasLocalParameters { get { return true; } }
143      private double value;
144      protected TerminalNode(TerminalNode original, Cloner cloner)
145        : base(original, cloner) {
146        this.value = original.value;
147      }
148      [StorableConstructor]
149      protected TerminalNode(bool deserializing) : base(deserializing) { }
150      public TerminalNode(Terminal symbol) : base(symbol) { }
151
152      public override IDeepCloneable Clone(Cloner cloner) {
153        return new TerminalNode(this, cloner);
154      }
155      public override void ResetLocalParameters(Core.IRandom random) {
156        base.ResetLocalParameters(random);
157        value = random.NextDouble();
158      }
159      public override void ShakeLocalParameters(Core.IRandom random, double shakingFactor) {
160        base.ShakeLocalParameters(random, shakingFactor);
161        value = random.NextDouble();
162      }
163      public override string ToString() {
164        return value.ToString("E4");
165      }
166    }
167
168    [StorableClass]
169    private class SimpleArithmeticGrammar : SymbolicExpressionGrammar {
170      [StorableConstructor]
171      protected SimpleArithmeticGrammar(bool deserializing) : base(deserializing) { }
172      protected SimpleArithmeticGrammar(SimpleArithmeticGrammar original, Cloner cloner) : base(original, cloner) { }
173      public SimpleArithmeticGrammar()
174        : base("Grammar for unit tests", "") {
175        Initialize();
176      }
177
178      public override IDeepCloneable Clone(Cloner cloner) {
179        return new SimpleArithmeticGrammar(this, cloner);
180      }
181
182      private void Initialize() {
183        var add = new Addition();
184        var sub = new Subtraction();
185        var mul = new Multiplication();
186        var div = new Division();
187        div.InitialFrequency = 0.0; // disable division symbol
188        var terminal = new Terminal();
189
190        var allSymbols = new List<Symbol>() { add, sub, mul, div, terminal };
191        var functionSymbols = new List<Symbol>() { add, sub, mul, div };
192        foreach (var symb in allSymbols)
193          AddSymbol(symb);
194
195        foreach (var funSymb in functionSymbols) {
196          SetSubtreeCount(funSymb, 1, 3);
197        }
198        SetSubtreeCount(terminal, 0, 0);
199
200        // allow each symbol as child of the start symbol
201        foreach (var symb in allSymbols) {
202          AddAllowedChildSymbol(StartSymbol, symb);
203          AddAllowedChildSymbol(DefunSymbol, symb);
204        }
205
206        // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)
207        foreach (var parent in functionSymbols) {
208          foreach (var child in allSymbols) {
209            AddAllowedChildSymbol(parent, child);
210          }
211        }
212      }
213    }
214
215    public static ISymbolicExpressionGrammar CreateSimpleArithmeticGrammar() {
216      var g = new TypeCoherentExpressionGrammar();
217      g.ConfigureAsDefaultRegressionGrammar();
218      g.Symbols.OfType<Variable>().First().Enabled = false;
219      //var g = new SimpleArithmeticGrammar();
220      g.MaximumFunctionArguments = 0;
221      g.MinimumFunctionArguments = 0;
222      g.MaximumFunctionDefinitions = 0;
223      g.MinimumFunctionDefinitions = 0;
224      return g;
225    }
226
227    public static ISymbolicExpressionGrammar CreateArithmeticAndAdfGrammar() {
228      var g = new TypeCoherentExpressionGrammar();
229      g.ConfigureAsDefaultRegressionGrammar();
230      g.Symbols.OfType<Variable>().First().Enabled = false;
231      g.MaximumFunctionArguments = 3;
232      g.MinimumFunctionArguments = 0;
233      g.MaximumFunctionDefinitions = 3;
234      g.MinimumFunctionDefinitions = 0;
235      return g;
236    }
237  }
238}
Note: See TracBrowser for help on using the repository browser.