[5574] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9456] | 3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5574] | 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.Random;
|
---|
| 28 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
[9885] | 29 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Tests {
|
---|
[5574] | 30 |
|
---|
| 31 |
|
---|
| 32 | [TestClass()]
|
---|
| 33 | public class SymbolicDataAnalysisExpressionTreeInterpreterTest {
|
---|
| 34 | private const int N = 1000;
|
---|
| 35 | private const int Rows = 1000;
|
---|
| 36 | private const int Columns = 50;
|
---|
| 37 | private TestContext testContextInstance;
|
---|
| 38 |
|
---|
| 39 | /// <summary>
|
---|
| 40 | ///Gets or sets the test context which provides
|
---|
| 41 | ///information about and functionality for the current test run.
|
---|
| 42 | ///</summary>
|
---|
| 43 | public TestContext TestContext {
|
---|
| 44 | get {
|
---|
| 45 | return testContextInstance;
|
---|
| 46 | }
|
---|
| 47 | set {
|
---|
| 48 | testContextInstance = value;
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | [TestMethod]
|
---|
[9885] | 53 | [TestCategory("Problems.DataAnalysis")]
|
---|
| 54 | [TestProperty("Time", "long")]
|
---|
[7926] | 55 | public void SymbolicDataAnalysisExpressionTreeInterpreterTypeCoherentGrammarPerformanceTest() {
|
---|
| 56 | TypeCoherentGrammarPerformanceTest(new SymbolicDataAnalysisExpressionTreeInterpreter(), 12.5e6);
|
---|
| 57 | }
|
---|
[9885] | 58 |
|
---|
[7926] | 59 | [TestMethod]
|
---|
[9885] | 60 | [TestCategory("Problems.DataAnalysis")]
|
---|
| 61 | [TestProperty("Time", "long")]
|
---|
[6732] | 62 | public void SymbolicDataAnalysisExpressionTreeInterpreterFullGrammarPerformanceTest() {
|
---|
| 63 | FullGrammarPerformanceTest(new SymbolicDataAnalysisExpressionTreeInterpreter(), 12.5e6);
|
---|
| 64 | }
|
---|
[9885] | 65 |
|
---|
[6732] | 66 | [TestMethod]
|
---|
[9885] | 67 | [TestCategory("Problems.DataAnalysis")]
|
---|
| 68 | [TestProperty("Time", "long")]
|
---|
[6732] | 69 | public void SymbolicDataAnalysisExpressionTreeInterpreterArithmeticGrammarPerformanceTest() {
|
---|
[7707] | 70 | ArithmeticGrammarPerformanceTest(new SymbolicDataAnalysisExpressionTreeInterpreter(), 12.5e6);
|
---|
[6732] | 71 | }
|
---|
| 72 |
|
---|
| 73 | [TestMethod]
|
---|
[9885] | 74 | [TestCategory("Problems.DataAnalysis")]
|
---|
| 75 | [TestProperty("Time", "long")]
|
---|
[7926] | 76 | public void SymbolicDataAnalysisExpressionTreeILEmittingInterpreterTypeCoherentGrammarPerformanceTest() {
|
---|
| 77 | TypeCoherentGrammarPerformanceTest(new SymbolicDataAnalysisExpressionTreeILEmittingInterpreter(), 7.5e6);
|
---|
| 78 | }
|
---|
[9885] | 79 |
|
---|
[7926] | 80 | [TestMethod]
|
---|
[9885] | 81 | [TestCategory("Problems.DataAnalysis")]
|
---|
| 82 | [TestProperty("Time", "long")]
|
---|
[6732] | 83 | public void SymbolicDataAnalysisExpressionTreeILEmittingInterpreterFullGrammarPerformanceTest() {
|
---|
[6865] | 84 | FullGrammarPerformanceTest(new SymbolicDataAnalysisExpressionTreeILEmittingInterpreter(), 7.5e6);
|
---|
[6732] | 85 | }
|
---|
[9885] | 86 |
|
---|
| 87 | [TestCategory("Problems.DataAnalysis")]
|
---|
| 88 | [TestProperty("Time", "long")]
|
---|
[6732] | 89 | [TestMethod]
|
---|
| 90 | public void SymbolicDataAnalysisExpressionTreeILEmittingInterpreterArithmeticGrammarPerformanceTest() {
|
---|
[7707] | 91 | ArithmeticGrammarPerformanceTest(new SymbolicDataAnalysisExpressionTreeILEmittingInterpreter(), 7.5e6);
|
---|
[6732] | 92 | }
|
---|
| 93 |
|
---|
[7926] | 94 | private void TypeCoherentGrammarPerformanceTest(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, double nodesPerSecThreshold) {
|
---|
| 95 | var twister = new MersenneTwister(31415);
|
---|
| 96 | var dataset = Util.CreateRandomDataset(twister, Rows, Columns);
|
---|
| 97 | var grammar = new TypeCoherentExpressionGrammar();
|
---|
| 98 | grammar.ConfigureAsDefaultRegressionGrammar();
|
---|
| 99 | grammar.MaximumFunctionArguments = 0;
|
---|
| 100 | grammar.MaximumFunctionDefinitions = 0;
|
---|
| 101 | grammar.MinimumFunctionArguments = 0;
|
---|
| 102 | grammar.MinimumFunctionDefinitions = 0;
|
---|
| 103 | var randomTrees = Util.CreateRandomTrees(twister, dataset, grammar, N, 1, 100, 0, 0);
|
---|
| 104 | foreach (ISymbolicExpressionTree tree in randomTrees) {
|
---|
| 105 | Util.InitTree(tree, twister, new List<string>(dataset.VariableNames));
|
---|
| 106 | }
|
---|
| 107 | double nodesPerSec = Util.CalculateEvaluatedNodesPerSec(randomTrees, interpreter, dataset, 3);
|
---|
[9322] | 108 | //mkommend: commented due to performance issues on the builder
|
---|
| 109 | // Assert.IsTrue(nodesPerSec > nodesPerSecThreshold); // evaluated nodes per seconds must be larger than 15mNodes/sec
|
---|
[7926] | 110 | }
|
---|
| 111 |
|
---|
[6732] | 112 | private void FullGrammarPerformanceTest(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, double nodesPerSecThreshold) {
|
---|
[5574] | 113 | var twister = new MersenneTwister(31415);
|
---|
| 114 | var dataset = Util.CreateRandomDataset(twister, Rows, Columns);
|
---|
[5686] | 115 | var grammar = new FullFunctionalExpressionGrammar();
|
---|
| 116 | grammar.MaximumFunctionArguments = 0;
|
---|
| 117 | grammar.MaximumFunctionDefinitions = 0;
|
---|
| 118 | grammar.MinimumFunctionArguments = 0;
|
---|
| 119 | grammar.MinimumFunctionDefinitions = 0;
|
---|
[5574] | 120 | var randomTrees = Util.CreateRandomTrees(twister, dataset, grammar, N, 1, 100, 0, 0);
|
---|
| 121 | foreach (ISymbolicExpressionTree tree in randomTrees) {
|
---|
| 122 | Util.InitTree(tree, twister, new List<string>(dataset.VariableNames));
|
---|
| 123 | }
|
---|
| 124 | double nodesPerSec = Util.CalculateEvaluatedNodesPerSec(randomTrees, interpreter, dataset, 3);
|
---|
[9322] | 125 | //mkommend: commented due to performance issues on the builder
|
---|
| 126 | //Assert.IsTrue(nodesPerSec > nodesPerSecThreshold); // evaluated nodes per seconds must be larger than 15mNodes/sec
|
---|
[5574] | 127 | }
|
---|
| 128 |
|
---|
[6732] | 129 | private void ArithmeticGrammarPerformanceTest(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, double nodesPerSecThreshold) {
|
---|
[5574] | 130 | var twister = new MersenneTwister(31415);
|
---|
| 131 | var dataset = Util.CreateRandomDataset(twister, Rows, Columns);
|
---|
[5686] | 132 | var grammar = new ArithmeticExpressionGrammar();
|
---|
| 133 | grammar.MaximumFunctionArguments = 0;
|
---|
| 134 | grammar.MaximumFunctionDefinitions = 0;
|
---|
| 135 | grammar.MinimumFunctionArguments = 0;
|
---|
| 136 | grammar.MinimumFunctionDefinitions = 0;
|
---|
[5574] | 137 | var randomTrees = Util.CreateRandomTrees(twister, dataset, grammar, N, 1, 100, 0, 0);
|
---|
| 138 | foreach (SymbolicExpressionTree tree in randomTrees) {
|
---|
| 139 | Util.InitTree(tree, twister, new List<string>(dataset.VariableNames));
|
---|
| 140 | }
|
---|
[6732] | 141 |
|
---|
[5574] | 142 | double nodesPerSec = Util.CalculateEvaluatedNodesPerSec(randomTrees, interpreter, dataset, 3);
|
---|
[9322] | 143 | //mkommend: commented due to performance issues on the builder
|
---|
| 144 | //Assert.IsTrue(nodesPerSec > nodesPerSecThreshold); // evaluated nodes per seconds must be larger than 15mNodes/sec
|
---|
[5574] | 145 | }
|
---|
| 146 |
|
---|
| 147 |
|
---|
| 148 | /// <summary>
|
---|
| 149 | ///A test for Evaluate
|
---|
| 150 | ///</summary>
|
---|
| 151 | [TestMethod]
|
---|
[9885] | 152 | [TestCategory("Problems.DataAnalysis")]
|
---|
| 153 | [TestProperty("Time", "short")]
|
---|
[6732] | 154 | public void SymbolicDataAnalysisExpressionTreeInterpreterEvaluateTest() {
|
---|
| 155 | Dataset ds = new Dataset(new string[] { "Y", "A", "B" }, new double[,] {
|
---|
| 156 | { 1.0, 1.0, 1.0 },
|
---|
| 157 | { 2.0, 2.0, 2.0 },
|
---|
[6809] | 158 | { 3.0, 1.0, 2.0 },
|
---|
| 159 | { 4.0, 1.0, 1.0 },
|
---|
| 160 | { 5.0, 2.0, 2.0 },
|
---|
| 161 | { 6.0, 1.0, 2.0 },
|
---|
| 162 | { 7.0, 1.0, 1.0 },
|
---|
| 163 | { 8.0, 2.0, 2.0 },
|
---|
| 164 | { 9.0, 1.0, 2.0 },
|
---|
| 165 | { 10.0, 1.0, 1.0 },
|
---|
| 166 | { 11.0, 2.0, 2.0 },
|
---|
| 167 | { 12.0, 1.0, 2.0 }
|
---|
[6732] | 168 | });
|
---|
[5574] | 169 |
|
---|
[6732] | 170 | var interpreter = new SymbolicDataAnalysisExpressionTreeInterpreter();
|
---|
| 171 | EvaluateTerminals(interpreter, ds);
|
---|
| 172 | EvaluateOperations(interpreter, ds);
|
---|
| 173 | EvaluateAdf(interpreter, ds);
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | [TestMethod]
|
---|
[9885] | 177 | [TestCategory("Problems.DataAnalysis")]
|
---|
| 178 | [TestProperty("Time", "short")]
|
---|
[6732] | 179 | public void SymbolicDataAnalysisExpressionILEmittingTreeInterpreterEvaluateTest() {
|
---|
[5574] | 180 | Dataset ds = new Dataset(new string[] { "Y", "A", "B" }, new double[,] {
|
---|
| 181 | { 1.0, 1.0, 1.0 },
|
---|
| 182 | { 2.0, 2.0, 2.0 },
|
---|
[6809] | 183 | { 3.0, 1.0, 2.0 },
|
---|
| 184 | { 4.0, 1.0, 1.0 },
|
---|
| 185 | { 5.0, 2.0, 2.0 },
|
---|
| 186 | { 6.0, 1.0, 2.0 },
|
---|
| 187 | { 7.0, 1.0, 1.0 },
|
---|
| 188 | { 8.0, 2.0, 2.0 },
|
---|
| 189 | { 9.0, 1.0, 2.0 },
|
---|
| 190 | { 10.0, 1.0, 1.0 },
|
---|
| 191 | { 11.0, 2.0, 2.0 },
|
---|
| 192 | { 12.0, 1.0, 2.0 }
|
---|
[5574] | 193 | });
|
---|
| 194 |
|
---|
[6732] | 195 | var interpreter = new SymbolicDataAnalysisExpressionTreeILEmittingInterpreter();
|
---|
| 196 | EvaluateTerminals(interpreter, ds);
|
---|
| 197 | EvaluateOperations(interpreter, ds);
|
---|
| 198 | }
|
---|
[5574] | 199 |
|
---|
[6732] | 200 | private void EvaluateTerminals(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, Dataset ds) {
|
---|
[5574] | 201 | // constants
|
---|
| 202 | Evaluate(interpreter, ds, "(+ 1.5 3.5)", 0, 5.0);
|
---|
| 203 |
|
---|
| 204 | // variables
|
---|
| 205 | Evaluate(interpreter, ds, "(variable 2.0 a)", 0, 2.0);
|
---|
| 206 | Evaluate(interpreter, ds, "(variable 2.0 a)", 1, 4.0);
|
---|
[6732] | 207 | }
|
---|
[5574] | 208 |
|
---|
[6732] | 209 | private void EvaluateAdf(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, Dataset ds) {
|
---|
[5574] | 210 |
|
---|
[6732] | 211 | // ADF
|
---|
| 212 | Evaluate(interpreter, ds, @"(PROG
|
---|
| 213 | (MAIN
|
---|
| 214 | (CALL ADF0))
|
---|
| 215 | (defun ADF0 1.0))", 1, 1.0);
|
---|
| 216 | Evaluate(interpreter, ds, @"(PROG
|
---|
| 217 | (MAIN
|
---|
| 218 | (* (CALL ADF0) (CALL ADF0)))
|
---|
| 219 | (defun ADF0 2.0))", 1, 4.0);
|
---|
| 220 | Evaluate(interpreter, ds, @"(PROG
|
---|
| 221 | (MAIN
|
---|
| 222 | (CALL ADF0 2.0 3.0))
|
---|
| 223 | (defun ADF0
|
---|
| 224 | (+ (ARG 0) (ARG 1))))", 1, 5.0);
|
---|
| 225 | Evaluate(interpreter, ds, @"(PROG
|
---|
| 226 | (MAIN (CALL ADF1 2.0 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 | Evaluate(interpreter, ds, @"(PROG
|
---|
| 233 | (MAIN (CALL ADF1 (variable 2.0 a) 3.0))
|
---|
| 234 | (defun ADF0
|
---|
| 235 | (- (ARG 1) (ARG 0)))
|
---|
| 236 | (defun ADF1
|
---|
| 237 | (CALL ADF0 (ARG 1) (ARG 0))))", 1, 1.0);
|
---|
| 238 | Evaluate(interpreter, ds,
|
---|
| 239 | @"(PROG
|
---|
| 240 | (MAIN (CALL ADF1 (variable 2.0 a) 3.0))
|
---|
| 241 | (defun ADF0
|
---|
| 242 | (- (ARG 1) (ARG 0)))
|
---|
| 243 | (defun ADF1
|
---|
| 244 | (+ (CALL ADF0 (ARG 1) (ARG 0))
|
---|
| 245 | (CALL ADF0 (ARG 0) (ARG 1)))))", 1, 0.0);
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | private void EvaluateOperations(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, Dataset ds) {
|
---|
[5574] | 249 | // addition
|
---|
| 250 | Evaluate(interpreter, ds, "(+ (variable 2.0 a ))", 1, 4.0);
|
---|
| 251 | Evaluate(interpreter, ds, "(+ (variable 2.0 a ) (variable 3.0 b ))", 0, 5.0);
|
---|
| 252 | Evaluate(interpreter, ds, "(+ (variable 2.0 a ) (variable 3.0 b ))", 1, 10.0);
|
---|
| 253 | Evaluate(interpreter, ds, "(+ (variable 2.0 a) (variable 3.0 b ))", 2, 8.0);
|
---|
| 254 | Evaluate(interpreter, ds, "(+ 8.0 2.0 2.0)", 0, 12.0);
|
---|
| 255 |
|
---|
| 256 | // subtraction
|
---|
| 257 | Evaluate(interpreter, ds, "(- (variable 2.0 a ))", 1, -4.0);
|
---|
| 258 | Evaluate(interpreter, ds, "(- (variable 2.0 a ) (variable 3.0 b))", 0, -1.0);
|
---|
| 259 | Evaluate(interpreter, ds, "(- (variable 2.0 a ) (variable 3.0 b ))", 1, -2.0);
|
---|
| 260 | Evaluate(interpreter, ds, "(- (variable 2.0 a ) (variable 3.0 b ))", 2, -4.0);
|
---|
| 261 | Evaluate(interpreter, ds, "(- 8.0 2.0 2.0)", 0, 4.0);
|
---|
| 262 |
|
---|
| 263 | // multiplication
|
---|
| 264 | Evaluate(interpreter, ds, "(* (variable 2.0 a ))", 0, 2.0);
|
---|
| 265 | Evaluate(interpreter, ds, "(* (variable 2.0 a ) (variable 3.0 b ))", 0, 6.0);
|
---|
| 266 | Evaluate(interpreter, ds, "(* (variable 2.0 a ) (variable 3.0 b ))", 1, 24.0);
|
---|
| 267 | Evaluate(interpreter, ds, "(* (variable 2.0 a ) (variable 3.0 b ))", 2, 12.0);
|
---|
| 268 | Evaluate(interpreter, ds, "(* 8.0 2.0 2.0)", 0, 32.0);
|
---|
| 269 |
|
---|
| 270 | // division
|
---|
| 271 | Evaluate(interpreter, ds, "(/ (variable 2.0 a ))", 1, 1.0 / 4.0);
|
---|
| 272 | Evaluate(interpreter, ds, "(/ (variable 2.0 a ) 2.0)", 0, 1.0);
|
---|
| 273 | Evaluate(interpreter, ds, "(/ (variable 2.0 a ) 2.0)", 1, 2.0);
|
---|
| 274 | Evaluate(interpreter, ds, "(/ (variable 3.0 b ) 2.0)", 2, 3.0);
|
---|
| 275 | Evaluate(interpreter, ds, "(/ 8.0 2.0 2.0)", 0, 2.0);
|
---|
| 276 |
|
---|
| 277 | // gt
|
---|
| 278 | Evaluate(interpreter, ds, "(> (variable 2.0 a) 2.0)", 0, -1.0);
|
---|
| 279 | Evaluate(interpreter, ds, "(> 2.0 (variable 2.0 a))", 0, -1.0);
|
---|
| 280 | Evaluate(interpreter, ds, "(> (variable 2.0 a) 1.9)", 0, 1.0);
|
---|
| 281 | Evaluate(interpreter, ds, "(> 1.9 (variable 2.0 a))", 0, -1.0);
|
---|
[6732] | 282 | Evaluate(interpreter, ds, "(> (log -1.0) (log -1.0))", 0, -1.0); // (> nan nan) should be false
|
---|
[5574] | 283 |
|
---|
| 284 | // lt
|
---|
| 285 | Evaluate(interpreter, ds, "(< (variable 2.0 a) 2.0)", 0, -1.0);
|
---|
| 286 | Evaluate(interpreter, ds, "(< 2.0 (variable 2.0 a))", 0, -1.0);
|
---|
| 287 | Evaluate(interpreter, ds, "(< (variable 2.0 a) 1.9)", 0, -1.0);
|
---|
| 288 | Evaluate(interpreter, ds, "(< 1.9 (variable 2.0 a))", 0, 1.0);
|
---|
[6732] | 289 | Evaluate(interpreter, ds, "(< (log -1.0) (log -1.0))", 0, -1.0); // (< nan nan) should be false
|
---|
[5574] | 290 |
|
---|
| 291 | // If
|
---|
| 292 | Evaluate(interpreter, ds, "(if -10.0 2.0 3.0)", 0, 3.0);
|
---|
| 293 | Evaluate(interpreter, ds, "(if -1.0 2.0 3.0)", 0, 3.0);
|
---|
| 294 | Evaluate(interpreter, ds, "(if 0.0 2.0 3.0)", 0, 3.0);
|
---|
| 295 | Evaluate(interpreter, ds, "(if 1.0 2.0 3.0)", 0, 2.0);
|
---|
| 296 | Evaluate(interpreter, ds, "(if 10.0 2.0 3.0)", 0, 2.0);
|
---|
[6732] | 297 | Evaluate(interpreter, ds, "(if (log -1.0) 2.0 3.0)", 0, 3.0); // if(nan) should return the else branch
|
---|
[5574] | 298 |
|
---|
| 299 | // NOT
|
---|
| 300 | Evaluate(interpreter, ds, "(not -1.0)", 0, 1.0);
|
---|
| 301 | Evaluate(interpreter, ds, "(not -2.0)", 0, 1.0);
|
---|
| 302 | Evaluate(interpreter, ds, "(not 1.0)", 0, -1.0);
|
---|
| 303 | Evaluate(interpreter, ds, "(not 2.0)", 0, -1.0);
|
---|
| 304 | Evaluate(interpreter, ds, "(not 0.0)", 0, 1.0);
|
---|
[6732] | 305 | Evaluate(interpreter, ds, "(not (log -1.0))", 0, 1.0);
|
---|
[5574] | 306 |
|
---|
| 307 | // AND
|
---|
| 308 | Evaluate(interpreter, ds, "(and -1.0 -2.0)", 0, -1.0);
|
---|
| 309 | Evaluate(interpreter, ds, "(and -1.0 2.0)", 0, -1.0);
|
---|
| 310 | Evaluate(interpreter, ds, "(and 1.0 -2.0)", 0, -1.0);
|
---|
| 311 | Evaluate(interpreter, ds, "(and 1.0 0.0)", 0, -1.0);
|
---|
| 312 | Evaluate(interpreter, ds, "(and 0.0 0.0)", 0, -1.0);
|
---|
| 313 | Evaluate(interpreter, ds, "(and 1.0 2.0)", 0, 1.0);
|
---|
| 314 | Evaluate(interpreter, ds, "(and 1.0 2.0 3.0)", 0, 1.0);
|
---|
| 315 | Evaluate(interpreter, ds, "(and 1.0 -2.0 3.0)", 0, -1.0);
|
---|
[6732] | 316 | Evaluate(interpreter, ds, "(and (log -1.0))", 0, -1.0); // (and NaN)
|
---|
| 317 | Evaluate(interpreter, ds, "(and (log -1.0) 1.0)", 0, -1.0); // (and NaN 1.0)
|
---|
[5574] | 318 |
|
---|
[6732] | 319 |
|
---|
[5574] | 320 | // OR
|
---|
| 321 | Evaluate(interpreter, ds, "(or -1.0 -2.0)", 0, -1.0);
|
---|
| 322 | Evaluate(interpreter, ds, "(or -1.0 2.0)", 0, 1.0);
|
---|
| 323 | Evaluate(interpreter, ds, "(or 1.0 -2.0)", 0, 1.0);
|
---|
| 324 | Evaluate(interpreter, ds, "(or 1.0 2.0)", 0, 1.0);
|
---|
| 325 | Evaluate(interpreter, ds, "(or 0.0 0.0)", 0, -1.0);
|
---|
| 326 | Evaluate(interpreter, ds, "(or -1.0 -2.0 -3.0)", 0, -1.0);
|
---|
| 327 | Evaluate(interpreter, ds, "(or -1.0 -2.0 3.0)", 0, 1.0);
|
---|
[6732] | 328 | Evaluate(interpreter, ds, "(or (log -1.0))", 0, -1.0); // (or NaN)
|
---|
| 329 | Evaluate(interpreter, ds, "(or (log -1.0) 1.0)", 0, -1.0); // (or NaN 1.0)
|
---|
[5574] | 330 |
|
---|
| 331 | // sin, cos, tan
|
---|
| 332 | Evaluate(interpreter, ds, "(sin " + Math.PI.ToString(NumberFormatInfo.InvariantInfo) + ")", 0, 0.0);
|
---|
| 333 | Evaluate(interpreter, ds, "(sin 0.0)", 0, 0.0);
|
---|
| 334 | Evaluate(interpreter, ds, "(cos " + Math.PI.ToString(NumberFormatInfo.InvariantInfo) + ")", 0, -1.0);
|
---|
| 335 | Evaluate(interpreter, ds, "(cos 0.0)", 0, 1.0);
|
---|
| 336 | Evaluate(interpreter, ds, "(tan " + Math.PI.ToString(NumberFormatInfo.InvariantInfo) + ")", 0, Math.Tan(Math.PI));
|
---|
| 337 | Evaluate(interpreter, ds, "(tan 0.0)", 0, Math.Tan(Math.PI));
|
---|
| 338 |
|
---|
| 339 | // exp, log
|
---|
| 340 | Evaluate(interpreter, ds, "(log (exp 7.0))", 0, Math.Log(Math.Exp(7)));
|
---|
| 341 | Evaluate(interpreter, ds, "(exp (log 7.0))", 0, Math.Exp(Math.Log(7)));
|
---|
| 342 | Evaluate(interpreter, ds, "(log -3.0)", 0, Math.Log(-3));
|
---|
| 343 |
|
---|
[6755] | 344 | // power
|
---|
| 345 | Evaluate(interpreter, ds, "(pow 2.0 3.0)", 0, 8.0);
|
---|
| 346 | 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)
|
---|
| 347 | 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)
|
---|
| 348 | Evaluate(interpreter, ds, "(pow -2.0 3.0)", 0, -8.0);
|
---|
| 349 | Evaluate(interpreter, ds, "(pow 2.0 -3.0)", 0, 1.0 / 8.0);
|
---|
| 350 | Evaluate(interpreter, ds, "(pow -2.0 -3.0)", 0, -1.0 / 8.0);
|
---|
| 351 |
|
---|
| 352 | // root
|
---|
| 353 | Evaluate(interpreter, ds, "(root 9.0 2.0)", 0, 3.0);
|
---|
| 354 | Evaluate(interpreter, ds, "(root 27.0 3.0)", 0, 3.0);
|
---|
| 355 | Evaluate(interpreter, ds, "(root 2.0 -3.0)", 0, Math.Pow(2.0, -1.0 / 3.0));
|
---|
| 356 |
|
---|
[5574] | 357 | // mean
|
---|
| 358 | Evaluate(interpreter, ds, "(mean -1.0 1.0 -1.0)", 0, -1.0 / 3.0);
|
---|
| 359 |
|
---|
[6769] | 360 | // lag
|
---|
| 361 | Evaluate(interpreter, ds, "(lagVariable 1.0 a -1) ", 1, ds.GetDoubleValue("A", 0));
|
---|
| 362 | Evaluate(interpreter, ds, "(lagVariable 1.0 a -1) ", 2, ds.GetDoubleValue("A", 1));
|
---|
| 363 | Evaluate(interpreter, ds, "(lagVariable 1.0 a 0) ", 2, ds.GetDoubleValue("A", 2));
|
---|
| 364 | Evaluate(interpreter, ds, "(lagVariable 1.0 a 1) ", 0, ds.GetDoubleValue("A", 1));
|
---|
[6809] | 365 |
|
---|
| 366 | // integral
|
---|
| 367 | Evaluate(interpreter, ds, "(integral -1.0 (variable 1.0 a)) ", 1, ds.GetDoubleValue("A", 0) + ds.GetDoubleValue("A", 1));
|
---|
| 368 | Evaluate(interpreter, ds, "(integral -1.0 (lagVariable 1.0 a 1)) ", 1, ds.GetDoubleValue("A", 1) + ds.GetDoubleValue("A", 2));
|
---|
| 369 | Evaluate(interpreter, ds, "(integral -2.0 (variable 1.0 a)) ", 2, ds.GetDoubleValue("A", 0) + ds.GetDoubleValue("A", 1) + ds.GetDoubleValue("A", 2));
|
---|
| 370 | 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));
|
---|
| 371 | Evaluate(interpreter, ds, "(integral -2.0 3.0)", 1, 9.0);
|
---|
| 372 |
|
---|
| 373 | // derivative
|
---|
| 374 | // (f_0 + 2 * f_1 - 2 * f_3 - f_4) / 8; // h = 1
|
---|
| 375 | 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);
|
---|
| 376 | 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);
|
---|
| 377 | Evaluate(interpreter, ds, "(diff (* (variable 1.0 a) (variable 1.0 b)))", 5, +
|
---|
| 378 | (ds.GetDoubleValue("A", 5) * ds.GetDoubleValue("B", 5) +
|
---|
| 379 | 2 * ds.GetDoubleValue("A", 4) * ds.GetDoubleValue("B", 4) -
|
---|
| 380 | 2 * ds.GetDoubleValue("A", 2) * ds.GetDoubleValue("B", 2) -
|
---|
| 381 | ds.GetDoubleValue("A", 1) * ds.GetDoubleValue("B", 1)) / 8.0);
|
---|
| 382 | Evaluate(interpreter, ds, "(diff -2.0 3.0)", 5, 0.0);
|
---|
| 383 |
|
---|
| 384 | // timelag
|
---|
| 385 | Evaluate(interpreter, ds, "(lag -1.0 (lagVariable 1.0 a 2)) ", 1, ds.GetDoubleValue("A", 2));
|
---|
| 386 | Evaluate(interpreter, ds, "(lag -2.0 (lagVariable 1.0 a 2)) ", 2, ds.GetDoubleValue("A", 2));
|
---|
| 387 | 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));
|
---|
| 388 | Evaluate(interpreter, ds, "(lag -2.0 3.0)", 1, 3.0);
|
---|
[8123] | 389 |
|
---|
| 390 | {
|
---|
| 391 | // special functions
|
---|
| 392 | Action<double> checkAiry = (x) => {
|
---|
| 393 | double ai, aip, bi, bip;
|
---|
| 394 | alglib.airy(x, out ai, out aip, out bi, out bip);
|
---|
| 395 | Evaluate(interpreter, ds, "(airya " + x + ")", 0, ai);
|
---|
| 396 | Evaluate(interpreter, ds, "(airyb " + x + ")", 0, bi);
|
---|
| 397 | };
|
---|
| 398 |
|
---|
| 399 | Action<double> checkBessel = (x) => {
|
---|
| 400 | Evaluate(interpreter, ds, "(bessel " + x + ")", 0, alglib.besseli0(x));
|
---|
| 401 | };
|
---|
| 402 |
|
---|
| 403 | Action<double> checkSinCosIntegrals = (x) => {
|
---|
| 404 | double si, ci;
|
---|
| 405 | alglib.sinecosineintegrals(x, out si, out ci);
|
---|
| 406 | Evaluate(interpreter, ds, "(cosint " + x + ")", 0, ci);
|
---|
| 407 | Evaluate(interpreter, ds, "(sinint " + x + ")", 0, si);
|
---|
| 408 | };
|
---|
| 409 | Action<double> checkHypSinCosIntegrals = (x) => {
|
---|
| 410 | double shi, chi;
|
---|
| 411 | alglib.hyperbolicsinecosineintegrals(x, out shi, out chi);
|
---|
| 412 | Evaluate(interpreter, ds, "(hypcosint " + x + ")", 0, chi);
|
---|
| 413 | Evaluate(interpreter, ds, "(hypsinint " + x + ")", 0, shi);
|
---|
| 414 | };
|
---|
| 415 | Action<double> checkFresnelSinCosIntegrals = (x) => {
|
---|
| 416 | double c = 0, s = 0;
|
---|
| 417 | alglib.fresnelintegral(x, ref c, ref s);
|
---|
| 418 | Evaluate(interpreter, ds, "(fresnelcosint " + x + ")", 0, c);
|
---|
| 419 | Evaluate(interpreter, ds, "(fresnelsinint " + x + ")", 0, s);
|
---|
| 420 | };
|
---|
| 421 | Action<double> checkNormErf = (x) => {
|
---|
| 422 | Evaluate(interpreter, ds, "(norm " + x + ")", 0, alglib.normaldistribution(x));
|
---|
| 423 | Evaluate(interpreter, ds, "(erf " + x + ")", 0, alglib.errorfunction(x));
|
---|
| 424 | };
|
---|
| 425 |
|
---|
| 426 | Action<double> checkGamma = (x) => {
|
---|
| 427 | Evaluate(interpreter, ds, "(gamma " + x + ")", 0, alglib.gammafunction(x));
|
---|
| 428 | };
|
---|
| 429 | Action<double> checkPsi = (x) => {
|
---|
| 430 | try {
|
---|
| 431 | Evaluate(interpreter, ds, "(psi " + x + ")", 0, alglib.psi(x));
|
---|
[9885] | 432 | } catch (alglib.alglibexception) { // ignore cases where alglib throws an exception
|
---|
[8123] | 433 | }
|
---|
| 434 | };
|
---|
| 435 | Action<double> checkDawson = (x) => {
|
---|
| 436 | Evaluate(interpreter, ds, "(dawson " + x + ")", 0, alglib.dawsonintegral(x));
|
---|
| 437 | };
|
---|
| 438 | Action<double> checkExpInt = (x) => {
|
---|
| 439 | Evaluate(interpreter, ds, "(expint " + x + ")", 0, alglib.exponentialintegralei(x));
|
---|
| 440 | };
|
---|
| 441 |
|
---|
| 442 |
|
---|
| 443 |
|
---|
| 444 | foreach (var e in new[] { -2.0, -1.0, 0.0, 1.0, 2.0 }) {
|
---|
| 445 | checkAiry(e);
|
---|
| 446 | checkBessel(e);
|
---|
| 447 | checkSinCosIntegrals(e);
|
---|
| 448 | checkGamma(e);
|
---|
| 449 | checkExpInt(e);
|
---|
| 450 | checkDawson(e);
|
---|
| 451 | checkPsi(e);
|
---|
| 452 | checkNormErf(e);
|
---|
| 453 | checkFresnelSinCosIntegrals(e);
|
---|
| 454 | checkHypSinCosIntegrals(e);
|
---|
| 455 | }
|
---|
| 456 | }
|
---|
[5574] | 457 | }
|
---|
| 458 |
|
---|
[6732] | 459 | private void Evaluate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, Dataset ds, string expr, int index, double expected) {
|
---|
[5574] | 460 | var importer = new SymbolicExpressionImporter();
|
---|
| 461 | ISymbolicExpressionTree tree = importer.Import(expr);
|
---|
| 462 |
|
---|
| 463 | double actual = interpreter.GetSymbolicExpressionTreeValues(tree, ds, Enumerable.Range(index, 1)).First();
|
---|
| 464 |
|
---|
[6809] | 465 | Assert.IsFalse(double.IsNaN(actual) && !double.IsNaN(expected));
|
---|
| 466 | Assert.IsFalse(!double.IsNaN(actual) && double.IsNaN(expected));
|
---|
[5574] | 467 | Assert.AreEqual(expected, actual, 1.0E-12, expr);
|
---|
| 468 | }
|
---|
| 469 | }
|
---|
| 470 | }
|
---|