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.SymbolicExpressionTreeEncoding;
|
---|
27 | using HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis;
|
---|
28 | using HeuristicLab.Problems.DataAnalysis.Symbolic_34.Tests;
|
---|
29 | using HeuristicLab.Random;
|
---|
30 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
31 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis_34.Tests {
|
---|
32 |
|
---|
33 | [TestClass()]
|
---|
34 | public class SymbolicTimeSeriesPrognosisInterpreterTest {
|
---|
35 | private const int N = 1000;
|
---|
36 | private const int Rows = 100;
|
---|
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 SymbolicTimeSeriesPrognosisTreeInterpreterTypeCoherentGrammarPerformanceTest() {
|
---|
55 | TypeCoherentGrammarPerformanceTest(new SymbolicTimeSeriesPrognosisExpressionTreeInterpreter("y"), 12.5e6);
|
---|
56 | }
|
---|
57 | [TestMethod]
|
---|
58 | public void SymbolicTimeSeriesPrognosisTreeInterpreterFullGrammarPerformanceTest() {
|
---|
59 | FullGrammarPerformanceTest(new SymbolicTimeSeriesPrognosisExpressionTreeInterpreter("y"), 12.5e6);
|
---|
60 | }
|
---|
61 | [TestMethod]
|
---|
62 | public void SymbolicTimeSeriesPrognosisTreeInterpreterArithmeticGrammarPerformanceTest() {
|
---|
63 | ArithmeticGrammarPerformanceTest(new SymbolicTimeSeriesPrognosisExpressionTreeInterpreter("y"), 12.5e6);
|
---|
64 | }
|
---|
65 |
|
---|
66 | private void TypeCoherentGrammarPerformanceTest(ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter interpreter, double nodesPerSecThreshold) {
|
---|
67 | var twister = new MersenneTwister(31415);
|
---|
68 | var dataset = Util.CreateRandomDataset(twister, Rows, Columns);
|
---|
69 | var grammar = new TypeCoherentExpressionGrammar();
|
---|
70 | grammar.ConfigureAsDefaultRegressionGrammar();
|
---|
71 | grammar.MaximumFunctionArguments = 0;
|
---|
72 | grammar.MaximumFunctionDefinitions = 0;
|
---|
73 | grammar.MinimumFunctionArguments = 0;
|
---|
74 | grammar.MinimumFunctionDefinitions = 0;
|
---|
75 | var randomTrees = Util.CreateRandomTrees(twister, dataset, grammar, N, 1, 100, 0, 0);
|
---|
76 | foreach (ISymbolicExpressionTree tree in randomTrees) {
|
---|
77 | Util.InitTree(tree, twister, new List<string>(dataset.VariableNames));
|
---|
78 | }
|
---|
79 | double nodesPerSec = Util.CalculateEvaluatedNodesPerSec(randomTrees, interpreter, dataset, 3);
|
---|
80 | Assert.IsTrue(nodesPerSec > nodesPerSecThreshold); // evaluated nodes per seconds must be larger than 15mNodes/sec
|
---|
81 | }
|
---|
82 |
|
---|
83 | private void FullGrammarPerformanceTest(ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter interpreter, double nodesPerSecThreshold) {
|
---|
84 | var twister = new MersenneTwister(31415);
|
---|
85 | var dataset = Util.CreateRandomDataset(twister, Rows, Columns);
|
---|
86 | var grammar = new FullFunctionalExpressionGrammar();
|
---|
87 | grammar.MaximumFunctionArguments = 0;
|
---|
88 | grammar.MaximumFunctionDefinitions = 0;
|
---|
89 | grammar.MinimumFunctionArguments = 0;
|
---|
90 | grammar.MinimumFunctionDefinitions = 0;
|
---|
91 | var randomTrees = Util.CreateRandomTrees(twister, dataset, grammar, N, 1, 100, 0, 0);
|
---|
92 | foreach (ISymbolicExpressionTree tree in randomTrees) {
|
---|
93 | Util.InitTree(tree, twister, new List<string>(dataset.VariableNames));
|
---|
94 | }
|
---|
95 | double nodesPerSec = Util.CalculateEvaluatedNodesPerSec(randomTrees, interpreter, dataset, 3);
|
---|
96 | Assert.IsTrue(nodesPerSec > nodesPerSecThreshold); // evaluated nodes per seconds must be larger than 15mNodes/sec
|
---|
97 | }
|
---|
98 |
|
---|
99 | private void ArithmeticGrammarPerformanceTest(ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter interpreter, double nodesPerSecThreshold) {
|
---|
100 | var twister = new MersenneTwister(31415);
|
---|
101 | var dataset = Util.CreateRandomDataset(twister, Rows, Columns);
|
---|
102 | var grammar = new ArithmeticExpressionGrammar();
|
---|
103 | grammar.MaximumFunctionArguments = 0;
|
---|
104 | grammar.MaximumFunctionDefinitions = 0;
|
---|
105 | grammar.MinimumFunctionArguments = 0;
|
---|
106 | grammar.MinimumFunctionDefinitions = 0;
|
---|
107 | var randomTrees = Util.CreateRandomTrees(twister, dataset, grammar, N, 1, 100, 0, 0);
|
---|
108 | foreach (SymbolicExpressionTree tree in randomTrees) {
|
---|
109 | Util.InitTree(tree, twister, new List<string>(dataset.VariableNames));
|
---|
110 | }
|
---|
111 |
|
---|
112 | double nodesPerSec = Util.CalculateEvaluatedNodesPerSec(randomTrees, interpreter, dataset, 3);
|
---|
113 | Assert.IsTrue(nodesPerSec > nodesPerSecThreshold); // evaluated nodes per seconds must be larger than 15mNodes/sec
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | /// <summary>
|
---|
118 | ///A test for Evaluate
|
---|
119 | ///</summary>
|
---|
120 | [TestMethod]
|
---|
121 | public void SymbolicDataAnalysisExpressionTreeInterpreterEvaluateTest() {
|
---|
122 | Dataset ds = new Dataset(new string[] { "Y", "A", "B" }, new double[,] {
|
---|
123 | { 1.0, 1.0, 1.0 },
|
---|
124 | { 2.0, 2.0, 2.0 },
|
---|
125 | { 3.0, 1.0, 2.0 },
|
---|
126 | { 4.0, 1.0, 1.0 },
|
---|
127 | { 5.0, 2.0, 2.0 },
|
---|
128 | { 6.0, 1.0, 2.0 },
|
---|
129 | { 7.0, 1.0, 1.0 },
|
---|
130 | { 8.0, 2.0, 2.0 },
|
---|
131 | { 9.0, 1.0, 2.0 },
|
---|
132 | { 10.0, 1.0, 1.0 },
|
---|
133 | { 11.0, 2.0, 2.0 },
|
---|
134 | { 12.0, 1.0, 2.0 }
|
---|
135 | });
|
---|
136 |
|
---|
137 | var interpreter = new SymbolicDataAnalysisExpressionTreeInterpreter();
|
---|
138 | EvaluateTerminals(interpreter, ds);
|
---|
139 | EvaluateOperations(interpreter, ds);
|
---|
140 | EvaluateAdf(interpreter, ds);
|
---|
141 | }
|
---|
142 |
|
---|
143 | //[TestMethod]
|
---|
144 | //public void SymbolicDataAnalysisExpressionILEmittingTreeInterpreterEvaluateTest() {
|
---|
145 | // Dataset ds = new Dataset(new string[] { "Y", "A", "B" }, new double[,] {
|
---|
146 | // { 1.0, 1.0, 1.0 },
|
---|
147 | // { 2.0, 2.0, 2.0 },
|
---|
148 | // { 3.0, 1.0, 2.0 },
|
---|
149 | // { 4.0, 1.0, 1.0 },
|
---|
150 | // { 5.0, 2.0, 2.0 },
|
---|
151 | // { 6.0, 1.0, 2.0 },
|
---|
152 | // { 7.0, 1.0, 1.0 },
|
---|
153 | // { 8.0, 2.0, 2.0 },
|
---|
154 | // { 9.0, 1.0, 2.0 },
|
---|
155 | // { 10.0, 1.0, 1.0 },
|
---|
156 | // { 11.0, 2.0, 2.0 },
|
---|
157 | // { 12.0, 1.0, 2.0 }
|
---|
158 | // });
|
---|
159 |
|
---|
160 | // var interpreter = new SymbolicDataAnalysisExpressionTreeILEmittingInterpreter();
|
---|
161 | // EvaluateTerminals(interpreter, ds);
|
---|
162 | // EvaluateOperations(interpreter, ds);
|
---|
163 | //}
|
---|
164 |
|
---|
165 | private void EvaluateTerminals(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, Dataset ds) {
|
---|
166 | // constants
|
---|
167 | Evaluate(interpreter, ds, "(+ 1.5 3.5)", 0, 5.0);
|
---|
168 |
|
---|
169 | // variables
|
---|
170 | Evaluate(interpreter, ds, "(variable 2.0 a)", 0, 2.0);
|
---|
171 | Evaluate(interpreter, ds, "(variable 2.0 a)", 1, 4.0);
|
---|
172 | }
|
---|
173 |
|
---|
174 | private void EvaluateAdf(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, Dataset ds) {
|
---|
175 |
|
---|
176 | // ADF
|
---|
177 | Evaluate(interpreter, ds, @"(PROG
|
---|
178 | (MAIN
|
---|
179 | (CALL ADF0))
|
---|
180 | (defun ADF0 1.0))", 1, 1.0);
|
---|
181 | Evaluate(interpreter, ds, @"(PROG
|
---|
182 | (MAIN
|
---|
183 | (* (CALL ADF0) (CALL ADF0)))
|
---|
184 | (defun ADF0 2.0))", 1, 4.0);
|
---|
185 | Evaluate(interpreter, ds, @"(PROG
|
---|
186 | (MAIN
|
---|
187 | (CALL ADF0 2.0 3.0))
|
---|
188 | (defun ADF0
|
---|
189 | (+ (ARG 0) (ARG 1))))", 1, 5.0);
|
---|
190 | Evaluate(interpreter, ds, @"(PROG
|
---|
191 | (MAIN (CALL ADF1 2.0 3.0))
|
---|
192 | (defun ADF0
|
---|
193 | (- (ARG 1) (ARG 0)))
|
---|
194 | (defun ADF1
|
---|
195 | (+ (CALL ADF0 (ARG 1) (ARG 0))
|
---|
196 | (CALL ADF0 (ARG 0) (ARG 1)))))", 1, 0.0);
|
---|
197 | Evaluate(interpreter, ds, @"(PROG
|
---|
198 | (MAIN (CALL ADF1 (variable 2.0 a) 3.0))
|
---|
199 | (defun ADF0
|
---|
200 | (- (ARG 1) (ARG 0)))
|
---|
201 | (defun ADF1
|
---|
202 | (CALL ADF0 (ARG 1) (ARG 0))))", 1, 1.0);
|
---|
203 | Evaluate(interpreter, ds,
|
---|
204 | @"(PROG
|
---|
205 | (MAIN (CALL ADF1 (variable 2.0 a) 3.0))
|
---|
206 | (defun ADF0
|
---|
207 | (- (ARG 1) (ARG 0)))
|
---|
208 | (defun ADF1
|
---|
209 | (+ (CALL ADF0 (ARG 1) (ARG 0))
|
---|
210 | (CALL ADF0 (ARG 0) (ARG 1)))))", 1, 0.0);
|
---|
211 | }
|
---|
212 |
|
---|
213 | private void EvaluateOperations(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, Dataset ds) {
|
---|
214 | // addition
|
---|
215 | Evaluate(interpreter, ds, "(+ (variable 2.0 a ))", 1, 4.0);
|
---|
216 | Evaluate(interpreter, ds, "(+ (variable 2.0 a ) (variable 3.0 b ))", 0, 5.0);
|
---|
217 | Evaluate(interpreter, ds, "(+ (variable 2.0 a ) (variable 3.0 b ))", 1, 10.0);
|
---|
218 | Evaluate(interpreter, ds, "(+ (variable 2.0 a) (variable 3.0 b ))", 2, 8.0);
|
---|
219 | Evaluate(interpreter, ds, "(+ 8.0 2.0 2.0)", 0, 12.0);
|
---|
220 |
|
---|
221 | // subtraction
|
---|
222 | Evaluate(interpreter, ds, "(- (variable 2.0 a ))", 1, -4.0);
|
---|
223 | Evaluate(interpreter, ds, "(- (variable 2.0 a ) (variable 3.0 b))", 0, -1.0);
|
---|
224 | Evaluate(interpreter, ds, "(- (variable 2.0 a ) (variable 3.0 b ))", 1, -2.0);
|
---|
225 | Evaluate(interpreter, ds, "(- (variable 2.0 a ) (variable 3.0 b ))", 2, -4.0);
|
---|
226 | Evaluate(interpreter, ds, "(- 8.0 2.0 2.0)", 0, 4.0);
|
---|
227 |
|
---|
228 | // multiplication
|
---|
229 | Evaluate(interpreter, ds, "(* (variable 2.0 a ))", 0, 2.0);
|
---|
230 | Evaluate(interpreter, ds, "(* (variable 2.0 a ) (variable 3.0 b ))", 0, 6.0);
|
---|
231 | Evaluate(interpreter, ds, "(* (variable 2.0 a ) (variable 3.0 b ))", 1, 24.0);
|
---|
232 | Evaluate(interpreter, ds, "(* (variable 2.0 a ) (variable 3.0 b ))", 2, 12.0);
|
---|
233 | Evaluate(interpreter, ds, "(* 8.0 2.0 2.0)", 0, 32.0);
|
---|
234 |
|
---|
235 | // division
|
---|
236 | Evaluate(interpreter, ds, "(/ (variable 2.0 a ))", 1, 1.0 / 4.0);
|
---|
237 | Evaluate(interpreter, ds, "(/ (variable 2.0 a ) 2.0)", 0, 1.0);
|
---|
238 | Evaluate(interpreter, ds, "(/ (variable 2.0 a ) 2.0)", 1, 2.0);
|
---|
239 | Evaluate(interpreter, ds, "(/ (variable 3.0 b ) 2.0)", 2, 3.0);
|
---|
240 | Evaluate(interpreter, ds, "(/ 8.0 2.0 2.0)", 0, 2.0);
|
---|
241 |
|
---|
242 | // gt
|
---|
243 | Evaluate(interpreter, ds, "(> (variable 2.0 a) 2.0)", 0, -1.0);
|
---|
244 | Evaluate(interpreter, ds, "(> 2.0 (variable 2.0 a))", 0, -1.0);
|
---|
245 | Evaluate(interpreter, ds, "(> (variable 2.0 a) 1.9)", 0, 1.0);
|
---|
246 | Evaluate(interpreter, ds, "(> 1.9 (variable 2.0 a))", 0, -1.0);
|
---|
247 | Evaluate(interpreter, ds, "(> (log -1.0) (log -1.0))", 0, -1.0); // (> nan nan) should be false
|
---|
248 |
|
---|
249 | // lt
|
---|
250 | Evaluate(interpreter, ds, "(< (variable 2.0 a) 2.0)", 0, -1.0);
|
---|
251 | Evaluate(interpreter, ds, "(< 2.0 (variable 2.0 a))", 0, -1.0);
|
---|
252 | Evaluate(interpreter, ds, "(< (variable 2.0 a) 1.9)", 0, -1.0);
|
---|
253 | Evaluate(interpreter, ds, "(< 1.9 (variable 2.0 a))", 0, 1.0);
|
---|
254 | Evaluate(interpreter, ds, "(< (log -1.0) (log -1.0))", 0, -1.0); // (< nan nan) should be false
|
---|
255 |
|
---|
256 | // If
|
---|
257 | Evaluate(interpreter, ds, "(if -10.0 2.0 3.0)", 0, 3.0);
|
---|
258 | Evaluate(interpreter, ds, "(if -1.0 2.0 3.0)", 0, 3.0);
|
---|
259 | Evaluate(interpreter, ds, "(if 0.0 2.0 3.0)", 0, 3.0);
|
---|
260 | Evaluate(interpreter, ds, "(if 1.0 2.0 3.0)", 0, 2.0);
|
---|
261 | Evaluate(interpreter, ds, "(if 10.0 2.0 3.0)", 0, 2.0);
|
---|
262 | Evaluate(interpreter, ds, "(if (log -1.0) 2.0 3.0)", 0, 3.0); // if(nan) should return the else branch
|
---|
263 |
|
---|
264 | // NOT
|
---|
265 | Evaluate(interpreter, ds, "(not -1.0)", 0, 1.0);
|
---|
266 | Evaluate(interpreter, ds, "(not -2.0)", 0, 1.0);
|
---|
267 | Evaluate(interpreter, ds, "(not 1.0)", 0, -1.0);
|
---|
268 | Evaluate(interpreter, ds, "(not 2.0)", 0, -1.0);
|
---|
269 | Evaluate(interpreter, ds, "(not 0.0)", 0, 1.0);
|
---|
270 | Evaluate(interpreter, ds, "(not (log -1.0))", 0, 1.0);
|
---|
271 |
|
---|
272 | // AND
|
---|
273 | Evaluate(interpreter, ds, "(and -1.0 -2.0)", 0, -1.0);
|
---|
274 | Evaluate(interpreter, ds, "(and -1.0 2.0)", 0, -1.0);
|
---|
275 | Evaluate(interpreter, ds, "(and 1.0 -2.0)", 0, -1.0);
|
---|
276 | Evaluate(interpreter, ds, "(and 1.0 0.0)", 0, -1.0);
|
---|
277 | Evaluate(interpreter, ds, "(and 0.0 0.0)", 0, -1.0);
|
---|
278 | Evaluate(interpreter, ds, "(and 1.0 2.0)", 0, 1.0);
|
---|
279 | Evaluate(interpreter, ds, "(and 1.0 2.0 3.0)", 0, 1.0);
|
---|
280 | Evaluate(interpreter, ds, "(and 1.0 -2.0 3.0)", 0, -1.0);
|
---|
281 | Evaluate(interpreter, ds, "(and (log -1.0))", 0, -1.0); // (and NaN)
|
---|
282 | Evaluate(interpreter, ds, "(and (log -1.0) 1.0)", 0, -1.0); // (and NaN 1.0)
|
---|
283 |
|
---|
284 |
|
---|
285 | // OR
|
---|
286 | Evaluate(interpreter, ds, "(or -1.0 -2.0)", 0, -1.0);
|
---|
287 | Evaluate(interpreter, ds, "(or -1.0 2.0)", 0, 1.0);
|
---|
288 | Evaluate(interpreter, ds, "(or 1.0 -2.0)", 0, 1.0);
|
---|
289 | Evaluate(interpreter, ds, "(or 1.0 2.0)", 0, 1.0);
|
---|
290 | Evaluate(interpreter, ds, "(or 0.0 0.0)", 0, -1.0);
|
---|
291 | Evaluate(interpreter, ds, "(or -1.0 -2.0 -3.0)", 0, -1.0);
|
---|
292 | Evaluate(interpreter, ds, "(or -1.0 -2.0 3.0)", 0, 1.0);
|
---|
293 | Evaluate(interpreter, ds, "(or (log -1.0))", 0, -1.0); // (or NaN)
|
---|
294 | Evaluate(interpreter, ds, "(or (log -1.0) 1.0)", 0, -1.0); // (or NaN 1.0)
|
---|
295 |
|
---|
296 | // sin, cos, tan
|
---|
297 | Evaluate(interpreter, ds, "(sin " + Math.PI.ToString(NumberFormatInfo.InvariantInfo) + ")", 0, 0.0);
|
---|
298 | Evaluate(interpreter, ds, "(sin 0.0)", 0, 0.0);
|
---|
299 | Evaluate(interpreter, ds, "(cos " + Math.PI.ToString(NumberFormatInfo.InvariantInfo) + ")", 0, -1.0);
|
---|
300 | Evaluate(interpreter, ds, "(cos 0.0)", 0, 1.0);
|
---|
301 | Evaluate(interpreter, ds, "(tan " + Math.PI.ToString(NumberFormatInfo.InvariantInfo) + ")", 0, Math.Tan(Math.PI));
|
---|
302 | Evaluate(interpreter, ds, "(tan 0.0)", 0, Math.Tan(Math.PI));
|
---|
303 |
|
---|
304 | // exp, log
|
---|
305 | Evaluate(interpreter, ds, "(log (exp 7.0))", 0, Math.Log(Math.Exp(7)));
|
---|
306 | Evaluate(interpreter, ds, "(exp (log 7.0))", 0, Math.Exp(Math.Log(7)));
|
---|
307 | Evaluate(interpreter, ds, "(log -3.0)", 0, Math.Log(-3));
|
---|
308 |
|
---|
309 | // power
|
---|
310 | Evaluate(interpreter, ds, "(pow 2.0 3.0)", 0, 8.0);
|
---|
311 | 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)
|
---|
312 | 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)
|
---|
313 | Evaluate(interpreter, ds, "(pow -2.0 3.0)", 0, -8.0);
|
---|
314 | Evaluate(interpreter, ds, "(pow 2.0 -3.0)", 0, 1.0 / 8.0);
|
---|
315 | Evaluate(interpreter, ds, "(pow -2.0 -3.0)", 0, -1.0 / 8.0);
|
---|
316 |
|
---|
317 | // root
|
---|
318 | Evaluate(interpreter, ds, "(root 9.0 2.0)", 0, 3.0);
|
---|
319 | Evaluate(interpreter, ds, "(root 27.0 3.0)", 0, 3.0);
|
---|
320 | Evaluate(interpreter, ds, "(root 2.0 -3.0)", 0, Math.Pow(2.0, -1.0 / 3.0));
|
---|
321 |
|
---|
322 | // mean
|
---|
323 | Evaluate(interpreter, ds, "(mean -1.0 1.0 -1.0)", 0, -1.0 / 3.0);
|
---|
324 |
|
---|
325 | // lag
|
---|
326 | Evaluate(interpreter, ds, "(lagVariable 1.0 a -1) ", 1, ds.GetDoubleValue("A", 0));
|
---|
327 | Evaluate(interpreter, ds, "(lagVariable 1.0 a -1) ", 2, ds.GetDoubleValue("A", 1));
|
---|
328 | Evaluate(interpreter, ds, "(lagVariable 1.0 a 0) ", 2, ds.GetDoubleValue("A", 2));
|
---|
329 | Evaluate(interpreter, ds, "(lagVariable 1.0 a 1) ", 0, ds.GetDoubleValue("A", 1));
|
---|
330 |
|
---|
331 | // integral
|
---|
332 | Evaluate(interpreter, ds, "(integral -1.0 (variable 1.0 a)) ", 1, ds.GetDoubleValue("A", 0) + ds.GetDoubleValue("A", 1));
|
---|
333 | Evaluate(interpreter, ds, "(integral -1.0 (lagVariable 1.0 a 1)) ", 1, ds.GetDoubleValue("A", 1) + ds.GetDoubleValue("A", 2));
|
---|
334 | Evaluate(interpreter, ds, "(integral -2.0 (variable 1.0 a)) ", 2, ds.GetDoubleValue("A", 0) + ds.GetDoubleValue("A", 1) + ds.GetDoubleValue("A", 2));
|
---|
335 | 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));
|
---|
336 | Evaluate(interpreter, ds, "(integral -2.0 3.0)", 1, 9.0);
|
---|
337 |
|
---|
338 | // derivative
|
---|
339 | // (f_0 + 2 * f_1 - 2 * f_3 - f_4) / 8; // h = 1
|
---|
340 | 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);
|
---|
341 | 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);
|
---|
342 | Evaluate(interpreter, ds, "(diff (* (variable 1.0 a) (variable 1.0 b)))", 5, +
|
---|
343 | (ds.GetDoubleValue("A", 5) * ds.GetDoubleValue("B", 5) +
|
---|
344 | 2 * ds.GetDoubleValue("A", 4) * ds.GetDoubleValue("B", 4) -
|
---|
345 | 2 * ds.GetDoubleValue("A", 2) * ds.GetDoubleValue("B", 2) -
|
---|
346 | ds.GetDoubleValue("A", 1) * ds.GetDoubleValue("B", 1)) / 8.0);
|
---|
347 | Evaluate(interpreter, ds, "(diff -2.0 3.0)", 5, 0.0);
|
---|
348 |
|
---|
349 | // timelag
|
---|
350 | Evaluate(interpreter, ds, "(lag -1.0 (lagVariable 1.0 a 2)) ", 1, ds.GetDoubleValue("A", 2));
|
---|
351 | Evaluate(interpreter, ds, "(lag -2.0 (lagVariable 1.0 a 2)) ", 2, ds.GetDoubleValue("A", 2));
|
---|
352 | Evaluate(interpreter, ds, "(lag -1.0 (* (lagVariable 1.0 a 1) (lagVariable 1.0 b 2)))", 1, ds.GetDoubleValue("A", 1) * ds.GetDoubleValue("B", 2));
|
---|
353 | Evaluate(interpreter, ds, "(lag -2.0 3.0)", 1, 3.0);
|
---|
354 | }
|
---|
355 |
|
---|
356 | private void Evaluate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, Dataset ds, string expr, int index, double expected) {
|
---|
357 | var importer = new SymbolicExpressionImporter();
|
---|
358 | ISymbolicExpressionTree tree = importer.Import(expr);
|
---|
359 |
|
---|
360 | double actual = interpreter.GetSymbolicExpressionTreeValues(tree, ds, Enumerable.Range(index, 1)).First();
|
---|
361 |
|
---|
362 | Assert.IsFalse(double.IsNaN(actual) && !double.IsNaN(expected));
|
---|
363 | Assert.IsFalse(!double.IsNaN(actual) && double.IsNaN(expected));
|
---|
364 | Assert.AreEqual(expected, actual, 1.0E-12, expr);
|
---|
365 | }
|
---|
366 | }
|
---|
367 | }
|
---|