Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Problems.DataAnalysis/3.3/Tests/Util.cs @ 7461

Last change on this file since 7461 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 4.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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
22using System.Collections.Generic;
23using System.Diagnostics;
24using System.Linq;
25using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;
27using HeuristicLab.Problems.DataAnalysis.Symbolic;
28using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
29using HeuristicLab.Random;
30using Microsoft.VisualStudio.TestTools.UnitTesting;
31using HeuristicLab.Problems.DataAnalysis.Evaluators;
32using System;
33namespace HeuristicLab.Problems.DataAnalysis.Tests {
34  internal class Util {
35
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;
40          varNode.Weight = twister.NextDouble() * 20.0 - 10.0;
41          varNode.VariableName = varNames[twister.Next(varNames.Count)];
42        } else if (node is ConstantTreeNode) {
43          var constantNode = node as ConstantTreeNode;
44          constantNode.Value = twister.NextDouble() * 20.0 - 10.0;
45        }
46      }
47    }
48
49
50    public static SymbolicExpressionTree[] CreateRandomTrees(MersenneTwister twister, Dataset dataset, ISymbolicExpressionGrammar grammar, int popSize) {
51      return CreateRandomTrees(twister, dataset, grammar, popSize, 1, 200, 3, 3);
52    }
53
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);
59      }
60      SymbolicExpressionTree[] randomTrees = new SymbolicExpressionTree[popSize];
61      for (int i = 0; i < randomTrees.Length; i++) {
62        randomTrees[i] = ProbabilisticTreeCreator.Create(twister, grammar, maxSize, 10, maxFunctionDefinitions, maxFunctionArguments);
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      }
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);
77      return ds;
78    }
79
80    public static double NodesPerSecond(long nNodes, Stopwatch watch) {
81      return nNodes / (watch.ElapsedMilliseconds / 1000.0);
82    }
83
84    public static double CalculateEvaluatedNodesPerSec(SymbolicExpressionTree[] trees, ISymbolicExpressionTreeInterpreter interpreter, Dataset dataset, int repetitions) {
85      // warm up
86      IEnumerable<int> rows = Enumerable.Range(0, dataset.Rows);
87      long nNodes = 0;
88      double c = 0;
89      for (int i = 0; i < trees.Length; i++) {
90        nNodes += trees[i].Size * (dataset.Rows - 1);
91        c = interpreter.GetSymbolicExpressionTreeValues(trees[i], dataset, rows).Count(); // count needs to evaluate all rows
92      }
93
94      Stopwatch watch = new Stopwatch();
95      for (int rep = 0; rep < repetitions; rep++) {
96        watch.Start();
97        c = 0;
98        for (int i = 0; i < trees.Length; i++) {
99          interpreter.GetSymbolicExpressionTreeValues(trees[i], dataset, rows).Count(); // count needs to evaluate all rows
100        }
101        watch.Stop();
102      }
103      Console.WriteLine("Random tree evaluation performance of " + interpreter.GetType() + ": " +
104        watch.ElapsedMilliseconds + "ms " +
105        Util.NodesPerSecond(nNodes * repetitions, watch) + " nodes/sec");
106      return Util.NodesPerSecond(nNodes * repetitions, watch);
107    }
108  }
109}
Note: See TracBrowser for help on using the repository browser.