Free cookie consent management tool by TermsFeed Policy Generator

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

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

Merged improvements of symbolic simplifier (revisions: r4220, r4226, r4235:4238) back into trunk. #1026

File size: 5.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.IO;
23using System;
24using HeuristicLab.Random;
25using HeuristicLab.Common;
26using System.Collections.Generic;
27using System.Diagnostics;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Problems.DataAnalysis.Symbolic;
30using Microsoft.VisualStudio.TestTools.UnitTesting;
31using System.Linq;
32using System.Globalization;
33using HeuristicLab.Problems.DataAnalysis.Regression.Symbolic;
34using HeuristicLab.Problems.DataAnalysis.Evaluators;
35using HeuristicLab.Core;
36using HeuristicLab.Optimization;
37using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
38namespace HeuristicLab.Problems.DataAnalysis.Tests {
39
40  [TestClass()]
41  public class SymbolicSimplifierTest {
42    [TestMethod()]
43    [DeploymentItem(@"RegressionSolution01.hl")]
44    [DeploymentItem(@"RegressionSolution02.hl")]
45    [DeploymentItem(@"RegressionSolution03.hl")]
46    [DeploymentItem(@"RegressionSolution04.hl")]
47    [DeploymentItem(@"RegressionSolution05.hl")]
48    [DeploymentItem(@"RegressionSolution06.hl")]
49    public void SimplifyRegressionSolutionsTest() {
50      ContentManager.Initialize(new PersistenceContentManager());
51
52      {
53        SymbolicRegressionSolution solution = LoadSolution("RegressionSolution01.hl");
54        SymbolicRegressionSolution simplifiedSolution = SimplifySolution(solution);
55        AssertEqualEnumerations(solution.EstimatedValues, simplifiedSolution.EstimatedValues);
56      }
57      {
58        SymbolicRegressionSolution solution = LoadSolution("RegressionSolution02.hl");
59        SymbolicRegressionSolution simplifiedSolution = SimplifySolution(solution);
60        AssertEqualEnumerations(solution.EstimatedValues, simplifiedSolution.EstimatedValues);
61      }
62      {
63        SymbolicRegressionSolution solution = LoadSolution("RegressionSolution03.hl");
64        SymbolicRegressionSolution simplifiedSolution = SimplifySolution(solution);
65        AssertEqualEnumerations(solution.EstimatedValues, simplifiedSolution.EstimatedValues);
66      }
67      {
68        SymbolicRegressionSolution solution = LoadSolution("RegressionSolution04.hl");
69        SymbolicRegressionSolution simplifiedSolution = SimplifySolution(solution);
70        AssertEqualEnumerations(solution.EstimatedValues, simplifiedSolution.EstimatedValues);
71      }
72      {
73        SymbolicRegressionSolution solution = LoadSolution("RegressionSolution05.hl");
74        SymbolicRegressionSolution simplifiedSolution = SimplifySolution(solution);
75        AssertEqualEnumerations(solution.EstimatedValues, simplifiedSolution.EstimatedValues);
76      }
77      {
78        SymbolicRegressionSolution solution = LoadSolution("RegressionSolution06.hl");
79        SymbolicRegressionSolution simplifiedSolution = SimplifySolution(solution);
80        AssertEqualEnumerations(solution.EstimatedValues, simplifiedSolution.EstimatedValues);
81      }
82    }
83
84    private SymbolicRegressionSolution LoadSolution(string fileName) {
85      var doc = ContentManager.Load(fileName);
86      Result result = doc as Result;
87      if (result != null) {
88        return (SymbolicRegressionSolution)result.Value;
89      }
90      SymbolicRegressionSolution solution = doc as SymbolicRegressionSolution;
91      if (solution != null) {
92        return solution;
93      }
94      Assert.Fail("Cannot load file " + fileName);
95      throw new AssertFailedException();
96    }
97
98    private SymbolicRegressionSolution SimplifySolution(SymbolicRegressionSolution original) {
99      SymbolicSimplifier simplifier = new SymbolicSimplifier();
100      SymbolicExpressionTree simplifiedExpression = simplifier.Simplify(original.Model.SymbolicExpressionTree);
101      SymbolicExpressionTreeNode root = new ProgramRootSymbol().CreateTreeNode();
102      SymbolicExpressionTreeNode start = new StartSymbol().CreateTreeNode();
103      root.AddSubTree(start);
104      start.AddSubTree(simplifiedExpression.Root);
105      SymbolicExpressionTree simplifiedTree = new SymbolicExpressionTree(root);
106      SymbolicRegressionModel simplifiedModel = new SymbolicRegressionModel(original.Model.Interpreter, simplifiedTree);
107      return new SymbolicRegressionSolution(original.ProblemData, simplifiedModel, original.LowerEstimationLimit, original.UpperEstimationLimit);
108    }
109
110    private void AssertEqualEnumerations(IEnumerable<double> expected, IEnumerable<double> actual) {
111      var expectedEnumerator = expected.GetEnumerator();
112      var actualEnumerator = actual.GetEnumerator();
113      while (expectedEnumerator.MoveNext() & actualEnumerator.MoveNext()) {
114        Assert.AreEqual(expectedEnumerator.Current, actualEnumerator.Current, Math.Abs(1E-6 * expectedEnumerator.Current));
115      }
116      if (expectedEnumerator.MoveNext() | actualEnumerator.MoveNext())
117        Assert.Fail("Number of elements in enumerations do not match");
118    }
119  }
120}
Note: See TracBrowser for help on using the repository browser.