Changeset 9417
- Timestamp:
- 05/02/13 11:27:12 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Tests/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4/Grammars.cs
r7915 r9417 20 20 #endregion 21 21 22 using System.Collections.Generic;23 22 using System.Linq; 24 using HeuristicLab.Common;25 23 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;27 24 using HeuristicLab.Problems.DataAnalysis.Symbolic; 28 25 29 26 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_34.Tests { 30 27 public static class Grammars { 31 [StorableClass]32 private class Addition : Symbol {33 private const int minimumArity = 1;34 private const int maximumArity = byte.MaxValue;35 36 public override int MinimumArity {37 get { return minimumArity; }38 }39 public override int MaximumArity {40 get { return maximumArity; }41 }42 43 [StorableConstructor]44 protected Addition(bool deserializing) : base(deserializing) { }45 protected Addition(Addition original, Cloner cloner) : base(original, cloner) { }46 public Addition() : base("Addition", "") { }47 public override IDeepCloneable Clone(Cloner cloner) {48 return new Addition(this, cloner);49 }50 }51 52 [StorableClass]53 private class Subtraction : Symbol {54 private const int minimumArity = 1;55 private const int maximumArity = byte.MaxValue;56 57 public override int MinimumArity {58 get { return minimumArity; }59 }60 public override int MaximumArity {61 get { return maximumArity; }62 }63 64 [StorableConstructor]65 protected Subtraction(bool deserializing) : base(deserializing) { }66 protected Subtraction(Subtraction original, Cloner cloner) : base(original, cloner) { }67 public Subtraction() : base("Subtraction", "") { }68 public override IDeepCloneable Clone(Cloner cloner) {69 return new Subtraction(this, cloner);70 }71 }72 73 [StorableClass]74 private class Multiplication : Symbol {75 private const int minimumArity = 1;76 private const int maximumArity = byte.MaxValue;77 78 public override int MinimumArity {79 get { return minimumArity; }80 }81 public override int MaximumArity {82 get { return maximumArity; }83 }84 85 [StorableConstructor]86 protected Multiplication(bool deserializing) : base(deserializing) { }87 protected Multiplication(Multiplication original, Cloner cloner) : base(original, cloner) { }88 public Multiplication() : base("Multiplication", "") { }89 public override IDeepCloneable Clone(Cloner cloner) {90 return new Multiplication(this, cloner);91 }92 }93 94 [StorableClass]95 private class Division : Symbol {96 private const int minimumArity = 1;97 private const int maximumArity = byte.MaxValue;98 99 public override int MinimumArity {100 get { return minimumArity; }101 }102 public override int MaximumArity {103 get { return maximumArity; }104 }105 106 [StorableConstructor]107 protected Division(bool deserializing) : base(deserializing) { }108 protected Division(Division original, Cloner cloner) : base(original, cloner) { }109 public Division() : base("Division", "") { }110 public override IDeepCloneable Clone(Cloner cloner) {111 return new Division(this, cloner);112 }113 }114 115 [StorableClass]116 private class Terminal : Symbol {117 private const int minimumArity = 0;118 private const int maximumArity = 0;119 120 public override int MinimumArity {121 get { return minimumArity; }122 }123 public override int MaximumArity {124 get { return maximumArity; }125 }126 127 [StorableConstructor]128 protected Terminal(bool deserializing) : base(deserializing) { }129 protected Terminal(Terminal original, Cloner cloner) : base(original, cloner) { }130 public Terminal() : base("Terminal", "") { }131 public override IDeepCloneable Clone(Cloner cloner) {132 return new Terminal(this, cloner);133 }134 135 public override ISymbolicExpressionTreeNode CreateTreeNode() {136 return new TerminalNode(this);137 }138 }139 140 [StorableClass]141 private class TerminalNode : SymbolicExpressionTreeTerminalNode {142 public override bool HasLocalParameters { get { return true; } }143 private double value;144 protected TerminalNode(TerminalNode original, Cloner cloner)145 : base(original, cloner) {146 this.value = original.value;147 }148 [StorableConstructor]149 protected TerminalNode(bool deserializing) : base(deserializing) { }150 public TerminalNode(Terminal symbol) : base(symbol) { }151 152 public override IDeepCloneable Clone(Cloner cloner) {153 return new TerminalNode(this, cloner);154 }155 public override void ResetLocalParameters(Core.IRandom random) {156 base.ResetLocalParameters(random);157 value = random.NextDouble();158 }159 public override void ShakeLocalParameters(Core.IRandom random, double shakingFactor) {160 base.ShakeLocalParameters(random, shakingFactor);161 value = random.NextDouble();162 }163 public override string ToString() {164 return value.ToString("E4");165 }166 }167 168 [StorableClass]169 private class SimpleArithmeticGrammar : SymbolicExpressionGrammar {170 [StorableConstructor]171 protected SimpleArithmeticGrammar(bool deserializing) : base(deserializing) { }172 protected SimpleArithmeticGrammar(SimpleArithmeticGrammar original, Cloner cloner) : base(original, cloner) { }173 public SimpleArithmeticGrammar()174 : base("Grammar for unit tests", "") {175 Initialize();176 }177 178 public override IDeepCloneable Clone(Cloner cloner) {179 return new SimpleArithmeticGrammar(this, cloner);180 }181 182 private void Initialize() {183 var add = new Addition();184 var sub = new Subtraction();185 var mul = new Multiplication();186 var div = new Division();187 div.InitialFrequency = 0.0; // disable division symbol188 var terminal = new Terminal();189 190 var allSymbols = new List<Symbol>() { add, sub, mul, div, terminal };191 var functionSymbols = new List<Symbol>() { add, sub, mul, div };192 foreach (var symb in allSymbols)193 AddSymbol(symb);194 195 foreach (var funSymb in functionSymbols) {196 SetSubtreeCount(funSymb, 1, 3);197 }198 SetSubtreeCount(terminal, 0, 0);199 200 // allow each symbol as child of the start symbol201 foreach (var symb in allSymbols) {202 AddAllowedChildSymbol(StartSymbol, symb);203 AddAllowedChildSymbol(DefunSymbol, symb);204 }205 206 // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)207 foreach (var parent in functionSymbols) {208 foreach (var child in allSymbols) {209 AddAllowedChildSymbol(parent, child);210 }211 }212 }213 }214 215 28 public static ISymbolicExpressionGrammar CreateSimpleArithmeticGrammar() { 216 29 var g = new TypeCoherentExpressionGrammar();
Note: See TracChangeset
for help on using the changeset viewer.