[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 {
|
---|
[4722] | 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 | }
|
---|
[5567] | 63 |
|
---|
| 64 | public override ISymbolicExpressionTreeNode CreateTreeNode() {
|
---|
| 65 | return new TerminalNode(this);
|
---|
| 66 | }
|
---|
[4722] | 67 | }
|
---|
[3338] | 68 |
|
---|
[5567] | 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 |
|
---|
[5686] | 96 | private class SimpleArithmeticGrammar : SymbolicExpressionGrammar {
|
---|
[4722] | 97 | protected SimpleArithmeticGrammar(SimpleArithmeticGrammar original, Cloner cloner) : base(original, cloner) { }
|
---|
[3338] | 98 | public SimpleArithmeticGrammar()
|
---|
[5691] | 99 | : base("Grammar for unit tests", "") {
|
---|
[3338] | 100 | Initialize();
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[4722] | 103 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 104 | return new SimpleArithmeticGrammar(this, cloner);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[3338] | 107 | private void Initialize() {
|
---|
| 108 | var add = new Addition();
|
---|
| 109 | var sub = new Subtraction();
|
---|
| 110 | var mul = new Multiplication();
|
---|
| 111 | var div = new Division();
|
---|
[5411] | 112 | div.InitialFrequency = 0.0; // disable division symbol
|
---|
[3338] | 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) {
|
---|
[5686] | 121 | SetSubtreeCount(funSymb, 1, 3);
|
---|
[3338] | 122 | }
|
---|
[5686] | 123 | SetSubtreeCount(terminal, 0, 0);
|
---|
[3338] | 124 |
|
---|
| 125 | // allow each symbol as child of the start symbol
|
---|
| 126 | foreach (var symb in allSymbols) {
|
---|
[5686] | 127 | AddAllowedChildSymbol(StartSymbol, symb);
|
---|
| 128 | AddAllowedChildSymbol(DefunSymbol, symb);
|
---|
[3338] | 129 | }
|
---|
| 130 |
|
---|
| 131 | // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)
|
---|
[5686] | 132 | foreach (var parent in functionSymbols) {
|
---|
| 133 | foreach (var child in allSymbols) {
|
---|
| 134 | AddAllowedChildSymbol(parent, child);
|
---|
| 135 | }
|
---|
[3338] | 136 | }
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[5686] | 140 | public static ISymbolicExpressionGrammar CreateSimpleArithmeticGrammar() {
|
---|
| 141 | var g = new SimpleArithmeticGrammar();
|
---|
| 142 | g.MaximumFunctionArguments = 0;
|
---|
| 143 | g.MinimumFunctionArguments = 0;
|
---|
| 144 | g.MaximumFunctionDefinitions = 0;
|
---|
| 145 | g.MinimumFunctionDefinitions = 0;
|
---|
[3338] | 146 | return g;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
[5686] | 149 | public static ISymbolicExpressionGrammar CreateArithmeticAndAdfGrammar() {
|
---|
| 150 | var g = new SimpleArithmeticGrammar();
|
---|
| 151 | g.MaximumFunctionArguments = 3;
|
---|
| 152 | g.MinimumFunctionArguments = 0;
|
---|
| 153 | g.MaximumFunctionDefinitions = 3;
|
---|
| 154 | g.MinimumFunctionDefinitions = 0;
|
---|
[3338] | 155 | return g;
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
| 158 | }
|
---|