Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3855 was 3733, checked in by gkronber, 14 years ago

Added test cases for arithmetic expression interpreter. And fixed minor problems in the interpreter. #791

File size: 4.6 KB
Line 
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
22using Microsoft.VisualStudio.TestTools.UnitTesting;
23using System;
24using HeuristicLab.Random;
25using System.Collections.Generic;
26using System.Text;
27using System.Diagnostics;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
30using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;
31using HeuristicLab.Problems.DataAnalysis.Symbolic;
32using System.Linq;
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 void EvaluateTrees(SymbolicExpressionTree[] trees, ISymbolicExpressionTreeInterpreter interpreter, Dataset dataset, int repetitions) {
85      double[] estimation = new double[dataset.Rows];
86      // warm up
87      for (int i = 0; i < trees.Length; i++) {
88        estimation = interpreter.GetSymbolicExpressionTreeValues(trees[i], dataset, Enumerable.Range(0, dataset.Rows)).ToArray();
89      }
90
91      Stopwatch watch = new Stopwatch();
92      long nNodes = 0;
93      for (int rep = 0; rep < repetitions; rep++) {
94        watch.Start();
95        for (int i = 0; i < trees.Length; i++) {
96          nNodes += trees[i].Size * (dataset.Rows - 1);
97          estimation = interpreter.GetSymbolicExpressionTreeValues(trees[i], dataset, Enumerable.Range(0, dataset.Rows)).ToArray();
98        }
99        watch.Stop();
100      }
101      Assert.Inconclusive("Random tree evaluation performance of " + interpreter.GetType() + ":" +
102        watch.ElapsedMilliseconds + "ms " +
103        Util.NodesPerSecond(nNodes, watch) + " nodes/sec");
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.