[2447] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[3733] | 3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2447] | 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;
|
---|
[4068] | 24 | using System.Linq;
|
---|
[3733] | 25 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;
|
---|
| 27 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
[4068] | 28 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
|
---|
| 29 | using HeuristicLab.Random;
|
---|
| 30 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
[3733] | 31 | namespace HeuristicLab.Problems.DataAnalysis.Tests {
|
---|
| 32 | internal class Util {
|
---|
[2447] | 33 |
|
---|
[3733] | 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;
|
---|
[2447] | 38 | varNode.Weight = twister.NextDouble() * 20.0 - 10.0;
|
---|
| 39 | varNode.VariableName = varNames[twister.Next(varNames.Count)];
|
---|
[3733] | 40 | } else if (node is ConstantTreeNode) {
|
---|
| 41 | var constantNode = node as ConstantTreeNode;
|
---|
[2447] | 42 | constantNode.Value = twister.NextDouble() * 20.0 - 10.0;
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 |
|
---|
[3733] | 48 | public static SymbolicExpressionTree[] CreateRandomTrees(MersenneTwister twister, Dataset dataset, ISymbolicExpressionGrammar grammar, int popSize) {
|
---|
| 49 | return CreateRandomTrees(twister, dataset, grammar, popSize, 1, 200, 3, 3);
|
---|
[2447] | 50 | }
|
---|
| 51 |
|
---|
[3733] | 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);
|
---|
[2447] | 57 | }
|
---|
[3733] | 58 | SymbolicExpressionTree[] randomTrees = new SymbolicExpressionTree[popSize];
|
---|
[2447] | 59 | for (int i = 0; i < randomTrees.Length; i++) {
|
---|
[3733] | 60 | randomTrees[i] = ProbabilisticTreeCreator.Create(twister, grammar, maxSize, 10, maxFunctionDefinitions, maxFunctionArguments);
|
---|
[2447] | 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 | }
|
---|
[3733] | 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);
|
---|
[2447] | 75 | return ds;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | public static double NodesPerSecond(long nNodes, Stopwatch watch) {
|
---|
| 79 | return nNodes / (watch.ElapsedMilliseconds / 1000.0);
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[3733] | 82 | public static void EvaluateTrees(SymbolicExpressionTree[] trees, ISymbolicExpressionTreeInterpreter interpreter, Dataset dataset, int repetitions) {
|
---|
[2447] | 83 | double[] estimation = new double[dataset.Rows];
|
---|
| 84 | // warm up
|
---|
| 85 | for (int i = 0; i < trees.Length; i++) {
|
---|
[3733] | 86 | estimation = interpreter.GetSymbolicExpressionTreeValues(trees[i], dataset, Enumerable.Range(0, dataset.Rows)).ToArray();
|
---|
[2447] | 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++) {
|
---|
[3733] | 94 | nNodes += trees[i].Size * (dataset.Rows - 1);
|
---|
| 95 | estimation = interpreter.GetSymbolicExpressionTreeValues(trees[i], dataset, Enumerable.Range(0, dataset.Rows)).ToArray();
|
---|
[2447] | 96 | }
|
---|
| 97 | watch.Stop();
|
---|
| 98 | }
|
---|
[3733] | 99 | Assert.Inconclusive("Random tree evaluation performance of " + interpreter.GetType() + ":" +
|
---|
| 100 | watch.ElapsedMilliseconds + "ms " +
|
---|
[2447] | 101 | Util.NodesPerSecond(nNodes, watch) + " nodes/sec");
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 | }
|
---|