[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 {
|
---|
[6760] | 29 |
|
---|
| 30 | [StorableClass]
|
---|
[4722] | 31 | private class Addition : Symbol {
|
---|
[6760] | 32 | [StorableConstructor]
|
---|
| 33 | protected Addition(bool deserializing) : base(deserializing) { }
|
---|
[4722] | 34 | protected Addition(Addition original, Cloner cloner) : base(original, cloner) { }
|
---|
| 35 | public Addition() : base("Addition", "") { }
|
---|
| 36 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 37 | return new Addition(this, cloner);
|
---|
| 38 | }
|
---|
| 39 | }
|
---|
[6760] | 40 |
|
---|
| 41 | [StorableClass]
|
---|
[4722] | 42 | private class Subtraction : Symbol {
|
---|
[6760] | 43 | [StorableConstructor]
|
---|
| 44 | protected Subtraction(bool deserializing) : base(deserializing) { }
|
---|
[4722] | 45 | protected Subtraction(Subtraction original, Cloner cloner) : base(original, cloner) { }
|
---|
| 46 | public Subtraction() : base("Subtraction", "") { }
|
---|
| 47 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 48 | return new Subtraction(this, cloner);
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
[6760] | 51 |
|
---|
| 52 | [StorableClass]
|
---|
[4722] | 53 | private class Multiplication : Symbol {
|
---|
[6760] | 54 | [StorableConstructor]
|
---|
| 55 | protected Multiplication(bool deserializing) : base(deserializing) { }
|
---|
[4722] | 56 | protected Multiplication(Multiplication original, Cloner cloner) : base(original, cloner) { }
|
---|
| 57 | public Multiplication() : base("Multiplication", "") { }
|
---|
| 58 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 59 | return new Multiplication(this, cloner);
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
[6760] | 62 |
|
---|
| 63 | [StorableClass]
|
---|
[4722] | 64 | private class Division : Symbol {
|
---|
[6760] | 65 | [StorableConstructor]
|
---|
| 66 | protected Division(bool deserializing) : base(deserializing) { }
|
---|
[4722] | 67 | protected Division(Division original, Cloner cloner) : base(original, cloner) { }
|
---|
| 68 | public Division() : base("Division", "") { }
|
---|
| 69 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 70 | return new Division(this, cloner);
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
[6760] | 73 |
|
---|
| 74 | [StorableClass]
|
---|
[4722] | 75 | private class Terminal : Symbol {
|
---|
[6760] | 76 | [StorableConstructor]
|
---|
| 77 | protected Terminal(bool deserializing) : base(deserializing) { }
|
---|
[4722] | 78 | protected Terminal(Terminal original, Cloner cloner) : base(original, cloner) { }
|
---|
| 79 | public Terminal() : base("Terminal", "") { }
|
---|
| 80 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 81 | return new Terminal(this, cloner);
|
---|
| 82 | }
|
---|
[5567] | 83 |
|
---|
| 84 | public override ISymbolicExpressionTreeNode CreateTreeNode() {
|
---|
| 85 | return new TerminalNode(this);
|
---|
| 86 | }
|
---|
[4722] | 87 | }
|
---|
[3338] | 88 |
|
---|
[6760] | 89 | [StorableClass]
|
---|
[5567] | 90 | private class TerminalNode : SymbolicExpressionTreeTerminalNode {
|
---|
| 91 | public override bool HasLocalParameters { get { return true; } }
|
---|
| 92 | private double value;
|
---|
| 93 | protected TerminalNode(TerminalNode original, Cloner cloner)
|
---|
| 94 | : base(original, cloner) {
|
---|
| 95 | this.value = original.value;
|
---|
| 96 | }
|
---|
| 97 | [StorableConstructor]
|
---|
| 98 | protected TerminalNode(bool deserializing) : base(deserializing) { }
|
---|
| 99 | public TerminalNode(Terminal symbol) : base(symbol) { }
|
---|
| 100 |
|
---|
| 101 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 102 | return new TerminalNode(this, cloner);
|
---|
| 103 | }
|
---|
| 104 | public override void ResetLocalParameters(Core.IRandom random) {
|
---|
| 105 | base.ResetLocalParameters(random);
|
---|
| 106 | value = random.NextDouble();
|
---|
| 107 | }
|
---|
| 108 | public override void ShakeLocalParameters(Core.IRandom random, double shakingFactor) {
|
---|
| 109 | base.ShakeLocalParameters(random, shakingFactor);
|
---|
| 110 | value = random.NextDouble();
|
---|
| 111 | }
|
---|
| 112 | public override string ToString() {
|
---|
| 113 | return value.ToString("E4");
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 |
|
---|
[6760] | 117 | [StorableClass]
|
---|
[5686] | 118 | private class SimpleArithmeticGrammar : SymbolicExpressionGrammar {
|
---|
[6760] | 119 | [StorableConstructor]
|
---|
| 120 | protected SimpleArithmeticGrammar(bool deserializing) : base(deserializing) { }
|
---|
[4722] | 121 | protected SimpleArithmeticGrammar(SimpleArithmeticGrammar original, Cloner cloner) : base(original, cloner) { }
|
---|
[3338] | 122 | public SimpleArithmeticGrammar()
|
---|
[5691] | 123 | : base("Grammar for unit tests", "") {
|
---|
[3338] | 124 | Initialize();
|
---|
| 125 | }
|
---|
| 126 |
|
---|
[4722] | 127 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 128 | return new SimpleArithmeticGrammar(this, cloner);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[3338] | 131 | private void Initialize() {
|
---|
| 132 | var add = new Addition();
|
---|
| 133 | var sub = new Subtraction();
|
---|
| 134 | var mul = new Multiplication();
|
---|
| 135 | var div = new Division();
|
---|
[5411] | 136 | div.InitialFrequency = 0.0; // disable division symbol
|
---|
[3338] | 137 | var terminal = new Terminal();
|
---|
| 138 |
|
---|
| 139 | var allSymbols = new List<Symbol>() { add, sub, mul, div, terminal };
|
---|
| 140 | var functionSymbols = new List<Symbol>() { add, sub, mul, div };
|
---|
| 141 | foreach (var symb in allSymbols)
|
---|
| 142 | AddSymbol(symb);
|
---|
| 143 |
|
---|
| 144 | foreach (var funSymb in functionSymbols) {
|
---|
[5686] | 145 | SetSubtreeCount(funSymb, 1, 3);
|
---|
[3338] | 146 | }
|
---|
[5686] | 147 | SetSubtreeCount(terminal, 0, 0);
|
---|
[3338] | 148 |
|
---|
| 149 | // allow each symbol as child of the start symbol
|
---|
| 150 | foreach (var symb in allSymbols) {
|
---|
[5686] | 151 | AddAllowedChildSymbol(StartSymbol, symb);
|
---|
| 152 | AddAllowedChildSymbol(DefunSymbol, symb);
|
---|
[3338] | 153 | }
|
---|
| 154 |
|
---|
| 155 | // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)
|
---|
[5686] | 156 | foreach (var parent in functionSymbols) {
|
---|
| 157 | foreach (var child in allSymbols) {
|
---|
| 158 | AddAllowedChildSymbol(parent, child);
|
---|
| 159 | }
|
---|
[3338] | 160 | }
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 |
|
---|
[5686] | 164 | public static ISymbolicExpressionGrammar CreateSimpleArithmeticGrammar() {
|
---|
| 165 | var g = new SimpleArithmeticGrammar();
|
---|
| 166 | g.MaximumFunctionArguments = 0;
|
---|
| 167 | g.MinimumFunctionArguments = 0;
|
---|
| 168 | g.MaximumFunctionDefinitions = 0;
|
---|
| 169 | g.MinimumFunctionDefinitions = 0;
|
---|
[3338] | 170 | return g;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
[5686] | 173 | public static ISymbolicExpressionGrammar CreateArithmeticAndAdfGrammar() {
|
---|
| 174 | var g = new SimpleArithmeticGrammar();
|
---|
| 175 | g.MaximumFunctionArguments = 3;
|
---|
| 176 | g.MinimumFunctionArguments = 0;
|
---|
| 177 | g.MaximumFunctionDefinitions = 3;
|
---|
| 178 | g.MinimumFunctionDefinitions = 0;
|
---|
[3338] | 179 | return g;
|
---|
| 180 | }
|
---|
| 181 | }
|
---|
| 182 | }
|
---|