Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tests/SymbolicDataAnalysisExpressionTreeSimplifierTest.cs @ 5681

Last change on this file since 5681 was 5574, checked in by gkronber, 14 years ago

#1418 Added test projects for data-analysis and symbolic data-analysis plugin. Moved grammars to version 3.4. Fixed bugs in interpretation and simplification of 'not' symbols.

File size: 16.2 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27using HeuristicLab.Optimization;
28using HeuristicLab.Problems.DataAnalysis.Symbolic;
29using Microsoft.VisualStudio.TestTools.UnitTesting;
30namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Tests {
31
32  [TestClass()]
33  public class SymbolicDataAnalysisExpressionTreeSimplifierTest {   
34    [TestMethod]
35    public void SimplifierAxiomsTest() {
36      SymbolicExpressionImporter importer = new SymbolicExpressionImporter();
37      SymbolicDataAnalysisExpressionTreeSimplifier simplifier = new SymbolicDataAnalysisExpressionTreeSimplifier();
38      SymbolicExpressionTreeStringFormatter formatter = new SymbolicExpressionTreeStringFormatter();
39      #region single argument arithmetics
40      {
41        var actualTree = simplifier.Simplify(importer.Import("(+ 1.0)"));
42        var expectedTree = importer.Import("1.0");
43        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
44      }
45      {
46        var actualTree = simplifier.Simplify(importer.Import("(+ (variable 2.0 a))"));
47        var expectedTree = importer.Import("(variable 2.0 a)");
48        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
49      }
50      {
51        var actualTree = simplifier.Simplify(importer.Import("(- 1.0)"));
52        var expectedTree = importer.Import("-1.0");
53        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
54      }
55      {
56        var actualTree = simplifier.Simplify(importer.Import("(- (variable 2.0 a))"));
57        var expectedTree = importer.Import("(variable -2.0 a)");
58        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
59      }
60      {
61        var actualTree = simplifier.Simplify(importer.Import("(* 2.0)"));
62        var expectedTree = importer.Import("2.0");
63        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
64      }
65      {
66        var actualTree = simplifier.Simplify(importer.Import("(* (variable 2.0 a))"));
67        var expectedTree = importer.Import("(variable 2.0 a)");
68        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
69      }
70      {
71        var actualTree = simplifier.Simplify(importer.Import("(/ 2.0)"));
72        var expectedTree = importer.Import("0.5");
73        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
74      }
75      {
76        var actualTree = simplifier.Simplify(importer.Import("(/ (variable 2.0 a))"));
77        var expectedTree = importer.Import("(/ 1.0 (variable 2.0 a))");
78        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
79      }
80      #endregion
81      #region aggregation of constants into factors
82      {
83        var actualTree = simplifier.Simplify(importer.Import("(* 2.0 (variable 2.0 a))"));
84        var expectedTree = importer.Import("(variable 4.0 a)");
85        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
86      }
87      {
88        var actualTree = simplifier.Simplify(importer.Import("(/ (variable 2.0 a) 2.0)"));
89        var expectedTree = importer.Import("(variable 1.0 a)");
90        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
91      }
92      {
93        var actualTree = simplifier.Simplify(importer.Import("(/ (variable 2.0 a) (* 2.0 2.0))"));
94        var expectedTree = importer.Import("(variable 0.5 a)");
95        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
96      }
97      #endregion
98      #region constant and variable folding
99      {
100        var actualTree = simplifier.Simplify(importer.Import("(+ 1.0 2.0)"));
101        var expectedTree = importer.Import("3.0");
102        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
103      }
104      {
105        var actualTree = simplifier.Simplify(importer.Import("(+ (variable 2.0 a) (variable 2.0 a))"));
106        var expectedTree = importer.Import("(variable 4.0 a)");
107        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
108      }
109      {
110        var actualTree = simplifier.Simplify(importer.Import("(- (variable 2.0 a) (variable 1.0 a))"));
111        var expectedTree = importer.Import("(variable 1.0 a)");
112        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
113      }
114      {
115        var actualTree = simplifier.Simplify(importer.Import("(* (variable 2.0 a) (variable 2.0 a))"));
116        var expectedTree = importer.Import("(* (* (variable 1.0 a) (variable 1.0 a)) 4.0)");
117        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
118      }
119      {
120        var actualTree = simplifier.Simplify(importer.Import("(/ (variable 1.0 a) (variable 2.0 a))"));
121        var expectedTree = importer.Import("0.5");
122        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
123      }
124      #endregion
125      #region logarithm rules
126      {
127        // cancellation
128        var actualTree = simplifier.Simplify(importer.Import("(log (exp (variable 2.0 a)))"));
129        var expectedTree = importer.Import("(variable 2.0 a)");
130        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
131      }
132      {
133        // log transformation
134        var actualTree = simplifier.Simplify(importer.Import("(log (* (variable 2.0 a) (variable 3.0 b)))"));
135        var expectedTree = importer.Import("(+ (log (variable 1.0 a)) (log (variable 1.0 b)) 1.7918)");
136        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
137      }
138      {
139        // log transformation
140        var actualTree = simplifier.Simplify(importer.Import("(log (/ (variable 2.0 a) (variable 3.0 b)))"));
141        var expectedTree = importer.Import("(- (log (variable 2.0 a)) (log (variable 3.0 b)))");
142        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
143      }
144      #endregion
145      #region exponentiation rules
146      {
147        // cancellation
148        var actualTree = simplifier.Simplify(importer.Import("(exp (log (variable 2.0 a)))"));
149        var expectedTree = importer.Import("(variable 2.0 a)");
150        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
151      }
152      {
153        // exp transformation
154        var actualTree = simplifier.Simplify(importer.Import("(exp (+ (variable 2.0 a) (variable 3.0 b)))"));
155        var expectedTree = importer.Import("(* (exp (variable 2.0 a)) (exp (variable 3.0 b)))");
156        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
157      }
158      {
159        // exp transformation
160        var actualTree = simplifier.Simplify(importer.Import("(exp (- (variable 2.0 a) (variable 3.0 b)))"));
161        var expectedTree = importer.Import("(* (exp (variable 2.0 a)) (exp (variable -3.0 b)))");
162        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
163      }
164      #endregion
165      #region power rules
166      {
167        // cancellation
168        var actualTree = simplifier.Simplify(importer.Import("(pow (variable 2.0 a) 0.0)"));
169        var expectedTree = importer.Import("1.0");
170        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
171      }
172      {
173        // fixed point
174        var actualTree = simplifier.Simplify(importer.Import("(pow (variable 2.0 a) 1.0)"));
175        var expectedTree = importer.Import("(variable 2.0 a)");
176        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
177      }
178      {
179        // inversion fixed point
180        var actualTree = simplifier.Simplify(importer.Import("(pow (variable 2.0 a) -1.0)"));
181        var expectedTree = importer.Import("(/ 1.0 (variable 2.0 a))");
182        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
183      }
184      {
185        // inversion
186        var actualTree = simplifier.Simplify(importer.Import("(pow (variable 2.0 a) -2.0)"));
187        var expectedTree = importer.Import("(/ 1.0 (pow (variable 2.0 a) 2.0))");
188        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
189      }
190      {
191        // constant folding
192        var actualTree = simplifier.Simplify(importer.Import("(pow 3.0 2.0)"));
193        var expectedTree = importer.Import("9.0");
194        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
195      }
196      #endregion
197      #region root rules
198      {
199        // cancellation
200        var actualTree = simplifier.Simplify(importer.Import("(root (variable 2.0 a) 0.0)"));
201        var expectedTree = importer.Import("1.0");
202        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
203      }
204      {
205        // fixed point
206        var actualTree = simplifier.Simplify(importer.Import("(root (variable 2.0 a) 1.0)"));
207        var expectedTree = importer.Import("(variable 2.0 a)");
208        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
209      }
210      {
211        // inversion fixed point
212        var actualTree = simplifier.Simplify(importer.Import("(root (variable 2.0 a) -1.0)"));
213        var expectedTree = importer.Import("(/ 1.0 (variable 2.0 a))");
214        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
215      }
216      {
217        // inversion
218        var actualTree = simplifier.Simplify(importer.Import("(root (variable 2.0 a) -2.0)"));
219        var expectedTree = importer.Import("(/ 1.0 (root (variable 2.0 a) 2.0))");
220        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
221      }
222      {
223        // constant folding
224        var actualTree = simplifier.Simplify(importer.Import("(root 9.0 2.0)"));
225        var expectedTree = importer.Import("3.0");
226        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
227      }
228      #endregion
229      #region boolean operations
230      {
231        // always true and
232        var actualTree = simplifier.Simplify(importer.Import("(and 1.0 2.0)"));
233        var expectedTree = importer.Import("1.0");
234        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
235      }
236      {
237        // always false and
238        var actualTree = simplifier.Simplify(importer.Import("(and 1.0 -2.0)"));
239        var expectedTree = importer.Import("-1.0");
240        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
241      }
242      {
243        // always true or
244        var actualTree = simplifier.Simplify(importer.Import("(or -1.0 2.0)"));
245        var expectedTree = importer.Import("1.0");
246        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
247      }
248      {
249        // always false or
250        var actualTree = simplifier.Simplify(importer.Import("(or -1.0 -2.0)"));
251        var expectedTree = importer.Import("-1.0");
252        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
253      }
254      {
255        // constant not
256        var actualTree = simplifier.Simplify(importer.Import("(not -2.0)"));
257        var expectedTree = importer.Import("1.0");
258        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
259      }
260      {
261        // constant not
262        var actualTree = simplifier.Simplify(importer.Import("(not 2.0)"));
263        var expectedTree = importer.Import("-1.0");
264        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
265      }
266      {
267        // constant not
268        var actualTree = simplifier.Simplify(importer.Import("(not 0.0)"));
269        var expectedTree = importer.Import("1.0");
270        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
271      }
272      {
273        // nested nots
274        var actualTree = simplifier.Simplify(importer.Import("(not (not 1.0))"));
275        var expectedTree = importer.Import("1.0");
276        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
277      }
278      {
279        // not of non-Boolean argument
280        var actualTree = simplifier.Simplify(importer.Import("(not (variable 1.0 a))"));
281        var expectedTree = importer.Import("(not (> (variable 1.0 a) 0.0))");
282        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
283      }
284      {
285        // not Boolean argument
286        var actualTree = simplifier.Simplify(importer.Import("(not (and (> (variable 1.0 a) 0.0) (> (variable 1.0 a) 0.0)))"));
287        var expectedTree = importer.Import("(not (and (> (variable 1.0 a) 0.0) (> (variable 1.0 a) 0.0)))");
288        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
289      }
290      #endregion
291      #region conditionals
292      {
293        // always false
294        var actualTree = simplifier.Simplify(importer.Import("(if -1.0 (variable 2.0 a) (variable 3.0 a))"));
295        var expectedTree = importer.Import("(variable 3.0 a)");
296        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
297      }
298      {
299        // always true
300        var actualTree = simplifier.Simplify(importer.Import("(if 1.0 (variable 2.0 a) (variable 3.0 a))"));
301        var expectedTree = importer.Import("(variable 2.0 a)");
302        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
303      }
304      {
305        // always false (0.0)
306        var actualTree = simplifier.Simplify(importer.Import("(if 0.0 (variable 2.0 a) (variable 3.0 a))"));
307        var expectedTree = importer.Import("(variable 3.0 a)");
308        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
309      }
310      {
311        // complex constant condition (always false)
312        var actualTree = simplifier.Simplify(importer.Import("(if (* 1.0 -2.0) (variable 2.0 a) (variable 3.0 a))"));
313        var expectedTree = importer.Import("(variable 3.0 a)");
314        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
315      }
316      {
317        // complex constant condition (always false)
318        var actualTree = simplifier.Simplify(importer.Import("(if (/ (variable 1.0 a) (variable -2.0 a)) (variable 2.0 a) (variable 3.0 a))"));
319        var expectedTree = importer.Import("(variable 3.0 a)");
320        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
321      }
322      {
323        // insertion of relational operator
324        var actualTree = simplifier.Simplify(importer.Import("(if (variable 1.0 a) (variable 2.0 a) (variable 3.0 a))"));
325        var expectedTree = importer.Import("(if (> (variable 1.0 a) 0.0) (variable 2.0 a) (variable 3.0 a))");
326        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
327      }
328      #endregion
329    }
330
331    private void AssertEqualEnumerations(IEnumerable<double> expected, IEnumerable<double> actual) {
332      var expectedEnumerator = expected.GetEnumerator();
333      var actualEnumerator = actual.GetEnumerator();
334      while (expectedEnumerator.MoveNext() & actualEnumerator.MoveNext()) {
335        Assert.AreEqual(expectedEnumerator.Current, actualEnumerator.Current, Math.Abs(1E-6 * expectedEnumerator.Current));
336      }
337      if (expectedEnumerator.MoveNext() | actualEnumerator.MoveNext())
338        Assert.Fail("Number of elements in enumerations do not match");
339    }
340  }
341}
Note: See TracBrowser for help on using the repository browser.