Free cookie consent management tool by TermsFeed Policy Generator

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

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

Merged r4458, r4459,r4462,r4464 from data analysis exploration branch into trunk. #1142

File size: 11.0 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;
23using System.Collections.Generic;
24using System.Globalization;
25using System.Linq;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27using HeuristicLab.Problems.DataAnalysis.Symbolic;
28using HeuristicLab.Random;
29using Microsoft.VisualStudio.TestTools.UnitTesting;
30namespace HeuristicLab.Problems.DataAnalysis.Tests {
31
32
33  /// <summary>
34  ///This is a test class for SimpleArithmeticExpressionInterpreter and is intended
35  ///to contain all SimpleArithmeticExpressionInterpreter Unit Tests
36  ///</summary>
37  [TestClass()]
38  public class SimpleArithmeticExpressionInterpreterTest {
39    private const int N = 1000;
40    private const int Rows = 1000;
41    private const int Columns = 50;
42    private static SymbolicExpressionTree[] randomTrees;
43    private static Dataset dataset;
44    private static MersenneTwister twister;
45    private TestContext testContextInstance;
46
47    /// <summary>
48    ///Gets or sets the test context which provides
49    ///information about and functionality for the current test run.
50    ///</summary>
51    public TestContext TestContext {
52      get {
53        return testContextInstance;
54      }
55      set {
56        testContextInstance = value;
57      }
58    }
59
60    [ClassInitialize()]
61    public static void CreateRandomTrees(TestContext testContext) {
62      twister = new MersenneTwister();
63      dataset = Util.CreateRandomDataset(twister, Rows, Columns);
64      var grammar = new GlobalSymbolicExpressionGrammar(new FullFunctionalExpressionGrammar());
65      grammar.MaxFunctionArguments = 0;
66      grammar.MaxFunctionDefinitions = 0;
67      grammar.MinFunctionArguments = 0;
68      grammar.MinFunctionDefinitions = 0;
69      randomTrees = Util.CreateRandomTrees(twister, dataset, grammar, N, 1, 100, 0, 0);
70    }
71
72    [TestMethod]
73    public void SimpleArithmeticExpressionInterpreterPerformanceTest() {
74      double[] estimation = new double[Rows];
75      foreach (SymbolicExpressionTree tree in randomTrees) {
76        Util.InitTree(tree, twister, new List<string>(dataset.VariableNames));
77      }
78      SimpleArithmeticExpressionInterpreter interpreter = new SimpleArithmeticExpressionInterpreter();
79      Util.EvaluateTrees(randomTrees, interpreter, dataset, 10);
80    }
81
82
83    /// <summary>
84    ///A test for Evaluate
85    ///</summary>
86    [TestMethod]
87    public void SimpleArithmeticExpressionInterpreterEvaluateTest() {
88
89      Dataset ds = new Dataset(new string[] { "Y", "A", "B" }, new double[,] {
90        { 1.0, 1.0, 1.0 },
91        { 2.0, 2.0, 2.0 },
92        { 3.0, 1.0, 2.0 }
93      });
94
95      SimpleArithmeticExpressionInterpreter interpreter = new SimpleArithmeticExpressionInterpreter();
96
97      // constants
98      Evaluate(interpreter, ds, "(+ 1.5 3.5)", 0, 5.0);
99
100      // variables
101      Evaluate(interpreter, ds, "(variable 2.0 a)", 0, 2.0);
102      Evaluate(interpreter, ds, "(variable 2.0 a)", 1, 4.0);
103
104
105      // addition
106      Evaluate(interpreter, ds, "(+ (variable 2.0 a ))", 1, 4.0);
107      Evaluate(interpreter, ds, "(+ (variable 2.0 a ) (variable 3.0 b ))", 0, 5.0);
108      Evaluate(interpreter, ds, "(+ (variable 2.0 a ) (variable 3.0 b ))", 1, 10.0);
109      Evaluate(interpreter, ds, "(+ (variable 2.0 a) (variable 3.0 b ))", 2, 8.0);
110      Evaluate(interpreter, ds, "(+ 8.0 2.0 2.0)", 0, 12.0);
111
112      // subtraction
113      Evaluate(interpreter, ds, "(- (variable 2.0 a ))", 1, -4.0);
114      Evaluate(interpreter, ds, "(- (variable 2.0 a ) (variable 3.0 b))", 0, -1.0);
115      Evaluate(interpreter, ds, "(- (variable 2.0 a ) (variable 3.0 b ))", 1, -2.0);
116      Evaluate(interpreter, ds, "(- (variable 2.0 a ) (variable 3.0 b ))", 2, -4.0);
117      Evaluate(interpreter, ds, "(- 8.0 2.0 2.0)", 0, 4.0);
118
119      // multiplication
120      Evaluate(interpreter, ds, "(* (variable 2.0 a ))", 0, 2.0);
121      Evaluate(interpreter, ds, "(* (variable 2.0 a ) (variable 3.0 b ))", 0, 6.0);
122      Evaluate(interpreter, ds, "(* (variable 2.0 a ) (variable 3.0 b ))", 1, 24.0);
123      Evaluate(interpreter, ds, "(* (variable 2.0 a ) (variable 3.0 b ))", 2, 12.0);
124      Evaluate(interpreter, ds, "(* 8.0 2.0 2.0)", 0, 32.0);
125
126      // division
127      Evaluate(interpreter, ds, "(/ (variable 2.0 a ))", 1, 1.0 / 4.0);
128      Evaluate(interpreter, ds, "(/ (variable 2.0 a ) 2.0)", 0, 1.0);
129      Evaluate(interpreter, ds, "(/ (variable 2.0 a ) 2.0)", 1, 2.0);
130      Evaluate(interpreter, ds, "(/ (variable 3.0 b ) 2.0)", 2, 3.0);
131      Evaluate(interpreter, ds, "(/ 8.0 2.0 2.0)", 0, 2.0);
132
133      // gt
134      Evaluate(interpreter, ds, "(> (variable 2.0 a) 2.0)", 0, -1.0);
135      Evaluate(interpreter, ds, "(> 2.0 (variable 2.0 a))", 0, -1.0);
136      Evaluate(interpreter, ds, "(> (variable 2.0 a) 1.9)", 0, 1.0);
137      Evaluate(interpreter, ds, "(> 1.9 (variable 2.0 a))", 0, -1.0);
138      //Evaluate(interpreter, ds, "(> (sqrt -1.0) (log -1.0))", 0, -1.0); // (> nan nan) should be false
139
140      // lt
141      Evaluate(interpreter, ds, "(< (variable 2.0 a) 2.0)", 0, -1.0);
142      Evaluate(interpreter, ds, "(< 2.0 (variable 2.0 a))", 0, -1.0);
143      Evaluate(interpreter, ds, "(< (variable 2.0 a) 1.9)", 0, -1.0);
144      Evaluate(interpreter, ds, "(< 1.9 (variable 2.0 a))", 0, 1.0);
145      //Evaluate(interpreter, ds, "(< (sqrt -1,0) (log -1,0))", 0, -1.0); // (< nan nan) should be false
146
147      // If
148      Evaluate(interpreter, ds, "(if -10.0 2.0 3.0)", 0, 3.0);
149      Evaluate(interpreter, ds, "(if -1.0 2.0 3.0)", 0, 3.0);
150      Evaluate(interpreter, ds, "(if 0.0 2.0 3.0)", 0, 3.0);
151      Evaluate(interpreter, ds, "(if 1.0 2.0 3.0)", 0, 2.0);
152      Evaluate(interpreter, ds, "(if 10.0 2.0 3.0)", 0, 2.0);
153      // Evaluate(interpreter, ds, "(if (sqrt -1.0) 2.0 3.0)", 0, 3.0); // if(nan) should return the else branch
154
155      // NOT
156      Evaluate(interpreter, ds, "(not -1.0)", 0, 1.0);
157      Evaluate(interpreter, ds, "(not -2.0)", 0, 2.0);
158      Evaluate(interpreter, ds, "(not 1.0)", 0, -1.0);
159      Evaluate(interpreter, ds, "(not 2.0)", 0, -2.0);
160      Evaluate(interpreter, ds, "(not 0.0)", 0, 0.0);
161
162      // AND
163      Evaluate(interpreter, ds, "(and -1.0 -2.0)", 0, -1.0);
164      Evaluate(interpreter, ds, "(and -1.0 2.0)", 0, -1.0);
165      Evaluate(interpreter, ds, "(and 1.0 -2.0)", 0, -1.0);
166      Evaluate(interpreter, ds, "(and 1.0 0.0)", 0, -1.0);
167      Evaluate(interpreter, ds, "(and 0.0 0.0)", 0, -1.0);
168      Evaluate(interpreter, ds, "(and 1.0 2.0)", 0, 1.0);
169      Evaluate(interpreter, ds, "(and 1.0 2.0 3.0)", 0, 1.0);
170      Evaluate(interpreter, ds, "(and 1.0 -2.0 3.0)", 0, -1.0);
171
172      // OR
173      Evaluate(interpreter, ds, "(or -1.0 -2.0)", 0, -1.0);
174      Evaluate(interpreter, ds, "(or -1.0 2.0)", 0, 1.0);
175      Evaluate(interpreter, ds, "(or 1.0 -2.0)", 0, 1.0);
176      Evaluate(interpreter, ds, "(or 1.0 2.0)", 0, 1.0);
177      Evaluate(interpreter, ds, "(or 0.0 0.0)", 0, -1.0);
178      Evaluate(interpreter, ds, "(or -1.0 -2.0 -3.0)", 0, -1.0);
179      Evaluate(interpreter, ds, "(or -1.0 -2.0 3.0)", 0, 1.0);
180
181      // sin, cos, tan
182      Evaluate(interpreter, ds, "(sin " + Math.PI.ToString(NumberFormatInfo.InvariantInfo) + ")", 0, 0.0);
183      Evaluate(interpreter, ds, "(sin 0.0)", 0, 0.0);
184      Evaluate(interpreter, ds, "(cos " + Math.PI.ToString(NumberFormatInfo.InvariantInfo) + ")", 0, -1.0);
185      Evaluate(interpreter, ds, "(cos 0.0)", 0, 1.0);
186      Evaluate(interpreter, ds, "(tan " + Math.PI.ToString(NumberFormatInfo.InvariantInfo) + ")", 0, Math.Tan(Math.PI));
187      Evaluate(interpreter, ds, "(tan 0.0)", 0, Math.Tan(Math.PI));
188
189      // exp, log
190      Evaluate(interpreter, ds, "(log (exp 7.0))", 0, Math.Log(Math.Exp(7)));
191      Evaluate(interpreter, ds, "(exp (log 7.0))", 0, Math.Exp(Math.Log(7)));
192      Evaluate(interpreter, ds, "(log -3.0)", 0, Math.Log(-3));
193
194      // mean
195      Evaluate(interpreter, ds, "(mean -1.0 1.0 -1.0)", 0, -1.0 / 3.0);
196
197      // ADF     
198      Evaluate(interpreter, ds, @"(PROG
199                                    (MAIN
200                                      (CALL ADF0))
201                                    (defun ADF0 1.0))", 1, 1.0);
202      Evaluate(interpreter, ds, @"(PROG
203                                    (MAIN
204                                      (* (CALL ADF0) (CALL ADF0)))
205                                    (defun ADF0 2.0))", 1, 4.0);
206      Evaluate(interpreter, ds, @"(PROG
207                                    (MAIN
208                                      (CALL ADF0 2.0 3.0))
209                                    (defun ADF0
210                                      (+ (ARG 0) (ARG 1))))", 1, 5.0);
211      Evaluate(interpreter, ds, @"(PROG
212                                    (MAIN (CALL ADF1 2.0 3.0))
213                                    (defun ADF0
214                                      (- (ARG 1) (ARG 0)))
215                                    (defun ADF1
216                                      (+ (CALL ADF0 (ARG 1) (ARG 0))
217                                         (CALL ADF0 (ARG 0) (ARG 1)))))", 1, 0.0);
218      Evaluate(interpreter, ds, @"(PROG
219                                    (MAIN (CALL ADF1 (variable 2.0 a) 3.0))
220                                    (defun ADF0
221                                      (- (ARG 1) (ARG 0)))
222                                    (defun ADF1                                                                             
223                                      (CALL ADF0 (ARG 1) (ARG 0))))", 1, 1.0);
224      Evaluate(interpreter, ds, @"(PROG
225                                    (MAIN (CALL ADF1 (variable 2.0 a) 3.0))
226                                    (defun ADF0
227                                      (- (ARG 1) (ARG 0)))
228                                    (defun ADF1                                                                             
229                                      (+ (CALL ADF0 (ARG 1) (ARG 0))
230                                         (CALL ADF0 (ARG 0) (ARG 1)))))", 1, 0.0);
231    }
232
233    private void Evaluate(SimpleArithmeticExpressionInterpreter interpreter, Dataset ds, string expr, int index, double expected) {
234      var importer = new SymbolicExpressionImporter();
235      SymbolicExpressionTree tree = importer.Import(expr);
236
237      double actual = interpreter.GetSymbolicExpressionTreeValues(tree, ds, Enumerable.Range(index, 1)).First();
238
239      Assert.AreEqual(expected, actual, 1.0E-12, expr);
240    }
241  }
242}
Note: See TracBrowser for help on using the repository browser.