Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis/3.3/Tests/SymbolicSimplifierTest.cs @ 10552

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

Fixed data analysis test cases. #1142

File size: 5.3 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    [DeploymentItem(@"RegressionSolution01.hl")]
43    [DeploymentItem(@"RegressionSolution02.hl")]
44    [DeploymentItem(@"RegressionSolution03.hl")]
45    [DeploymentItem(@"RegressionSolution04.hl")]
46    [DeploymentItem(@"RegressionSolution05.hl")]
47    [DeploymentItem(@"RegressionSolution06.hl")]
48    [TestMethod]
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 simplifiedTree = simplifier.Simplify(original.Model.SymbolicExpressionTree);
101      SymbolicRegressionModel simplifiedModel = new SymbolicRegressionModel(original.Model.Interpreter, simplifiedTree);
102      return new SymbolicRegressionSolution(original.ProblemData, simplifiedModel, original.LowerEstimationLimit, original.UpperEstimationLimit);
103    }
104
105    private void AssertEqualEnumerations(IEnumerable<double> expected, IEnumerable<double> actual) {
106      var expectedEnumerator = expected.GetEnumerator();
107      var actualEnumerator = actual.GetEnumerator();
108      while (expectedEnumerator.MoveNext() & actualEnumerator.MoveNext()) {
109        Assert.AreEqual(expectedEnumerator.Current, actualEnumerator.Current, Math.Abs(1E-6 * expectedEnumerator.Current));
110      }
111      if (expectedEnumerator.MoveNext() | actualEnumerator.MoveNext())
112        Assert.Fail("Number of elements in enumerations do not match");
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.