[4951] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 27 | using HeuristicLab.Optimization;
|
---|
| 28 | using HeuristicLab.Problems.DataAnalysis.Regression.Symbolic;
|
---|
| 29 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
| 30 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
| 31 | namespace HeuristicLab.Problems.DataAnalysis.Tests {
|
---|
| 32 |
|
---|
| 33 | [TestClass()]
|
---|
| 34 | public class SymbolicSimplifierTest {
|
---|
| 35 | [DeploymentItem(@"RegressionSolution01.hl")]
|
---|
| 36 | [DeploymentItem(@"RegressionSolution02.hl")]
|
---|
| 37 | [DeploymentItem(@"RegressionSolution03.hl")]
|
---|
| 38 | [DeploymentItem(@"RegressionSolution04.hl")]
|
---|
| 39 | [DeploymentItem(@"RegressionSolution05.hl")]
|
---|
| 40 | [DeploymentItem(@"RegressionSolution06.hl")]
|
---|
| 41 | [TestMethod]
|
---|
| 42 | public void SimplifyRegressionSolutionsTest() {
|
---|
| 43 | ContentManager.Initialize(new PersistenceContentManager());
|
---|
| 44 |
|
---|
| 45 | {
|
---|
| 46 | SymbolicRegressionSolution solution = LoadSolution("RegressionSolution01.hl");
|
---|
| 47 | SymbolicRegressionSolution simplifiedSolution = SimplifySolution(solution);
|
---|
| 48 | AssertEqualEnumerations(solution.EstimatedValues, simplifiedSolution.EstimatedValues);
|
---|
| 49 | }
|
---|
| 50 | {
|
---|
| 51 | SymbolicRegressionSolution solution = LoadSolution("RegressionSolution02.hl");
|
---|
| 52 | SymbolicRegressionSolution simplifiedSolution = SimplifySolution(solution);
|
---|
| 53 | AssertEqualEnumerations(solution.EstimatedValues, simplifiedSolution.EstimatedValues);
|
---|
| 54 | }
|
---|
| 55 | {
|
---|
| 56 | SymbolicRegressionSolution solution = LoadSolution("RegressionSolution03.hl");
|
---|
| 57 | SymbolicRegressionSolution simplifiedSolution = SimplifySolution(solution);
|
---|
| 58 | AssertEqualEnumerations(solution.EstimatedValues, simplifiedSolution.EstimatedValues);
|
---|
| 59 | }
|
---|
| 60 | {
|
---|
| 61 | SymbolicRegressionSolution solution = LoadSolution("RegressionSolution04.hl");
|
---|
| 62 | SymbolicRegressionSolution simplifiedSolution = SimplifySolution(solution);
|
---|
| 63 | AssertEqualEnumerations(solution.EstimatedValues, simplifiedSolution.EstimatedValues);
|
---|
| 64 | }
|
---|
| 65 | {
|
---|
| 66 | SymbolicRegressionSolution solution = LoadSolution("RegressionSolution05.hl");
|
---|
| 67 | SymbolicRegressionSolution simplifiedSolution = SimplifySolution(solution);
|
---|
| 68 | AssertEqualEnumerations(solution.EstimatedValues, simplifiedSolution.EstimatedValues);
|
---|
| 69 | }
|
---|
| 70 | {
|
---|
| 71 | SymbolicRegressionSolution solution = LoadSolution("RegressionSolution06.hl");
|
---|
| 72 | SymbolicRegressionSolution simplifiedSolution = SimplifySolution(solution);
|
---|
| 73 | AssertEqualEnumerations(solution.EstimatedValues, simplifiedSolution.EstimatedValues);
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | private SymbolicRegressionSolution LoadSolution(string fileName) {
|
---|
| 78 | var doc = ContentManager.Load(fileName);
|
---|
| 79 | Result result = doc as Result;
|
---|
| 80 | if (result != null) {
|
---|
| 81 | return (SymbolicRegressionSolution)result.Value;
|
---|
| 82 | }
|
---|
| 83 | SymbolicRegressionSolution solution = doc as SymbolicRegressionSolution;
|
---|
| 84 | if (solution != null) {
|
---|
| 85 | return solution;
|
---|
| 86 | }
|
---|
| 87 | Assert.Fail("Cannot load file " + fileName);
|
---|
| 88 | throw new AssertFailedException();
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | private SymbolicRegressionSolution SimplifySolution(SymbolicRegressionSolution original) {
|
---|
| 92 | SymbolicSimplifier simplifier = new SymbolicSimplifier();
|
---|
| 93 | SymbolicExpressionTree simplifiedTree = simplifier.Simplify(original.Model.SymbolicExpressionTree);
|
---|
| 94 | SymbolicRegressionModel simplifiedModel = new SymbolicRegressionModel(original.Model.Interpreter, simplifiedTree);
|
---|
| 95 | return new SymbolicRegressionSolution(original.ProblemData, simplifiedModel, original.LowerEstimationLimit, original.UpperEstimationLimit);
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | private void AssertEqualEnumerations(IEnumerable<double> expected, IEnumerable<double> actual) {
|
---|
| 99 | var expectedEnumerator = expected.GetEnumerator();
|
---|
| 100 | var actualEnumerator = actual.GetEnumerator();
|
---|
| 101 | while (expectedEnumerator.MoveNext() & actualEnumerator.MoveNext()) {
|
---|
| 102 | Assert.AreEqual(expectedEnumerator.Current, actualEnumerator.Current, Math.Abs(1E-6 * expectedEnumerator.Current));
|
---|
| 103 | }
|
---|
| 104 | if (expectedEnumerator.MoveNext() | actualEnumerator.MoveNext())
|
---|
| 105 | Assert.Fail("Number of elements in enumerations do not match");
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 | }
|
---|