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