Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3569 was 3369, checked in by gkronber, 15 years ago

Changed way the grammar is stored in tree nodes to make it more efficient and fixed bugs in symbolic expression tree operators. #290 (Implement ADFs)

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