Free cookie consent management tool by TermsFeed Policy Generator

Changeset 5460 for trunk/sources


Ignore:
Timestamp:
02/15/11 09:14:08 (13 years ago)
Author:
gkronber
Message:

#1227 Added test method for simplifier axioms.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Tests/SymbolicSimplifierTest.cs

    r5445 r5460  
    2929using HeuristicLab.Problems.DataAnalysis.Symbolic;
    3030using Microsoft.VisualStudio.TestTools.UnitTesting;
     31using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Formatters;
    3132namespace HeuristicLab.Problems.DataAnalysis.Tests {
    3233
     
    7576    }
    7677
     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
    77280    private SymbolicRegressionSolution LoadSolution(string fileName) {
    78281      var doc = ContentManager.Load(fileName);
Note: See TracChangeset for help on using the changeset viewer.