Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/SubroutineCreaterTest.cs @ 3307

Last change on this file since 3307 was 3307, checked in by gkronber, 12 years ago

Added ADF symbols to symbolic expression grammar for the test class for the subroutine creater. #290 (Implement ADFs)

File size: 7.2 KB
Line 
1using System;
2using System.Text;
3using System.Collections.Generic;
4using System.Linq;
5using Microsoft.VisualStudio.TestTools.UnitTesting;
6using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
7using HeuristicLab.Random;
8using System.Diagnostics;
9using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureAlteringOperators;
10using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.GeneralSymbols;
11
12namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._3.Tests {
13  [TestClass]
14  public class SubroutineCreaterTest {
15    public SubroutineCreaterTest() {
16    }
17
18    private TestContext testContextInstance;
19
20    /// <summary>
21    ///Gets or sets the test context which provides
22    ///information about and functionality for the current test run.
23    ///</summary>
24    public TestContext TestContext {
25      get {
26        return testContextInstance;
27      }
28      set {
29        testContextInstance = value;
30      }
31    }
32
33    [ClassInitialize()]
34    public static void SubroutineCreaterTestInitialize(TestContext testContext) {
35      var randomTrees = new List<SymbolicExpressionTree>();
36      subroutineTrees = new List<SymbolicExpressionTree>();
37      int populationSize = 1000;
38      var grammar = new TestGrammar();
39      var random = new MersenneTwister();
40      for (int i = 0; i < populationSize; i++) {
41        var randTree = ProbabilisticTreeCreator.Create(random, grammar, 100, 10);
42        Assert.IsTrue(grammar.IsValidExpression(randTree));
43        randomTrees.Add(randTree);
44      }
45      var newPopulation = new List<SymbolicExpressionTree>();
46      for (int i = 0; i < populationSize; i++) {
47        var par0 = (SymbolicExpressionTree)randomTrees[random.Next(populationSize)].Clone();
48        bool success = SubroutineCreater.CreateSubroutine(random, par0, grammar, 100, 10, 3, 3);
49        Assert.IsTrue(grammar.IsValidExpression(par0));
50        subroutineTrees.Add(par0);
51      }
52    }
53
54
55
56    private static List<SymbolicExpressionTree> subroutineTrees;
57    private static int failedEvents;
58
59    private class Addition : Symbol { }
60    private class Subtraction : Symbol { }
61    private class Multiplication : Symbol { }
62    private class Division : Symbol { }
63    private class Terminal : Symbol { }
64
65    private class TestGrammar : DefaultSymbolicExpressionGrammar {
66      public TestGrammar()
67        : base(0, 3, 0, 3) {
68        Initialize();
69      }
70
71      private void Initialize() {
72        var add = new Addition();
73        var sub = new Subtraction();
74        var mul = new Multiplication();
75        var div = new Division();
76        var terminal = new Terminal();
77
78        var defun = new Defun();
79        var invoke = new InvokeFunction();
80
81        var allSymbols = new List<Symbol>() { add, sub, mul, div, terminal, invoke };
82        var functionSymbols = new List<Symbol>() { add, sub, mul, div };
83        foreach (var symb in allSymbols) {
84          AddAllowedSymbols(StartSymbol, 0, symb);
85          AddAllowedSymbols(defun, 0, symb);
86        }
87        SetMinSubTreeCount(invoke, 0);
88        SetMaxSubTreeCount(invoke, 3);
89        for (int i = 0; i < 3; i++) {
90          allSymbols.ForEach(s => AddAllowedSymbols(invoke, i, s));
91        }
92        SetMinSubTreeCount(terminal, 0);
93        SetMaxSubTreeCount(terminal, 0);
94        int maxSubTrees = 3;
95        foreach (var functionSymbol in functionSymbols) {
96          SetMinSubTreeCount(functionSymbol, 1);
97          SetMaxSubTreeCount(functionSymbol, maxSubTrees);
98          foreach (var childSymbol in allSymbols) {
99            for (int argumentIndex = 0; argumentIndex < maxSubTrees; argumentIndex++) {
100              AddAllowedSymbols(functionSymbol, argumentIndex, childSymbol);
101            }
102          }
103        }
104      }
105    }
106
107    [TestMethod()]
108    public void SubroutineCreaterSizeDistributionTest() {
109      int[] histogram = new int[105 / 5];
110      for (int i = 0; i < subroutineTrees.Count; i++) {
111        histogram[subroutineTrees[i].Size / 5]++;
112      }
113      StringBuilder strBuilder = new StringBuilder();
114      for (int i = 0; i < histogram.Length; i++) {
115        strBuilder.Append(Environment.NewLine);
116        strBuilder.Append("< "); strBuilder.Append((i + 1) * 5);
117        strBuilder.Append(": "); strBuilder.AppendFormat("{0:#0.00%}", histogram[i] / (double)subroutineTrees.Count);
118      }
119      Assert.Inconclusive("Size distribution of SubroutineCreater: " + strBuilder);
120    }
121
122    [TestMethod()]
123    public void SubroutineCreaterFunctionDistributionTest() {
124      Dictionary<Symbol, int> occurances = new Dictionary<Symbol, int>();
125      double n = 0.0;
126      for (int i = 0; i < subroutineTrees.Count; i++) {
127        foreach (var node in subroutineTrees[i].IterateNodesPrefix()) {
128          if (node.SubTrees.Count > 0) {
129            if (!occurances.ContainsKey(node.Symbol))
130              occurances[node.Symbol] = 0;
131            occurances[node.Symbol]++;
132            n++;
133          }
134        }
135      }
136      StringBuilder strBuilder = new StringBuilder();
137      foreach (var function in occurances.Keys) {
138        strBuilder.Append(Environment.NewLine);
139        strBuilder.Append(function.Name); strBuilder.Append(": ");
140        strBuilder.AppendFormat("{0:#0.00%}", occurances[function] / n);
141      }
142      Assert.Inconclusive("Function distribution of SubroutineCreater: " + strBuilder);
143    }
144
145    [TestMethod()]
146    public void SubroutineCreaterNumberOfSubTreesDistributionTest() {
147      Dictionary<int, int> occurances = new Dictionary<int, int>();
148      double n = 0.0;
149      for (int i = 0; i < subroutineTrees.Count; i++) {
150        foreach (var node in subroutineTrees[i].IterateNodesPrefix()) {
151          if (!occurances.ContainsKey(node.SubTrees.Count))
152            occurances[node.SubTrees.Count] = 0;
153          occurances[node.SubTrees.Count]++;
154          n++;
155        }
156      }
157      StringBuilder strBuilder = new StringBuilder();
158      foreach (var arity in occurances.Keys) {
159        strBuilder.Append(Environment.NewLine);
160        strBuilder.Append(arity); strBuilder.Append(": ");
161        strBuilder.AppendFormat("{0:#0.00%}", occurances[arity] / n);
162      }
163      Assert.Inconclusive("Distribution of function arities of SubroutineCreater: " + strBuilder);
164    }
165
166
167    [TestMethod()]
168    public void SubroutineCreaterTerminalDistributionTest() {
169      Dictionary<Symbol, int> occurances = new Dictionary<Symbol, int>();
170      double n = 0.0;
171      for (int i = 0; i < subroutineTrees.Count; i++) {
172        foreach (var node in subroutineTrees[i].IterateNodesPrefix()) {
173          if (node.SubTrees.Count == 0) {
174            if (!occurances.ContainsKey(node.Symbol))
175              occurances[node.Symbol] = 0;
176            occurances[node.Symbol]++;
177            n++;
178          }
179        }
180      }
181      StringBuilder strBuilder = new StringBuilder();
182      foreach (var function in occurances.Keys) {
183        strBuilder.Append(Environment.NewLine);
184        strBuilder.Append(function.Name); strBuilder.Append(": ");
185        strBuilder.AppendFormat("{0:#0.00%}", occurances[function] / n);
186      }
187      Assert.Inconclusive("Terminal distribution of SubroutineCreater: " + strBuilder);
188    }
189  }
190}
Note: See TracBrowser for help on using the repository browser.