1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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.IntegerVectorEncoding;
|
---|
27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
28 | using HeuristicLab.Random;
|
---|
29 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
30 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Tests {
|
---|
31 |
|
---|
32 |
|
---|
33 | [TestClass()]
|
---|
34 | public class SymbolicDataAnalysisExpressionTreeInterpreterTest {
|
---|
35 | private const int N = 1000;
|
---|
36 | private const int Rows = 1000;
|
---|
37 | private const int Columns = 50;
|
---|
38 | private TestContext testContextInstance;
|
---|
39 |
|
---|
40 | /// <summary>
|
---|
41 | ///Gets or sets the test context which provides
|
---|
42 | ///information about and functionality for the current test run.
|
---|
43 | ///</summary>
|
---|
44 | public TestContext TestContext {
|
---|
45 | get {
|
---|
46 | return testContextInstance;
|
---|
47 | }
|
---|
48 | set {
|
---|
49 | testContextInstance = value;
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | [TestMethod]
|
---|
54 | public void SymbolicDataAnalysisExpressionTreeInterpreterFullGrammarPerformanceTest() {
|
---|
55 | FullGrammarPerformanceTest(new SymbolicDataAnalysisExpressionTreeInterpreter(), 12.5e6);
|
---|
56 | }
|
---|
57 | [TestMethod]
|
---|
58 | public void SymbolicDataAnalysisExpressionTreeInterpreterArithmeticGrammarPerformanceTest() {
|
---|
59 | FullGrammarPerformanceTest(new SymbolicDataAnalysisExpressionTreeInterpreter(), 12.5e6);
|
---|
60 | }
|
---|
61 |
|
---|
62 | [TestMethod]
|
---|
63 | public void SymbolicDataAnalysisExpressionTreeILEmittingInterpreterFullGrammarPerformanceTest() {
|
---|
64 | FullGrammarPerformanceTest(new SymbolicDataAnalysisExpressionTreeILEmittingInterpreter(), 7.5e6);
|
---|
65 | }
|
---|
66 | [TestMethod]
|
---|
67 | public void SymbolicDataAnalysisExpressionTreeILEmittingInterpreterArithmeticGrammarPerformanceTest() {
|
---|
68 | FullGrammarPerformanceTest(new SymbolicDataAnalysisExpressionTreeILEmittingInterpreter(), 7.5e6);
|
---|
69 | }
|
---|
70 |
|
---|
71 | private void FullGrammarPerformanceTest(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, double nodesPerSecThreshold) {
|
---|
72 | var twister = new MersenneTwister(31415);
|
---|
73 | var dataset = Util.CreateRandomDataset(twister, Rows, Columns);
|
---|
74 | var grammar = new FullFunctionalExpressionGrammar();
|
---|
75 | grammar.MaximumFunctionArguments = 0;
|
---|
76 | grammar.MaximumFunctionDefinitions = 0;
|
---|
77 | grammar.MinimumFunctionArguments = 0;
|
---|
78 | grammar.MinimumFunctionDefinitions = 0;
|
---|
79 | var randomTrees = Util.CreateRandomTrees(twister, dataset, grammar, N, 1, 100, 0, 0);
|
---|
80 | foreach (ISymbolicExpressionTree tree in randomTrees) {
|
---|
81 | Util.InitTree(tree, twister, new List<string>(dataset.VariableNames));
|
---|
82 | }
|
---|
83 | double nodesPerSec = Util.CalculateEvaluatedNodesPerSec(randomTrees, interpreter, dataset, 3);
|
---|
84 | Assert.IsTrue(nodesPerSec > nodesPerSecThreshold); // evaluated nodes per seconds must be larger than 15mNodes/sec
|
---|
85 | }
|
---|
86 |
|
---|
87 | private void ArithmeticGrammarPerformanceTest(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, double nodesPerSecThreshold) {
|
---|
88 | var twister = new MersenneTwister(31415);
|
---|
89 | var dataset = Util.CreateRandomDataset(twister, Rows, Columns);
|
---|
90 | var grammar = new ArithmeticExpressionGrammar();
|
---|
91 | grammar.MaximumFunctionArguments = 0;
|
---|
92 | grammar.MaximumFunctionDefinitions = 0;
|
---|
93 | grammar.MinimumFunctionArguments = 0;
|
---|
94 | grammar.MinimumFunctionDefinitions = 0;
|
---|
95 | var randomTrees = Util.CreateRandomTrees(twister, dataset, grammar, N, 1, 100, 0, 0);
|
---|
96 | foreach (SymbolicExpressionTree tree in randomTrees) {
|
---|
97 | Util.InitTree(tree, twister, new List<string>(dataset.VariableNames));
|
---|
98 | }
|
---|
99 |
|
---|
100 | double nodesPerSec = Util.CalculateEvaluatedNodesPerSec(randomTrees, interpreter, dataset, 3);
|
---|
101 | Assert.IsTrue(nodesPerSec > nodesPerSecThreshold); // evaluated nodes per seconds must be larger than 15mNodes/sec
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | /// <summary>
|
---|
106 | ///A test for Evaluate
|
---|
107 | ///</summary>
|
---|
108 | [TestMethod]
|
---|
109 | public void SymbolicDataAnalysisExpressionTreeInterpreterEvaluateTest() {
|
---|
110 | Dataset ds = new Dataset(new string[] { "Y", "A", "B" }, new double[,] {
|
---|
111 | { 1.0, 1.0, 1.0 },
|
---|
112 | { 2.0, 2.0, 2.0 },
|
---|
113 | { 3.0, 1.0, 2.0 },
|
---|
114 | { 4.0, 1.0, 1.0 },
|
---|
115 | { 5.0, 2.0, 2.0 },
|
---|
116 | { 6.0, 1.0, 2.0 },
|
---|
117 | { 7.0, 1.0, 1.0 },
|
---|
118 | { 8.0, 2.0, 2.0 },
|
---|
119 | { 9.0, 1.0, 2.0 },
|
---|
120 | { 10.0, 1.0, 1.0 },
|
---|
121 | { 11.0, 2.0, 2.0 },
|
---|
122 | { 12.0, 1.0, 2.0 }
|
---|
123 | });
|
---|
124 |
|
---|
125 | var interpreter = new SymbolicDataAnalysisExpressionTreeInterpreter();
|
---|
126 | EvaluateTerminals(interpreter, ds);
|
---|
127 | EvaluateOperations(interpreter, ds);
|
---|
128 | EvaluateAdf(interpreter, ds);
|
---|
129 | }
|
---|
130 |
|
---|
131 | [TestMethod]
|
---|
132 | public void SymbolicDataAnalysisExpressionILEmittingTreeInterpreterEvaluateTest() {
|
---|
133 | Dataset ds = new Dataset(new string[] { "Y", "A", "B" }, new double[,] {
|
---|
134 | { 1.0, 1.0, 1.0 },
|
---|
135 | { 2.0, 2.0, 2.0 },
|
---|
136 | { 3.0, 1.0, 2.0 },
|
---|
137 | { 4.0, 1.0, 1.0 },
|
---|
138 | { 5.0, 2.0, 2.0 },
|
---|
139 | { 6.0, 1.0, 2.0 },
|
---|
140 | { 7.0, 1.0, 1.0 },
|
---|
141 | { 8.0, 2.0, 2.0 },
|
---|
142 | { 9.0, 1.0, 2.0 },
|
---|
143 | { 10.0, 1.0, 1.0 },
|
---|
144 | { 11.0, 2.0, 2.0 },
|
---|
145 | { 12.0, 1.0, 2.0 }
|
---|
146 | });
|
---|
147 |
|
---|
148 | var interpreter = new SymbolicDataAnalysisExpressionTreeILEmittingInterpreter();
|
---|
149 | EvaluateTerminals(interpreter, ds);
|
---|
150 | EvaluateOperations(interpreter, ds);
|
---|
151 | }
|
---|
152 |
|
---|
153 | private void EvaluateTerminals(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, Dataset ds) {
|
---|
154 | // constants
|
---|
155 | Evaluate(interpreter, ds, "(+ 1.5 3.5)", 0, 5.0);
|
---|
156 |
|
---|
157 | // variables
|
---|
158 | Evaluate(interpreter, ds, "(variable 2.0 a)", 0, 2.0);
|
---|
159 | Evaluate(interpreter, ds, "(variable 2.0 a)", 1, 4.0);
|
---|
160 | }
|
---|
161 |
|
---|
162 | private void EvaluateAdf(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, Dataset ds) {
|
---|
163 |
|
---|
164 | // ADF
|
---|
165 | Evaluate(interpreter, ds, @"(PROG
|
---|
166 | (MAIN
|
---|
167 | (CALL ADF0))
|
---|
168 | (defun ADF0 1.0))", 1, 1.0);
|
---|
169 | Evaluate(interpreter, ds, @"(PROG
|
---|
170 | (MAIN
|
---|
171 | (* (CALL ADF0) (CALL ADF0)))
|
---|
172 | (defun ADF0 2.0))", 1, 4.0);
|
---|
173 | Evaluate(interpreter, ds, @"(PROG
|
---|
174 | (MAIN
|
---|
175 | (CALL ADF0 2.0 3.0))
|
---|
176 | (defun ADF0
|
---|
177 | (+ (ARG 0) (ARG 1))))", 1, 5.0);
|
---|
178 | Evaluate(interpreter, ds, @"(PROG
|
---|
179 | (MAIN (CALL ADF1 2.0 3.0))
|
---|
180 | (defun ADF0
|
---|
181 | (- (ARG 1) (ARG 0)))
|
---|
182 | (defun ADF1
|
---|
183 | (+ (CALL ADF0 (ARG 1) (ARG 0))
|
---|
184 | (CALL ADF0 (ARG 0) (ARG 1)))))", 1, 0.0);
|
---|
185 | Evaluate(interpreter, ds, @"(PROG
|
---|
186 | (MAIN (CALL ADF1 (variable 2.0 a) 3.0))
|
---|
187 | (defun ADF0
|
---|
188 | (- (ARG 1) (ARG 0)))
|
---|
189 | (defun ADF1
|
---|
190 | (CALL ADF0 (ARG 1) (ARG 0))))", 1, 1.0);
|
---|
191 | Evaluate(interpreter, ds,
|
---|
192 | @"(PROG
|
---|
193 | (MAIN (CALL ADF1 (variable 2.0 a) 3.0))
|
---|
194 | (defun ADF0
|
---|
195 | (- (ARG 1) (ARG 0)))
|
---|
196 | (defun ADF1
|
---|
197 | (+ (CALL ADF0 (ARG 1) (ARG 0))
|
---|
198 | (CALL ADF0 (ARG 0) (ARG 1)))))", 1, 0.0);
|
---|
199 | }
|
---|
200 |
|
---|
201 | private void EvaluateOperations(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, Dataset ds) {
|
---|
202 | // addition
|
---|
203 | Evaluate(interpreter, ds, "(+ (variable 2.0 a ))", 1, 4.0);
|
---|
204 | Evaluate(interpreter, ds, "(+ (variable 2.0 a ) (variable 3.0 b ))", 0, 5.0);
|
---|
205 | Evaluate(interpreter, ds, "(+ (variable 2.0 a ) (variable 3.0 b ))", 1, 10.0);
|
---|
206 | Evaluate(interpreter, ds, "(+ (variable 2.0 a) (variable 3.0 b ))", 2, 8.0);
|
---|
207 | Evaluate(interpreter, ds, "(+ 8.0 2.0 2.0)", 0, 12.0);
|
---|
208 |
|
---|
209 | // subtraction
|
---|
210 | Evaluate(interpreter, ds, "(- (variable 2.0 a ))", 1, -4.0);
|
---|
211 | Evaluate(interpreter, ds, "(- (variable 2.0 a ) (variable 3.0 b))", 0, -1.0);
|
---|
212 | Evaluate(interpreter, ds, "(- (variable 2.0 a ) (variable 3.0 b ))", 1, -2.0);
|
---|
213 | Evaluate(interpreter, ds, "(- (variable 2.0 a ) (variable 3.0 b ))", 2, -4.0);
|
---|
214 | Evaluate(interpreter, ds, "(- 8.0 2.0 2.0)", 0, 4.0);
|
---|
215 |
|
---|
216 | // multiplication
|
---|
217 | Evaluate(interpreter, ds, "(* (variable 2.0 a ))", 0, 2.0);
|
---|
218 | Evaluate(interpreter, ds, "(* (variable 2.0 a ) (variable 3.0 b ))", 0, 6.0);
|
---|
219 | Evaluate(interpreter, ds, "(* (variable 2.0 a ) (variable 3.0 b ))", 1, 24.0);
|
---|
220 | Evaluate(interpreter, ds, "(* (variable 2.0 a ) (variable 3.0 b ))", 2, 12.0);
|
---|
221 | Evaluate(interpreter, ds, "(* 8.0 2.0 2.0)", 0, 32.0);
|
---|
222 |
|
---|
223 | // division
|
---|
224 | Evaluate(interpreter, ds, "(/ (variable 2.0 a ))", 1, 1.0 / 4.0);
|
---|
225 | Evaluate(interpreter, ds, "(/ (variable 2.0 a ) 2.0)", 0, 1.0);
|
---|
226 | Evaluate(interpreter, ds, "(/ (variable 2.0 a ) 2.0)", 1, 2.0);
|
---|
227 | Evaluate(interpreter, ds, "(/ (variable 3.0 b ) 2.0)", 2, 3.0);
|
---|
228 | Evaluate(interpreter, ds, "(/ 8.0 2.0 2.0)", 0, 2.0);
|
---|
229 |
|
---|
230 | // gt
|
---|
231 | Evaluate(interpreter, ds, "(> (variable 2.0 a) 2.0)", 0, -1.0);
|
---|
232 | Evaluate(interpreter, ds, "(> 2.0 (variable 2.0 a))", 0, -1.0);
|
---|
233 | Evaluate(interpreter, ds, "(> (variable 2.0 a) 1.9)", 0, 1.0);
|
---|
234 | Evaluate(interpreter, ds, "(> 1.9 (variable 2.0 a))", 0, -1.0);
|
---|
235 | Evaluate(interpreter, ds, "(> (log -1.0) (log -1.0))", 0, -1.0); // (> nan nan) should be false
|
---|
236 |
|
---|
237 | // lt
|
---|
238 | Evaluate(interpreter, ds, "(< (variable 2.0 a) 2.0)", 0, -1.0);
|
---|
239 | Evaluate(interpreter, ds, "(< 2.0 (variable 2.0 a))", 0, -1.0);
|
---|
240 | Evaluate(interpreter, ds, "(< (variable 2.0 a) 1.9)", 0, -1.0);
|
---|
241 | Evaluate(interpreter, ds, "(< 1.9 (variable 2.0 a))", 0, 1.0);
|
---|
242 | Evaluate(interpreter, ds, "(< (log -1.0) (log -1.0))", 0, -1.0); // (< nan nan) should be false
|
---|
243 |
|
---|
244 | // If
|
---|
245 | Evaluate(interpreter, ds, "(if -10.0 2.0 3.0)", 0, 3.0);
|
---|
246 | Evaluate(interpreter, ds, "(if -1.0 2.0 3.0)", 0, 3.0);
|
---|
247 | Evaluate(interpreter, ds, "(if 0.0 2.0 3.0)", 0, 3.0);
|
---|
248 | Evaluate(interpreter, ds, "(if 1.0 2.0 3.0)", 0, 2.0);
|
---|
249 | Evaluate(interpreter, ds, "(if 10.0 2.0 3.0)", 0, 2.0);
|
---|
250 | Evaluate(interpreter, ds, "(if (log -1.0) 2.0 3.0)", 0, 3.0); // if(nan) should return the else branch
|
---|
251 |
|
---|
252 | // NOT
|
---|
253 | Evaluate(interpreter, ds, "(not -1.0)", 0, 1.0);
|
---|
254 | Evaluate(interpreter, ds, "(not -2.0)", 0, 1.0);
|
---|
255 | Evaluate(interpreter, ds, "(not 1.0)", 0, -1.0);
|
---|
256 | Evaluate(interpreter, ds, "(not 2.0)", 0, -1.0);
|
---|
257 | Evaluate(interpreter, ds, "(not 0.0)", 0, 1.0);
|
---|
258 | Evaluate(interpreter, ds, "(not (log -1.0))", 0, 1.0);
|
---|
259 |
|
---|
260 | // AND
|
---|
261 | Evaluate(interpreter, ds, "(and -1.0 -2.0)", 0, -1.0);
|
---|
262 | Evaluate(interpreter, ds, "(and -1.0 2.0)", 0, -1.0);
|
---|
263 | Evaluate(interpreter, ds, "(and 1.0 -2.0)", 0, -1.0);
|
---|
264 | Evaluate(interpreter, ds, "(and 1.0 0.0)", 0, -1.0);
|
---|
265 | Evaluate(interpreter, ds, "(and 0.0 0.0)", 0, -1.0);
|
---|
266 | Evaluate(interpreter, ds, "(and 1.0 2.0)", 0, 1.0);
|
---|
267 | Evaluate(interpreter, ds, "(and 1.0 2.0 3.0)", 0, 1.0);
|
---|
268 | Evaluate(interpreter, ds, "(and 1.0 -2.0 3.0)", 0, -1.0);
|
---|
269 | Evaluate(interpreter, ds, "(and (log -1.0))", 0, -1.0); // (and NaN)
|
---|
270 | Evaluate(interpreter, ds, "(and (log -1.0) 1.0)", 0, -1.0); // (and NaN 1.0)
|
---|
271 |
|
---|
272 |
|
---|
273 | // OR
|
---|
274 | Evaluate(interpreter, ds, "(or -1.0 -2.0)", 0, -1.0);
|
---|
275 | Evaluate(interpreter, ds, "(or -1.0 2.0)", 0, 1.0);
|
---|
276 | Evaluate(interpreter, ds, "(or 1.0 -2.0)", 0, 1.0);
|
---|
277 | Evaluate(interpreter, ds, "(or 1.0 2.0)", 0, 1.0);
|
---|
278 | Evaluate(interpreter, ds, "(or 0.0 0.0)", 0, -1.0);
|
---|
279 | Evaluate(interpreter, ds, "(or -1.0 -2.0 -3.0)", 0, -1.0);
|
---|
280 | Evaluate(interpreter, ds, "(or -1.0 -2.0 3.0)", 0, 1.0);
|
---|
281 | Evaluate(interpreter, ds, "(or (log -1.0))", 0, -1.0); // (or NaN)
|
---|
282 | Evaluate(interpreter, ds, "(or (log -1.0) 1.0)", 0, -1.0); // (or NaN 1.0)
|
---|
283 |
|
---|
284 | // sin, cos, tan
|
---|
285 | Evaluate(interpreter, ds, "(sin " + Math.PI.ToString(NumberFormatInfo.InvariantInfo) + ")", 0, 0.0);
|
---|
286 | Evaluate(interpreter, ds, "(sin 0.0)", 0, 0.0);
|
---|
287 | Evaluate(interpreter, ds, "(cos " + Math.PI.ToString(NumberFormatInfo.InvariantInfo) + ")", 0, -1.0);
|
---|
288 | Evaluate(interpreter, ds, "(cos 0.0)", 0, 1.0);
|
---|
289 | Evaluate(interpreter, ds, "(tan " + Math.PI.ToString(NumberFormatInfo.InvariantInfo) + ")", 0, Math.Tan(Math.PI));
|
---|
290 | Evaluate(interpreter, ds, "(tan 0.0)", 0, Math.Tan(Math.PI));
|
---|
291 |
|
---|
292 | // exp, log
|
---|
293 | Evaluate(interpreter, ds, "(log (exp 7.0))", 0, Math.Log(Math.Exp(7)));
|
---|
294 | Evaluate(interpreter, ds, "(exp (log 7.0))", 0, Math.Exp(Math.Log(7)));
|
---|
295 | Evaluate(interpreter, ds, "(log -3.0)", 0, Math.Log(-3));
|
---|
296 |
|
---|
297 | // power
|
---|
298 | Evaluate(interpreter, ds, "(pow 2.0 3.0)", 0, 8.0);
|
---|
299 | Evaluate(interpreter, ds, "(pow 4.0 0.5)", 0, 1.0); // interpreter should round to the nearest integer value value (.5 is rounded to the even number)
|
---|
300 | Evaluate(interpreter, ds, "(pow 4.0 2.5)", 0, 16.0); // interpreter should round to the nearest integer value value (.5 is rounded to the even number)
|
---|
301 | Evaluate(interpreter, ds, "(pow -2.0 3.0)", 0, -8.0);
|
---|
302 | Evaluate(interpreter, ds, "(pow 2.0 -3.0)", 0, 1.0 / 8.0);
|
---|
303 | Evaluate(interpreter, ds, "(pow -2.0 -3.0)", 0, -1.0 / 8.0);
|
---|
304 |
|
---|
305 | // root
|
---|
306 | Evaluate(interpreter, ds, "(root 9.0 2.0)", 0, 3.0);
|
---|
307 | Evaluate(interpreter, ds, "(root 27.0 3.0)", 0, 3.0);
|
---|
308 | Evaluate(interpreter, ds, "(root 2.0 -3.0)", 0, Math.Pow(2.0, -1.0 / 3.0));
|
---|
309 |
|
---|
310 | // mean
|
---|
311 | Evaluate(interpreter, ds, "(mean -1.0 1.0 -1.0)", 0, -1.0 / 3.0);
|
---|
312 |
|
---|
313 | // lag
|
---|
314 | Evaluate(interpreter, ds, "(lagVariable 1.0 a -1) ", 1, ds.GetDoubleValue("A", 0));
|
---|
315 | Evaluate(interpreter, ds, "(lagVariable 1.0 a -1) ", 2, ds.GetDoubleValue("A", 1));
|
---|
316 | Evaluate(interpreter, ds, "(lagVariable 1.0 a 0) ", 2, ds.GetDoubleValue("A", 2));
|
---|
317 | //Evaluate(interpreter, ds, "(lagVariable 1.0 a 1) ", 0, ds.GetDoubleValue("A", 1));
|
---|
318 |
|
---|
319 | // integral
|
---|
320 | Evaluate(interpreter, ds, "(integral -1.0 (variable 1.0 a)) ", 1, ds.GetDoubleValue("A", 0) + ds.GetDoubleValue("A", 1));
|
---|
321 | Evaluate(interpreter, ds, "(integral -1.0 (lagVariable 1.0 a -1)) ", 3, ds.GetDoubleValue("A", 1) + ds.GetDoubleValue("A", 2));
|
---|
322 | Evaluate(interpreter, ds, "(integral -2.0 (variable 1.0 a)) ", 2, ds.GetDoubleValue("A", 0) + ds.GetDoubleValue("A", 1) + ds.GetDoubleValue("A", 2));
|
---|
323 | Evaluate(interpreter, ds, "(integral -1.0 (* (variable 1.0 a) (variable 1.0 b)))", 1, ds.GetDoubleValue("A", 0) * ds.GetDoubleValue("B", 0) + ds.GetDoubleValue("A", 1) * ds.GetDoubleValue("B", 1));
|
---|
324 | Evaluate(interpreter, ds, "(integral -2.0 3.0)", 1, 9.0);
|
---|
325 |
|
---|
326 | // derivative
|
---|
327 | // (f_0 + 2 * f_1 - 2 * f_3 - f_4) / 8; // h = 1
|
---|
328 | Evaluate(interpreter, ds, "(diff (variable 1.0 a)) ", 5, (ds.GetDoubleValue("A", 5) + 2 * ds.GetDoubleValue("A", 4) - 2 * ds.GetDoubleValue("A", 2) - ds.GetDoubleValue("A", 1)) / 8.0);
|
---|
329 | Evaluate(interpreter, ds, "(diff (variable 1.0 b)) ", 5, (ds.GetDoubleValue("B", 5) + 2 * ds.GetDoubleValue("B", 4) - 2 * ds.GetDoubleValue("B", 2) - ds.GetDoubleValue("B", 1)) / 8.0);
|
---|
330 | Evaluate(interpreter, ds, "(diff (* (variable 1.0 a) (variable 1.0 b)))", 5, +
|
---|
331 | (ds.GetDoubleValue("A", 5) * ds.GetDoubleValue("B", 5) +
|
---|
332 | 2 * ds.GetDoubleValue("A", 4) * ds.GetDoubleValue("B", 4) -
|
---|
333 | 2 * ds.GetDoubleValue("A", 2) * ds.GetDoubleValue("B", 2) -
|
---|
334 | ds.GetDoubleValue("A", 1) * ds.GetDoubleValue("B", 1)) / 8.0);
|
---|
335 | Evaluate(interpreter, ds, "(diff -2.0 3.0)", 5, 0.0);
|
---|
336 |
|
---|
337 | // timelag
|
---|
338 | Evaluate(interpreter, ds, "(lag -1.0 (lagVariable 1.0 a -2)) ", 5, ds.GetDoubleValue("A", 2));
|
---|
339 | Evaluate(interpreter, ds, "(lag -2.0 (lagVariable 1.0 a -2)) ", 5, ds.GetDoubleValue("A", 1));
|
---|
340 | Evaluate(interpreter, ds, "(lag -1.0 (* (lagVariable 1.0 a -1) (lagVariable 1.0 b -2)))", 3, ds.GetDoubleValue("A", 1) * ds.GetDoubleValue("B", 0));
|
---|
341 | Evaluate(interpreter, ds, "(lag -2.0 3.0)", 1, 3.0);
|
---|
342 |
|
---|
343 | // prognosis
|
---|
344 | Evaluate(interpreter, ds, "(lagVariable 1.0 a -1)", new string[] { "A" }, Enumerable.Range(1, 1), 3, new[] { new double[] { 1.0, 1.0, 1.0 } });
|
---|
345 | Evaluate(interpreter, ds, "(lagVariable 2.0 a -1)", new string[] { "A" }, Enumerable.Range(1, 1), 3, new[] { new double[] { 2.0, 4.0, 8.0 } });
|
---|
346 | Evaluate(interpreter, ds, "(lagVariable 1.0 a -2)", new string[] { "A" }, Enumerable.Range(2, 1), 4, new[] { new double[] { 1.0, 2.0, 1.0, 2.0 } });
|
---|
347 |
|
---|
348 | // multi-variate
|
---|
349 | Evaluate(interpreter, ds, @"(PROG
|
---|
350 | (MAIN (lagVariable 1.0 a -1) (lagVariable 1.0 b -1)))", new string[] { "A", "B" }, Enumerable.Range(1, 2), 3,
|
---|
351 | new[] { new[] { 1.0, 1.0},
|
---|
352 | new[] { 1.0, 1.0},
|
---|
353 | new[] { 1.0, 1.0}},
|
---|
354 | new[] { new[] { 2.0, 2.0},
|
---|
355 | new[] { 2.0, 2.0},
|
---|
356 | new[] { 2.0, 2.0} }
|
---|
357 | );
|
---|
358 |
|
---|
359 | //1.0, 1.0
|
---|
360 | //2.0, 2.0
|
---|
361 | //1.0, 2.0
|
---|
362 | //1.0, 1.0
|
---|
363 | //2.0, 2.0
|
---|
364 | //1.0, 2.0
|
---|
365 | //1.0, 1.0
|
---|
366 | //2.0, 2.0
|
---|
367 | //1.0, 2.0
|
---|
368 | Evaluate(interpreter, ds, @"(PROG
|
---|
369 | (MAIN (lagVariable 2.0 a -2) (lagVariable 1.0 a -1)))", new string[] { "A", "B" }, Enumerable.Range(2, 3), 4,
|
---|
370 | new[] { new[] { 2.0, 2.0},
|
---|
371 | new[] { 4.0, 2.0},
|
---|
372 | new[] { 4.0, 4.0},
|
---|
373 | new[] { 8.0, 4.0}},
|
---|
374 | new[] { new[] { 4.0, 1.0},
|
---|
375 | new[] { 2.0, 4.0},
|
---|
376 | new[] { 8.0, 2.0},
|
---|
377 | new[] { 4.0, 8.0}},
|
---|
378 | new[] { new[] { 2.0, 1.0},
|
---|
379 | new[] { 2.0, 2.0},
|
---|
380 | new[] { 4.0, 2.0},
|
---|
381 | new[] { 4.0, 4.0}}
|
---|
382 | );
|
---|
383 | }
|
---|
384 |
|
---|
385 | private void Evaluate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, Dataset ds, string expr, int index, double expected) {
|
---|
386 | var importer = new SymbolicExpressionImporter();
|
---|
387 | ISymbolicExpressionTree tree = importer.Import(expr);
|
---|
388 |
|
---|
389 | double actual = interpreter.GetSymbolicExpressionTreeValues(tree, ds, Enumerable.Range(index, 1)).First();
|
---|
390 |
|
---|
391 | Assert.IsFalse(double.IsNaN(actual) && !double.IsNaN(expected));
|
---|
392 | Assert.IsFalse(!double.IsNaN(actual) && double.IsNaN(expected));
|
---|
393 | Assert.AreEqual(expected, actual, 1.0E-12, expr);
|
---|
394 | }
|
---|
395 | private void Evaluate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, Dataset ds, string expr, string[] targetVariables, IEnumerable<int> rows, int horizon, params IEnumerable<IEnumerable<double>>[] expected) {
|
---|
396 | var importer = new SymbolicExpressionImporter();
|
---|
397 | ISymbolicExpressionTree tree = importer.Import(expr);
|
---|
398 |
|
---|
399 | var allPrognosis = interpreter.GetSymbolicExpressionTreeValues(tree, ds, targetVariables, rows, horizon).GetEnumerator();
|
---|
400 | int i = 0;
|
---|
401 | var expectedEnumerator = expected[i].GetEnumerator();
|
---|
402 | while (allPrognosis.MoveNext()) {
|
---|
403 | while (expectedEnumerator.MoveNext()) {
|
---|
404 | var eEnumerator = expectedEnumerator.Current.GetEnumerator();
|
---|
405 | while (eEnumerator.MoveNext()) {
|
---|
406 | Assert.IsFalse(double.IsNaN(allPrognosis.Current) && !double.IsNaN(eEnumerator.Current));
|
---|
407 | Assert.IsFalse(!double.IsNaN(allPrognosis.Current) && double.IsNaN(eEnumerator.Current));
|
---|
408 | Assert.AreEqual(eEnumerator.Current, allPrognosis.Current, 1.0E-12, expr);
|
---|
409 | allPrognosis.MoveNext();
|
---|
410 | }
|
---|
411 | }
|
---|
412 | i++;
|
---|
413 | }
|
---|
414 | }
|
---|
415 | }
|
---|
416 | }
|
---|