source: branches/symbreg-factors-2650/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/SymbolicDataAnalysisExpressionTreeSimplifierTest.cs @ 14534

Last change on this file since 14534 was 14534, checked in by gkronber, 6 years ago

#2650: added simplifier unit tests for factor symbols

File size: 12.3 KB
Line 
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
21using System;
22using System.Globalization;
23using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
24using Microsoft.VisualStudio.TestTools.UnitTesting;
25
26namespace 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      SymbolicDataAnalysisExpressionTreeSimplifier simplifier = new SymbolicDataAnalysisExpressionTreeSimplifier();
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("(* 2.0 (factor a 4.0 6.0))", "(factor a 8.0 12.0)");
162      AssertEqualAfterSimplification("(* (factor a 4.0 6.0) 2.0)", "(factor a 8.0 12.0)");
163      AssertEqualAfterSimplification("(* (factor a 4.0 6.0) (variable 2.0 a))", "(* (factor a 4.0 6.0) (variable 2.0 a))");   // not possible
164      AssertEqualAfterSimplification(
165        "(log (factor a 10.0 100.0))",
166        string.Format(CultureInfo.InvariantCulture, "(factor a {0} {1})", Math.Log(10.0), Math.Log(100.0)));
167      AssertEqualAfterSimplification(
168        "(exp (factor a 2.0 3.0))",
169        string.Format(CultureInfo.InvariantCulture, "(factor a {0} {1})", Math.Exp(2.0), Math.Exp(3.0)));
170      AssertEqualAfterSimplification("(sqrt (factor a 9.0 16.0))", "(factor a 3.0 4.0))");
171      AssertEqualAfterSimplification("(sqr (factor a 2.0 3.0))", "(factor a 4.0 9.0))");
172      AssertEqualAfterSimplification("(root (factor a 8.0 27.0) 3)", "(factor a 2.0 3.0))");
173      AssertEqualAfterSimplification("(power (factor a 2.0 3.0) 3)", "(factor a 8.0 27.0))");
174
175      AssertEqualAfterSimplification("(sin (factor a 1.0 2.0) )",
176        string.Format(CultureInfo.InvariantCulture, "(factor a {0} {1}))", Math.Sin(1.0), Math.Sin(2.0)));
177      AssertEqualAfterSimplification("(cos (factor a 1.0 2.0) )",
178        string.Format(CultureInfo.InvariantCulture, "(factor a {0} {1}))", Math.Cos(1.0), Math.Cos(2.0)));
179      AssertEqualAfterSimplification("(tan (factor a 1.0 2.0) )",
180        string.Format(CultureInfo.InvariantCulture, "(factor a {0} {1}))", Math.Tan(1.0), Math.Tan(2.0)));
181
182
183      AssertEqualAfterSimplification("(binfactor a val 1.0)", "(binfactor a val 1.0)");
184      // binfactor folding
185      AssertEqualAfterSimplification("(+ (binfactor a val 1.0) (binfactor a val 2.0))", "(binfactor a val 3.0)");
186      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)
187      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)
188      AssertEqualAfterSimplification("(- (binfactor a val 1.0) (binfactor a val 2.0))", "(binfactor a val -1.0)");
189      AssertEqualAfterSimplification("(* (binfactor a val 2.0) (binfactor a val 3.0))", "(binfactor a val 6.0)");
190      AssertEqualAfterSimplification("(/ (binfactor a val 6.0) (binfactor a val 3.0))", "(binfactor a val 2.0)");
191      AssertEqualAfterSimplification("(/ (binfactor a val 4.0))", "(binfactor a val 0.25)");
192
193      AssertEqualAfterSimplification("(+ 3.0 (binfactor a val 4.0 ))", "(binfactor a val 7.0 )");
194      AssertEqualAfterSimplification("(+ (binfactor a val 4.0 ) 3.0)", "(binfactor a val 7.0 )");
195      AssertEqualAfterSimplification("(* 2.0 (binfactor a val 4.0))", "(binfactor a val 8.0 )");
196      AssertEqualAfterSimplification("(* (binfactor a val 4.0) 2.0)", "(binfactor a val 8.0 )");
197
198      // TODO same set of functions as for factor symbols
199
200      // combination of factor and binfactor
201      // TODO: should we support this?
202      AssertEqualAfterSimplification("(+ (binfactor a x0 2.0) (factor a 2.0 3.0))", "(factor a 4.0 3.0)");
203      #endregion
204    }
205
206
207    private void AssertEqualAfterSimplification(string original, string expected) {
208      var simplifier = new SymbolicDataAnalysisExpressionTreeSimplifier();
209      var formatter = new SymbolicExpressionTreeStringFormatter();
210      var importer = new SymbolicExpressionImporter();
211      var actualTree = simplifier.Simplify(importer.Import(original));
212      var expectedTree = importer.Import(expected);
213      Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
214
215    }
216  }
217}
218
Note: See TracBrowser for help on using the repository browser.