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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Globalization;
|
---|
25 | using System.Linq;
|
---|
26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
27 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
28 | using HeuristicLab.Random;
|
---|
29 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
30 | namespace 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 |
|
---|
73 | [TestMethod()]
|
---|
74 | public void SimpleArithmeticExpressionInterpreterPerformanceTest() {
|
---|
75 | double[] estimation = new double[Rows];
|
---|
76 | foreach (SymbolicExpressionTree tree in randomTrees) {
|
---|
77 | Util.InitTree(tree, twister, new List<string>(dataset.VariableNames));
|
---|
78 | }
|
---|
79 | SimpleArithmeticExpressionInterpreter interpreter = new SimpleArithmeticExpressionInterpreter();
|
---|
80 | Util.EvaluateTrees(randomTrees, interpreter, dataset, 10);
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 | /// <summary>
|
---|
85 | ///A test for Evaluate
|
---|
86 | ///</summary>
|
---|
87 | [TestMethod()]
|
---|
88 | public void SimpleArithmeticExpressionInterpreterEvaluateTest() {
|
---|
89 |
|
---|
90 | Dataset ds = new Dataset(new string[] { "Y", "A", "B" }, new double[,] {
|
---|
91 | { 1.0, 1.0, 1.0 },
|
---|
92 | { 2.0, 2.0, 2.0 },
|
---|
93 | { 3.0, 1.0, 2.0 }
|
---|
94 | });
|
---|
95 |
|
---|
96 | SimpleArithmeticExpressionInterpreter interpreter = new SimpleArithmeticExpressionInterpreter();
|
---|
97 |
|
---|
98 | // constants
|
---|
99 | Evaluate(interpreter, ds, "(+ 1.5 3.5)", 0, 5.0);
|
---|
100 |
|
---|
101 | // variables
|
---|
102 | Evaluate(interpreter, ds, "(variable 2.0 a)", 0, 2.0);
|
---|
103 | Evaluate(interpreter, ds, "(variable 2.0 a)", 1, 4.0);
|
---|
104 |
|
---|
105 |
|
---|
106 | // addition
|
---|
107 | Evaluate(interpreter, ds, "(+ (variable 2.0 a ))", 1, 4.0);
|
---|
108 | Evaluate(interpreter, ds, "(+ (variable 2.0 a ) (variable 3.0 b ))", 0, 5.0);
|
---|
109 | Evaluate(interpreter, ds, "(+ (variable 2.0 a ) (variable 3.0 b ))", 1, 10.0);
|
---|
110 | Evaluate(interpreter, ds, "(+ (variable 2.0 a) (variable 3.0 b ))", 2, 8.0);
|
---|
111 | Evaluate(interpreter, ds, "(+ 8.0 2.0 2.0)", 0, 12.0);
|
---|
112 |
|
---|
113 | // subtraction
|
---|
114 | Evaluate(interpreter, ds, "(- (variable 2.0 a ))", 1, -4.0);
|
---|
115 | Evaluate(interpreter, ds, "(- (variable 2.0 a ) (variable 3.0 b))", 0, -1.0);
|
---|
116 | Evaluate(interpreter, ds, "(- (variable 2.0 a ) (variable 3.0 b ))", 1, -2.0);
|
---|
117 | Evaluate(interpreter, ds, "(- (variable 2.0 a ) (variable 3.0 b ))", 2, -4.0);
|
---|
118 | Evaluate(interpreter, ds, "(- 8.0 2.0 2.0)", 0, 4.0);
|
---|
119 |
|
---|
120 | // multiplication
|
---|
121 | Evaluate(interpreter, ds, "(* (variable 2.0 a ))", 0, 2.0);
|
---|
122 | Evaluate(interpreter, ds, "(* (variable 2.0 a ) (variable 3.0 b ))", 0, 6.0);
|
---|
123 | Evaluate(interpreter, ds, "(* (variable 2.0 a ) (variable 3.0 b ))", 1, 24.0);
|
---|
124 | Evaluate(interpreter, ds, "(* (variable 2.0 a ) (variable 3.0 b ))", 2, 12.0);
|
---|
125 | Evaluate(interpreter, ds, "(* 8.0 2.0 2.0)", 0, 32.0);
|
---|
126 |
|
---|
127 | // division
|
---|
128 | Evaluate(interpreter, ds, "(/ (variable 2.0 a ))", 1, 1.0 / 4.0);
|
---|
129 | Evaluate(interpreter, ds, "(/ (variable 2.0 a ) 2.0)", 0, 1.0);
|
---|
130 | Evaluate(interpreter, ds, "(/ (variable 2.0 a ) 2.0)", 1, 2.0);
|
---|
131 | Evaluate(interpreter, ds, "(/ (variable 3.0 b ) 2.0)", 2, 3.0);
|
---|
132 | Evaluate(interpreter, ds, "(/ 8.0 2.0 2.0)", 0, 2.0);
|
---|
133 |
|
---|
134 | // gt
|
---|
135 | Evaluate(interpreter, ds, "(> (variable 2.0 a) 2.0)", 0, -1.0);
|
---|
136 | Evaluate(interpreter, ds, "(> 2.0 (variable 2.0 a))", 0, -1.0);
|
---|
137 | Evaluate(interpreter, ds, "(> (variable 2.0 a) 1.9)", 0, 1.0);
|
---|
138 | Evaluate(interpreter, ds, "(> 1.9 (variable 2.0 a))", 0, -1.0);
|
---|
139 | //Evaluate(interpreter, ds, "(> (sqrt -1.0) (log -1.0))", 0, -1.0); // (> nan nan) should be false
|
---|
140 |
|
---|
141 | // lt
|
---|
142 | Evaluate(interpreter, ds, "(< (variable 2.0 a) 2.0)", 0, -1.0);
|
---|
143 | Evaluate(interpreter, ds, "(< 2.0 (variable 2.0 a))", 0, -1.0);
|
---|
144 | Evaluate(interpreter, ds, "(< (variable 2.0 a) 1.9)", 0, -1.0);
|
---|
145 | Evaluate(interpreter, ds, "(< 1.9 (variable 2.0 a))", 0, 1.0);
|
---|
146 | //Evaluate(interpreter, ds, "(< (sqrt -1,0) (log -1,0))", 0, -1.0); // (< nan nan) should be false
|
---|
147 |
|
---|
148 | // If
|
---|
149 | Evaluate(interpreter, ds, "(if -10.0 2.0 3.0)", 0, 3.0);
|
---|
150 | Evaluate(interpreter, ds, "(if -1.0 2.0 3.0)", 0, 3.0);
|
---|
151 | Evaluate(interpreter, ds, "(if 0.0 2.0 3.0)", 0, 3.0);
|
---|
152 | Evaluate(interpreter, ds, "(if 1.0 2.0 3.0)", 0, 2.0);
|
---|
153 | Evaluate(interpreter, ds, "(if 10.0 2.0 3.0)", 0, 2.0);
|
---|
154 | // Evaluate(interpreter, ds, "(if (sqrt -1.0) 2.0 3.0)", 0, 3.0); // if(nan) should return the else branch
|
---|
155 |
|
---|
156 | // NOT
|
---|
157 | Evaluate(interpreter, ds, "(not -1.0)", 0, 1.0);
|
---|
158 | Evaluate(interpreter, ds, "(not -2.0)", 0, 2.0);
|
---|
159 | Evaluate(interpreter, ds, "(not 1.0)", 0, -1.0);
|
---|
160 | Evaluate(interpreter, ds, "(not 2.0)", 0, -2.0);
|
---|
161 | Evaluate(interpreter, ds, "(not 0.0)", 0, 0.0);
|
---|
162 |
|
---|
163 | // AND
|
---|
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 -2.0)", 0, -1.0);
|
---|
167 | Evaluate(interpreter, ds, "(and 1.0 0.0)", 0, -1.0);
|
---|
168 | Evaluate(interpreter, ds, "(and 0.0 0.0)", 0, -1.0);
|
---|
169 | Evaluate(interpreter, ds, "(and 1.0 2.0)", 0, 1.0);
|
---|
170 | Evaluate(interpreter, ds, "(and 1.0 2.0 3.0)", 0, 1.0);
|
---|
171 | Evaluate(interpreter, ds, "(and 1.0 -2.0 3.0)", 0, -1.0);
|
---|
172 |
|
---|
173 | // OR
|
---|
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 1.0 2.0)", 0, 1.0);
|
---|
178 | Evaluate(interpreter, ds, "(or 0.0 0.0)", 0, -1.0);
|
---|
179 | Evaluate(interpreter, ds, "(or -1.0 -2.0 -3.0)", 0, -1.0);
|
---|
180 | Evaluate(interpreter, ds, "(or -1.0 -2.0 3.0)", 0, 1.0);
|
---|
181 |
|
---|
182 | // sin, cos, tan
|
---|
183 | Evaluate(interpreter, ds, "(sin " + Math.PI.ToString(NumberFormatInfo.InvariantInfo) + ")", 0, 0.0);
|
---|
184 | Evaluate(interpreter, ds, "(sin 0.0)", 0, 0.0);
|
---|
185 | Evaluate(interpreter, ds, "(cos " + Math.PI.ToString(NumberFormatInfo.InvariantInfo) + ")", 0, -1.0);
|
---|
186 | Evaluate(interpreter, ds, "(cos 0.0)", 0, 1.0);
|
---|
187 | Evaluate(interpreter, ds, "(tan " + Math.PI.ToString(NumberFormatInfo.InvariantInfo) + ")", 0, Math.Tan(Math.PI));
|
---|
188 | Evaluate(interpreter, ds, "(tan 0.0)", 0, Math.Tan(Math.PI));
|
---|
189 |
|
---|
190 | // exp, log
|
---|
191 | Evaluate(interpreter, ds, "(log (exp 7.0))", 0, Math.Log(Math.Exp(7)));
|
---|
192 | Evaluate(interpreter, ds, "(exp (log 7.0))", 0, Math.Exp(Math.Log(7)));
|
---|
193 | Evaluate(interpreter, ds, "(log -3.0)", 0, Math.Log(-3));
|
---|
194 |
|
---|
195 | // mean
|
---|
196 | Evaluate(interpreter, ds, "(mean -1.0 1.0 -1.0)", 0, -1.0 / 3.0);
|
---|
197 |
|
---|
198 | // ADF
|
---|
199 | Evaluate(interpreter, ds, @"(PROG
|
---|
200 | (MAIN
|
---|
201 | (CALL ADF0))
|
---|
202 | (defun ADF0 1.0))", 1, 1.0);
|
---|
203 | Evaluate(interpreter, ds, @"(PROG
|
---|
204 | (MAIN
|
---|
205 | (* (CALL ADF0) (CALL ADF0)))
|
---|
206 | (defun ADF0 2.0))", 1, 4.0);
|
---|
207 | Evaluate(interpreter, ds, @"(PROG
|
---|
208 | (MAIN
|
---|
209 | (CALL ADF0 2.0 3.0))
|
---|
210 | (defun ADF0
|
---|
211 | (+ (ARG 0) (ARG 1))))", 1, 5.0);
|
---|
212 | Evaluate(interpreter, ds, @"(PROG
|
---|
213 | (MAIN (CALL ADF1 2.0 3.0))
|
---|
214 | (defun ADF0
|
---|
215 | (- (ARG 1) (ARG 0)))
|
---|
216 | (defun ADF1
|
---|
217 | (+ (CALL ADF0 (ARG 1) (ARG 0))
|
---|
218 | (CALL ADF0 (ARG 0) (ARG 1)))))", 1, 0.0);
|
---|
219 | Evaluate(interpreter, ds, @"(PROG
|
---|
220 | (MAIN (CALL ADF1 (variable 2.0 a) 3.0))
|
---|
221 | (defun ADF0
|
---|
222 | (- (ARG 1) (ARG 0)))
|
---|
223 | (defun ADF1
|
---|
224 | (CALL ADF0 (ARG 1) (ARG 0))))", 1, 1.0);
|
---|
225 | Evaluate(interpreter, ds, @"(PROG
|
---|
226 | (MAIN (CALL ADF1 (variable 2.0 a) 3.0))
|
---|
227 | (defun ADF0
|
---|
228 | (- (ARG 1) (ARG 0)))
|
---|
229 | (defun ADF1
|
---|
230 | (+ (CALL ADF0 (ARG 1) (ARG 0))
|
---|
231 | (CALL ADF0 (ARG 0) (ARG 1)))))", 1, 0.0);
|
---|
232 | }
|
---|
233 |
|
---|
234 | private void Evaluate(SimpleArithmeticExpressionInterpreter interpreter, Dataset ds, string expr, int index, double expected) {
|
---|
235 | var importer = new SymbolicExpressionImporter();
|
---|
236 | SymbolicExpressionTree tree = importer.Import(expr);
|
---|
237 |
|
---|
238 | double actual = interpreter.GetSymbolicExpressionTreeValues(tree, ds, Enumerable.Range(index, 1)).First();
|
---|
239 |
|
---|
240 | Assert.AreEqual(expected, actual, 1.0E-12, expr);
|
---|
241 | }
|
---|
242 | }
|
---|
243 | }
|
---|