[5574] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2011 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.Random;
|
---|
| 28 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
| 29 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Tests {
|
---|
| 30 |
|
---|
| 31 |
|
---|
| 32 | /// <summary>
|
---|
| 33 | ///This is a test class for SimpleArithmeticExpressionInterpreter and is intended
|
---|
| 34 | ///to contain all SimpleArithmeticExpressionInterpreter Unit Tests
|
---|
| 35 | ///</summary>
|
---|
| 36 | [TestClass()]
|
---|
| 37 | public class SymbolicDataAnalysisExpressionTreeInterpreterTest {
|
---|
| 38 | private const int N = 1000;
|
---|
| 39 | private const int Rows = 1000;
|
---|
| 40 | private const int Columns = 50;
|
---|
| 41 | private TestContext testContextInstance;
|
---|
| 42 |
|
---|
| 43 | /// <summary>
|
---|
| 44 | ///Gets or sets the test context which provides
|
---|
| 45 | ///information about and functionality for the current test run.
|
---|
| 46 | ///</summary>
|
---|
| 47 | public TestContext TestContext {
|
---|
| 48 | get {
|
---|
| 49 | return testContextInstance;
|
---|
| 50 | }
|
---|
| 51 | set {
|
---|
| 52 | testContextInstance = value;
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | [TestMethod]
|
---|
| 57 | public void FullGrammarSimpleArithmeticExpressionInterpreterPerformanceTest() {
|
---|
| 58 | var twister = new MersenneTwister(31415);
|
---|
| 59 | var dataset = Util.CreateRandomDataset(twister, Rows, Columns);
|
---|
[5686] | 60 | var grammar = new FullFunctionalExpressionGrammar();
|
---|
| 61 | grammar.MaximumFunctionArguments = 0;
|
---|
| 62 | grammar.MaximumFunctionDefinitions = 0;
|
---|
| 63 | grammar.MinimumFunctionArguments = 0;
|
---|
| 64 | grammar.MinimumFunctionDefinitions = 0;
|
---|
[5574] | 65 | var randomTrees = Util.CreateRandomTrees(twister, dataset, grammar, N, 1, 100, 0, 0);
|
---|
| 66 | double[] estimation = new double[Rows];
|
---|
| 67 | foreach (ISymbolicExpressionTree tree in randomTrees) {
|
---|
| 68 | Util.InitTree(tree, twister, new List<string>(dataset.VariableNames));
|
---|
| 69 | }
|
---|
| 70 | SymbolicDataAnalysisExpressionTreeInterpreter interpreter = new SymbolicDataAnalysisExpressionTreeInterpreter();
|
---|
| 71 | double nodesPerSec = Util.CalculateEvaluatedNodesPerSec(randomTrees, interpreter, dataset, 3);
|
---|
[5792] | 72 | Assert.IsTrue(nodesPerSec > 12.5e6); // evaluated nodes per seconds must be larger than 15mNodes/sec
|
---|
[5574] | 73 | }
|
---|
| 74 |
|
---|
| 75 | [TestMethod]
|
---|
| 76 | public void ArithmeticGrammarSimpleArithmeticExpressionInterpreterPerformanceTest() {
|
---|
| 77 | var twister = new MersenneTwister(31415);
|
---|
| 78 | var dataset = Util.CreateRandomDataset(twister, Rows, Columns);
|
---|
[5686] | 79 | var grammar = new ArithmeticExpressionGrammar();
|
---|
| 80 | grammar.MaximumFunctionArguments = 0;
|
---|
| 81 | grammar.MaximumFunctionDefinitions = 0;
|
---|
| 82 | grammar.MinimumFunctionArguments = 0;
|
---|
| 83 | grammar.MinimumFunctionDefinitions = 0;
|
---|
[5574] | 84 | var randomTrees = Util.CreateRandomTrees(twister, dataset, grammar, N, 1, 100, 0, 0);
|
---|
| 85 | double[] estimation = new double[Rows];
|
---|
| 86 | foreach (SymbolicExpressionTree tree in randomTrees) {
|
---|
| 87 | Util.InitTree(tree, twister, new List<string>(dataset.VariableNames));
|
---|
| 88 | }
|
---|
| 89 | SymbolicDataAnalysisExpressionTreeInterpreter interpreter = new SymbolicDataAnalysisExpressionTreeInterpreter();
|
---|
| 90 | double nodesPerSec = Util.CalculateEvaluatedNodesPerSec(randomTrees, interpreter, dataset, 3);
|
---|
[5792] | 91 | Assert.IsTrue(nodesPerSec > 12.5e6); // evaluated nodes per seconds must be larger than 15mNodes/sec
|
---|
[5574] | 92 | }
|
---|
| 93 |
|
---|
| 94 |
|
---|
| 95 | /// <summary>
|
---|
| 96 | ///A test for Evaluate
|
---|
| 97 | ///</summary>
|
---|
| 98 | [TestMethod]
|
---|
| 99 | public void SimpleArithmeticExpressionInterpreterEvaluateTest() {
|
---|
| 100 |
|
---|
| 101 | Dataset ds = new Dataset(new string[] { "Y", "A", "B" }, new double[,] {
|
---|
| 102 | { 1.0, 1.0, 1.0 },
|
---|
| 103 | { 2.0, 2.0, 2.0 },
|
---|
| 104 | { 3.0, 1.0, 2.0 }
|
---|
| 105 | });
|
---|
| 106 |
|
---|
| 107 | SymbolicDataAnalysisExpressionTreeInterpreter interpreter = new SymbolicDataAnalysisExpressionTreeInterpreter();
|
---|
| 108 |
|
---|
| 109 | // constants
|
---|
| 110 | Evaluate(interpreter, ds, "(+ 1.5 3.5)", 0, 5.0);
|
---|
| 111 |
|
---|
| 112 | // variables
|
---|
| 113 | Evaluate(interpreter, ds, "(variable 2.0 a)", 0, 2.0);
|
---|
| 114 | Evaluate(interpreter, ds, "(variable 2.0 a)", 1, 4.0);
|
---|
| 115 |
|
---|
| 116 |
|
---|
| 117 | // addition
|
---|
| 118 | Evaluate(interpreter, ds, "(+ (variable 2.0 a ))", 1, 4.0);
|
---|
| 119 | Evaluate(interpreter, ds, "(+ (variable 2.0 a ) (variable 3.0 b ))", 0, 5.0);
|
---|
| 120 | Evaluate(interpreter, ds, "(+ (variable 2.0 a ) (variable 3.0 b ))", 1, 10.0);
|
---|
| 121 | Evaluate(interpreter, ds, "(+ (variable 2.0 a) (variable 3.0 b ))", 2, 8.0);
|
---|
| 122 | Evaluate(interpreter, ds, "(+ 8.0 2.0 2.0)", 0, 12.0);
|
---|
| 123 |
|
---|
| 124 | // subtraction
|
---|
| 125 | Evaluate(interpreter, ds, "(- (variable 2.0 a ))", 1, -4.0);
|
---|
| 126 | Evaluate(interpreter, ds, "(- (variable 2.0 a ) (variable 3.0 b))", 0, -1.0);
|
---|
| 127 | Evaluate(interpreter, ds, "(- (variable 2.0 a ) (variable 3.0 b ))", 1, -2.0);
|
---|
| 128 | Evaluate(interpreter, ds, "(- (variable 2.0 a ) (variable 3.0 b ))", 2, -4.0);
|
---|
| 129 | Evaluate(interpreter, ds, "(- 8.0 2.0 2.0)", 0, 4.0);
|
---|
| 130 |
|
---|
| 131 | // multiplication
|
---|
| 132 | Evaluate(interpreter, ds, "(* (variable 2.0 a ))", 0, 2.0);
|
---|
| 133 | Evaluate(interpreter, ds, "(* (variable 2.0 a ) (variable 3.0 b ))", 0, 6.0);
|
---|
| 134 | Evaluate(interpreter, ds, "(* (variable 2.0 a ) (variable 3.0 b ))", 1, 24.0);
|
---|
| 135 | Evaluate(interpreter, ds, "(* (variable 2.0 a ) (variable 3.0 b ))", 2, 12.0);
|
---|
| 136 | Evaluate(interpreter, ds, "(* 8.0 2.0 2.0)", 0, 32.0);
|
---|
| 137 |
|
---|
| 138 | // division
|
---|
| 139 | Evaluate(interpreter, ds, "(/ (variable 2.0 a ))", 1, 1.0 / 4.0);
|
---|
| 140 | Evaluate(interpreter, ds, "(/ (variable 2.0 a ) 2.0)", 0, 1.0);
|
---|
| 141 | Evaluate(interpreter, ds, "(/ (variable 2.0 a ) 2.0)", 1, 2.0);
|
---|
| 142 | Evaluate(interpreter, ds, "(/ (variable 3.0 b ) 2.0)", 2, 3.0);
|
---|
| 143 | Evaluate(interpreter, ds, "(/ 8.0 2.0 2.0)", 0, 2.0);
|
---|
| 144 |
|
---|
| 145 | // gt
|
---|
| 146 | Evaluate(interpreter, ds, "(> (variable 2.0 a) 2.0)", 0, -1.0);
|
---|
| 147 | Evaluate(interpreter, ds, "(> 2.0 (variable 2.0 a))", 0, -1.0);
|
---|
| 148 | Evaluate(interpreter, ds, "(> (variable 2.0 a) 1.9)", 0, 1.0);
|
---|
| 149 | Evaluate(interpreter, ds, "(> 1.9 (variable 2.0 a))", 0, -1.0);
|
---|
| 150 | //Evaluate(interpreter, ds, "(> (sqrt -1.0) (log -1.0))", 0, -1.0); // (> nan nan) should be false
|
---|
| 151 |
|
---|
| 152 | // lt
|
---|
| 153 | Evaluate(interpreter, ds, "(< (variable 2.0 a) 2.0)", 0, -1.0);
|
---|
| 154 | Evaluate(interpreter, ds, "(< 2.0 (variable 2.0 a))", 0, -1.0);
|
---|
| 155 | Evaluate(interpreter, ds, "(< (variable 2.0 a) 1.9)", 0, -1.0);
|
---|
| 156 | Evaluate(interpreter, ds, "(< 1.9 (variable 2.0 a))", 0, 1.0);
|
---|
| 157 | //Evaluate(interpreter, ds, "(< (sqrt -1,0) (log -1,0))", 0, -1.0); // (< nan nan) should be false
|
---|
| 158 |
|
---|
| 159 | // If
|
---|
| 160 | Evaluate(interpreter, ds, "(if -10.0 2.0 3.0)", 0, 3.0);
|
---|
| 161 | Evaluate(interpreter, ds, "(if -1.0 2.0 3.0)", 0, 3.0);
|
---|
| 162 | Evaluate(interpreter, ds, "(if 0.0 2.0 3.0)", 0, 3.0);
|
---|
| 163 | Evaluate(interpreter, ds, "(if 1.0 2.0 3.0)", 0, 2.0);
|
---|
| 164 | Evaluate(interpreter, ds, "(if 10.0 2.0 3.0)", 0, 2.0);
|
---|
| 165 | // Evaluate(interpreter, ds, "(if (sqrt -1.0) 2.0 3.0)", 0, 3.0); // if(nan) should return the else branch
|
---|
| 166 |
|
---|
| 167 | // NOT
|
---|
| 168 | Evaluate(interpreter, ds, "(not -1.0)", 0, 1.0);
|
---|
| 169 | Evaluate(interpreter, ds, "(not -2.0)", 0, 1.0);
|
---|
| 170 | Evaluate(interpreter, ds, "(not 1.0)", 0, -1.0);
|
---|
| 171 | Evaluate(interpreter, ds, "(not 2.0)", 0, -1.0);
|
---|
| 172 | Evaluate(interpreter, ds, "(not 0.0)", 0, 1.0);
|
---|
| 173 |
|
---|
| 174 | // AND
|
---|
| 175 | Evaluate(interpreter, ds, "(and -1.0 -2.0)", 0, -1.0);
|
---|
| 176 | Evaluate(interpreter, ds, "(and -1.0 2.0)", 0, -1.0);
|
---|
| 177 | Evaluate(interpreter, ds, "(and 1.0 -2.0)", 0, -1.0);
|
---|
| 178 | Evaluate(interpreter, ds, "(and 1.0 0.0)", 0, -1.0);
|
---|
| 179 | Evaluate(interpreter, ds, "(and 0.0 0.0)", 0, -1.0);
|
---|
| 180 | Evaluate(interpreter, ds, "(and 1.0 2.0)", 0, 1.0);
|
---|
| 181 | Evaluate(interpreter, ds, "(and 1.0 2.0 3.0)", 0, 1.0);
|
---|
| 182 | Evaluate(interpreter, ds, "(and 1.0 -2.0 3.0)", 0, -1.0);
|
---|
| 183 |
|
---|
| 184 | // OR
|
---|
| 185 | Evaluate(interpreter, ds, "(or -1.0 -2.0)", 0, -1.0);
|
---|
| 186 | Evaluate(interpreter, ds, "(or -1.0 2.0)", 0, 1.0);
|
---|
| 187 | Evaluate(interpreter, ds, "(or 1.0 -2.0)", 0, 1.0);
|
---|
| 188 | Evaluate(interpreter, ds, "(or 1.0 2.0)", 0, 1.0);
|
---|
| 189 | Evaluate(interpreter, ds, "(or 0.0 0.0)", 0, -1.0);
|
---|
| 190 | Evaluate(interpreter, ds, "(or -1.0 -2.0 -3.0)", 0, -1.0);
|
---|
| 191 | Evaluate(interpreter, ds, "(or -1.0 -2.0 3.0)", 0, 1.0);
|
---|
| 192 |
|
---|
| 193 | // sin, cos, tan
|
---|
| 194 | Evaluate(interpreter, ds, "(sin " + Math.PI.ToString(NumberFormatInfo.InvariantInfo) + ")", 0, 0.0);
|
---|
| 195 | Evaluate(interpreter, ds, "(sin 0.0)", 0, 0.0);
|
---|
| 196 | Evaluate(interpreter, ds, "(cos " + Math.PI.ToString(NumberFormatInfo.InvariantInfo) + ")", 0, -1.0);
|
---|
| 197 | Evaluate(interpreter, ds, "(cos 0.0)", 0, 1.0);
|
---|
| 198 | Evaluate(interpreter, ds, "(tan " + Math.PI.ToString(NumberFormatInfo.InvariantInfo) + ")", 0, Math.Tan(Math.PI));
|
---|
| 199 | Evaluate(interpreter, ds, "(tan 0.0)", 0, Math.Tan(Math.PI));
|
---|
| 200 |
|
---|
| 201 | // exp, log
|
---|
| 202 | Evaluate(interpreter, ds, "(log (exp 7.0))", 0, Math.Log(Math.Exp(7)));
|
---|
| 203 | Evaluate(interpreter, ds, "(exp (log 7.0))", 0, Math.Exp(Math.Log(7)));
|
---|
| 204 | Evaluate(interpreter, ds, "(log -3.0)", 0, Math.Log(-3));
|
---|
| 205 |
|
---|
| 206 | // mean
|
---|
| 207 | Evaluate(interpreter, ds, "(mean -1.0 1.0 -1.0)", 0, -1.0 / 3.0);
|
---|
| 208 |
|
---|
| 209 | // ADF
|
---|
| 210 | Evaluate(interpreter, ds, @"(PROG
|
---|
| 211 | (MAIN
|
---|
| 212 | (CALL ADF0))
|
---|
| 213 | (defun ADF0 1.0))", 1, 1.0);
|
---|
| 214 | Evaluate(interpreter, ds, @"(PROG
|
---|
| 215 | (MAIN
|
---|
| 216 | (* (CALL ADF0) (CALL ADF0)))
|
---|
| 217 | (defun ADF0 2.0))", 1, 4.0);
|
---|
| 218 | Evaluate(interpreter, ds, @"(PROG
|
---|
| 219 | (MAIN
|
---|
| 220 | (CALL ADF0 2.0 3.0))
|
---|
| 221 | (defun ADF0
|
---|
| 222 | (+ (ARG 0) (ARG 1))))", 1, 5.0);
|
---|
| 223 | Evaluate(interpreter, ds, @"(PROG
|
---|
| 224 | (MAIN (CALL ADF1 2.0 3.0))
|
---|
| 225 | (defun ADF0
|
---|
| 226 | (- (ARG 1) (ARG 0)))
|
---|
| 227 | (defun ADF1
|
---|
| 228 | (+ (CALL ADF0 (ARG 1) (ARG 0))
|
---|
| 229 | (CALL ADF0 (ARG 0) (ARG 1)))))", 1, 0.0);
|
---|
| 230 | Evaluate(interpreter, ds, @"(PROG
|
---|
| 231 | (MAIN (CALL ADF1 (variable 2.0 a) 3.0))
|
---|
| 232 | (defun ADF0
|
---|
| 233 | (- (ARG 1) (ARG 0)))
|
---|
| 234 | (defun ADF1
|
---|
| 235 | (CALL ADF0 (ARG 1) (ARG 0))))", 1, 1.0);
|
---|
| 236 | Evaluate(interpreter, ds, @"(PROG
|
---|
| 237 | (MAIN (CALL ADF1 (variable 2.0 a) 3.0))
|
---|
| 238 | (defun ADF0
|
---|
| 239 | (- (ARG 1) (ARG 0)))
|
---|
| 240 | (defun ADF1
|
---|
| 241 | (+ (CALL ADF0 (ARG 1) (ARG 0))
|
---|
| 242 | (CALL ADF0 (ARG 0) (ARG 1)))))", 1, 0.0);
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | private void Evaluate(SymbolicDataAnalysisExpressionTreeInterpreter interpreter, Dataset ds, string expr, int index, double expected) {
|
---|
| 246 | var importer = new SymbolicExpressionImporter();
|
---|
| 247 | ISymbolicExpressionTree tree = importer.Import(expr);
|
---|
| 248 |
|
---|
| 249 | double actual = interpreter.GetSymbolicExpressionTreeValues(tree, ds, Enumerable.Range(index, 1)).First();
|
---|
| 250 |
|
---|
| 251 | Assert.AreEqual(expected, actual, 1.0E-12, expr);
|
---|
| 252 | }
|
---|
| 253 | }
|
---|
| 254 | }
|
---|