Changeset 13256


Ignore:
Timestamp:
11/18/15 15:16:59 (3 years ago)
Author:
mkommend
Message:

#2442: Added new unit tests for interpreters.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/SymbolicDataAnalysisExpressionTreeInterpreterTest.cs

    r12012 r13256  
    7070    }
    7171
     72    [TestMethod]
     73    [TestCategory("Problems.DataAnalysis.Symbolic")]
     74    [TestProperty("Time", "long")]
     75    public void CompiledInterpreterTestTypeCoherentGrammarPerformance() {
     76      TestTypeCoherentGrammarPerformance(new SymbolicDataAnalysisExpressionCompiledTreeInterpreter(), 12.5e6);
     77    }
     78    [TestMethod]
     79    [TestCategory("Problems.DataAnalysis.Symbolic")]
     80    [TestProperty("Time", "long")]
     81    public void CompiledInterpreterTestFullGrammarPerformance() {
     82      TestFullGrammarPerformance(new SymbolicDataAnalysisExpressionCompiledTreeInterpreter(), 12.5e6);
     83    }
     84    [TestMethod]
     85    [TestCategory("Problems.DataAnalysis.Symbolic")]
     86    [TestProperty("Time", "long")]
     87    public void CompiledInterpreterTestArithmeticGrammarPerformance() {
     88      TestArithmeticGrammarPerformance(new SymbolicDataAnalysisExpressionCompiledTreeInterpreter(), 12.5e6);
     89    }
    7290
    7391    [TestMethod]
     
    113131      var twister = new MersenneTwister(31415);
    114132      var dataset = Util.CreateRandomDataset(twister, Rows, Columns);
     133
    115134      var grammar = new TypeCoherentExpressionGrammar();
    116135      grammar.ConfigureAsDefaultRegressionGrammar();
    117       grammar.MaximumFunctionArguments = 0;
    118       grammar.MaximumFunctionDefinitions = 0;
    119       grammar.MinimumFunctionArguments = 0;
    120       grammar.MinimumFunctionDefinitions = 0;
     136
    121137      var randomTrees = Util.CreateRandomTrees(twister, dataset, grammar, N, 1, 100, 0, 0);
    122138      foreach (ISymbolicExpressionTree tree in randomTrees) {
     
    131147      var twister = new MersenneTwister(31415);
    132148      var dataset = Util.CreateRandomDataset(twister, Rows, Columns);
     149
    133150      var grammar = new FullFunctionalExpressionGrammar();
    134       grammar.MaximumFunctionArguments = 0;
    135       grammar.MaximumFunctionDefinitions = 0;
    136       grammar.MinimumFunctionArguments = 0;
    137       grammar.MinimumFunctionDefinitions = 0;
    138151      var randomTrees = Util.CreateRandomTrees(twister, dataset, grammar, N, 1, 100, 0, 0);
    139152      foreach (ISymbolicExpressionTree tree in randomTrees) {
     
    148161      var twister = new MersenneTwister(31415);
    149162      var dataset = Util.CreateRandomDataset(twister, Rows, Columns);
     163
    150164      var grammar = new ArithmeticExpressionGrammar();
    151       //grammar.Symbols.OfType<Variable>().First().Enabled = false;
    152       grammar.MaximumFunctionArguments = 0;
    153       grammar.MaximumFunctionDefinitions = 0;
    154       grammar.MinimumFunctionArguments = 0;
    155       grammar.MinimumFunctionDefinitions = 0;
    156165      var randomTrees = Util.CreateRandomTrees(twister, dataset, grammar, N, 1, 100, 0, 0);
    157166      foreach (SymbolicExpressionTree tree in randomTrees) {
     
    190199    [TestCategory("Problems.DataAnalysis.Symbolic")]
    191200    [TestProperty("Time", "short")]
     201    public void CompiledInterpreterTestEvaluation() {
     202      var interpreter = new SymbolicDataAnalysisExpressionCompiledTreeInterpreter();
     203      // ADFs are not supported by the compiled tree interpreter
     204      EvaluateTerminals(interpreter, ds);
     205      EvaluateOperations(interpreter, ds);
     206    }
     207
     208    [TestMethod]
     209    [TestCategory("Problems.DataAnalysis.Symbolic")]
     210    [TestProperty("Time", "short")]
    192211    public void LinearInterpreterTestEvaluation() {
    193212      var interpreter = new SymbolicDataAnalysisExpressionTreeLinearInterpreter();
    194 
    195213      //ADFs are not supported by the linear interpreter
    196214      EvaluateTerminals(interpreter, ds);
    197215      EvaluateOperations(interpreter, ds);
     216    }
     217
     218    [TestMethod]
     219    [TestCategory("Problems.DataAnalysis.Symbolic")]
     220    [TestProperty("Time", "long")]
     221    public void TestInterpreterEvaluationResults() {
     222
     223      var twister = new MersenneTwister();
     224      int seed = twister.Next(0, int.MaxValue);
     225      twister.Seed((uint)seed);
     226
     227      const int numRows = 100;
     228      var dataset = Util.CreateRandomDataset(twister, numRows, Columns);
     229
     230      var grammar = new TypeCoherentExpressionGrammar();
     231      var randomTrees = Util.CreateRandomTrees(twister, dataset, grammar, N, 1, 10, 0, 0);
     232      foreach (ISymbolicExpressionTree tree in randomTrees) {
     233        Util.InitTree(tree, twister, new List<string>(dataset.VariableNames));
     234      }
     235
     236      var interpreters = new ISymbolicDataAnalysisExpressionTreeInterpreter[] {
     237        //new SymbolicDataAnalysisExpressionCompiledTreeInterpreter(),
     238        //new SymbolicDataAnalysisExpressionTreeILEmittingInterpreter(),
     239        new SymbolicDataAnalysisExpressionTreeLinearInterpreter(),
     240        new SymbolicDataAnalysisExpressionTreeInterpreter(),
     241      };
     242
     243      var rows = Enumerable.Range(0, numRows).ToList();
     244      for (int i = 0; i < randomTrees.Length; ++i) {
     245        var tree = randomTrees[i];
     246        var valuesMatrix = interpreters.Select(x => x.GetSymbolicExpressionTreeValues(tree, dataset, rows)).ToList();
     247        for (int m = 0; m < interpreters.Length - 1; ++m) {
     248          var sum = valuesMatrix[m].Sum();
     249          for (int n = m + 1; n < interpreters.Length; ++n) {
     250            var s = valuesMatrix[n].Sum();
     251            if (double.IsNaN(sum) && double.IsNaN(s)) continue;
     252
     253            string errorMessage = string.Format("Interpreters {0} and {1} do not agree on tree {2} (seed = {3}).",
     254              interpreters[m].Name, interpreters[n].Name, i, seed);
     255            Assert.AreEqual(sum, valuesMatrix[n].Sum(), 1.0E-12, errorMessage);
     256          }
     257        }
     258      }
    198259    }
    199260
Note: See TracChangeset for help on using the changeset viewer.