1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
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 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests {
|
---|
28 | public static class Grammars {
|
---|
29 |
|
---|
30 | [StorableClass]
|
---|
31 | private class Addition : Symbol {
|
---|
32 | [StorableConstructor]
|
---|
33 | protected Addition(bool deserializing) : base(deserializing) { }
|
---|
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 | }
|
---|
40 |
|
---|
41 | [StorableClass]
|
---|
42 | private class Subtraction : Symbol {
|
---|
43 | [StorableConstructor]
|
---|
44 | protected Subtraction(bool deserializing) : base(deserializing) { }
|
---|
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 | }
|
---|
51 |
|
---|
52 | [StorableClass]
|
---|
53 | private class Multiplication : Symbol {
|
---|
54 | [StorableConstructor]
|
---|
55 | protected Multiplication(bool deserializing) : base(deserializing) { }
|
---|
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 | }
|
---|
62 |
|
---|
63 | [StorableClass]
|
---|
64 | private class Division : Symbol {
|
---|
65 | [StorableConstructor]
|
---|
66 | protected Division(bool deserializing) : base(deserializing) { }
|
---|
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 | }
|
---|
73 |
|
---|
74 | [StorableClass]
|
---|
75 | private class Terminal : Symbol {
|
---|
76 | [StorableConstructor]
|
---|
77 | protected Terminal(bool deserializing) : base(deserializing) { }
|
---|
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 | }
|
---|
83 |
|
---|
84 | public override ISymbolicExpressionTreeNode CreateTreeNode() {
|
---|
85 | return new TerminalNode(this);
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | [StorableClass]
|
---|
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 |
|
---|
117 | [StorableClass]
|
---|
118 | private class SimpleArithmeticGrammar : SymbolicExpressionGrammar {
|
---|
119 | [StorableConstructor]
|
---|
120 | protected SimpleArithmeticGrammar(bool deserializing) : base(deserializing) { }
|
---|
121 | protected SimpleArithmeticGrammar(SimpleArithmeticGrammar original, Cloner cloner) : base(original, cloner) { }
|
---|
122 | public SimpleArithmeticGrammar()
|
---|
123 | : base("Grammar for unit tests", "") {
|
---|
124 | Initialize();
|
---|
125 | }
|
---|
126 |
|
---|
127 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
128 | return new SimpleArithmeticGrammar(this, cloner);
|
---|
129 | }
|
---|
130 |
|
---|
131 | private void Initialize() {
|
---|
132 | var add = new Addition();
|
---|
133 | var sub = new Subtraction();
|
---|
134 | var mul = new Multiplication();
|
---|
135 | var div = new Division();
|
---|
136 | div.InitialFrequency = 0.0; // disable division symbol
|
---|
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) {
|
---|
145 | SetSubtreeCount(funSymb, 1, 3);
|
---|
146 | }
|
---|
147 | SetSubtreeCount(terminal, 0, 0);
|
---|
148 |
|
---|
149 | // allow each symbol as child of the start symbol
|
---|
150 | foreach (var symb in allSymbols) {
|
---|
151 | AddAllowedChildSymbol(StartSymbol, symb);
|
---|
152 | AddAllowedChildSymbol(DefunSymbol, symb);
|
---|
153 | }
|
---|
154 |
|
---|
155 | // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)
|
---|
156 | foreach (var parent in functionSymbols) {
|
---|
157 | foreach (var child in allSymbols) {
|
---|
158 | AddAllowedChildSymbol(parent, child);
|
---|
159 | }
|
---|
160 | }
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
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;
|
---|
170 | return g;
|
---|
171 | }
|
---|
172 |
|
---|
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;
|
---|
179 | return g;
|
---|
180 | }
|
---|
181 | }
|
---|
182 | }
|
---|