Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/Util.cs @ 4106

Last change on this file since 4106 was 4106, checked in by gkronber, 14 years ago

Fixed bugs in SubtreeCrossover, ArgumentCreater and ArgumentDuplicater and updated unit tests for symbolic expression tree operators. #1103

File size: 6.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27using Microsoft.VisualStudio.TestTools.UnitTesting;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
29
30namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._3.Tests {
31  public static class Util {
32    public static string GetSizeDistributionString(IList<SymbolicExpressionTree> trees, int maxTreeSize, int binSize) {
33      int[] histogram = new int[maxTreeSize / binSize];
34      for (int i = 0; i < trees.Count; i++) {
35        int binIndex = Math.Min(histogram.Length - 1, trees[i].Size / binSize);
36        histogram[binIndex]++;
37      }
38      StringBuilder strBuilder = new StringBuilder();
39      for (int i = 0; i < histogram.Length - 1; i++) {
40        strBuilder.Append(Environment.NewLine);
41        strBuilder.Append("< "); strBuilder.Append((i + 1) * binSize);
42        strBuilder.Append(": "); strBuilder.AppendFormat("{0:#0.00%}", histogram[i] / (double)trees.Count);
43      }
44      strBuilder.Append(Environment.NewLine);
45      strBuilder.Append(">= "); strBuilder.Append(histogram.Length * binSize);
46      strBuilder.Append(": "); strBuilder.AppendFormat("{0:#0.00%}", histogram[histogram.Length - 1] / (double)trees.Count);
47
48      return "Size distribution: " + strBuilder;
49    }
50
51    public static string GetFunctionDistributionString(IList<SymbolicExpressionTree> trees) {
52      Dictionary<string, int> occurances = new Dictionary<string, int>();
53      double n = 0.0;
54      for (int i = 0; i < trees.Count; i++) {
55        foreach (var node in trees[i].IterateNodesPrefix()) {
56          if (node.SubTrees.Count > 0) {
57            if (!occurances.ContainsKey(node.Symbol.Name))
58              occurances[node.Symbol.Name] = 0;
59            occurances[node.Symbol.Name]++;
60            n++;
61          }
62        }
63      }
64      StringBuilder strBuilder = new StringBuilder();
65      foreach (var function in occurances.Keys) {
66        strBuilder.Append(Environment.NewLine);
67        strBuilder.Append(function); strBuilder.Append(": ");
68        strBuilder.AppendFormat("{0:#0.00%}", occurances[function] / n);
69      }
70      return "Function distribution: " + strBuilder;
71    }
72
73    public static string GetNumberOfSubTreesDistributionString(IList<SymbolicExpressionTree> trees) {
74      Dictionary<int, int> occurances = new Dictionary<int, int>();
75      double n = 0.0;
76      for (int i = 0; i < trees.Count; i++) {
77        foreach (var node in trees[i].IterateNodesPrefix()) {
78          if (!occurances.ContainsKey(node.SubTrees.Count))
79            occurances[node.SubTrees.Count] = 0;
80          occurances[node.SubTrees.Count]++;
81          n++;
82        }
83      }
84      StringBuilder strBuilder = new StringBuilder();
85      foreach (var arity in occurances.Keys) {
86        strBuilder.Append(Environment.NewLine);
87        strBuilder.Append(arity); strBuilder.Append(": ");
88        strBuilder.AppendFormat("{0:#0.00%}", occurances[arity] / n);
89      }
90      return "Distribution of function arities: " + strBuilder;
91    }
92
93
94    public static string GetTerminalDistributionString(IList<SymbolicExpressionTree> trees) {
95      Dictionary<string, int> occurances = new Dictionary<string, int>();
96      double n = 0.0;
97      for (int i = 0; i < trees.Count; i++) {
98        foreach (var node in trees[i].IterateNodesPrefix()) {
99          if (node.SubTrees.Count == 0) {
100            if (!occurances.ContainsKey(node.Symbol.Name))
101              occurances[node.Symbol.Name] = 0;
102            occurances[node.Symbol.Name]++;
103            n++;
104          }
105        }
106      }
107      StringBuilder strBuilder = new StringBuilder();
108      foreach (var function in occurances.Keys) {
109        strBuilder.Append(Environment.NewLine);
110        strBuilder.Append(function); strBuilder.Append(": ");
111        strBuilder.AppendFormat("{0:#0.00%}", occurances[function] / n);
112      }
113      return "Terminal distribution: " + strBuilder;
114    }
115
116    public static void IsValid(SymbolicExpressionTree tree) {
117      foreach (var defunTreeNode in tree.Root.SubTrees.OfType<DefunTreeNode>()) {
118        int arity = defunTreeNode.NumberOfArguments;
119        var invoke = new InvokeFunction(defunTreeNode.FunctionName);
120        foreach (var otherRootNode in tree.Root.SubTrees) {
121          if (otherRootNode.Grammar.ContainsSymbol(invoke)) {
122            Assert.IsTrue(otherRootNode.Grammar.GetMinSubtreeCount(invoke) == arity);
123            Assert.IsTrue(otherRootNode.Grammar.GetMaxSubtreeCount(invoke) == arity);
124          }
125        }
126      }
127      //Assert.AreEqual(tree.Root.Symbol, tree.Root.Grammar.StartSymbol);
128      //foreach (var subtree in tree.Root.SubTrees)
129      //  Assert.AreNotSame(subtree.Grammar, tree.Root.Grammar);
130      IsValid(tree.Root);
131    }
132
133    public static void IsValid(SymbolicExpressionTreeNode treeNode) {
134      var matchingSymbol = (from symb in treeNode.Grammar.Symbols
135                            where symb.Name == treeNode.Symbol.Name
136                            select symb).SingleOrDefault();
137      Assert.IsTrue(treeNode.SubTrees.Count >= treeNode.Grammar.GetMinSubtreeCount(matchingSymbol));
138      Assert.IsTrue(treeNode.SubTrees.Count <= treeNode.Grammar.GetMaxSubtreeCount(matchingSymbol));
139      for (int i = 0; i < treeNode.SubTrees.Count; i++) {
140        Assert.IsTrue(treeNode.GetAllowedSymbols(i).Select(x => x.Name).Contains(treeNode.SubTrees[i].Symbol.Name));
141        IsValid(treeNode.SubTrees[i]);
142      }
143    }
144  }
145}
Note: See TracBrowser for help on using the repository browser.