1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using System.Diagnostics;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
26 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
27 | using HeuristicLab.Random;
|
---|
28 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
29 | using System;
|
---|
30 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Tests {
|
---|
31 | internal class Util {
|
---|
32 |
|
---|
33 | public static void InitTree(ISymbolicExpressionTree tree, MersenneTwister twister, List<string> varNames) {
|
---|
34 | foreach (var node in tree.IterateNodesPostfix()) {
|
---|
35 | if (node is VariableTreeNode) {
|
---|
36 | var varNode = node as VariableTreeNode;
|
---|
37 | varNode.Weight = twister.NextDouble() * 20.0 - 10.0;
|
---|
38 | varNode.VariableName = varNames[twister.Next(varNames.Count)];
|
---|
39 | } else if (node is ConstantTreeNode) {
|
---|
40 | var constantNode = node as ConstantTreeNode;
|
---|
41 | constantNode.Value = twister.NextDouble() * 20.0 - 10.0;
|
---|
42 | }
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 |
|
---|
47 | public static ISymbolicExpressionTree[] CreateRandomTrees(MersenneTwister twister, Dataset dataset, ISymbolicExpressionGrammar grammar, int popSize) {
|
---|
48 | return CreateRandomTrees(twister, dataset, grammar, popSize, 1, 200, 3, 3);
|
---|
49 | }
|
---|
50 |
|
---|
51 | public static ISymbolicExpressionTree[] CreateRandomTrees(MersenneTwister twister, Dataset dataset, ISymbolicExpressionGrammar grammar,
|
---|
52 | int popSize, int minSize, int maxSize,
|
---|
53 | int maxFunctionDefinitions, int maxFunctionArguments) {
|
---|
54 | foreach (Variable variableSymbol in grammar.Symbols.OfType<Variable>()) {
|
---|
55 | variableSymbol.VariableNames = dataset.VariableNames.Skip(1);
|
---|
56 | }
|
---|
57 | ISymbolicExpressionTree[] randomTrees = new ISymbolicExpressionTree[popSize];
|
---|
58 | for (int i = 0; i < randomTrees.Length; i++) {
|
---|
59 | randomTrees[i] = ProbabilisticTreeCreator.Create(twister, grammar, maxSize, 10);
|
---|
60 | }
|
---|
61 | return randomTrees;
|
---|
62 | }
|
---|
63 |
|
---|
64 |
|
---|
65 | public static Dataset CreateRandomDataset(MersenneTwister twister, int rows, int columns) {
|
---|
66 | double[,] data = new double[rows, columns];
|
---|
67 | for (int i = 0; i < rows; i++) {
|
---|
68 | for (int j = 0; j < columns; j++) {
|
---|
69 | data[i, j] = twister.NextDouble() * 2.0 - 1.0;
|
---|
70 | }
|
---|
71 | }
|
---|
72 | IEnumerable<string> variableNames = new string[] { "y" }.Concat(Enumerable.Range(0, columns - 1).Select(x => "x" + x.ToString()));
|
---|
73 | Dataset ds = new Dataset(variableNames, data);
|
---|
74 | return ds;
|
---|
75 | }
|
---|
76 |
|
---|
77 | public static double NodesPerSecond(long nNodes, Stopwatch watch) {
|
---|
78 | return nNodes / (watch.ElapsedMilliseconds / 1000.0);
|
---|
79 | }
|
---|
80 |
|
---|
81 | public static double CalculateEvaluatedNodesPerSec(ISymbolicExpressionTree[] trees, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, Dataset dataset, int repetitions) {
|
---|
82 | // warm up
|
---|
83 | IEnumerable<int> rows = Enumerable.Range(0, dataset.Rows);
|
---|
84 | long nNodes = 0;
|
---|
85 | double c = 0;
|
---|
86 | for (int i = 0; i < trees.Length; i++) {
|
---|
87 | nNodes += trees[i].Length * (dataset.Rows - 1);
|
---|
88 | c = interpreter.GetSymbolicExpressionTreeValues(trees[i], dataset, rows).Count(); // count needs to evaluate all rows
|
---|
89 | }
|
---|
90 |
|
---|
91 | Stopwatch watch = new Stopwatch();
|
---|
92 | for (int rep = 0; rep < repetitions; rep++) {
|
---|
93 | watch.Start();
|
---|
94 | c = 0;
|
---|
95 | for (int i = 0; i < trees.Length; i++) {
|
---|
96 | interpreter.GetSymbolicExpressionTreeValues(trees[i], dataset, rows).Count(); // count needs to evaluate all rows
|
---|
97 | }
|
---|
98 | watch.Stop();
|
---|
99 | }
|
---|
100 | Console.WriteLine("Random tree evaluation performance of " + interpreter.GetType() + ": " +
|
---|
101 | watch.ElapsedMilliseconds + "ms " +
|
---|
102 | Util.NodesPerSecond(nNodes * repetitions, watch) + " nodes/sec");
|
---|
103 | return Util.NodesPerSecond(nNodes * repetitions, watch);
|
---|
104 | }
|
---|
105 |
|
---|
106 | public static void IsValid(ISymbolicExpressionTree tree) {
|
---|
107 | int reportedSize = tree.Length;
|
---|
108 | int actualSize = tree.IterateNodesPostfix().Count();
|
---|
109 | Assert.AreEqual(actualSize, reportedSize);
|
---|
110 |
|
---|
111 | foreach (var defunTreeNode in tree.Root.Subtrees.OfType<DefunTreeNode>()) {
|
---|
112 | int arity = defunTreeNode.NumberOfArguments;
|
---|
113 |
|
---|
114 | foreach (var argTreenode in defunTreeNode.IterateNodesPrefix().OfType<ArgumentTreeNode>()) {
|
---|
115 | Assert.IsTrue(argTreenode.SubtreeCount == 0);
|
---|
116 | Assert.IsTrue(((Argument)argTreenode.Symbol).ArgumentIndex < arity);
|
---|
117 | }
|
---|
118 |
|
---|
119 | foreach (var argSymbol in Enumerable.Range(0, defunTreeNode.NumberOfArguments).Select(x => new Argument(x))) {
|
---|
120 | Assert.IsTrue(defunTreeNode.Grammar.ContainsSymbol(argSymbol));
|
---|
121 | Assert.IsTrue(defunTreeNode.Grammar.GetMaximumSubtreeCount(argSymbol) == 0);
|
---|
122 | Assert.IsTrue(defunTreeNode.Grammar.GetMinimumSubtreeCount(argSymbol) == 0);
|
---|
123 | }
|
---|
124 |
|
---|
125 | var invoke = new InvokeFunction(defunTreeNode.FunctionName);
|
---|
126 | foreach (var otherRootNode in tree.Root.Subtrees) {
|
---|
127 | if (otherRootNode.Grammar.ContainsSymbol(invoke)) {
|
---|
128 | Assert.IsTrue(otherRootNode.Grammar.GetMinimumSubtreeCount(invoke) == arity);
|
---|
129 | Assert.IsTrue(otherRootNode.Grammar.GetMaximumSubtreeCount(invoke) == arity);
|
---|
130 | Assert.IsFalse(otherRootNode.Grammar.IsAllowedChildSymbol(invoke, invoke));
|
---|
131 | for (int i = 0; i < arity; i++) {
|
---|
132 | Assert.IsFalse(otherRootNode.Grammar.IsAllowedChildSymbol(invoke, invoke, i));
|
---|
133 | }
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | }
|
---|
138 |
|
---|
139 | foreach (var subtree in tree.Root.Subtrees) {
|
---|
140 | Assert.AreNotSame(subtree.Grammar, tree.Root.Grammar);
|
---|
141 | IsValid(subtree.Grammar);
|
---|
142 | }
|
---|
143 |
|
---|
144 | IsValid(tree.Root.Grammar);
|
---|
145 | IsValid(tree.Root);
|
---|
146 | }
|
---|
147 |
|
---|
148 | public static void IsValid(ISymbolicExpressionTreeGrammar grammar) {
|
---|
149 | Assert.IsTrue(grammar.Symbols.Count() == grammar.Symbols.Distinct().Count());
|
---|
150 | foreach (ISymbol symbol in grammar.AllowedSymbols) {
|
---|
151 | Assert.IsTrue(grammar.GetAllowedChildSymbols(symbol).Count() == grammar.GetAllowedChildSymbols(symbol).Distinct().Count());
|
---|
152 | for (int i = 0; i < grammar.GetMaximumSubtreeCount(symbol); i++) {
|
---|
153 | Assert.IsTrue(grammar.GetAllowedChildSymbols(symbol, i).Count() == grammar.GetAllowedChildSymbols(symbol, i).Distinct().Count());
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | foreach (var symbol in grammar.ModifyableSymbols) {
|
---|
158 | //check if ever symbol has at least one allowed child
|
---|
159 | for (int i = 0; i < grammar.GetMaximumSubtreeCount(symbol); i++)
|
---|
160 | Assert.IsTrue(grammar.GetAllowedChildSymbols(symbol, i).Any());
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | public static void IsValid(ISymbolicExpressionTreeNode treeNode) {
|
---|
165 | var matchingSymbol = (from symb in treeNode.Grammar.Symbols
|
---|
166 | where symb.Name == treeNode.Symbol.Name
|
---|
167 | select symb).SingleOrDefault();
|
---|
168 | Assert.IsTrue(treeNode.Subtrees.Count() >= treeNode.Grammar.GetMinimumSubtreeCount(matchingSymbol));
|
---|
169 | Assert.IsTrue(treeNode.Subtrees.Count() <= treeNode.Grammar.GetMaximumSubtreeCount(matchingSymbol));
|
---|
170 | Assert.AreNotEqual(0.0, matchingSymbol.InitialFrequency); // check that no deactivated symbols occur in the tree
|
---|
171 | for (int i = 0; i < treeNode.Subtrees.Count(); i++) {
|
---|
172 | Assert.IsTrue(treeNode.Grammar.GetAllowedChildSymbols(treeNode.Symbol, i).Select(x => x.Name).Contains(treeNode.GetSubtree(i).Symbol.Name));
|
---|
173 | IsValid(treeNode.GetSubtree(i));
|
---|
174 | }
|
---|
175 | }
|
---|
176 | }
|
---|
177 | }
|
---|