Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added test classes for crossover and subroutine creater. #290 (Implement ADFs)

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