[4236] | 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.IO;
|
---|
| 23 | using System;
|
---|
| 24 | using HeuristicLab.Random;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using System.Collections.Generic;
|
---|
| 27 | using System.Diagnostics;
|
---|
| 28 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 29 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
| 30 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
| 31 | using System.Linq;
|
---|
| 32 | using System.Globalization;
|
---|
| 33 | using HeuristicLab.Problems.DataAnalysis.Regression.Symbolic;
|
---|
| 34 | using HeuristicLab.Problems.DataAnalysis.Evaluators;
|
---|
| 35 | using HeuristicLab.Core;
|
---|
| 36 | using HeuristicLab.Optimization;
|
---|
| 37 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
|
---|
| 38 | namespace 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")]
|
---|
[4459] | 48 | [TestMethod]
|
---|
[4236] | 49 | public void SimplifyRegressionSolutionsTest() {
|
---|
| 50 | ContentManager.Initialize(new PersistenceContentManager());
|
---|
| 51 |
|
---|
| 52 | {
|
---|
[4237] | 53 | SymbolicRegressionSolution solution = LoadSolution("RegressionSolution01.hl");
|
---|
[4236] | 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) {
|
---|
[4237] | 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();
|
---|
[4236] | 96 | }
|
---|
| 97 |
|
---|
| 98 | private SymbolicRegressionSolution SimplifySolution(SymbolicRegressionSolution original) {
|
---|
| 99 | SymbolicSimplifier simplifier = new SymbolicSimplifier();
|
---|
[4459] | 100 | SymbolicExpressionTree simplifiedTree = simplifier.Simplify(original.Model.SymbolicExpressionTree);
|
---|
[4236] | 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()) {
|
---|
[4237] | 109 | Assert.AreEqual(expectedEnumerator.Current, actualEnumerator.Current, Math.Abs(1E-6 * expectedEnumerator.Current));
|
---|
[4236] | 110 | }
|
---|
| 111 | if (expectedEnumerator.MoveNext() | actualEnumerator.MoveNext())
|
---|
| 112 | Assert.Fail("Number of elements in enumerations do not match");
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 | }
|
---|