[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 |
|
---|
[3338] | 96 | private class SimpleArithmeticGrammar : DefaultSymbolicExpressionGrammar {
|
---|
[4722] | 97 | protected SimpleArithmeticGrammar(SimpleArithmeticGrammar original, Cloner cloner) : base(original, cloner) { }
|
---|
[3338] | 98 | public SimpleArithmeticGrammar()
|
---|
| 99 | : base() {
|
---|
| 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) {
|
---|
| 121 | SetMinSubtreeCount(funSymb, 1);
|
---|
| 122 | SetMaxSubtreeCount(funSymb, 3);
|
---|
| 123 | }
|
---|
| 124 | SetMinSubtreeCount(terminal, 0);
|
---|
| 125 | SetMaxSubtreeCount(terminal, 0);
|
---|
| 126 |
|
---|
| 127 | // allow each symbol as child of the start symbol
|
---|
| 128 | foreach (var symb in allSymbols) {
|
---|
| 129 | SetAllowedChild(StartSymbol, symb, 0);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)
|
---|
| 133 | foreach (var parent in allSymbols) {
|
---|
| 134 | for (int i = 0; i < GetMaxSubtreeCount(parent); i++)
|
---|
| 135 | foreach (var child in allSymbols) {
|
---|
| 136 | SetAllowedChild(parent, child, i);
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 |
|
---|
[5549] | 142 | public static ISymbolicExpressionTreeGrammar CreateSimpleArithmeticGrammar() {
|
---|
[3338] | 143 | var g = new GlobalSymbolicExpressionGrammar(new SimpleArithmeticGrammar());
|
---|
| 144 | g.MaxFunctionArguments = 0;
|
---|
| 145 | g.MinFunctionArguments = 0;
|
---|
| 146 | g.MaxFunctionDefinitions = 0;
|
---|
| 147 | g.MinFunctionDefinitions = 0;
|
---|
| 148 | return g;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
[5549] | 151 | public static ISymbolicExpressionTreeGrammar CreateArithmeticAndAdfGrammar() {
|
---|
[3338] | 152 | var g = new GlobalSymbolicExpressionGrammar(new SimpleArithmeticGrammar());
|
---|
| 153 | g.MaxFunctionArguments = 3;
|
---|
| 154 | g.MinFunctionArguments = 0;
|
---|
| 155 | g.MaxFunctionDefinitions = 3;
|
---|
| 156 | g.MinFunctionDefinitions = 0;
|
---|
| 157 | return g;
|
---|
| 158 | }
|
---|
| 159 | }
|
---|
| 160 | }
|
---|