[4236] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5445] | 3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[4236] | 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;
|
---|
[4722] | 23 | using System.Collections.Generic;
|
---|
[4236] | 24 | using HeuristicLab.Common;
|
---|
[4722] | 25 | using HeuristicLab.Core;
|
---|
[4236] | 26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[4722] | 27 | using HeuristicLab.Optimization;
|
---|
| 28 | using HeuristicLab.Problems.DataAnalysis.Regression.Symbolic;
|
---|
[4236] | 29 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
| 30 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
[5460] | 31 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Formatters;
|
---|
[4236] | 32 | namespace 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")]
|
---|
[4477] | 42 | [TestMethod]
|
---|
[4236] | 43 | public void SimplifyRegressionSolutionsTest() {
|
---|
| 44 | ContentManager.Initialize(new PersistenceContentManager());
|
---|
| 45 |
|
---|
| 46 | {
|
---|
[4237] | 47 | SymbolicRegressionSolution solution = LoadSolution("RegressionSolution01.hl");
|
---|
[4236] | 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 |
|
---|
[5460] | 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 logarithm rules
|
---|
| 143 | {
|
---|
| 144 | // cancellation
|
---|
| 145 | var actualTree = simplifier.Simplify(importer.Import("(exp (log (variable 2.0 a)))"));
|
---|
| 146 | var expectedTree = importer.Import("(variable 2.0 a)");
|
---|
| 147 | Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
|
---|
| 148 | }
|
---|
| 149 | {
|
---|
| 150 | // cancellation
|
---|
| 151 | var actualTree = simplifier.Simplify(importer.Import("(log (exp (variable 2.0 a)))"));
|
---|
| 152 | var expectedTree = importer.Import("(variable 2.0 a)");
|
---|
| 153 | Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
|
---|
| 154 | }
|
---|
| 155 | {
|
---|
| 156 | // log transformation
|
---|
| 157 | var actualTree = simplifier.Simplify(importer.Import("(log (* (variable 2.0 a) (variable 3.0 b))"));
|
---|
| 158 | var expectedTree = importer.Import("(+ (log (variable 2.0 a)) (log (variable 3.0 b)))");
|
---|
| 159 | Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
|
---|
| 160 | }
|
---|
| 161 | {
|
---|
| 162 | // log transformation
|
---|
| 163 | var actualTree = simplifier.Simplify(importer.Import("(log (/ (variable 2.0 a) (variable 3.0 b))"));
|
---|
| 164 | var expectedTree = importer.Import("(- (log (variable 2.0 a)) (log (variable 3.0 b)))");
|
---|
| 165 | Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
|
---|
| 166 | }
|
---|
| 167 | #endregion
|
---|
| 168 | #region boolean operations
|
---|
| 169 | {
|
---|
| 170 | // always true and
|
---|
| 171 | var actualTree = simplifier.Simplify(importer.Import("(and 1.0 2.0)"));
|
---|
| 172 | var expectedTree = importer.Import("1.0");
|
---|
| 173 | Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
|
---|
| 174 | }
|
---|
| 175 | {
|
---|
| 176 | // always false and
|
---|
| 177 | var actualTree = simplifier.Simplify(importer.Import("(and 1.0 -2.0)"));
|
---|
| 178 | var expectedTree = importer.Import("-1.0");
|
---|
| 179 | Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
|
---|
| 180 | }
|
---|
| 181 | {
|
---|
| 182 | // always true or
|
---|
| 183 | var actualTree = simplifier.Simplify(importer.Import("(or -1.0 2.0)"));
|
---|
| 184 | var expectedTree = importer.Import("1.0");
|
---|
| 185 | Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
|
---|
| 186 | }
|
---|
| 187 | {
|
---|
| 188 | // always false or
|
---|
| 189 | var actualTree = simplifier.Simplify(importer.Import("(or -1.0 -2.0)"));
|
---|
| 190 | var expectedTree = importer.Import("-1.0");
|
---|
| 191 | Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
|
---|
| 192 | }
|
---|
| 193 | {
|
---|
| 194 | // constant not
|
---|
| 195 | var actualTree = simplifier.Simplify(importer.Import("(not -2.0)"));
|
---|
| 196 | var expectedTree = importer.Import("1.0");
|
---|
| 197 | Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
|
---|
| 198 | }
|
---|
| 199 | {
|
---|
| 200 | // constant not
|
---|
| 201 | var actualTree = simplifier.Simplify(importer.Import("(not 2.0)"));
|
---|
| 202 | var expectedTree = importer.Import("-1.0");
|
---|
| 203 | Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
|
---|
| 204 | }
|
---|
| 205 | {
|
---|
| 206 | // constant not
|
---|
| 207 | var actualTree = simplifier.Simplify(importer.Import("(not 0.0)"));
|
---|
| 208 | var expectedTree = importer.Import("1.0");
|
---|
| 209 | Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
|
---|
| 210 | }
|
---|
| 211 | #endregion
|
---|
| 212 | #region conditionals
|
---|
| 213 | {
|
---|
| 214 | // always false
|
---|
| 215 | var actualTree = simplifier.Simplify(importer.Import("(if -1.0 (variable 2.0 a) (variable 3.0 a))"));
|
---|
| 216 | var expectedTree = importer.Import("(variable 3.0 a)");
|
---|
| 217 | Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
|
---|
| 218 | }
|
---|
| 219 | {
|
---|
| 220 | // always true
|
---|
| 221 | var actualTree = simplifier.Simplify(importer.Import("(if 1.0 (variable 2.0 a) (variable 3.0 a))"));
|
---|
| 222 | var expectedTree = importer.Import("(variable 2.0 a)");
|
---|
| 223 | Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
|
---|
| 224 | }
|
---|
| 225 | {
|
---|
| 226 | // always false (0.0)
|
---|
| 227 | var actualTree = simplifier.Simplify(importer.Import("(if 0.0 (variable 2.0 a) (variable 3.0 a))"));
|
---|
| 228 | var expectedTree = importer.Import("(variable 3.0 a)");
|
---|
| 229 | Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
|
---|
| 230 | }
|
---|
| 231 | {
|
---|
| 232 | // complex constant condition (always false)
|
---|
| 233 | var actualTree = simplifier.Simplify(importer.Import("(if (* 1.0 -2.0) (variable 2.0 a) (variable 3.0 a))"));
|
---|
| 234 | var expectedTree = importer.Import("(variable 3.0 a)");
|
---|
| 235 | Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
|
---|
| 236 | }
|
---|
| 237 | {
|
---|
| 238 | // complex constant condition (always false)
|
---|
| 239 | var actualTree = simplifier.Simplify(importer.Import("(if (/ (variable 1.0 a) (variable -2.0 a)) (variable 2.0 a) (variable 3.0 a))"));
|
---|
| 240 | var expectedTree = importer.Import("(variable 3.0 a)");
|
---|
| 241 | Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
|
---|
| 242 | }
|
---|
| 243 | {
|
---|
| 244 | // insertion of relational operator
|
---|
| 245 | var actualTree = simplifier.Simplify(importer.Import("(if (variable 1.0 a) (variable 2.0 a) (variable 3.0 a))"));
|
---|
| 246 | var expectedTree = importer.Import("(if (> (variable 1.0 a) 0.0) (variable 2.0 a) (variable 3.0 a))");
|
---|
| 247 | Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
|
---|
| 248 | }
|
---|
| 249 | #endregion
|
---|
| 250 | #region constant and variable folding
|
---|
| 251 | {
|
---|
| 252 | var actualTree = simplifier.Simplify(importer.Import("(+ 1.0 2.0)"));
|
---|
| 253 | var expectedTree = importer.Import("3.0");
|
---|
| 254 | Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
|
---|
| 255 | }
|
---|
| 256 | {
|
---|
| 257 | var actualTree = simplifier.Simplify(importer.Import("(+ (variable 2.0 a) (variable 2.0 a))"));
|
---|
| 258 | var expectedTree = importer.Import("(variable 4.0 a)");
|
---|
| 259 | Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
|
---|
| 260 | }
|
---|
| 261 | {
|
---|
| 262 | var actualTree = simplifier.Simplify(importer.Import("(- (variable 2.0 a) (variable 1.0 a))"));
|
---|
| 263 | var expectedTree = importer.Import("(variable 1.0 a)");
|
---|
| 264 | Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
|
---|
| 265 | }
|
---|
| 266 | {
|
---|
| 267 | var actualTree = simplifier.Simplify(importer.Import("(* (variable 2.0 a) (variable 2.0 a))"));
|
---|
| 268 | var expectedTree = importer.Import("(* (* (variable 1.0 a) (variable 1.0 a)) 4.0)");
|
---|
| 269 | Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
|
---|
| 270 | }
|
---|
| 271 | {
|
---|
| 272 | var actualTree = simplifier.Simplify(importer.Import("(/ (variable 1.0 a) (variable 2.0 a))"));
|
---|
| 273 | var expectedTree = importer.Import("0.5");
|
---|
| 274 | Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
|
---|
| 275 | }
|
---|
| 276 | #endregion
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 |
|
---|
[4236] | 280 | private SymbolicRegressionSolution LoadSolution(string fileName) {
|
---|
[4237] | 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();
|
---|
[4236] | 292 | }
|
---|
| 293 |
|
---|
| 294 | private SymbolicRegressionSolution SimplifySolution(SymbolicRegressionSolution original) {
|
---|
| 295 | SymbolicSimplifier simplifier = new SymbolicSimplifier();
|
---|
[4477] | 296 | SymbolicExpressionTree simplifiedTree = simplifier.Simplify(original.Model.SymbolicExpressionTree);
|
---|
[4236] | 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()) {
|
---|
[4237] | 305 | Assert.AreEqual(expectedEnumerator.Current, actualEnumerator.Current, Math.Abs(1E-6 * expectedEnumerator.Current));
|
---|
[4236] | 306 | }
|
---|
| 307 | if (expectedEnumerator.MoveNext() | actualEnumerator.MoveNext())
|
---|
| 308 | Assert.Fail("Number of elements in enumerations do not match");
|
---|
| 309 | }
|
---|
| 310 | }
|
---|
| 311 | }
|
---|