[5574] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11171] | 3 | * Copyright (C) 2002-2014 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;
|
---|
[9764] | 29 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Tests {
|
---|
[5574] | 30 |
|
---|
| 31 |
|
---|
[9828] | 32 | [TestClass]
|
---|
[5574] | 33 | public class SymbolicDataAnalysisExpressionTreeInterpreterTest {
|
---|
| 34 | private const int N = 1000;
|
---|
| 35 | private const int Rows = 1000;
|
---|
| 36 | private const int Columns = 50;
|
---|
| 37 |
|
---|
[9828] | 38 | private static Dataset ds = new Dataset(new string[] { "Y", "A", "B" }, new double[,] {
|
---|
| 39 | { 1.0, 1.0, 1.0 },
|
---|
| 40 | { 2.0, 2.0, 2.0 },
|
---|
| 41 | { 3.0, 1.0, 2.0 },
|
---|
| 42 | { 4.0, 1.0, 1.0 },
|
---|
| 43 | { 5.0, 2.0, 2.0 },
|
---|
| 44 | { 6.0, 1.0, 2.0 },
|
---|
| 45 | { 7.0, 1.0, 1.0 },
|
---|
| 46 | { 8.0, 2.0, 2.0 },
|
---|
| 47 | { 9.0, 1.0, 2.0 },
|
---|
| 48 | { 10.0, 1.0, 1.0 },
|
---|
| 49 | { 11.0, 2.0, 2.0 },
|
---|
| 50 | { 12.0, 1.0, 2.0 }
|
---|
| 51 | });
|
---|
[5574] | 52 |
|
---|
| 53 | [TestMethod]
|
---|
[9828] | 54 | [TestCategory("Problems.DataAnalysis.Symbolic")]
|
---|
[9785] | 55 | [TestProperty("Time", "long")]
|
---|
[9828] | 56 | public void StandardInterpreterTestTypeCoherentGrammarPerformance() {
|
---|
| 57 | TestTypeCoherentGrammarPerformance(new SymbolicDataAnalysisExpressionTreeInterpreter(), 12.5e6);
|
---|
[7926] | 58 | }
|
---|
| 59 | [TestMethod]
|
---|
[9828] | 60 | [TestCategory("Problems.DataAnalysis.Symbolic")]
|
---|
[9785] | 61 | [TestProperty("Time", "long")]
|
---|
[9828] | 62 | public void StandardInterpreterTestFullGrammarPerformance() {
|
---|
| 63 | TestFullGrammarPerformance(new SymbolicDataAnalysisExpressionTreeInterpreter(), 12.5e6);
|
---|
[6732] | 64 | }
|
---|
| 65 | [TestMethod]
|
---|
[9828] | 66 | [TestCategory("Problems.DataAnalysis.Symbolic")]
|
---|
[9785] | 67 | [TestProperty("Time", "long")]
|
---|
[9828] | 68 | public void StandardInterpreterTestArithmeticGrammarPerformance() {
|
---|
| 69 | TestArithmeticGrammarPerformance(new SymbolicDataAnalysisExpressionTreeInterpreter(), 12.5e6);
|
---|
[6732] | 70 | }
|
---|
| 71 |
|
---|
[9828] | 72 |
|
---|
[6732] | 73 | [TestMethod]
|
---|
[9828] | 74 | [TestCategory("Problems.DataAnalysis.Symbolic")]
|
---|
[9785] | 75 | [TestProperty("Time", "long")]
|
---|
[9828] | 76 | public void ILEmittingInterpreterTestTypeCoherentGrammarPerformance() {
|
---|
| 77 | TestTypeCoherentGrammarPerformance(new SymbolicDataAnalysisExpressionTreeILEmittingInterpreter(), 7.5e6);
|
---|
[7926] | 78 | }
|
---|
| 79 | [TestMethod]
|
---|
[9828] | 80 | [TestCategory("Problems.DataAnalysis.Symbolic")]
|
---|
[9785] | 81 | [TestProperty("Time", "long")]
|
---|
[9828] | 82 | public void ILEmittingInterpreterTestFullGrammarPerformance() {
|
---|
| 83 | TestFullGrammarPerformance(new SymbolicDataAnalysisExpressionTreeILEmittingInterpreter(), 7.5e6);
|
---|
[6732] | 84 | }
|
---|
[9828] | 85 | [TestMethod]
|
---|
| 86 | [TestCategory("Problems.DataAnalysis.Symbolic")]
|
---|
| 87 | [TestProperty("Time", "long")]
|
---|
| 88 | public void ILEmittingInterpreterTestArithmeticGrammarPerformance() {
|
---|
| 89 | TestArithmeticGrammarPerformance(new SymbolicDataAnalysisExpressionTreeILEmittingInterpreter(), 7.5e6);
|
---|
| 90 | }
|
---|
[9785] | 91 |
|
---|
[9828] | 92 |
|
---|
| 93 | [TestMethod]
|
---|
| 94 | [TestCategory("Problems.DataAnalysis.Symbolic")]
|
---|
[9785] | 95 | [TestProperty("Time", "long")]
|
---|
[9828] | 96 | public void LinearInterpreterTestTypeCoherentGrammarPerformance() {
|
---|
| 97 | TestTypeCoherentGrammarPerformance(new SymbolicDataAnalysisExpressionTreeLinearInterpreter(), 12.5e6);
|
---|
| 98 | }
|
---|
[6732] | 99 | [TestMethod]
|
---|
[9828] | 100 | [TestCategory("Problems.DataAnalysis.Symbolic")]
|
---|
| 101 | [TestProperty("Time", "long")]
|
---|
| 102 | public void LinearInterpreterTestFullGrammarPerformance() {
|
---|
| 103 | TestFullGrammarPerformance(new SymbolicDataAnalysisExpressionTreeLinearInterpreter(), 12.5e6);
|
---|
[6732] | 104 | }
|
---|
[9828] | 105 | [TestMethod]
|
---|
| 106 | [TestCategory("Problems.DataAnalysis.Symbolic")]
|
---|
| 107 | [TestProperty("Time", "long")]
|
---|
| 108 | public void LinearInterpreterTestArithmeticGrammarPerformance() {
|
---|
| 109 | TestArithmeticGrammarPerformance(new SymbolicDataAnalysisExpressionTreeLinearInterpreter(), 12.5e6);
|
---|
| 110 | }
|
---|
[6732] | 111 |
|
---|
[9828] | 112 | private void TestTypeCoherentGrammarPerformance(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, double nodesPerSecThreshold) {
|
---|
[7926] | 113 | var twister = new MersenneTwister(31415);
|
---|
| 114 | var dataset = Util.CreateRandomDataset(twister, Rows, Columns);
|
---|
| 115 | var grammar = new TypeCoherentExpressionGrammar();
|
---|
| 116 | grammar.ConfigureAsDefaultRegressionGrammar();
|
---|
| 117 | grammar.MaximumFunctionArguments = 0;
|
---|
| 118 | grammar.MaximumFunctionDefinitions = 0;
|
---|
| 119 | grammar.MinimumFunctionArguments = 0;
|
---|
| 120 | grammar.MinimumFunctionDefinitions = 0;
|
---|
| 121 | var randomTrees = Util.CreateRandomTrees(twister, dataset, grammar, N, 1, 100, 0, 0);
|
---|
| 122 | foreach (ISymbolicExpressionTree tree in randomTrees) {
|
---|
| 123 | Util.InitTree(tree, twister, new List<string>(dataset.VariableNames));
|
---|
| 124 | }
|
---|
| 125 | double nodesPerSec = Util.CalculateEvaluatedNodesPerSec(randomTrees, interpreter, dataset, 3);
|
---|
[9322] | 126 | //mkommend: commented due to performance issues on the builder
|
---|
| 127 | // Assert.IsTrue(nodesPerSec > nodesPerSecThreshold); // evaluated nodes per seconds must be larger than 15mNodes/sec
|
---|
[7926] | 128 | }
|
---|
| 129 |
|
---|
[9828] | 130 | private void TestFullGrammarPerformance(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, double nodesPerSecThreshold) {
|
---|
[5574] | 131 | var twister = new MersenneTwister(31415);
|
---|
| 132 | var dataset = Util.CreateRandomDataset(twister, Rows, Columns);
|
---|
[5686] | 133 | var grammar = new FullFunctionalExpressionGrammar();
|
---|
| 134 | grammar.MaximumFunctionArguments = 0;
|
---|
| 135 | grammar.MaximumFunctionDefinitions = 0;
|
---|
| 136 | grammar.MinimumFunctionArguments = 0;
|
---|
| 137 | grammar.MinimumFunctionDefinitions = 0;
|
---|
[5574] | 138 | var randomTrees = Util.CreateRandomTrees(twister, dataset, grammar, N, 1, 100, 0, 0);
|
---|
| 139 | foreach (ISymbolicExpressionTree tree in randomTrees) {
|
---|
| 140 | Util.InitTree(tree, twister, new List<string>(dataset.VariableNames));
|
---|
| 141 | }
|
---|
| 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 |
|
---|
[9828] | 147 | private void TestArithmeticGrammarPerformance(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, double nodesPerSecThreshold) {
|
---|
[5574] | 148 | var twister = new MersenneTwister(31415);
|
---|
| 149 | var dataset = Util.CreateRandomDataset(twister, Rows, Columns);
|
---|
[5686] | 150 | var grammar = new ArithmeticExpressionGrammar();
|
---|
[9955] | 151 | //grammar.Symbols.OfType<Variable>().First().Enabled = false;
|
---|
[5686] | 152 | grammar.MaximumFunctionArguments = 0;
|
---|
| 153 | grammar.MaximumFunctionDefinitions = 0;
|
---|
| 154 | grammar.MinimumFunctionArguments = 0;
|
---|
| 155 | grammar.MinimumFunctionDefinitions = 0;
|
---|
[5574] | 156 | var randomTrees = Util.CreateRandomTrees(twister, dataset, grammar, N, 1, 100, 0, 0);
|
---|
| 157 | foreach (SymbolicExpressionTree tree in randomTrees) {
|
---|
| 158 | Util.InitTree(tree, twister, new List<string>(dataset.VariableNames));
|
---|
| 159 | }
|
---|
[6732] | 160 |
|
---|
[5574] | 161 | double nodesPerSec = Util.CalculateEvaluatedNodesPerSec(randomTrees, interpreter, dataset, 3);
|
---|
[9322] | 162 | //mkommend: commented due to performance issues on the builder
|
---|
| 163 | //Assert.IsTrue(nodesPerSec > nodesPerSecThreshold); // evaluated nodes per seconds must be larger than 15mNodes/sec
|
---|
[5574] | 164 | }
|
---|
| 165 |
|
---|
| 166 |
|
---|
| 167 | /// <summary>
|
---|
| 168 | ///A test for Evaluate
|
---|
| 169 | ///</summary>
|
---|
| 170 | [TestMethod]
|
---|
[9828] | 171 | [TestCategory("Problems.DataAnalysis.Symbolic")]
|
---|
[9785] | 172 | [TestProperty("Time", "short")]
|
---|
[9828] | 173 | public void StandardInterpreterTestEvaluation() {
|
---|
[6732] | 174 | var interpreter = new SymbolicDataAnalysisExpressionTreeInterpreter();
|
---|
| 175 | EvaluateTerminals(interpreter, ds);
|
---|
| 176 | EvaluateOperations(interpreter, ds);
|
---|
| 177 | EvaluateAdf(interpreter, ds);
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | [TestMethod]
|
---|
[9828] | 181 | [TestCategory("Problems.DataAnalysis.Symbolic")]
|
---|
[9785] | 182 | [TestProperty("Time", "short")]
|
---|
[9828] | 183 | public void ILEmittingInterpreterTestEvaluation() {
|
---|
[6732] | 184 | var interpreter = new SymbolicDataAnalysisExpressionTreeILEmittingInterpreter();
|
---|
| 185 | EvaluateTerminals(interpreter, ds);
|
---|
| 186 | EvaluateOperations(interpreter, ds);
|
---|
| 187 | }
|
---|
[5574] | 188 |
|
---|
[9828] | 189 | [TestMethod]
|
---|
| 190 | [TestCategory("Problems.DataAnalysis.Symbolic")]
|
---|
| 191 | [TestProperty("Time", "short")]
|
---|
| 192 | public void LinearInterpreterTestEvaluation() {
|
---|
| 193 | var interpreter = new SymbolicDataAnalysisExpressionTreeLinearInterpreter();
|
---|
| 194 |
|
---|
| 195 | //ADFs are not supported by the linear interpreter
|
---|
| 196 | EvaluateTerminals(interpreter, ds);
|
---|
| 197 | EvaluateOperations(interpreter, ds);
|
---|
| 198 | }
|
---|
| 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 |
|
---|
| 319 | // OR
|
---|
| 320 | Evaluate(interpreter, ds, "(or -1.0 -2.0)", 0, -1.0);
|
---|
| 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 0.0 0.0)", 0, -1.0);
|
---|
| 325 | Evaluate(interpreter, ds, "(or -1.0 -2.0 -3.0)", 0, -1.0);
|
---|
| 326 | Evaluate(interpreter, ds, "(or -1.0 -2.0 3.0)", 0, 1.0);
|
---|
[6732] | 327 | Evaluate(interpreter, ds, "(or (log -1.0))", 0, -1.0); // (or NaN)
|
---|
| 328 | Evaluate(interpreter, ds, "(or (log -1.0) 1.0)", 0, -1.0); // (or NaN 1.0)
|
---|
[5574] | 329 |
|
---|
[10790] | 330 | // XOR
|
---|
| 331 | Evaluate(interpreter, ds, "(xor -1.0 -2.0)", 0, -1.0);
|
---|
| 332 | Evaluate(interpreter, ds, "(xor -1.0 2.0)", 0, 1.0);
|
---|
| 333 | Evaluate(interpreter, ds, "(xor 1.0 -2.0)", 0, 1.0);
|
---|
| 334 | Evaluate(interpreter, ds, "(xor 1.0 2.0)", 0, -1.0);
|
---|
| 335 | Evaluate(interpreter, ds, "(xor 0.0 0.0)", 0, -1.0);
|
---|
| 336 | Evaluate(interpreter, ds, "(xor -1.0 -2.0 -3.0)", 0, -1.0);
|
---|
| 337 | Evaluate(interpreter, ds, "(xor -1.0 -2.0 3.0)", 0, 1.0);
|
---|
| 338 | Evaluate(interpreter, ds, "(xor -1.0 2.0 3.0)", 0, -1.0);
|
---|
| 339 | Evaluate(interpreter, ds, "(xor 1.0 2.0 3.0)", 0, 1.0);
|
---|
| 340 | Evaluate(interpreter, ds, "(xor (log -1.0))", 0, -1.0);
|
---|
| 341 | Evaluate(interpreter, ds, "(xor (log -1.0) 1.0)", 0, 1.0);
|
---|
| 342 |
|
---|
[5574] | 343 | // sin, cos, tan
|
---|
| 344 | Evaluate(interpreter, ds, "(sin " + Math.PI.ToString(NumberFormatInfo.InvariantInfo) + ")", 0, 0.0);
|
---|
| 345 | Evaluate(interpreter, ds, "(sin 0.0)", 0, 0.0);
|
---|
| 346 | Evaluate(interpreter, ds, "(cos " + Math.PI.ToString(NumberFormatInfo.InvariantInfo) + ")", 0, -1.0);
|
---|
| 347 | Evaluate(interpreter, ds, "(cos 0.0)", 0, 1.0);
|
---|
| 348 | Evaluate(interpreter, ds, "(tan " + Math.PI.ToString(NumberFormatInfo.InvariantInfo) + ")", 0, Math.Tan(Math.PI));
|
---|
| 349 | Evaluate(interpreter, ds, "(tan 0.0)", 0, Math.Tan(Math.PI));
|
---|
| 350 |
|
---|
| 351 | // exp, log
|
---|
| 352 | Evaluate(interpreter, ds, "(log (exp 7.0))", 0, Math.Log(Math.Exp(7)));
|
---|
| 353 | Evaluate(interpreter, ds, "(exp (log 7.0))", 0, Math.Exp(Math.Log(7)));
|
---|
| 354 | Evaluate(interpreter, ds, "(log -3.0)", 0, Math.Log(-3));
|
---|
| 355 |
|
---|
[6755] | 356 | // power
|
---|
| 357 | Evaluate(interpreter, ds, "(pow 2.0 3.0)", 0, 8.0);
|
---|
| 358 | 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)
|
---|
| 359 | 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)
|
---|
| 360 | Evaluate(interpreter, ds, "(pow -2.0 3.0)", 0, -8.0);
|
---|
| 361 | Evaluate(interpreter, ds, "(pow 2.0 -3.0)", 0, 1.0 / 8.0);
|
---|
| 362 | Evaluate(interpreter, ds, "(pow -2.0 -3.0)", 0, -1.0 / 8.0);
|
---|
| 363 |
|
---|
| 364 | // root
|
---|
| 365 | Evaluate(interpreter, ds, "(root 9.0 2.0)", 0, 3.0);
|
---|
| 366 | Evaluate(interpreter, ds, "(root 27.0 3.0)", 0, 3.0);
|
---|
| 367 | Evaluate(interpreter, ds, "(root 2.0 -3.0)", 0, Math.Pow(2.0, -1.0 / 3.0));
|
---|
| 368 |
|
---|
[5574] | 369 | // mean
|
---|
| 370 | Evaluate(interpreter, ds, "(mean -1.0 1.0 -1.0)", 0, -1.0 / 3.0);
|
---|
| 371 |
|
---|
[6769] | 372 | // lag
|
---|
| 373 | Evaluate(interpreter, ds, "(lagVariable 1.0 a -1) ", 1, ds.GetDoubleValue("A", 0));
|
---|
| 374 | Evaluate(interpreter, ds, "(lagVariable 1.0 a -1) ", 2, ds.GetDoubleValue("A", 1));
|
---|
| 375 | Evaluate(interpreter, ds, "(lagVariable 1.0 a 0) ", 2, ds.GetDoubleValue("A", 2));
|
---|
| 376 | Evaluate(interpreter, ds, "(lagVariable 1.0 a 1) ", 0, ds.GetDoubleValue("A", 1));
|
---|
[6809] | 377 |
|
---|
| 378 | // integral
|
---|
| 379 | Evaluate(interpreter, ds, "(integral -1.0 (variable 1.0 a)) ", 1, ds.GetDoubleValue("A", 0) + ds.GetDoubleValue("A", 1));
|
---|
| 380 | Evaluate(interpreter, ds, "(integral -1.0 (lagVariable 1.0 a 1)) ", 1, ds.GetDoubleValue("A", 1) + ds.GetDoubleValue("A", 2));
|
---|
| 381 | Evaluate(interpreter, ds, "(integral -2.0 (variable 1.0 a)) ", 2, ds.GetDoubleValue("A", 0) + ds.GetDoubleValue("A", 1) + ds.GetDoubleValue("A", 2));
|
---|
| 382 | 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));
|
---|
| 383 | Evaluate(interpreter, ds, "(integral -2.0 3.0)", 1, 9.0);
|
---|
| 384 |
|
---|
| 385 | // derivative
|
---|
| 386 | // (f_0 + 2 * f_1 - 2 * f_3 - f_4) / 8; // h = 1
|
---|
| 387 | 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);
|
---|
| 388 | 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);
|
---|
| 389 | Evaluate(interpreter, ds, "(diff (* (variable 1.0 a) (variable 1.0 b)))", 5, +
|
---|
| 390 | (ds.GetDoubleValue("A", 5) * ds.GetDoubleValue("B", 5) +
|
---|
| 391 | 2 * ds.GetDoubleValue("A", 4) * ds.GetDoubleValue("B", 4) -
|
---|
| 392 | 2 * ds.GetDoubleValue("A", 2) * ds.GetDoubleValue("B", 2) -
|
---|
| 393 | ds.GetDoubleValue("A", 1) * ds.GetDoubleValue("B", 1)) / 8.0);
|
---|
| 394 | Evaluate(interpreter, ds, "(diff -2.0 3.0)", 5, 0.0);
|
---|
| 395 |
|
---|
| 396 | // timelag
|
---|
| 397 | Evaluate(interpreter, ds, "(lag -1.0 (lagVariable 1.0 a 2)) ", 1, ds.GetDoubleValue("A", 2));
|
---|
| 398 | Evaluate(interpreter, ds, "(lag -2.0 (lagVariable 1.0 a 2)) ", 2, ds.GetDoubleValue("A", 2));
|
---|
| 399 | 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));
|
---|
| 400 | Evaluate(interpreter, ds, "(lag -2.0 3.0)", 1, 3.0);
|
---|
[8123] | 401 |
|
---|
| 402 | {
|
---|
| 403 | // special functions
|
---|
| 404 | Action<double> checkAiry = (x) => {
|
---|
| 405 | double ai, aip, bi, bip;
|
---|
| 406 | alglib.airy(x, out ai, out aip, out bi, out bip);
|
---|
| 407 | Evaluate(interpreter, ds, "(airya " + x + ")", 0, ai);
|
---|
| 408 | Evaluate(interpreter, ds, "(airyb " + x + ")", 0, bi);
|
---|
| 409 | };
|
---|
| 410 |
|
---|
| 411 | Action<double> checkBessel = (x) => {
|
---|
| 412 | Evaluate(interpreter, ds, "(bessel " + x + ")", 0, alglib.besseli0(x));
|
---|
| 413 | };
|
---|
| 414 |
|
---|
| 415 | Action<double> checkSinCosIntegrals = (x) => {
|
---|
| 416 | double si, ci;
|
---|
| 417 | alglib.sinecosineintegrals(x, out si, out ci);
|
---|
| 418 | Evaluate(interpreter, ds, "(cosint " + x + ")", 0, ci);
|
---|
| 419 | Evaluate(interpreter, ds, "(sinint " + x + ")", 0, si);
|
---|
| 420 | };
|
---|
| 421 | Action<double> checkHypSinCosIntegrals = (x) => {
|
---|
| 422 | double shi, chi;
|
---|
| 423 | alglib.hyperbolicsinecosineintegrals(x, out shi, out chi);
|
---|
| 424 | Evaluate(interpreter, ds, "(hypcosint " + x + ")", 0, chi);
|
---|
| 425 | Evaluate(interpreter, ds, "(hypsinint " + x + ")", 0, shi);
|
---|
| 426 | };
|
---|
| 427 | Action<double> checkFresnelSinCosIntegrals = (x) => {
|
---|
| 428 | double c = 0, s = 0;
|
---|
| 429 | alglib.fresnelintegral(x, ref c, ref s);
|
---|
| 430 | Evaluate(interpreter, ds, "(fresnelcosint " + x + ")", 0, c);
|
---|
| 431 | Evaluate(interpreter, ds, "(fresnelsinint " + x + ")", 0, s);
|
---|
| 432 | };
|
---|
| 433 | Action<double> checkNormErf = (x) => {
|
---|
| 434 | Evaluate(interpreter, ds, "(norm " + x + ")", 0, alglib.normaldistribution(x));
|
---|
| 435 | Evaluate(interpreter, ds, "(erf " + x + ")", 0, alglib.errorfunction(x));
|
---|
| 436 | };
|
---|
| 437 |
|
---|
| 438 | Action<double> checkGamma = (x) => {
|
---|
| 439 | Evaluate(interpreter, ds, "(gamma " + x + ")", 0, alglib.gammafunction(x));
|
---|
| 440 | };
|
---|
| 441 | Action<double> checkPsi = (x) => {
|
---|
| 442 | try {
|
---|
| 443 | Evaluate(interpreter, ds, "(psi " + x + ")", 0, alglib.psi(x));
|
---|
| 444 | }
|
---|
[9828] | 445 | catch (alglib.alglibexception) { // ignore cases where alglib throws an exception
|
---|
| 446 | }
|
---|
[8123] | 447 | };
|
---|
| 448 | Action<double> checkDawson = (x) => {
|
---|
| 449 | Evaluate(interpreter, ds, "(dawson " + x + ")", 0, alglib.dawsonintegral(x));
|
---|
| 450 | };
|
---|
| 451 | Action<double> checkExpInt = (x) => {
|
---|
| 452 | Evaluate(interpreter, ds, "(expint " + x + ")", 0, alglib.exponentialintegralei(x));
|
---|
| 453 | };
|
---|
| 454 |
|
---|
| 455 |
|
---|
| 456 |
|
---|
| 457 | foreach (var e in new[] { -2.0, -1.0, 0.0, 1.0, 2.0 }) {
|
---|
| 458 | checkAiry(e);
|
---|
| 459 | checkBessel(e);
|
---|
| 460 | checkSinCosIntegrals(e);
|
---|
| 461 | checkGamma(e);
|
---|
| 462 | checkExpInt(e);
|
---|
| 463 | checkDawson(e);
|
---|
| 464 | checkPsi(e);
|
---|
| 465 | checkNormErf(e);
|
---|
| 466 | checkFresnelSinCosIntegrals(e);
|
---|
| 467 | checkHypSinCosIntegrals(e);
|
---|
| 468 | }
|
---|
| 469 | }
|
---|
[5574] | 470 | }
|
---|
| 471 |
|
---|
[6732] | 472 | private void Evaluate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, Dataset ds, string expr, int index, double expected) {
|
---|
[5574] | 473 | var importer = new SymbolicExpressionImporter();
|
---|
| 474 | ISymbolicExpressionTree tree = importer.Import(expr);
|
---|
| 475 |
|
---|
| 476 | double actual = interpreter.GetSymbolicExpressionTreeValues(tree, ds, Enumerable.Range(index, 1)).First();
|
---|
| 477 |
|
---|
[6809] | 478 | Assert.IsFalse(double.IsNaN(actual) && !double.IsNaN(expected));
|
---|
| 479 | Assert.IsFalse(!double.IsNaN(actual) && double.IsNaN(expected));
|
---|
[5574] | 480 | Assert.AreEqual(expected, actual, 1.0E-12, expr);
|
---|
| 481 | }
|
---|
| 482 | }
|
---|
| 483 | }
|
---|