Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Tests/SimpleArithmeticExpressionInterpreterTest.cs @ 3733

Last change on this file since 3733 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: 5.7 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 System.IO;
23using System;
24using HeuristicLab.Random;
25using System.Collections.Generic;
26using System.Diagnostics;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Problems.DataAnalysis.Symbolic;
29using Microsoft.VisualStudio.TestTools.UnitTesting;
30using System.Linq;
31namespace HeuristicLab.Problems.DataAnalysis.Tests {
32
33
34  /// <summary>
35  ///This is a test class for SimpleArithmeticExpressionInterpreter and is intended
36  ///to contain all SimpleArithmeticExpressionInterpreter Unit Tests
37  ///</summary>
38  [TestClass()]
39  public class SimpleArithmeticExpressionInterpreterTest {
40    private const int N = 1000;
41    private const int Rows = 1000;
42    private const int Columns = 50;
43    private static SymbolicExpressionTree[] randomTrees;
44    private static Dataset dataset;
45    private static MersenneTwister twister;
46    private TestContext testContextInstance;
47
48    /// <summary>
49    ///Gets or sets the test context which provides
50    ///information about and functionality for the current test run.
51    ///</summary>
52    public TestContext TestContext {
53      get {
54        return testContextInstance;
55      }
56      set {
57        testContextInstance = value;
58      }
59    }
60
61    [ClassInitialize()]
62    public static void CreateRandomTrees(TestContext testContext) {
63      twister = new MersenneTwister();
64      dataset = Util.CreateRandomDataset(twister, Rows, Columns);
65      var grammar = new GlobalSymbolicExpressionGrammar(new ArithmeticExpressionGrammar());
66      grammar.MaxFunctionArguments = 0;
67      grammar.MaxFunctionDefinitions = 0;
68      grammar.MinFunctionArguments = 0;
69      grammar.MinFunctionDefinitions = 0;
70      randomTrees = Util.CreateRandomTrees(twister, dataset, grammar, N, 1, 100, 0, 0);
71    }
72
73
74    [TestMethod()]
75    public void SimpleArithmeticExpressionInterpreterPerformanceTest() {
76      double[] estimation = new double[Rows];
77      foreach (SymbolicExpressionTree tree in randomTrees) {
78        Util.InitTree(tree, twister, new List<string>(dataset.VariableNames));
79      }
80      SimpleArithmeticExpressionInterpreter interpreter = new SimpleArithmeticExpressionInterpreter();
81      Util.EvaluateTrees(randomTrees, interpreter, dataset, 10);
82    }
83
84
85    /// <summary>
86    ///A test for Evaluate
87    ///</summary>
88    [TestMethod()]
89    public void SimpleArithmeticExpressionInterpreterEvaluateTest() {
90
91      Dataset ds = new Dataset(new string[] { "y", "a", "b" }, new double[,] {
92        { 1.0, 1.0, 1.0 },
93        { 2.0, 2.0, 2.0 },
94        { 3.0, 1.0, 2.0 }
95      });
96
97      SimpleArithmeticExpressionInterpreter interpreter = new SimpleArithmeticExpressionInterpreter();
98
99      // constants
100      Evaluate(interpreter, ds, "(+ 1.5 3.5)", 0, 5.0);
101
102      // variables
103      Evaluate(interpreter, ds, "(variable 2.0 a)", 0, 2.0);
104      Evaluate(interpreter, ds, "(variable 2.0 a)", 1, 4.0);
105
106
107      // addition
108      Evaluate(interpreter, ds, "(+ (variable 2.0 a ))", 1, 4.0);
109      Evaluate(interpreter, ds, "(+ (variable 2.0 a ) (variable 3.0 b ))", 0, 5.0);
110      Evaluate(interpreter, ds, "(+ (variable 2.0 a ) (variable 3.0 b ))", 1, 10.0);
111      Evaluate(interpreter, ds, "(+ (variable 2.0 a) (variable 3.0 b ))", 2, 8.0);
112      Evaluate(interpreter, ds, "(+ 8.0 2.0 2.0)", 0, 12.0);
113
114      // subtraction
115      Evaluate(interpreter, ds, "(- (variable 2.0 a ))", 1, -4.0);
116      Evaluate(interpreter, ds, "(- (variable 2.0 a ) (variable 3.0 b))", 0, -1.0);
117      Evaluate(interpreter, ds, "(- (variable 2.0 a ) (variable 3.0 b ))", 1, -2.0);
118      Evaluate(interpreter, ds, "(- (variable 2.0 a ) (variable 3.0 b ))", 2, -4.0);
119      Evaluate(interpreter, ds, "(- 8.0 2.0 2.0)", 0, 4.0);
120
121      // multiplication
122      Evaluate(interpreter, ds, "(* (variable 2.0 a ))", 0, 2.0);
123      Evaluate(interpreter, ds, "(* (variable 2.0 a ) (variable 3.0 b ))", 0, 6.0);
124      Evaluate(interpreter, ds, "(* (variable 2.0 a ) (variable 3.0 b ))", 1, 24.0);
125      Evaluate(interpreter, ds, "(* (variable 2.0 a ) (variable 3.0 b ))", 2, 12.0);
126      Evaluate(interpreter, ds, "(* 8.0 2.0 2.0)", 0, 32.0);
127
128      // division
129      Evaluate(interpreter, ds, "(/ (variable 2.0 a ))", 1, 1.0 / 4.0);
130      Evaluate(interpreter, ds, "(/ (variable 2.0 a ) 2.0)", 0, 1.0);
131      Evaluate(interpreter, ds, "(/ (variable 2.0 a ) 2.0)", 1, 2.0);
132      Evaluate(interpreter, ds, "(/ (variable 3.0 b ) 2.0)", 2, 3.0);
133      Evaluate(interpreter, ds, "(/ 8.0 2.0 2.0)", 0, 2.0);
134
135      // TODO ADF     
136    }
137
138    private void Evaluate(SimpleArithmeticExpressionInterpreter interpreter, Dataset ds, string expr, int index, double expected) {
139      var importer = new SymbolicExpressionImporter();
140      SymbolicExpressionTree tree = importer.Import(expr);
141
142      double actual = interpreter.GetSymbolicExpressionTreeValues(tree, ds, Enumerable.Range(index, 1)).First();
143
144      Assert.AreEqual(expected, actual, 1.0E-12, expr);
145    }
146  }
147}
Note: See TracBrowser for help on using the repository browser.