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 | [StorableClass]
|
---|
30 | private class Addition : Symbol {
|
---|
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 |
|
---|
41 | [StorableConstructor]
|
---|
42 | protected Addition(bool deserializing) : base(deserializing) { }
|
---|
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 | }
|
---|
49 |
|
---|
50 | [StorableClass]
|
---|
51 | private class Subtraction : Symbol {
|
---|
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 |
|
---|
62 | [StorableConstructor]
|
---|
63 | protected Subtraction(bool deserializing) : base(deserializing) { }
|
---|
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 | }
|
---|
70 |
|
---|
71 | [StorableClass]
|
---|
72 | private class Multiplication : Symbol {
|
---|
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 |
|
---|
83 | [StorableConstructor]
|
---|
84 | protected Multiplication(bool deserializing) : base(deserializing) { }
|
---|
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 | }
|
---|
91 |
|
---|
92 | [StorableClass]
|
---|
93 | private class Division : Symbol {
|
---|
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 |
|
---|
104 | [StorableConstructor]
|
---|
105 | protected Division(bool deserializing) : base(deserializing) { }
|
---|
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 | }
|
---|
112 |
|
---|
113 | [StorableClass]
|
---|
114 | private class Terminal : Symbol {
|
---|
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 |
|
---|
125 | [StorableConstructor]
|
---|
126 | protected Terminal(bool deserializing) : base(deserializing) { }
|
---|
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 | }
|
---|
132 |
|
---|
133 | public override ISymbolicExpressionTreeNode CreateTreeNode() {
|
---|
134 | return new TerminalNode(this);
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | [StorableClass]
|
---|
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 |
|
---|
166 | [StorableClass]
|
---|
167 | private class SimpleArithmeticGrammar : SymbolicExpressionGrammar {
|
---|
168 | [StorableConstructor]
|
---|
169 | protected SimpleArithmeticGrammar(bool deserializing) : base(deserializing) { }
|
---|
170 | protected SimpleArithmeticGrammar(SimpleArithmeticGrammar original, Cloner cloner) : base(original, cloner) { }
|
---|
171 | public SimpleArithmeticGrammar()
|
---|
172 | : base("Grammar for unit tests", "") {
|
---|
173 | Initialize();
|
---|
174 | }
|
---|
175 |
|
---|
176 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
177 | return new SimpleArithmeticGrammar(this, cloner);
|
---|
178 | }
|
---|
179 |
|
---|
180 | private void Initialize() {
|
---|
181 | var add = new Addition();
|
---|
182 | var sub = new Subtraction();
|
---|
183 | var mul = new Multiplication();
|
---|
184 | var div = new Division();
|
---|
185 | div.InitialFrequency = 0.0; // disable division symbol
|
---|
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) {
|
---|
194 | SetSubtreeCount(funSymb, 1, 3);
|
---|
195 | }
|
---|
196 | SetSubtreeCount(terminal, 0, 0);
|
---|
197 |
|
---|
198 | // allow each symbol as child of the start symbol
|
---|
199 | foreach (var symb in allSymbols) {
|
---|
200 | AddAllowedChildSymbol(StartSymbol, symb);
|
---|
201 | AddAllowedChildSymbol(DefunSymbol, symb);
|
---|
202 | }
|
---|
203 |
|
---|
204 | // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)
|
---|
205 | foreach (var parent in functionSymbols) {
|
---|
206 | foreach (var child in allSymbols) {
|
---|
207 | AddAllowedChildSymbol(parent, child);
|
---|
208 | }
|
---|
209 | }
|
---|
210 | }
|
---|
211 | }
|
---|
212 |
|
---|
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;
|
---|
219 | return g;
|
---|
220 | }
|
---|
221 |
|
---|
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;
|
---|
228 | return g;
|
---|
229 | }
|
---|
230 | }
|
---|
231 | }
|
---|