1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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 | using System;
|
---|
22 | using System.Globalization;
|
---|
23 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
24 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
25 |
|
---|
26 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Tests {
|
---|
27 |
|
---|
28 | [TestClass()]
|
---|
29 | public class SymbolicDataAnalysisExpressionTreeSimplifierTest {
|
---|
30 |
|
---|
31 | [TestMethod]
|
---|
32 | [TestCategory("Problems.DataAnalysis")]
|
---|
33 | [TestProperty("Time", "short")]
|
---|
34 | public void SimplifierAxiomsTest() {
|
---|
35 | SymbolicExpressionImporter importer = new SymbolicExpressionImporter();
|
---|
36 | TreeSimplifier simplifier = new TreeSimplifier();
|
---|
37 | SymbolicExpressionTreeStringFormatter formatter = new SymbolicExpressionTreeStringFormatter();
|
---|
38 | #region single argument arithmetics
|
---|
39 |
|
---|
40 | AssertEqualAfterSimplification("(+ 1.0)", "1.0");
|
---|
41 | AssertEqualAfterSimplification("(- 1.0)", "-1.0");
|
---|
42 | AssertEqualAfterSimplification("(- (variable 2.0 a))", "(variable -2.0 a)");
|
---|
43 | AssertEqualAfterSimplification("(* 2.0)", "2.0");
|
---|
44 | AssertEqualAfterSimplification("(* (variable 2.0 a))", "(variable 2.0 a)");
|
---|
45 | AssertEqualAfterSimplification("(/ 2.0)", "0.5");
|
---|
46 | AssertEqualAfterSimplification("(/ (variable 2.0 a))", "(/ 1.0 (variable 2.0 a))");
|
---|
47 | #endregion
|
---|
48 |
|
---|
49 | #region aggregation of constants into factors
|
---|
50 | AssertEqualAfterSimplification("(* 2.0 (variable 2.0 a))", "(variable 4.0 a)");
|
---|
51 | AssertEqualAfterSimplification("(/ (variable 2.0 a) 2.0)", "(variable 1.0 a)");
|
---|
52 | AssertEqualAfterSimplification("(/ (variable 2.0 a) (* 2.0 2.0))", "(variable 0.5 a)");
|
---|
53 | #endregion
|
---|
54 |
|
---|
55 | #region constant and variable folding
|
---|
56 | AssertEqualAfterSimplification("(+ 1.0 2.0)", "3.0");
|
---|
57 | AssertEqualAfterSimplification("(+ (variable 2.0 a) (variable 2.0 a))", "(variable 4.0 a)");
|
---|
58 | AssertEqualAfterSimplification("(- (variable 2.0 a) (variable 1.0 a))", "(variable 1.0 a)");
|
---|
59 | AssertEqualAfterSimplification("(* (variable 2.0 a) (variable 2.0 a))", "(* (* (variable 1.0 a) (variable 1.0 a)) 4.0)");
|
---|
60 | AssertEqualAfterSimplification("(/ (variable 1.0 a) (variable 2.0 a))", "0.5");
|
---|
61 | #endregion
|
---|
62 |
|
---|
63 | #region logarithm rules
|
---|
64 |
|
---|
65 | // cancellation
|
---|
66 | AssertEqualAfterSimplification("(log (exp (variable 2.0 a)))", "(variable 2.0 a)");
|
---|
67 | // must not transform logs in this way as we do not know wether both variables are positive
|
---|
68 | AssertEqualAfterSimplification("(log (* (variable 1.0 a) (variable 1.0 b)))", "(log (* (variable 1.0 a) (variable 1.0 b)))");
|
---|
69 | // must not transform logs in this way as we do not know wether both variables are positive
|
---|
70 | AssertEqualAfterSimplification("(log (/ (variable 1.0 a) (variable 1.0 b)))", "(log (/ (variable 1.0 a) (variable 1.0 b)))");
|
---|
71 | #endregion
|
---|
72 |
|
---|
73 | #region exponentiation rules
|
---|
74 | // cancellation
|
---|
75 | AssertEqualAfterSimplification("(exp (log (variable 2.0 a)))", "(variable 2.0 a)");
|
---|
76 | // exp transformation
|
---|
77 | AssertEqualAfterSimplification("(exp (+ (variable 2.0 a) (variable 3.0 b)))", "(* (exp (variable 2.0 a)) (exp (variable 3.0 b)))");
|
---|
78 | // exp transformation
|
---|
79 | AssertEqualAfterSimplification("(exp (- (variable 2.0 a) (variable 3.0 b)))", "(* (exp (variable 2.0 a)) (exp (variable -3.0 b)))");
|
---|
80 | // exp transformation
|
---|
81 | AssertEqualAfterSimplification("(exp (- (variable 2.0 a) (* (variable 3.0 b) (variable 4.0 c))))", "(* (exp (variable 2.0 a)) (exp (* (variable 1.0 b) (variable 1.0 c) -12.0)))");
|
---|
82 | // exp transformation
|
---|
83 | AssertEqualAfterSimplification("(exp (- (variable 2.0 a) (* (variable 3.0 b) (cos (variable 4.0 c)))))", "(* (exp (variable 2.0 a)) (exp (* (variable 1.0 b) (cos (variable 4.0 c)) -3.0)))");
|
---|
84 | #endregion
|
---|
85 |
|
---|
86 | #region power rules
|
---|
87 |
|
---|
88 | // cancellation
|
---|
89 | AssertEqualAfterSimplification("(pow (variable 2.0 a) 0.0)", "1.0");
|
---|
90 | // fixed point
|
---|
91 | AssertEqualAfterSimplification("(pow (variable 2.0 a) 1.0)", "(variable 2.0 a)");
|
---|
92 | // inversion fixed point
|
---|
93 | AssertEqualAfterSimplification("(pow (variable 2.0 a) -1.0)", "(/ 1.0 (variable 2.0 a))");
|
---|
94 | // inversion
|
---|
95 | AssertEqualAfterSimplification("(pow (variable 2.0 a) -2.0)", "(/ 1.0 (pow (variable 2.0 a) 2.0))");
|
---|
96 | // constant folding
|
---|
97 | AssertEqualAfterSimplification("(pow 3.0 2.0)", "9.0");
|
---|
98 | #endregion
|
---|
99 |
|
---|
100 | #region root rules
|
---|
101 | // cancellation
|
---|
102 | AssertEqualAfterSimplification("(root (variable 2.0 a) 0.0)", "1.0");
|
---|
103 | // fixed point
|
---|
104 | AssertEqualAfterSimplification("(root (variable 2.0 a) 1.0)", "(variable 2.0 a)");
|
---|
105 | // inversion fixed point
|
---|
106 | AssertEqualAfterSimplification("(root (variable 2.0 a) -1.0)", "(/ 1.0 (variable 2.0 a))");
|
---|
107 | // inversion
|
---|
108 | AssertEqualAfterSimplification("(root (variable 2.0 a) -2.0)", "(/ 1.0 (root (variable 2.0 a) 2.0))");
|
---|
109 | // constant folding
|
---|
110 | AssertEqualAfterSimplification("(root 9.0 2.0)", "3.0");
|
---|
111 | #endregion
|
---|
112 |
|
---|
113 | #region boolean operations
|
---|
114 | // always true and
|
---|
115 | AssertEqualAfterSimplification("(and 1.0 2.0)", "1.0");
|
---|
116 | // always false and
|
---|
117 | AssertEqualAfterSimplification("(and 1.0 -2.0)", "-1.0");
|
---|
118 | // always true or
|
---|
119 | AssertEqualAfterSimplification("(or -1.0 2.0)", "1.0");
|
---|
120 | // always false or
|
---|
121 | AssertEqualAfterSimplification("(or -1.0 -2.0)", "-1.0");
|
---|
122 | // constant not
|
---|
123 | AssertEqualAfterSimplification("(not -2.0)", "1.0");
|
---|
124 | // constant not
|
---|
125 | AssertEqualAfterSimplification("(not 2.0)", "-1.0");
|
---|
126 | // constant not
|
---|
127 | AssertEqualAfterSimplification("(not 0.0)", "1.0");
|
---|
128 | // nested nots
|
---|
129 | AssertEqualAfterSimplification("(not (not 1.0))", "1.0");
|
---|
130 | // not of non-Boolean argument
|
---|
131 | AssertEqualAfterSimplification("(not (variable 1.0 a))", "(not (> (variable 1.0 a) 0.0))");
|
---|
132 | // not Boolean argument
|
---|
133 | AssertEqualAfterSimplification("(not (and (> (variable 1.0 a) 0.0) (> (variable 1.0 a) 0.0)))", "(not (and (> (variable 1.0 a) 0.0) (> (variable 1.0 a) 0.0)))");
|
---|
134 | #endregion
|
---|
135 |
|
---|
136 | #region conditionals
|
---|
137 | // always false
|
---|
138 | AssertEqualAfterSimplification("(if -1.0 (variable 2.0 a) (variable 3.0 a))", "(variable 3.0 a)");
|
---|
139 | // always true
|
---|
140 | AssertEqualAfterSimplification("(if 1.0 (variable 2.0 a) (variable 3.0 a))", "(variable 2.0 a)");
|
---|
141 | // always false (0.0)
|
---|
142 | AssertEqualAfterSimplification("(if 0.0 (variable 2.0 a) (variable 3.0 a))", "(variable 3.0 a)");
|
---|
143 | // complex constant condition (always false)
|
---|
144 | AssertEqualAfterSimplification("(if (* 1.0 -2.0) (variable 2.0 a) (variable 3.0 a))", "(variable 3.0 a)");
|
---|
145 | // complex constant condition (always false)
|
---|
146 | AssertEqualAfterSimplification("(if (/ (variable 1.0 a) (variable -2.0 a)) (variable 2.0 a) (variable 3.0 a))", "(variable 3.0 a)");
|
---|
147 | // insertion of relational operator
|
---|
148 | AssertEqualAfterSimplification("(if (variable 1.0 a) (variable 2.0 a) (variable 3.0 a))", "(if (> (variable 1.0 a) 0.0) (variable 2.0 a) (variable 3.0 a))");
|
---|
149 | #endregion
|
---|
150 |
|
---|
151 | #region factor variables
|
---|
152 | AssertEqualAfterSimplification("(factor a 1.0)", "(factor a 1.0)");
|
---|
153 | // factor folding
|
---|
154 | AssertEqualAfterSimplification("(+ (factor a 1.0 1.0) (factor a 2.0 3.0))", "(factor a 3.0 4.0)");
|
---|
155 | AssertEqualAfterSimplification("(- (factor a 1.0 1.0) (factor a 2.0 3.0))", "(factor a -1.0 -2.0)");
|
---|
156 | AssertEqualAfterSimplification("(* (factor a 2.0 2.0) (factor a 2.0 3.0))", "(factor a 4.0 6.0)");
|
---|
157 | AssertEqualAfterSimplification("(/ (factor a 2.0 5.0))", "(factor a 0.5 0.2)");
|
---|
158 | AssertEqualAfterSimplification("(/ (factor a 4.0 6.0) (factor a 2.0 3.0))", "(factor a 2.0 2.0)");
|
---|
159 | AssertEqualAfterSimplification("(+ 3.0 (factor a 4.0 6.0))", "(factor a 7.0 9.0)");
|
---|
160 | AssertEqualAfterSimplification("(+ (factor a 4.0 6.0) 3.0)", "(factor a 7.0 9.0)");
|
---|
161 | AssertEqualAfterSimplification("(- 3.0 (factor a 4.0 6.0))", "(factor a -1.0 -3.0)");
|
---|
162 | AssertEqualAfterSimplification("(- (factor a 4.0 6.0) 3.0)", "(factor a 1.0 3.0)");
|
---|
163 | AssertEqualAfterSimplification("(* 2.0 (factor a 4.0 6.0))", "(factor a 8.0 12.0)");
|
---|
164 | AssertEqualAfterSimplification("(* (factor a 4.0 6.0) 2.0)", "(factor a 8.0 12.0)");
|
---|
165 | AssertEqualAfterSimplification("(* (factor a 4.0 6.0) (variable 2.0 a))", "(* (factor a 8.0 12.0) (variable 1.0 a))"); // not possible (a is used as factor and double variable) interpreter will fail
|
---|
166 | AssertEqualAfterSimplification(
|
---|
167 | "(log (factor a 10.0 100.0))",
|
---|
168 | string.Format(CultureInfo.InvariantCulture, "(factor a {0} {1})", Math.Log(10.0), Math.Log(100.0)));
|
---|
169 | AssertEqualAfterSimplification(
|
---|
170 | "(exp (factor a 2.0 3.0))",
|
---|
171 | string.Format(CultureInfo.InvariantCulture, "(factor a {0} {1})", Math.Exp(2.0), Math.Exp(3.0)));
|
---|
172 | AssertEqualAfterSimplification("(sqrt (factor a 9.0 16.0))", "(factor a 3.0 4.0))");
|
---|
173 | AssertEqualAfterSimplification("(sqr (factor a 2.0 3.0))", "(factor a 4.0 9.0))");
|
---|
174 | AssertEqualAfterSimplification("(root (factor a 8.0 27.0) 3)", "(factor a 2.0 3.0))");
|
---|
175 | AssertEqualAfterSimplification("(pow (factor a 2.0 3.0) 3)", "(factor a 8.0 27.0))");
|
---|
176 |
|
---|
177 | AssertEqualAfterSimplification("(sin (factor a 1.0 2.0) )",
|
---|
178 | string.Format(CultureInfo.InvariantCulture, "(factor a {0} {1}))", Math.Sin(1.0), Math.Sin(2.0)));
|
---|
179 | AssertEqualAfterSimplification("(cos (factor a 1.0 2.0) )",
|
---|
180 | string.Format(CultureInfo.InvariantCulture, "(factor a {0} {1}))", Math.Cos(1.0), Math.Cos(2.0)));
|
---|
181 | AssertEqualAfterSimplification("(tan (factor a 1.0 2.0) )",
|
---|
182 | string.Format(CultureInfo.InvariantCulture, "(factor a {0} {1}))", Math.Tan(1.0), Math.Tan(2.0)));
|
---|
183 |
|
---|
184 |
|
---|
185 | AssertEqualAfterSimplification("(binfactor a val 1.0)", "(binfactor a val 1.0)");
|
---|
186 | // binfactor folding
|
---|
187 | AssertEqualAfterSimplification("(+ (binfactor a val 1.0) (binfactor a val 2.0))", "(binfactor a val 3.0)");
|
---|
188 | AssertEqualAfterSimplification("(+ (binfactor a val0 1.0) (binfactor a val1 2.0))", "(+ (binfactor a val0 1.0) (binfactor a val1 2.0))"); // cannot be simplified (different vals)
|
---|
189 | AssertEqualAfterSimplification("(+ (binfactor a val 1.0) (binfactor b val 2.0))", "(+ (binfactor a val 1.0) (binfactor b val 2.0))"); // cannot be simplified (different vars)
|
---|
190 | AssertEqualAfterSimplification("(- (binfactor a val 1.0) (binfactor a val 2.0))", "(binfactor a val -1.0)");
|
---|
191 | AssertEqualAfterSimplification("(* (binfactor a val 2.0) (binfactor a val 3.0))", "(binfactor a val 6.0)");
|
---|
192 | AssertEqualAfterSimplification("(/ (binfactor a val 6.0) (binfactor a val 3.0))", "(/ (binfactor a val 6.0) (binfactor a val 3.0))"); // not allowed! 0/0 for other values than 'val'
|
---|
193 | AssertEqualAfterSimplification("(/ (binfactor a val 4.0))", "(/ 1.0 (binfactor a val 4.0))"); // not allowed!
|
---|
194 |
|
---|
195 | AssertEqualAfterSimplification("(+ 3.0 (binfactor a val 4.0 ))", "(+ (binfactor a val 4.0 ) 3.0))"); // not allowed
|
---|
196 | AssertEqualAfterSimplification("(- 3.0 (binfactor a val 4.0 ))", "(+ (binfactor a val -4.0 ) 3.0)");
|
---|
197 | AssertEqualAfterSimplification("(+ (binfactor a val 4.0 ) 3.0)", "(+ (binfactor a val 4.0 ) 3.0)"); // not allowed
|
---|
198 | AssertEqualAfterSimplification("(- (binfactor a val 4.0 ) 3.0)", "(+ (binfactor a val 4.0 ) -3.0)");
|
---|
199 | AssertEqualAfterSimplification("(* 2.0 (binfactor a val 4.0))", "(binfactor a val 8.0 )");
|
---|
200 | AssertEqualAfterSimplification("(* (binfactor a val 4.0) 2.0)", "(binfactor a val 8.0 )");
|
---|
201 | AssertEqualAfterSimplification("(* (binfactor a val 4.0) (variable 2.0 a))", "(* (binfactor a val 1.0) (variable 1.0 a) 8.0)"); // not possible (a is used as factor and double variable) interpreter will fail
|
---|
202 | AssertEqualAfterSimplification("(log (binfactor a val 10.0))", "(log (binfactor a val 10.0))"); // not allowed (log(0))
|
---|
203 |
|
---|
204 | // exp( binfactor w val=a) = if(val=a) exp(w) else exp(0) = binfactor( (exp(w) - 1) val a) + 1
|
---|
205 | AssertEqualAfterSimplification("(exp (binfactor a val 3.0))",
|
---|
206 | string.Format(CultureInfo.InvariantCulture, "(+ (binfactor a val {0}) 1.0)", Math.Exp(3.0) - 1)
|
---|
207 | );
|
---|
208 | AssertEqualAfterSimplification("(sqrt (binfactor a val 16.0))", "(binfactor a val 4.0))"); // sqrt(0) = 0
|
---|
209 | AssertEqualAfterSimplification("(sqr (binfactor a val 3.0))", "(binfactor a val 9.0))"); // 0*0 = 0
|
---|
210 | AssertEqualAfterSimplification("(root (binfactor a val 27.0) 3)", "(binfactor a val 3.0))");
|
---|
211 | AssertEqualAfterSimplification("(pow (binfactor a val 3.0) 3)", "(binfactor a val 27.0))");
|
---|
212 |
|
---|
213 | AssertEqualAfterSimplification("(sin (binfactor a val 2.0) )",
|
---|
214 | string.Format(CultureInfo.InvariantCulture, "(binfactor a val {0}))", Math.Sin(2.0))); // sin(0) = 0
|
---|
215 | AssertEqualAfterSimplification("(cos (binfactor a val 2.0) )",
|
---|
216 | string.Format(CultureInfo.InvariantCulture, "(+ (binfactor a val {0}) 1.0)", Math.Cos(2.0) - 1)); // cos(0) = 1
|
---|
217 | AssertEqualAfterSimplification("(tan (binfactor a val 2.0) )",
|
---|
218 | string.Format(CultureInfo.InvariantCulture, "(binfactor a val {0}))", Math.Tan(2.0))); // tan(0) = 0
|
---|
219 |
|
---|
220 | // combination of factor and binfactor
|
---|
221 | AssertEqualAfterSimplification("(+ (binfactor a x0 2.0) (factor a 2.0 3.0))", "(factor a 4.0 3.0)");
|
---|
222 | AssertEqualAfterSimplification("(+ (factor a 2.0 3.0) (binfactor a x0 2.0))", "(factor a 4.0 3.0)");
|
---|
223 | AssertEqualAfterSimplification("(* (binfactor a x1 2.0) (factor a 2.0 3.0))", "(binfactor a x1 6.0)"); // all other values have weight zero in binfactor
|
---|
224 | AssertEqualAfterSimplification("(* (factor a 2.0 3.0) (binfactor a x1 2.0))", "(binfactor a x1 6.0)"); // all other values have weight zero in binfactor
|
---|
225 | AssertEqualAfterSimplification("(/ (binfactor a x0 2.0) (factor a 2.0 3.0))", "(binfactor a x0 1.0)");
|
---|
226 | AssertEqualAfterSimplification("(/ (factor a 2.0 3.0) (binfactor a x0 2.0))",
|
---|
227 | string.Format(CultureInfo.InvariantCulture, "(factor a 1.0 {0})", 3.0 / 0.0));
|
---|
228 | AssertEqualAfterSimplification("(- (binfactor a x0 2.0) (factor a 2.0 3.0))", "(factor a 0.0 -3.0)");
|
---|
229 | AssertEqualAfterSimplification("(- (factor a 2.0 3.0) (binfactor a x0 2.0))", "(factor a 0.0 3.0)");
|
---|
230 | #endregion
|
---|
231 | }
|
---|
232 |
|
---|
233 |
|
---|
234 | private void AssertEqualAfterSimplification(string original, string expected) {
|
---|
235 | var simplifier = new TreeSimplifier();
|
---|
236 | var formatter = new SymbolicExpressionTreeStringFormatter();
|
---|
237 | var importer = new SymbolicExpressionImporter();
|
---|
238 | var actualTree = simplifier.Simplify(importer.Import(original));
|
---|
239 | var expectedTree = importer.Import(expected);
|
---|
240 | Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
|
---|
241 |
|
---|
242 | }
|
---|
243 | }
|
---|
244 | }
|
---|
245 |
|
---|