Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Tests/Util.cs @ 5440

Last change on this file since 5440 was 5412, checked in by gkronber, 13 years ago

Improved test cases for data analysis plugins. #1336

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