1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2008 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 HeuristicLab.GP.StructureIdentification;
|
---|
23 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
24 | using HeuristicLab.GP.Interfaces;
|
---|
25 | using System.IO;
|
---|
26 | using HeuristicLab.DataAnalysis;
|
---|
27 | using System;
|
---|
28 | using HeuristicLab.Random;
|
---|
29 | using System.Collections.Generic;
|
---|
30 | using HeuristicLab.GP.Operators;
|
---|
31 | using System.Diagnostics;
|
---|
32 | namespace HeuristicLab.GP.Test {
|
---|
33 |
|
---|
34 |
|
---|
35 | /// <summary>
|
---|
36 | ///This is a test class for HL3TreeEvaluatorTest and is intended
|
---|
37 | ///to contain all HL3TreeEvaluatorTest Unit Tests
|
---|
38 | ///</summary>
|
---|
39 | [TestClass()]
|
---|
40 | public class HL3TreeEvaluatorTest {
|
---|
41 | private const int N = 1000;
|
---|
42 | private const int Rows = 1000;
|
---|
43 | private const int Columns = 50;
|
---|
44 | private static IFunctionTree[] randomTrees;
|
---|
45 | private static Dataset dataset;
|
---|
46 | private static MersenneTwister twister;
|
---|
47 | private TestContext testContextInstance;
|
---|
48 |
|
---|
49 | /// <summary>
|
---|
50 | ///Gets or sets the test context which provides
|
---|
51 | ///information about and functionality for the current test run.
|
---|
52 | ///</summary>
|
---|
53 | public TestContext TestContext {
|
---|
54 | get {
|
---|
55 | return testContextInstance;
|
---|
56 | }
|
---|
57 | set {
|
---|
58 | testContextInstance = value;
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | [ClassInitialize()]
|
---|
63 | public static void CreateRandomTrees(TestContext testContext) {
|
---|
64 | twister = new MersenneTwister();
|
---|
65 | dataset = Util.CreateRandomDataset(twister, Rows, Columns);
|
---|
66 | randomTrees = Util.CreateRandomTrees(twister, dataset, N, 1, 100);
|
---|
67 | }
|
---|
68 |
|
---|
69 |
|
---|
70 | [TestMethod()]
|
---|
71 | public void PerformanceTest() {
|
---|
72 | double[] estimation = new double[Rows];
|
---|
73 | foreach (IFunctionTree tree in randomTrees) {
|
---|
74 | Util.InitTree(tree, twister, new List<string>(dataset.VariableNames));
|
---|
75 | }
|
---|
76 | HL3TreeEvaluator hl3TreeEvaluator = new HL3TreeEvaluator();
|
---|
77 | Util.EvaluateTrees(randomTrees, hl3TreeEvaluator, dataset, 10);
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | /// <summary>
|
---|
82 | ///A test for Evaluate
|
---|
83 | ///</summary>
|
---|
84 | [TestMethod()]
|
---|
85 | [DeploymentItem("HeuristicLab.GP.StructureIdentification-3.3.dll")]
|
---|
86 | public void HL3TreeEvaluatorEvaluateTest() {
|
---|
87 |
|
---|
88 | Dataset ds = new Dataset(new double[,] {
|
---|
89 | { 1.0, 1.0, 1.0 },
|
---|
90 | { 2.0, 2.0, 2.0 },
|
---|
91 | { 3.0, 1.0, 2.0 }
|
---|
92 | });
|
---|
93 |
|
---|
94 | ds.SetVariableName(0, "y");
|
---|
95 | ds.SetVariableName(1, "a");
|
---|
96 | ds.SetVariableName(2, "b");
|
---|
97 |
|
---|
98 | HL3TreeEvaluator evaluator = new HL3TreeEvaluator();
|
---|
99 |
|
---|
100 | // constants
|
---|
101 | Evaluate(evaluator, ds, "(+ 1,5 3,5)", 0, 5.0);
|
---|
102 |
|
---|
103 | // variables
|
---|
104 | Evaluate(evaluator, ds, "(variable 2,0 a 0)", 0, 2.0);
|
---|
105 | Evaluate(evaluator, ds, "(variable 2,0 a 0)", 1, 4.0);
|
---|
106 | Evaluate(evaluator, ds, "(variable 2,0 a -1)", 1, 2.0);
|
---|
107 | Evaluate(evaluator, ds, "(variable 2,0 a -2)", 2, 2.0);
|
---|
108 |
|
---|
109 | // differentials
|
---|
110 | Evaluate(evaluator, ds, "(differential 2,0 a 0)", 1, 2.0);
|
---|
111 | Evaluate(evaluator, ds, "(differential 2,0 a 0)", 2, -2.0);
|
---|
112 | Evaluate(evaluator, ds, "(differential 2,0 a -1)", 2, 2.0);
|
---|
113 | Evaluate(evaluator, ds, "(differential 2,0 a -2)", 3, 2.0);
|
---|
114 |
|
---|
115 | // addition
|
---|
116 | Evaluate(evaluator, ds, "(+ (variable 2,0 a 0))", 1, 4.0);
|
---|
117 | Evaluate(evaluator, ds, "(+ (variable 2,0 a 0) (variable 3,0 b 0))", 0, 5.0);
|
---|
118 | Evaluate(evaluator, ds, "(+ (variable 2,0 a 0) (variable 3,0 b 0))", 1, 10.0);
|
---|
119 | Evaluate(evaluator, ds, "(+ (variable 2,0 a 0) (variable 3,0 b 0))", 2, 8.0);
|
---|
120 | Evaluate(evaluator, ds, "(+ 8,0 2,0 2,0)", 0, 12.0);
|
---|
121 |
|
---|
122 | // subtraction
|
---|
123 | Evaluate(evaluator, ds, "(- (variable 2,0 a 0))", 1, -4.0);
|
---|
124 | Evaluate(evaluator, ds, "(- (variable 2,0 a 0) (variable 3,0 b 0))", 0, -1.0);
|
---|
125 | Evaluate(evaluator, ds, "(- (variable 2,0 a 0) (variable 3,0 b 0))", 1, -2.0);
|
---|
126 | Evaluate(evaluator, ds, "(- (variable 2,0 a 0) (variable 3,0 b 0))", 2, -4.0);
|
---|
127 | Evaluate(evaluator, ds, "(- 8,0 2,0 2,0)", 0, 4.0);
|
---|
128 |
|
---|
129 | // multiplication
|
---|
130 | Evaluate(evaluator, ds, "(* (variable 2,0 a 0))", 0, 2.0);
|
---|
131 | Evaluate(evaluator, ds, "(* (variable 2,0 a 0) (variable 3,0 b 0))", 0, 6.0);
|
---|
132 | Evaluate(evaluator, ds, "(* (variable 2,0 a 0) (variable 3,0 b 0))", 1, 24.0);
|
---|
133 | Evaluate(evaluator, ds, "(* (variable 2,0 a 0) (variable 3,0 b 0))", 2, 12.0);
|
---|
134 | Evaluate(evaluator, ds, "(* 8,0 2,0 2,0)", 0, 32.0);
|
---|
135 |
|
---|
136 | // division
|
---|
137 | Evaluate(evaluator, ds, "(/ (variable 2,0 a 0))", 1, 1.0 / 4.0);
|
---|
138 | Evaluate(evaluator, ds, "(/ (variable 2,0 a 0) 2,0)", 0, 1.0);
|
---|
139 | Evaluate(evaluator, ds, "(/ (variable 2,0 a 0) 2,0)", 1, 2.0);
|
---|
140 | Evaluate(evaluator, ds, "(/ (variable 3,0 b 0) 2,0)", 2, 3.0);
|
---|
141 | Evaluate(evaluator, ds, "(/ 8,0 2,0 2,0)", 0, 2.0);
|
---|
142 |
|
---|
143 | // boolean values semantic: false = x <= 0 , true x > 0
|
---|
144 | // equ
|
---|
145 | Evaluate(evaluator, ds, "(equ (variable 2,0 a 0) 2,0)", 0, 1.0);
|
---|
146 | Evaluate(evaluator, ds, "(equ (variable 2,0 a 0) 1,0)", 0, -1.0);
|
---|
147 | Evaluate(evaluator, ds, "(equ (sqrt -1,0) (log -1,0))", 0, -1.0); // (equ nan nan) should be false
|
---|
148 |
|
---|
149 |
|
---|
150 | // gt
|
---|
151 | Evaluate(evaluator, ds, "(> (variable 2,0 a 0) 2,0)", 0, -1.0);
|
---|
152 | Evaluate(evaluator, ds, "(> 2,0 (variable 2,0 a 0))", 0, -1.0);
|
---|
153 | Evaluate(evaluator, ds, "(> (variable 2,0 a 0) 1,9)", 0, 1.0);
|
---|
154 | Evaluate(evaluator, ds, "(> 1,9 (variable 2,0 a 0))", 0, -1.0);
|
---|
155 | Evaluate(evaluator, ds, "(> (sqrt -1,0) (log -1,0))", 0, -1.0); // (> nan nan) should be false
|
---|
156 |
|
---|
157 | // lt
|
---|
158 | Evaluate(evaluator, ds, "(< (variable 2,0 a 0) 2,0)", 0, -1.0);
|
---|
159 | Evaluate(evaluator, ds, "(< 2,0 (variable 2,0 a 0))", 0, -1.0);
|
---|
160 | Evaluate(evaluator, ds, "(< (variable 2,0 a 0) 1,9)", 0, -1.0);
|
---|
161 | Evaluate(evaluator, ds, "(< 1,9 (variable 2,0 a 0))", 0, 1.0);
|
---|
162 | Evaluate(evaluator, ds, "(< (sqrt -1,0) (log -1,0))", 0, -1.0); // (< nan nan) should be false
|
---|
163 |
|
---|
164 | // If
|
---|
165 | Evaluate(evaluator, ds, "(if -10 2,0 3,0)", 0, 3.0);
|
---|
166 | Evaluate(evaluator, ds, "(if -1,0 2,0 3,0)", 0, 3.0);
|
---|
167 | Evaluate(evaluator, ds, "(if 0,0 2,0 3,0)", 0, 3.0);
|
---|
168 | Evaluate(evaluator, ds, "(if 1,0 2,0 3,0)", 0, 2.0);
|
---|
169 | Evaluate(evaluator, ds, "(if 10 2,0 3,0)", 0, 2.0);
|
---|
170 | Evaluate(evaluator, ds, "(if (sqrt -1,0) 2,0 3,0)", 0, 3.0); // if(nan) should return the else branch
|
---|
171 |
|
---|
172 | // signum
|
---|
173 | Evaluate(evaluator, ds, "(sign -1,0)", 0, -1.0);
|
---|
174 | Evaluate(evaluator, ds, "(sign -2,0)", 0, -1.0);
|
---|
175 | Evaluate(evaluator, ds, "(sign 1,0)", 0, 1.0);
|
---|
176 | Evaluate(evaluator, ds, "(sign 2,0)", 0, 1.0);
|
---|
177 | Evaluate(evaluator, ds, "(sign 0,0)", 0, 0.0);
|
---|
178 |
|
---|
179 | // NOT
|
---|
180 | Evaluate(evaluator, ds, "(not -1,0)", 0, 1.0);
|
---|
181 | Evaluate(evaluator, ds, "(not -2,0)", 0, 2.0);
|
---|
182 | Evaluate(evaluator, ds, "(not 1,0)", 0, -1.0);
|
---|
183 | Evaluate(evaluator, ds, "(not 2,0)", 0, -2.0);
|
---|
184 | Evaluate(evaluator, ds, "(not 0,0)", 0, 0.0);
|
---|
185 |
|
---|
186 | // AND
|
---|
187 | Evaluate(evaluator, ds, "(and -1,0 -2,0)", 0, -1.0);
|
---|
188 | Evaluate(evaluator, ds, "(and -1,0 2,0)", 0, -1.0);
|
---|
189 | Evaluate(evaluator, ds, "(and 1,0 -2,0)", 0, -1.0);
|
---|
190 | Evaluate(evaluator, ds, "(and 1,0 0,0)", 0, -1.0);
|
---|
191 | Evaluate(evaluator, ds, "(and 0,0 0,0)", 0, -1.0);
|
---|
192 | Evaluate(evaluator, ds, "(and 1,0 2,0)", 0, 1.0);
|
---|
193 | Evaluate(evaluator, ds, "(and 1,0 2,0 3,0)", 0, 1.0);
|
---|
194 | Evaluate(evaluator, ds, "(and 1,0 -2,0 3,0)", 0, -1.0);
|
---|
195 |
|
---|
196 | // OR
|
---|
197 | Evaluate(evaluator, ds, "(or -1,0 -2,0)", 0, -1.0);
|
---|
198 | Evaluate(evaluator, ds, "(or -1,0 2,0)", 0, 1.0);
|
---|
199 | Evaluate(evaluator, ds, "(or 1,0 -2,0)", 0, 1.0);
|
---|
200 | Evaluate(evaluator, ds, "(or 1,0 2,0)", 0, 1.0);
|
---|
201 | Evaluate(evaluator, ds, "(or 0,0 0,0)", 0, -1.0);
|
---|
202 | Evaluate(evaluator, ds, "(or -1,0 -2,0 -3,0)", 0, -1.0);
|
---|
203 | Evaluate(evaluator, ds, "(or -1,0 -2,0 3,0)", 0, 1.0);
|
---|
204 |
|
---|
205 | // XOR
|
---|
206 | Evaluate(evaluator, ds, "(xor -1,0 -2,0)", 0, -1.0);
|
---|
207 | Evaluate(evaluator, ds, "(xor -1,0 2,0)", 0, 1.0);
|
---|
208 | Evaluate(evaluator, ds, "(xor 1,0 -2,0)", 0, 1.0);
|
---|
209 | Evaluate(evaluator, ds, "(xor 1,0 2,0)", 0, -1.0);
|
---|
210 | Evaluate(evaluator, ds, "(xor 0,0 0,0)", 0, -1.0);
|
---|
211 |
|
---|
212 | // sin, cos, tan
|
---|
213 | Evaluate(evaluator, ds, "(sin " + Math.PI + ")", 0, 0.0);
|
---|
214 | Evaluate(evaluator, ds, "(sin 0,0)", 0, 0.0);
|
---|
215 | Evaluate(evaluator, ds, "(cos " + Math.PI + ")", 0, -1.0);
|
---|
216 | Evaluate(evaluator, ds, "(cos 0,0)", 0, 1.0);
|
---|
217 | Evaluate(evaluator, ds, "(tan " + Math.PI + ")", 0, Math.Tan(Math.PI));
|
---|
218 | Evaluate(evaluator, ds, "(tan 0,0)", 0, Math.Tan(Math.PI));
|
---|
219 |
|
---|
220 |
|
---|
221 | // expt
|
---|
222 | Evaluate(evaluator, ds, "(expt 2,0 2,0)", 0, 4.0);
|
---|
223 | Evaluate(evaluator, ds, "(expt 2,0 3,0)", 0, 8.0);
|
---|
224 | Evaluate(evaluator, ds, "(expt 3,0 2,0)", 0, 9.0);
|
---|
225 |
|
---|
226 | // exp, log
|
---|
227 | Evaluate(evaluator, ds, "(log (exp 7,0))", 0, Math.Log(Math.Exp(7)));
|
---|
228 | Evaluate(evaluator, ds, "(exp (log 7,0))", 0, Math.Exp(Math.Log(7)));
|
---|
229 | Evaluate(evaluator, ds, "(log -3,0)", 0, Math.Log(-3));
|
---|
230 |
|
---|
231 | // sqrt
|
---|
232 | Evaluate(evaluator, ds, "(sqrt 4,0)", 0, 2.0);
|
---|
233 | Evaluate(evaluator, ds, "(sqrt (sqrt 16,0))", 0, 2.0);
|
---|
234 |
|
---|
235 | }
|
---|
236 |
|
---|
237 | private void Evaluate(HL3TreeEvaluator evaluator, Dataset ds, string expr, int index, double expected) {
|
---|
238 | var importer = new SymbolicExpressionImporter();
|
---|
239 | IFunctionTree tree = importer.Import(expr);
|
---|
240 |
|
---|
241 | evaluator.PrepareForEvaluation(ds, tree);
|
---|
242 |
|
---|
243 | double actual = evaluator.Evaluate(index);
|
---|
244 | Assert.AreEqual(expected, actual, 1.0E-12, expr);
|
---|
245 | }
|
---|
246 | }
|
---|
247 | }
|
---|