[4951] | 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 |
|
---|
| 22 | using System.Collections.Generic;
|
---|
| 23 | using System.Diagnostics;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;
|
---|
| 27 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
| 28 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
|
---|
| 29 | using HeuristicLab.Random;
|
---|
| 30 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
| 31 | namespace HeuristicLab.Problems.DataAnalysis.Tests {
|
---|
| 32 | internal class Util {
|
---|
| 33 |
|
---|
| 34 | public static void InitTree(SymbolicExpressionTree tree, MersenneTwister twister, List<string> varNames) {
|
---|
| 35 | foreach (var node in tree.IterateNodesPostfix()) {
|
---|
| 36 | if (node is VariableTreeNode) {
|
---|
| 37 | var varNode = node as VariableTreeNode;
|
---|
| 38 | varNode.Weight = twister.NextDouble() * 20.0 - 10.0;
|
---|
| 39 | varNode.VariableName = varNames[twister.Next(varNames.Count)];
|
---|
| 40 | } else if (node is ConstantTreeNode) {
|
---|
| 41 | var constantNode = node as ConstantTreeNode;
|
---|
| 42 | constantNode.Value = twister.NextDouble() * 20.0 - 10.0;
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 |
|
---|
| 48 | public static SymbolicExpressionTree[] CreateRandomTrees(MersenneTwister twister, Dataset dataset, ISymbolicExpressionGrammar grammar, int popSize) {
|
---|
| 49 | return CreateRandomTrees(twister, dataset, grammar, popSize, 1, 200, 3, 3);
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | public static SymbolicExpressionTree[] CreateRandomTrees(MersenneTwister twister, Dataset dataset, ISymbolicExpressionGrammar grammar,
|
---|
| 53 | int popSize, int minSize, int maxSize,
|
---|
| 54 | int maxFunctionDefinitions, int maxFunctionArguments) {
|
---|
| 55 | foreach (Variable variableSymbol in grammar.Symbols.OfType<Variable>()) {
|
---|
| 56 | variableSymbol.VariableNames = dataset.VariableNames.Skip(1);
|
---|
| 57 | }
|
---|
| 58 | SymbolicExpressionTree[] randomTrees = new SymbolicExpressionTree[popSize];
|
---|
| 59 | for (int i = 0; i < randomTrees.Length; i++) {
|
---|
| 60 | randomTrees[i] = ProbabilisticTreeCreator.Create(twister, grammar, maxSize, 10, maxFunctionDefinitions, maxFunctionArguments);
|
---|
| 61 | }
|
---|
| 62 | return randomTrees;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 |
|
---|
| 66 | public static Dataset CreateRandomDataset(MersenneTwister twister, int rows, int columns) {
|
---|
| 67 | double[,] data = new double[rows, columns];
|
---|
| 68 | for (int i = 0; i < rows; i++) {
|
---|
| 69 | for (int j = 0; j < columns; j++) {
|
---|
| 70 | data[i, j] = twister.NextDouble() * 2.0 - 1.0;
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 | IEnumerable<string> variableNames = new string[] { "y" }.Concat(Enumerable.Range(0, columns - 1).Select(x => "x" + x.ToString()));
|
---|
| 74 | Dataset ds = new Dataset(variableNames, data);
|
---|
| 75 | return ds;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | public static double NodesPerSecond(long nNodes, Stopwatch watch) {
|
---|
| 79 | return nNodes / (watch.ElapsedMilliseconds / 1000.0);
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | public static void EvaluateTrees(SymbolicExpressionTree[] trees, ISymbolicExpressionTreeInterpreter interpreter, Dataset dataset, int repetitions) {
|
---|
| 83 | double[] estimation = new double[dataset.Rows];
|
---|
| 84 | // warm up
|
---|
| 85 | for (int i = 0; i < trees.Length; i++) {
|
---|
| 86 | estimation = interpreter.GetSymbolicExpressionTreeValues(trees[i], dataset, Enumerable.Range(0, dataset.Rows)).ToArray();
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | Stopwatch watch = new Stopwatch();
|
---|
| 90 | long nNodes = 0;
|
---|
| 91 | for (int rep = 0; rep < repetitions; rep++) {
|
---|
| 92 | watch.Start();
|
---|
| 93 | for (int i = 0; i < trees.Length; i++) {
|
---|
| 94 | nNodes += trees[i].Size * (dataset.Rows - 1);
|
---|
| 95 | estimation = interpreter.GetSymbolicExpressionTreeValues(trees[i], dataset, Enumerable.Range(0, dataset.Rows)).ToArray();
|
---|
| 96 | }
|
---|
| 97 | watch.Stop();
|
---|
| 98 | }
|
---|
| 99 | Assert.Inconclusive("Random tree evaluation performance of " + interpreter.GetType() + ":" +
|
---|
| 100 | watch.ElapsedMilliseconds + "ms " +
|
---|
| 101 | Util.NodesPerSecond(nNodes, watch) + " nodes/sec");
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 | }
|
---|