1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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.IO;
|
---|
23 | using System;
|
---|
24 | using HeuristicLab.Random;
|
---|
25 | using System.Collections.Generic;
|
---|
26 | using System.Diagnostics;
|
---|
27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
28 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
29 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
30 | using System.Linq;
|
---|
31 | namespace HeuristicLab.Problems.DataAnalysis.Tests {
|
---|
32 |
|
---|
33 |
|
---|
34 | /// <summary>
|
---|
35 | ///This is a test class for SimpleArithmeticExpressionInterpreter and is intended
|
---|
36 | ///to contain all SimpleArithmeticExpressionInterpreter Unit Tests
|
---|
37 | ///</summary>
|
---|
38 | [TestClass()]
|
---|
39 | public class SimpleArithmeticExpressionInterpreterTest {
|
---|
40 | private const int N = 1000;
|
---|
41 | private const int Rows = 1000;
|
---|
42 | private const int Columns = 50;
|
---|
43 | private static SymbolicExpressionTree[] randomTrees;
|
---|
44 | private static Dataset dataset;
|
---|
45 | private static MersenneTwister twister;
|
---|
46 | private TestContext testContextInstance;
|
---|
47 |
|
---|
48 | /// <summary>
|
---|
49 | ///Gets or sets the test context which provides
|
---|
50 | ///information about and functionality for the current test run.
|
---|
51 | ///</summary>
|
---|
52 | public TestContext TestContext {
|
---|
53 | get {
|
---|
54 | return testContextInstance;
|
---|
55 | }
|
---|
56 | set {
|
---|
57 | testContextInstance = value;
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | [ClassInitialize()]
|
---|
62 | public static void CreateRandomTrees(TestContext testContext) {
|
---|
63 | twister = new MersenneTwister();
|
---|
64 | dataset = Util.CreateRandomDataset(twister, Rows, Columns);
|
---|
65 | var grammar = new GlobalSymbolicExpressionGrammar(new ArithmeticExpressionGrammar());
|
---|
66 | grammar.MaxFunctionArguments = 0;
|
---|
67 | grammar.MaxFunctionDefinitions = 0;
|
---|
68 | grammar.MinFunctionArguments = 0;
|
---|
69 | grammar.MinFunctionDefinitions = 0;
|
---|
70 | randomTrees = Util.CreateRandomTrees(twister, dataset, grammar, N, 1, 100, 0, 0);
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | [TestMethod()]
|
---|
75 | public void SimpleArithmeticExpressionInterpreterPerformanceTest() {
|
---|
76 | double[] estimation = new double[Rows];
|
---|
77 | foreach (SymbolicExpressionTree tree in randomTrees) {
|
---|
78 | Util.InitTree(tree, twister, new List<string>(dataset.VariableNames));
|
---|
79 | }
|
---|
80 | SimpleArithmeticExpressionInterpreter interpreter = new SimpleArithmeticExpressionInterpreter();
|
---|
81 | Util.EvaluateTrees(randomTrees, interpreter, dataset, 10);
|
---|
82 | }
|
---|
83 |
|
---|
84 |
|
---|
85 | /// <summary>
|
---|
86 | ///A test for Evaluate
|
---|
87 | ///</summary>
|
---|
88 | [TestMethod()]
|
---|
89 | public void SimpleArithmeticExpressionInterpreterEvaluateTest() {
|
---|
90 |
|
---|
91 | Dataset ds = new Dataset(new string[] { "Y", "A", "B" }, new double[,] {
|
---|
92 | { 1.0, 1.0, 1.0 },
|
---|
93 | { 2.0, 2.0, 2.0 },
|
---|
94 | { 3.0, 1.0, 2.0 }
|
---|
95 | });
|
---|
96 |
|
---|
97 | SimpleArithmeticExpressionInterpreter interpreter = new SimpleArithmeticExpressionInterpreter();
|
---|
98 |
|
---|
99 | // constants
|
---|
100 | Evaluate(interpreter, ds, "(+ 1.5 3.5)", 0, 5.0);
|
---|
101 |
|
---|
102 | // variables
|
---|
103 | Evaluate(interpreter, ds, "(variable 2.0 a)", 0, 2.0);
|
---|
104 | Evaluate(interpreter, ds, "(variable 2.0 a)", 1, 4.0);
|
---|
105 |
|
---|
106 |
|
---|
107 | // addition
|
---|
108 | Evaluate(interpreter, ds, "(+ (variable 2.0 a ))", 1, 4.0);
|
---|
109 | Evaluate(interpreter, ds, "(+ (variable 2.0 a ) (variable 3.0 b ))", 0, 5.0);
|
---|
110 | Evaluate(interpreter, ds, "(+ (variable 2.0 a ) (variable 3.0 b ))", 1, 10.0);
|
---|
111 | Evaluate(interpreter, ds, "(+ (variable 2.0 a) (variable 3.0 b ))", 2, 8.0);
|
---|
112 | Evaluate(interpreter, ds, "(+ 8.0 2.0 2.0)", 0, 12.0);
|
---|
113 |
|
---|
114 | // subtraction
|
---|
115 | Evaluate(interpreter, ds, "(- (variable 2.0 a ))", 1, -4.0);
|
---|
116 | Evaluate(interpreter, ds, "(- (variable 2.0 a ) (variable 3.0 b))", 0, -1.0);
|
---|
117 | Evaluate(interpreter, ds, "(- (variable 2.0 a ) (variable 3.0 b ))", 1, -2.0);
|
---|
118 | Evaluate(interpreter, ds, "(- (variable 2.0 a ) (variable 3.0 b ))", 2, -4.0);
|
---|
119 | Evaluate(interpreter, ds, "(- 8.0 2.0 2.0)", 0, 4.0);
|
---|
120 |
|
---|
121 | // multiplication
|
---|
122 | Evaluate(interpreter, ds, "(* (variable 2.0 a ))", 0, 2.0);
|
---|
123 | Evaluate(interpreter, ds, "(* (variable 2.0 a ) (variable 3.0 b ))", 0, 6.0);
|
---|
124 | Evaluate(interpreter, ds, "(* (variable 2.0 a ) (variable 3.0 b ))", 1, 24.0);
|
---|
125 | Evaluate(interpreter, ds, "(* (variable 2.0 a ) (variable 3.0 b ))", 2, 12.0);
|
---|
126 | Evaluate(interpreter, ds, "(* 8.0 2.0 2.0)", 0, 32.0);
|
---|
127 |
|
---|
128 | // division
|
---|
129 | Evaluate(interpreter, ds, "(/ (variable 2.0 a ))", 1, 1.0 / 4.0);
|
---|
130 | Evaluate(interpreter, ds, "(/ (variable 2.0 a ) 2.0)", 0, 1.0);
|
---|
131 | Evaluate(interpreter, ds, "(/ (variable 2.0 a ) 2.0)", 1, 2.0);
|
---|
132 | Evaluate(interpreter, ds, "(/ (variable 3.0 b ) 2.0)", 2, 3.0);
|
---|
133 | Evaluate(interpreter, ds, "(/ 8.0 2.0 2.0)", 0, 2.0);
|
---|
134 |
|
---|
135 | // ADF
|
---|
136 | Evaluate(interpreter, ds, @"(PROG
|
---|
137 | (MAIN
|
---|
138 | (CALL ADF0))
|
---|
139 | (defun ADF0 1.0))", 1, 1.0);
|
---|
140 | Evaluate(interpreter, ds, @"(PROG
|
---|
141 | (MAIN
|
---|
142 | (* (CALL ADF0) (CALL ADF0)))
|
---|
143 | (defun ADF0 2.0))", 1, 4.0);
|
---|
144 | Evaluate(interpreter, ds, @"(PROG
|
---|
145 | (MAIN
|
---|
146 | (CALL ADF0 2.0 3.0))
|
---|
147 | (defun ADF0
|
---|
148 | (+ (ARG 0) (ARG 1))))", 1, 5.0);
|
---|
149 | Evaluate(interpreter, ds, @"(PROG
|
---|
150 | (MAIN (CALL ADF1 2.0 3.0))
|
---|
151 | (defun ADF0
|
---|
152 | (- (ARG 1) (ARG 0)))
|
---|
153 | (defun ADF1
|
---|
154 | (+ (CALL ADF0 (ARG 1) (ARG 0))
|
---|
155 | (CALL ADF0 (ARG 0) (ARG 1)))))", 1, 0.0);
|
---|
156 | Evaluate(interpreter, ds, @"(PROG
|
---|
157 | (MAIN (CALL ADF1 (variable 2.0 a) 3.0))
|
---|
158 | (defun ADF0
|
---|
159 | (- (ARG 1) (ARG 0)))
|
---|
160 | (defun ADF1
|
---|
161 | (CALL ADF0 (ARG 1) (ARG 0))))", 1, 1.0);
|
---|
162 | Evaluate(interpreter, ds, @"(PROG
|
---|
163 | (MAIN (CALL ADF1 (variable 2.0 a) 3.0))
|
---|
164 | (defun ADF0
|
---|
165 | (- (ARG 1) (ARG 0)))
|
---|
166 | (defun ADF1
|
---|
167 | (+ (CALL ADF0 (ARG 1) (ARG 0))
|
---|
168 | (CALL ADF0 (ARG 0) (ARG 1)))))", 1, 0.0);
|
---|
169 | }
|
---|
170 |
|
---|
171 | private void Evaluate(SimpleArithmeticExpressionInterpreter interpreter, Dataset ds, string expr, int index, double expected) {
|
---|
172 | var importer = new SymbolicExpressionImporter();
|
---|
173 | SymbolicExpressionTree tree = importer.Import(expr);
|
---|
174 |
|
---|
175 | double actual = interpreter.GetSymbolicExpressionTreeValues(tree, ds, Enumerable.Range(index, 1)).First();
|
---|
176 |
|
---|
177 | Assert.AreEqual(expected, actual, 1.0E-12, expr);
|
---|
178 | }
|
---|
179 | }
|
---|
180 | }
|
---|