Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Tests/SymbolicSimplifierTest.cs @ 5461

Last change on this file since 5461 was 5461, checked in by gkronber, 12 years ago

#1227 implemented transformations in simplifier to successfully run current set of test cases.

File size: 14.8 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.Regression.Symbolic;
29using HeuristicLab.Problems.DataAnalysis.Symbolic;
30using Microsoft.VisualStudio.TestTools.UnitTesting;
31using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Formatters;
32namespace HeuristicLab.Problems.DataAnalysis.Tests {
33
34  [TestClass()]
35  public class SymbolicSimplifierTest {
36    [DeploymentItem(@"RegressionSolution01.hl")]
37    [DeploymentItem(@"RegressionSolution02.hl")]
38    [DeploymentItem(@"RegressionSolution03.hl")]
39    [DeploymentItem(@"RegressionSolution04.hl")]
40    [DeploymentItem(@"RegressionSolution05.hl")]
41    [DeploymentItem(@"RegressionSolution06.hl")]
42    [TestMethod]
43    public void SimplifyRegressionSolutionsTest() {
44      ContentManager.Initialize(new PersistenceContentManager());
45
46      {
47        SymbolicRegressionSolution solution = LoadSolution("RegressionSolution01.hl");
48        SymbolicRegressionSolution simplifiedSolution = SimplifySolution(solution);
49        AssertEqualEnumerations(solution.EstimatedValues, simplifiedSolution.EstimatedValues);
50      }
51      {
52        SymbolicRegressionSolution solution = LoadSolution("RegressionSolution02.hl");
53        SymbolicRegressionSolution simplifiedSolution = SimplifySolution(solution);
54        AssertEqualEnumerations(solution.EstimatedValues, simplifiedSolution.EstimatedValues);
55      }
56      {
57        SymbolicRegressionSolution solution = LoadSolution("RegressionSolution03.hl");
58        SymbolicRegressionSolution simplifiedSolution = SimplifySolution(solution);
59        AssertEqualEnumerations(solution.EstimatedValues, simplifiedSolution.EstimatedValues);
60      }
61      {
62        SymbolicRegressionSolution solution = LoadSolution("RegressionSolution04.hl");
63        SymbolicRegressionSolution simplifiedSolution = SimplifySolution(solution);
64        AssertEqualEnumerations(solution.EstimatedValues, simplifiedSolution.EstimatedValues);
65      }
66      {
67        SymbolicRegressionSolution solution = LoadSolution("RegressionSolution05.hl");
68        SymbolicRegressionSolution simplifiedSolution = SimplifySolution(solution);
69        AssertEqualEnumerations(solution.EstimatedValues, simplifiedSolution.EstimatedValues);
70      }
71      {
72        SymbolicRegressionSolution solution = LoadSolution("RegressionSolution06.hl");
73        SymbolicRegressionSolution simplifiedSolution = SimplifySolution(solution);
74        AssertEqualEnumerations(solution.EstimatedValues, simplifiedSolution.EstimatedValues);
75      }
76    }
77
78    [TestMethod]
79    public void SimplifierAxiomsTest() {
80      SymbolicExpressionImporter importer = new SymbolicExpressionImporter();
81      SymbolicSimplifier simplifier = new SymbolicSimplifier();
82      SymbolicExpressionTreeStringFormatter formatter = new SymbolicExpressionTreeStringFormatter();
83      #region single argument arithmetics
84      {
85        var actualTree = simplifier.Simplify(importer.Import("(+ 1.0)"));
86        var expectedTree = importer.Import("1.0");
87        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
88      }
89      {
90        var actualTree = simplifier.Simplify(importer.Import("(+ (variable 2.0 a))"));
91        var expectedTree = importer.Import("(variable 2.0 a)");
92        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
93      }
94      {
95        var actualTree = simplifier.Simplify(importer.Import("(- 1.0)"));
96        var expectedTree = importer.Import("-1.0");
97        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
98      }
99      {
100        var actualTree = simplifier.Simplify(importer.Import("(- (variable 2.0 a))"));
101        var expectedTree = importer.Import("(variable -2.0 a)");
102        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
103      }
104      {
105        var actualTree = simplifier.Simplify(importer.Import("(* 2.0)"));
106        var expectedTree = importer.Import("2.0");
107        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
108      }
109      {
110        var actualTree = simplifier.Simplify(importer.Import("(* (variable 2.0 a))"));
111        var expectedTree = importer.Import("(variable 2.0 a)");
112        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
113      }
114      {
115        var actualTree = simplifier.Simplify(importer.Import("(/ 2.0)"));
116        var expectedTree = importer.Import("0.5");
117        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
118      }
119      {
120        var actualTree = simplifier.Simplify(importer.Import("(/ (variable 2.0 a))"));
121        var expectedTree = importer.Import("(/ 1.0 (variable 2.0 a))");
122        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
123      }
124      #endregion
125      #region aggregation of constants into factors
126      {
127        var actualTree = simplifier.Simplify(importer.Import("(* 2.0 (variable 2.0 a))"));
128        var expectedTree = importer.Import("(variable 4.0 a)");
129        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
130      }
131      {
132        var actualTree = simplifier.Simplify(importer.Import("(/ (variable 2.0 a) 2.0)"));
133        var expectedTree = importer.Import("(variable 1.0 a)");
134        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
135      }
136      {
137        var actualTree = simplifier.Simplify(importer.Import("(/ (variable 2.0 a) (* 2.0 2.0))"));
138        var expectedTree = importer.Import("(variable 0.5 a)");
139        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
140      }
141      #endregion
142      #region constant and variable folding
143      {
144        var actualTree = simplifier.Simplify(importer.Import("(+ 1.0 2.0)"));
145        var expectedTree = importer.Import("3.0");
146        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
147      }
148      {
149        var actualTree = simplifier.Simplify(importer.Import("(+ (variable 2.0 a) (variable 2.0 a))"));
150        var expectedTree = importer.Import("(variable 4.0 a)");
151        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
152      }
153      {
154        var actualTree = simplifier.Simplify(importer.Import("(- (variable 2.0 a) (variable 1.0 a))"));
155        var expectedTree = importer.Import("(variable 1.0 a)");
156        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
157      }
158      {
159        var actualTree = simplifier.Simplify(importer.Import("(* (variable 2.0 a) (variable 2.0 a))"));
160        var expectedTree = importer.Import("(* (* (variable 1.0 a) (variable 1.0 a)) 4.0)");
161        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
162      }
163      {
164        var actualTree = simplifier.Simplify(importer.Import("(/ (variable 1.0 a) (variable 2.0 a))"));
165        var expectedTree = importer.Import("0.5");
166        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
167      }
168      #endregion
169      #region logarithm rules
170      {
171        // cancellation
172        var actualTree = simplifier.Simplify(importer.Import("(exp (log (variable 2.0 a)))"));
173        var expectedTree = importer.Import("(variable 2.0 a)");
174        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
175      }
176      {
177        // cancellation
178        var actualTree = simplifier.Simplify(importer.Import("(log (exp (variable 2.0 a)))"));
179        var expectedTree = importer.Import("(variable 2.0 a)");
180        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
181      }
182      {
183        // log transformation
184        var actualTree = simplifier.Simplify(importer.Import("(log (* (variable 2.0 a) (variable 3.0 b)))"));
185        var expectedTree = importer.Import("(+ (log (variable 1.0 a)) (log (variable 1.0 b)) 1.7918)");
186        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
187      }
188      {
189        // log transformation
190        var actualTree = simplifier.Simplify(importer.Import("(log (/ (variable 2.0 a) (variable 3.0 b)))"));
191        var expectedTree = importer.Import("(- (log (variable 2.0 a)) (log (variable 3.0 b)))");
192        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
193      }
194      #endregion
195      #region boolean operations
196      {
197        // always true and
198        var actualTree = simplifier.Simplify(importer.Import("(and 1.0 2.0)"));
199        var expectedTree = importer.Import("1.0");
200        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
201      }
202      {
203        // always false and
204        var actualTree = simplifier.Simplify(importer.Import("(and 1.0 -2.0)"));
205        var expectedTree = importer.Import("-1.0");
206        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
207      }
208      {
209        // always true or
210        var actualTree = simplifier.Simplify(importer.Import("(or -1.0 2.0)"));
211        var expectedTree = importer.Import("1.0");
212        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
213      }
214      {
215        // always false or
216        var actualTree = simplifier.Simplify(importer.Import("(or -1.0 -2.0)"));
217        var expectedTree = importer.Import("-1.0");
218        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
219      }
220      {
221        // constant not
222        var actualTree = simplifier.Simplify(importer.Import("(not -2.0)"));
223        var expectedTree = importer.Import("2.0");
224        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
225      }
226      {
227        // constant not
228        var actualTree = simplifier.Simplify(importer.Import("(not 2.0)"));
229        var expectedTree = importer.Import("-2.0");
230        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
231      }
232      {
233        // constant not
234        var actualTree = simplifier.Simplify(importer.Import("(not 0.0)"));
235        var expectedTree = importer.Import("0.0");
236        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
237      }
238      #endregion
239      #region conditionals
240      {
241        // always false
242        var actualTree = simplifier.Simplify(importer.Import("(if -1.0 (variable 2.0 a) (variable 3.0 a))"));
243        var expectedTree = importer.Import("(variable 3.0 a)");
244        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
245      }
246      {
247        // always true
248        var actualTree = simplifier.Simplify(importer.Import("(if 1.0 (variable 2.0 a) (variable 3.0 a))"));
249        var expectedTree = importer.Import("(variable 2.0 a)");
250        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
251      }
252      {
253        // always false (0.0)
254        var actualTree = simplifier.Simplify(importer.Import("(if 0.0 (variable 2.0 a) (variable 3.0 a))"));
255        var expectedTree = importer.Import("(variable 3.0 a)");
256        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
257      }
258      {
259        // complex constant condition (always false)
260        var actualTree = simplifier.Simplify(importer.Import("(if (* 1.0 -2.0) (variable 2.0 a) (variable 3.0 a))"));
261        var expectedTree = importer.Import("(variable 3.0 a)");
262        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
263      }
264      {
265        // complex constant condition (always false)
266        var actualTree = simplifier.Simplify(importer.Import("(if (/ (variable 1.0 a) (variable -2.0 a)) (variable 2.0 a) (variable 3.0 a))"));
267        var expectedTree = importer.Import("(variable 3.0 a)");
268        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
269      }
270      {
271        // insertion of relational operator
272        var actualTree = simplifier.Simplify(importer.Import("(if (variable 1.0 a) (variable 2.0 a) (variable 3.0 a))"));
273        var expectedTree = importer.Import("(if (> (variable 1.0 a) 0.0) (variable 2.0 a) (variable 3.0 a))");
274        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
275      }
276      #endregion
277    }
278
279
280    private SymbolicRegressionSolution LoadSolution(string fileName) {
281      var doc = ContentManager.Load(fileName);
282      Result result = doc as Result;
283      if (result != null) {
284        return (SymbolicRegressionSolution)result.Value;
285      }
286      SymbolicRegressionSolution solution = doc as SymbolicRegressionSolution;
287      if (solution != null) {
288        return solution;
289      }
290      Assert.Fail("Cannot load file " + fileName);
291      throw new AssertFailedException();
292    }
293
294    private SymbolicRegressionSolution SimplifySolution(SymbolicRegressionSolution original) {
295      SymbolicSimplifier simplifier = new SymbolicSimplifier();
296      SymbolicExpressionTree simplifiedTree = simplifier.Simplify(original.Model.SymbolicExpressionTree);
297      SymbolicRegressionModel simplifiedModel = new SymbolicRegressionModel(original.Model.Interpreter, simplifiedTree);
298      return new SymbolicRegressionSolution(original.ProblemData, simplifiedModel, original.LowerEstimationLimit, original.UpperEstimationLimit);
299    }
300
301    private void AssertEqualEnumerations(IEnumerable<double> expected, IEnumerable<double> actual) {
302      var expectedEnumerator = expected.GetEnumerator();
303      var actualEnumerator = actual.GetEnumerator();
304      while (expectedEnumerator.MoveNext() & actualEnumerator.MoveNext()) {
305        Assert.AreEqual(expectedEnumerator.Current, actualEnumerator.Current, Math.Abs(1E-6 * expectedEnumerator.Current));
306      }
307      if (expectedEnumerator.MoveNext() | actualEnumerator.MoveNext())
308        Assert.Fail("Number of elements in enumerations do not match");
309    }
310  }
311}
Note: See TracBrowser for help on using the repository browser.