Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.TimeSeries/HeuristicLab.Tests/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4/Grammars.cs @ 7461

Last change on this file since 7461 was 7268, checked in by gkronber, 13 years ago

#1081: merged r7214:7266 from trunk into time series branch.

File size: 8.5 KB
RevLine 
[3360]1#region License Information
2/* HeuristicLab
[7268]3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3360]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
[3338]22using System.Collections.Generic;
[6814]23using System.Linq;
[4722]24using HeuristicLab.Common;
[3338]25using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
[5567]26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[6814]27using HeuristicLab.Problems.DataAnalysis.Symbolic;
[3338]28
[5549]29namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests {
[3338]30  public static class Grammars {
[6375]31    [StorableClass]
[4722]32    private class Addition : Symbol {
[6803]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
[6375]43      [StorableConstructor]
44      protected Addition(bool deserializing) : base(deserializing) { }
[4722]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    }
[6375]51
52    [StorableClass]
[4722]53    private class Subtraction : Symbol {
[6803]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
[6375]64      [StorableConstructor]
65      protected Subtraction(bool deserializing) : base(deserializing) { }
[4722]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    }
[6375]72
73    [StorableClass]
[4722]74    private class Multiplication : Symbol {
[6803]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
[6375]85      [StorableConstructor]
86      protected Multiplication(bool deserializing) : base(deserializing) { }
[4722]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    }
[6375]93
94    [StorableClass]
[4722]95    private class Division : Symbol {
[6803]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
[6375]106      [StorableConstructor]
107      protected Division(bool deserializing) : base(deserializing) { }
[4722]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    }
[6375]114
115    [StorableClass]
[4722]116    private class Terminal : Symbol {
[6803]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
[6375]127      [StorableConstructor]
128      protected Terminal(bool deserializing) : base(deserializing) { }
[4722]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      }
[5567]134
135      public override ISymbolicExpressionTreeNode CreateTreeNode() {
136        return new TerminalNode(this);
137      }
[4722]138    }
[3338]139
[6375]140    [StorableClass]
[5567]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
[6375]168    [StorableClass]
[5686]169    private class SimpleArithmeticGrammar : SymbolicExpressionGrammar {
[6375]170      [StorableConstructor]
171      protected SimpleArithmeticGrammar(bool deserializing) : base(deserializing) { }
[4722]172      protected SimpleArithmeticGrammar(SimpleArithmeticGrammar original, Cloner cloner) : base(original, cloner) { }
[3338]173      public SimpleArithmeticGrammar()
[5691]174        : base("Grammar for unit tests", "") {
[3338]175        Initialize();
176      }
177
[4722]178      public override IDeepCloneable Clone(Cloner cloner) {
179        return new SimpleArithmeticGrammar(this, cloner);
180      }
181
[3338]182      private void Initialize() {
183        var add = new Addition();
184        var sub = new Subtraction();
185        var mul = new Multiplication();
186        var div = new Division();
[5411]187        div.InitialFrequency = 0.0; // disable division symbol
[3338]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) {
[5686]196          SetSubtreeCount(funSymb, 1, 3);
[3338]197        }
[5686]198        SetSubtreeCount(terminal, 0, 0);
[3338]199
200        // allow each symbol as child of the start symbol
201        foreach (var symb in allSymbols) {
[5686]202          AddAllowedChildSymbol(StartSymbol, symb);
203          AddAllowedChildSymbol(DefunSymbol, symb);
[3338]204        }
205
206        // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)
[5686]207        foreach (var parent in functionSymbols) {
208          foreach (var child in allSymbols) {
209            AddAllowedChildSymbol(parent, child);
210          }
[3338]211        }
212      }
213    }
214
[5686]215    public static ISymbolicExpressionGrammar CreateSimpleArithmeticGrammar() {
[6814]216      var g = new TypeCoherentExpressionGrammar();
217      g.ConfigureAsDefaultRegressionGrammar();
218      g.Symbols.OfType<Variable>().First().Enabled = false;
219      //var g = new SimpleArithmeticGrammar();
[5686]220      g.MaximumFunctionArguments = 0;
221      g.MinimumFunctionArguments = 0;
222      g.MaximumFunctionDefinitions = 0;
223      g.MinimumFunctionDefinitions = 0;
[3338]224      return g;
225    }
226
[5686]227    public static ISymbolicExpressionGrammar CreateArithmeticAndAdfGrammar() {
[6814]228      var g = new TypeCoherentExpressionGrammar();
229      g.ConfigureAsDefaultRegressionGrammar();
230      g.Symbols.OfType<Variable>().First().Enabled = false;
[5686]231      g.MaximumFunctionArguments = 3;
232      g.MinimumFunctionArguments = 0;
233      g.MaximumFunctionDefinitions = 3;
234      g.MinimumFunctionDefinitions = 0;
[3338]235      return g;
236    }
237  }
238}
Note: See TracBrowser for help on using the repository browser.