[3360] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5445] | 3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3360] | 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 |
|
---|
| 22 | using System;
|
---|
[3338] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
[4068] | 25 | using System.Text;
|
---|
| 26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[3338] | 27 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
| 28 |
|
---|
[5549] | 29 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests {
|
---|
[3338] | 30 | public static class Util {
|
---|
[5549] | 31 | public static string GetSizeDistributionString(IList<ISymbolicExpressionTree> trees, int maxTreeLength, int binSize) {
|
---|
| 32 | int[] histogram = new int[maxTreeLength / binSize];
|
---|
[3338] | 33 | for (int i = 0; i < trees.Count; i++) {
|
---|
[5549] | 34 | int binIndex = Math.Min(histogram.Length - 1, trees[i].Length / binSize);
|
---|
[3360] | 35 | histogram[binIndex]++;
|
---|
[3338] | 36 | }
|
---|
| 37 | StringBuilder strBuilder = new StringBuilder();
|
---|
[3360] | 38 | for (int i = 0; i < histogram.Length - 1; i++) {
|
---|
[3338] | 39 | strBuilder.Append(Environment.NewLine);
|
---|
| 40 | strBuilder.Append("< "); strBuilder.Append((i + 1) * binSize);
|
---|
| 41 | strBuilder.Append(": "); strBuilder.AppendFormat("{0:#0.00%}", histogram[i] / (double)trees.Count);
|
---|
| 42 | }
|
---|
[3360] | 43 | strBuilder.Append(Environment.NewLine);
|
---|
| 44 | strBuilder.Append(">= "); strBuilder.Append(histogram.Length * binSize);
|
---|
| 45 | strBuilder.Append(": "); strBuilder.AppendFormat("{0:#0.00%}", histogram[histogram.Length - 1] / (double)trees.Count);
|
---|
| 46 |
|
---|
[3338] | 47 | return "Size distribution: " + strBuilder;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[5549] | 50 | public static string GetFunctionDistributionString(IList<ISymbolicExpressionTree> trees) {
|
---|
[3338] | 51 | Dictionary<string, int> occurances = new Dictionary<string, int>();
|
---|
| 52 | double n = 0.0;
|
---|
| 53 | for (int i = 0; i < trees.Count; i++) {
|
---|
| 54 | foreach (var node in trees[i].IterateNodesPrefix()) {
|
---|
[5733] | 55 | if (node.Subtrees.Count() > 0) {
|
---|
[3338] | 56 | if (!occurances.ContainsKey(node.Symbol.Name))
|
---|
| 57 | occurances[node.Symbol.Name] = 0;
|
---|
| 58 | occurances[node.Symbol.Name]++;
|
---|
| 59 | n++;
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
| 63 | StringBuilder strBuilder = new StringBuilder();
|
---|
| 64 | foreach (var function in occurances.Keys) {
|
---|
| 65 | strBuilder.Append(Environment.NewLine);
|
---|
| 66 | strBuilder.Append(function); strBuilder.Append(": ");
|
---|
| 67 | strBuilder.AppendFormat("{0:#0.00%}", occurances[function] / n);
|
---|
| 68 | }
|
---|
| 69 | return "Function distribution: " + strBuilder;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[5733] | 72 | public static string GetNumberOfSubtreesDistributionString(IList<ISymbolicExpressionTree> trees) {
|
---|
[3338] | 73 | Dictionary<int, int> occurances = new Dictionary<int, int>();
|
---|
| 74 | double n = 0.0;
|
---|
| 75 | for (int i = 0; i < trees.Count; i++) {
|
---|
| 76 | foreach (var node in trees[i].IterateNodesPrefix()) {
|
---|
[5733] | 77 | if (!occurances.ContainsKey(node.Subtrees.Count()))
|
---|
| 78 | occurances[node.Subtrees.Count()] = 0;
|
---|
| 79 | occurances[node.Subtrees.Count()]++;
|
---|
[3338] | 80 | n++;
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 | StringBuilder strBuilder = new StringBuilder();
|
---|
| 84 | foreach (var arity in occurances.Keys) {
|
---|
| 85 | strBuilder.Append(Environment.NewLine);
|
---|
| 86 | strBuilder.Append(arity); strBuilder.Append(": ");
|
---|
| 87 | strBuilder.AppendFormat("{0:#0.00%}", occurances[arity] / n);
|
---|
| 88 | }
|
---|
| 89 | return "Distribution of function arities: " + strBuilder;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 |
|
---|
[5549] | 93 | public static string GetTerminalDistributionString(IList<ISymbolicExpressionTree> trees) {
|
---|
[3338] | 94 | Dictionary<string, int> occurances = new Dictionary<string, int>();
|
---|
| 95 | double n = 0.0;
|
---|
| 96 | for (int i = 0; i < trees.Count; i++) {
|
---|
| 97 | foreach (var node in trees[i].IterateNodesPrefix()) {
|
---|
[5733] | 98 | if (node.Subtrees.Count() == 0) {
|
---|
[3338] | 99 | if (!occurances.ContainsKey(node.Symbol.Name))
|
---|
| 100 | occurances[node.Symbol.Name] = 0;
|
---|
| 101 | occurances[node.Symbol.Name]++;
|
---|
| 102 | n++;
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 | StringBuilder strBuilder = new StringBuilder();
|
---|
| 107 | foreach (var function in occurances.Keys) {
|
---|
| 108 | strBuilder.Append(Environment.NewLine);
|
---|
| 109 | strBuilder.Append(function); strBuilder.Append(": ");
|
---|
| 110 | strBuilder.AppendFormat("{0:#0.00%}", occurances[function] / n);
|
---|
| 111 | }
|
---|
| 112 | return "Terminal distribution: " + strBuilder;
|
---|
| 113 | }
|
---|
[3360] | 114 |
|
---|
[5549] | 115 | public static void IsValid(ISymbolicExpressionTree tree) {
|
---|
| 116 | int reportedSize = tree.Length;
|
---|
[4524] | 117 | int actualSize = tree.IterateNodesPostfix().Count();
|
---|
| 118 | Assert.AreEqual(actualSize, reportedSize);
|
---|
| 119 |
|
---|
[5733] | 120 | foreach (var defunTreeNode in tree.Root.Subtrees.OfType<DefunTreeNode>()) {
|
---|
[4106] | 121 | int arity = defunTreeNode.NumberOfArguments;
|
---|
[5686] | 122 |
|
---|
| 123 | foreach (var argTreenode in defunTreeNode.IterateNodesPrefix().OfType<ArgumentTreeNode>()) {
|
---|
| 124 | Assert.IsTrue(argTreenode.SubtreesCount == 0);
|
---|
| 125 | Assert.IsTrue(((Argument)argTreenode.Symbol).ArgumentIndex < arity);
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | foreach (var argSymbol in Enumerable.Range(0, defunTreeNode.NumberOfArguments).Select(x => new Argument(x))) {
|
---|
| 129 | Assert.IsTrue(defunTreeNode.Grammar.ContainsSymbol(argSymbol));
|
---|
| 130 | Assert.IsTrue(defunTreeNode.Grammar.GetMaximumSubtreeCount(argSymbol) == 0);
|
---|
| 131 | Assert.IsTrue(defunTreeNode.Grammar.GetMinimumSubtreeCount(argSymbol) == 0);
|
---|
| 132 | }
|
---|
| 133 |
|
---|
[4106] | 134 | var invoke = new InvokeFunction(defunTreeNode.FunctionName);
|
---|
[5733] | 135 | foreach (var otherRootNode in tree.Root.Subtrees) {
|
---|
[4106] | 136 | if (otherRootNode.Grammar.ContainsSymbol(invoke)) {
|
---|
[5686] | 137 | Assert.IsTrue(otherRootNode.Grammar.GetMinimumSubtreeCount(invoke) == arity);
|
---|
| 138 | Assert.IsTrue(otherRootNode.Grammar.GetMaximumSubtreeCount(invoke) == arity);
|
---|
[4106] | 139 | }
|
---|
| 140 | }
|
---|
[5686] | 141 |
|
---|
[4106] | 142 | }
|
---|
[5792] | 143 |
|
---|
[5733] | 144 | foreach (var subtree in tree.Root.Subtrees) {
|
---|
[5411] | 145 | Assert.AreNotSame(subtree.Grammar, tree.Root.Grammar);
|
---|
[5686] | 146 | IsValid(subtree.Grammar);
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | IsValid(tree.Root.Grammar);
|
---|
[3360] | 150 | IsValid(tree.Root);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
[5686] | 153 | public static void IsValid(ISymbolicExpressionTreeGrammar grammar) {
|
---|
| 154 | Assert.IsTrue(grammar.Symbols.Count() == grammar.Symbols.Distinct().Count());
|
---|
| 155 | foreach (ISymbol symbol in grammar.Symbols) {
|
---|
| 156 | Assert.IsTrue(grammar.GetMinimumSubtreeCount(symbol) <= grammar.GetMaximumExpressionLength(symbol));
|
---|
| 157 | Assert.IsTrue(grammar.GetAllowedChildSymbols(symbol).Count() == grammar.GetAllowedChildSymbols(symbol).Distinct().Count());
|
---|
| 158 | for (int i = 0; i < grammar.GetMaximumSubtreeCount(symbol); i++) {
|
---|
| 159 | Assert.IsTrue(grammar.GetAllowedChildSymbols(symbol, i).Count() == grammar.GetAllowedChildSymbols(symbol, i).Distinct().Count());
|
---|
| 160 | }
|
---|
| 161 | }
|
---|
[5792] | 162 |
|
---|
| 163 | foreach (var symbol in grammar.ModifyableSymbols) {
|
---|
| 164 | //check if ever symbol has at least on
|
---|
| 165 | for (int i = 0; i < grammar.GetMaximumSubtreeCount(symbol); i++)
|
---|
| 166 | Assert.IsTrue(grammar.GetAllowedChildSymbols(symbol, i).Any());
|
---|
| 167 |
|
---|
| 168 | //if (symbol is ProgramRootSymbol) continue;
|
---|
| 169 | ////check if symbol is allowed as at least one child symbol
|
---|
| 170 | //bool result = false;
|
---|
| 171 | //foreach (var parentSymbol in grammar.Symbols) {
|
---|
| 172 | // if (result) break;
|
---|
| 173 | // for (int i = 0; i < grammar.GetMaximumSubtreeCount(parentSymbol); i++)
|
---|
| 174 | // result = result || grammar.IsAllowedChildSymbol(parentSymbol, symbol, i);
|
---|
| 175 | //}
|
---|
| 176 | //Assert.IsTrue(result);
|
---|
| 177 |
|
---|
| 178 | }
|
---|
[5686] | 179 | }
|
---|
| 180 |
|
---|
[5549] | 181 | public static void IsValid(ISymbolicExpressionTreeNode treeNode) {
|
---|
[4106] | 182 | var matchingSymbol = (from symb in treeNode.Grammar.Symbols
|
---|
| 183 | where symb.Name == treeNode.Symbol.Name
|
---|
| 184 | select symb).SingleOrDefault();
|
---|
[5733] | 185 | Assert.IsTrue(treeNode.Subtrees.Count() >= treeNode.Grammar.GetMinimumSubtreeCount(matchingSymbol));
|
---|
| 186 | Assert.IsTrue(treeNode.Subtrees.Count() <= treeNode.Grammar.GetMaximumSubtreeCount(matchingSymbol));
|
---|
[5411] | 187 | Assert.AreNotEqual(0.0, matchingSymbol.InitialFrequency); // check that no deactivated symbols occur in the tree
|
---|
[5733] | 188 | for (int i = 0; i < treeNode.Subtrees.Count(); i++) {
|
---|
| 189 | Assert.IsTrue(treeNode.Grammar.GetAllowedChildSymbols(treeNode.Symbol, i).Select(x => x.Name).Contains(treeNode.GetSubtree(i).Symbol.Name));
|
---|
| 190 | IsValid(treeNode.GetSubtree(i));
|
---|
[3360] | 191 | }
|
---|
| 192 | }
|
---|
[3338] | 193 | }
|
---|
| 194 | }
|
---|