Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/18/19 17:24:30 (6 years ago)
Author:
abeham
Message:

#2521: merged trunk changes up to r15681 into branch (removal of trunk/sources)

Location:
branches/2521_ProblemRefactoring
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/2521_ProblemRefactoring

  • branches/2521_ProblemRefactoring/HeuristicLab.Tests

  • branches/2521_ProblemRefactoring/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/SymbolicDataAnalysisExpressionTreeSimplifierTest.cs

    r12012 r16692  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    1919 */
    2020#endregion
    21 
    2221using System;
    23 using System.Collections.Generic;
     22using System.Globalization;
    2423using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    25 using HeuristicLab.Problems.DataAnalysis.Symbolic;
    2624using Microsoft.VisualStudio.TestTools.UnitTesting;
    2725
     
    3634    public void SimplifierAxiomsTest() {
    3735      SymbolicExpressionImporter importer = new SymbolicExpressionImporter();
    38       SymbolicDataAnalysisExpressionTreeSimplifier simplifier = new SymbolicDataAnalysisExpressionTreeSimplifier();
    3936      SymbolicExpressionTreeStringFormatter formatter = new SymbolicExpressionTreeStringFormatter();
    4037      #region single argument arithmetics
    41       {
    42         var actualTree = simplifier.Simplify(importer.Import("(+ 1.0)"));
    43         var expectedTree = importer.Import("1.0");
    44         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    45       }
    46       {
    47         var actualTree = simplifier.Simplify(importer.Import("(+ (variable 2.0 a))"));
    48         var expectedTree = importer.Import("(variable 2.0 a)");
    49         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    50       }
    51       {
    52         var actualTree = simplifier.Simplify(importer.Import("(- 1.0)"));
    53         var expectedTree = importer.Import("-1.0");
    54         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    55       }
    56       {
    57         var actualTree = simplifier.Simplify(importer.Import("(- (variable 2.0 a))"));
    58         var expectedTree = importer.Import("(variable -2.0 a)");
    59         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    60       }
    61       {
    62         var actualTree = simplifier.Simplify(importer.Import("(* 2.0)"));
    63         var expectedTree = importer.Import("2.0");
    64         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    65       }
    66       {
    67         var actualTree = simplifier.Simplify(importer.Import("(* (variable 2.0 a))"));
    68         var expectedTree = importer.Import("(variable 2.0 a)");
    69         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    70       }
    71       {
    72         var actualTree = simplifier.Simplify(importer.Import("(/ 2.0)"));
    73         var expectedTree = importer.Import("0.5");
    74         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    75       }
    76       {
    77         var actualTree = simplifier.Simplify(importer.Import("(/ (variable 2.0 a))"));
    78         var expectedTree = importer.Import("(/ 1.0 (variable 2.0 a))");
    79         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    80       }
    81       #endregion
     38
     39      AssertEqualAfterSimplification("(+ 1.0)", "1.0");
     40      AssertEqualAfterSimplification("(- 1.0)", "-1.0");
     41      AssertEqualAfterSimplification("(- (variable 2.0 a))", "(variable -2.0 a)");
     42      AssertEqualAfterSimplification("(* 2.0)", "2.0");
     43      AssertEqualAfterSimplification("(* (variable 2.0 a))", "(variable 2.0 a)");
     44      AssertEqualAfterSimplification("(/ 2.0)", "0.5");
     45      AssertEqualAfterSimplification("(/ (variable 2.0 a))", "(/ 1.0 (variable 2.0 a))");
     46      #endregion
     47
    8248      #region aggregation of constants into factors
    83       {
    84         var actualTree = simplifier.Simplify(importer.Import("(* 2.0 (variable 2.0 a))"));
    85         var expectedTree = importer.Import("(variable 4.0 a)");
    86         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    87       }
    88       {
    89         var actualTree = simplifier.Simplify(importer.Import("(/ (variable 2.0 a) 2.0)"));
    90         var expectedTree = importer.Import("(variable 1.0 a)");
    91         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    92       }
    93       {
    94         var actualTree = simplifier.Simplify(importer.Import("(/ (variable 2.0 a) (* 2.0 2.0))"));
    95         var expectedTree = importer.Import("(variable 0.5 a)");
    96         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    97       }
    98       #endregion
     49      AssertEqualAfterSimplification("(* 2.0 (variable 2.0 a))", "(variable 4.0 a)");
     50      AssertEqualAfterSimplification("(/ (variable 2.0 a) 2.0)", "(variable 1.0 a)");
     51      AssertEqualAfterSimplification("(/ (variable 2.0 a) (* 2.0 2.0))", "(variable 0.5 a)");
     52      #endregion
     53
    9954      #region constant and variable folding
    100       {
    101         var actualTree = simplifier.Simplify(importer.Import("(+ 1.0 2.0)"));
    102         var expectedTree = importer.Import("3.0");
    103         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    104       }
    105       {
    106         var actualTree = simplifier.Simplify(importer.Import("(+ (variable 2.0 a) (variable 2.0 a))"));
    107         var expectedTree = importer.Import("(variable 4.0 a)");
    108         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    109       }
    110       {
    111         var actualTree = simplifier.Simplify(importer.Import("(- (variable 2.0 a) (variable 1.0 a))"));
    112         var expectedTree = importer.Import("(variable 1.0 a)");
    113         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    114       }
    115       {
    116         var actualTree = simplifier.Simplify(importer.Import("(* (variable 2.0 a) (variable 2.0 a))"));
    117         var expectedTree = importer.Import("(* (* (variable 1.0 a) (variable 1.0 a)) 4.0)");
    118         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    119       }
    120       {
    121         var actualTree = simplifier.Simplify(importer.Import("(/ (variable 1.0 a) (variable 2.0 a))"));
    122         var expectedTree = importer.Import("0.5");
    123         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    124       }
    125       #endregion
     55      AssertEqualAfterSimplification("(+ 1.0 2.0)", "3.0");
     56      AssertEqualAfterSimplification("(+ (variable 2.0 a) (variable 2.0 a))", "(variable 4.0 a)");
     57      AssertEqualAfterSimplification("(- (variable 2.0 a) (variable 1.0 a))", "(variable 1.0 a)");
     58      AssertEqualAfterSimplification("(* (variable 2.0 a) (variable 2.0 a))", "(* (* (variable 1.0 a) (variable 1.0 a)) 4.0)");
     59      AssertEqualAfterSimplification("(/ (variable 1.0 a) (variable 2.0 a))", "0.5");
     60      #endregion
     61
    12662      #region logarithm rules
    127       {
    128         // cancellation
    129         var actualTree = simplifier.Simplify(importer.Import("(log (exp (variable 2.0 a)))"));
    130         var expectedTree = importer.Import("(variable 2.0 a)");
    131         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    132       }
    133       {
    134         // must not transform logs in this way as we do not know wether both variables are positive
    135         var actualTree = simplifier.Simplify(importer.Import("(log (* (variable 1.0 a) (variable 1.0 b)))"));
    136         var expectedTree = importer.Import("(log (* (variable 1.0 a) (variable 1.0 b)))");
    137         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    138       }
    139       {
    140         // must not transform logs in this way as we do not know wether both variables are positive
    141         var actualTree = simplifier.Simplify(importer.Import("(log (/ (variable 1.0 a) (variable 1.0 b)))"));
    142         var expectedTree = importer.Import("(log (/ (variable 1.0 a) (variable 1.0 b)))");
    143         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    144       }
    145       #endregion
     63
     64      // cancellation
     65      AssertEqualAfterSimplification("(log (exp (variable 2.0 a)))", "(variable 2.0 a)");
     66      // must not transform logs in this way as we do not know wether both variables are positive
     67      AssertEqualAfterSimplification("(log (* (variable 1.0 a) (variable 1.0 b)))", "(log (* (variable 1.0 a) (variable 1.0 b)))");
     68      // must not transform logs in this way as we do not know wether both variables are positive
     69      AssertEqualAfterSimplification("(log (/ (variable 1.0 a) (variable 1.0 b)))", "(log (/ (variable 1.0 a) (variable 1.0 b)))");
     70      #endregion
     71
    14672      #region exponentiation rules
    147       {
    148         // cancellation
    149         var actualTree = simplifier.Simplify(importer.Import("(exp (log (variable 2.0 a)))"));
    150         var expectedTree = importer.Import("(variable 2.0 a)");
    151         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    152       }
    153       {
    154         // exp transformation
    155         var actualTree = simplifier.Simplify(importer.Import("(exp (+ (variable 2.0 a) (variable 3.0 b)))"));
    156         var expectedTree = importer.Import("(* (exp (variable 2.0 a)) (exp (variable 3.0 b)))");
    157         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    158       }
    159       {
    160         // exp transformation
    161         var actualTree = simplifier.Simplify(importer.Import("(exp (- (variable 2.0 a) (variable 3.0 b)))"));
    162         var expectedTree = importer.Import("(* (exp (variable 2.0 a)) (exp (variable -3.0 b)))");
    163         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    164       }
    165       {
    166         // exp transformation
    167         var actualTree = simplifier.Simplify(importer.Import("(exp (- (variable 2.0 a) (* (variable 3.0 b) (variable 4.0 c))))"));
    168         var expectedTree = importer.Import("(* (exp (variable 2.0 a)) (exp (* (variable 1.0 b) (variable 1.0 c) -12.0)))");
    169         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    170       }
    171       {
    172         // exp transformation
    173         var actualTree = simplifier.Simplify(importer.Import("(exp (- (variable 2.0 a) (* (variable 3.0 b) (cos (variable 4.0 c)))))"));
    174         var expectedTree = importer.Import("(* (exp (variable 2.0 a)) (exp (* (variable 1.0 b) (cos (variable 4.0 c)) -3.0)))");
    175         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    176       }
    177       #endregion
     73      // cancellation
     74      AssertEqualAfterSimplification("(exp (log (variable 2.0 a)))", "(variable 2.0 a)");
     75      // exp transformation
     76      AssertEqualAfterSimplification("(exp (+ (variable 2.0 a) (variable 3.0 b)))", "(* (exp (variable 2.0 a)) (exp (variable 3.0 b)))");
     77      // exp transformation
     78      AssertEqualAfterSimplification("(exp (- (variable 2.0 a) (variable 3.0 b)))", "(* (exp (variable 2.0 a)) (exp (variable -3.0 b)))");
     79      // exp transformation
     80      AssertEqualAfterSimplification("(exp (- (variable 2.0 a) (* (variable 3.0 b) (variable 4.0 c))))", "(* (exp (variable 2.0 a)) (exp (* (variable 1.0 b) (variable 1.0 c) -12.0)))");
     81      // exp transformation
     82      AssertEqualAfterSimplification("(exp (- (variable 2.0 a) (* (variable 3.0 b) (cos (variable 4.0 c)))))", "(* (exp (variable 2.0 a)) (exp (* (variable 1.0 b) (cos (variable 4.0 c)) -3.0)))");
     83      #endregion
     84
    17885      #region power rules
    179       {
    180         // cancellation
    181         var actualTree = simplifier.Simplify(importer.Import("(pow (variable 2.0 a) 0.0)"));
    182         var expectedTree = importer.Import("1.0");
    183         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    184       }
    185       {
    186         // fixed point
    187         var actualTree = simplifier.Simplify(importer.Import("(pow (variable 2.0 a) 1.0)"));
    188         var expectedTree = importer.Import("(variable 2.0 a)");
    189         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    190       }
    191       {
    192         // inversion fixed point
    193         var actualTree = simplifier.Simplify(importer.Import("(pow (variable 2.0 a) -1.0)"));
    194         var expectedTree = importer.Import("(/ 1.0 (variable 2.0 a))");
    195         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    196       }
    197       {
    198         // inversion
    199         var actualTree = simplifier.Simplify(importer.Import("(pow (variable 2.0 a) -2.0)"));
    200         var expectedTree = importer.Import("(/ 1.0 (pow (variable 2.0 a) 2.0))");
    201         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    202       }
    203       {
    204         // constant folding
    205         var actualTree = simplifier.Simplify(importer.Import("(pow 3.0 2.0)"));
    206         var expectedTree = importer.Import("9.0");
    207         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    208       }
    209       #endregion
     86
     87      // cancellation
     88      AssertEqualAfterSimplification("(pow (variable 2.0 a) 0.0)", "1.0");
     89      // fixed point
     90      AssertEqualAfterSimplification("(pow (variable 2.0 a) 1.0)", "(variable 2.0 a)");
     91      // inversion fixed point
     92      AssertEqualAfterSimplification("(pow (variable 2.0 a) -1.0)", "(/ 1.0 (variable 2.0 a))");
     93      // inversion
     94      AssertEqualAfterSimplification("(pow (variable 2.0 a) -2.0)", "(/ 1.0 (pow (variable 2.0 a) 2.0))");
     95      // constant folding
     96      AssertEqualAfterSimplification("(pow 3.0 2.0)", "9.0");
     97      #endregion
     98
    21099      #region root rules
    211       {
    212         // cancellation
    213         var actualTree = simplifier.Simplify(importer.Import("(root (variable 2.0 a) 0.0)"));
    214         var expectedTree = importer.Import("1.0");
    215         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    216       }
    217       {
    218         // fixed point
    219         var actualTree = simplifier.Simplify(importer.Import("(root (variable 2.0 a) 1.0)"));
    220         var expectedTree = importer.Import("(variable 2.0 a)");
    221         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    222       }
    223       {
    224         // inversion fixed point
    225         var actualTree = simplifier.Simplify(importer.Import("(root (variable 2.0 a) -1.0)"));
    226         var expectedTree = importer.Import("(/ 1.0 (variable 2.0 a))");
    227         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    228       }
    229       {
    230         // inversion
    231         var actualTree = simplifier.Simplify(importer.Import("(root (variable 2.0 a) -2.0)"));
    232         var expectedTree = importer.Import("(/ 1.0 (root (variable 2.0 a) 2.0))");
    233         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    234       }
    235       {
    236         // constant folding
    237         var actualTree = simplifier.Simplify(importer.Import("(root 9.0 2.0)"));
    238         var expectedTree = importer.Import("3.0");
    239         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    240       }
    241       #endregion
     100      // cancellation
     101      AssertEqualAfterSimplification("(root (variable 2.0 a) 0.0)", "1.0");
     102      // fixed point
     103      AssertEqualAfterSimplification("(root (variable 2.0 a) 1.0)", "(variable 2.0 a)");
     104      // inversion fixed point
     105      AssertEqualAfterSimplification("(root (variable 2.0 a) -1.0)", "(/ 1.0 (variable 2.0 a))");
     106      // inversion
     107      AssertEqualAfterSimplification("(root (variable 2.0 a) -2.0)", "(/ 1.0 (root (variable 2.0 a) 2.0))");
     108      // constant folding
     109      AssertEqualAfterSimplification("(root 9.0 2.0)", "3.0");
     110      #endregion
     111
    242112      #region boolean operations
    243       {
    244         // always true and
    245         var actualTree = simplifier.Simplify(importer.Import("(and 1.0 2.0)"));
    246         var expectedTree = importer.Import("1.0");
    247         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    248       }
    249       {
    250         // always false and
    251         var actualTree = simplifier.Simplify(importer.Import("(and 1.0 -2.0)"));
    252         var expectedTree = importer.Import("-1.0");
    253         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    254       }
    255       {
    256         // always true or
    257         var actualTree = simplifier.Simplify(importer.Import("(or -1.0 2.0)"));
    258         var expectedTree = importer.Import("1.0");
    259         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    260       }
    261       {
    262         // always false or
    263         var actualTree = simplifier.Simplify(importer.Import("(or -1.0 -2.0)"));
    264         var expectedTree = importer.Import("-1.0");
    265         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    266       }
    267       {
    268         // constant not
    269         var actualTree = simplifier.Simplify(importer.Import("(not -2.0)"));
    270         var expectedTree = importer.Import("1.0");
    271         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    272       }
    273       {
    274         // constant not
    275         var actualTree = simplifier.Simplify(importer.Import("(not 2.0)"));
    276         var expectedTree = importer.Import("-1.0");
    277         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    278       }
    279       {
    280         // constant not
    281         var actualTree = simplifier.Simplify(importer.Import("(not 0.0)"));
    282         var expectedTree = importer.Import("1.0");
    283         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    284       }
    285       {
    286         // nested nots
    287         var actualTree = simplifier.Simplify(importer.Import("(not (not 1.0))"));
    288         var expectedTree = importer.Import("1.0");
    289         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    290       }
    291       {
    292         // not of non-Boolean argument
    293         var actualTree = simplifier.Simplify(importer.Import("(not (variable 1.0 a))"));
    294         var expectedTree = importer.Import("(not (> (variable 1.0 a) 0.0))");
    295         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    296       }
    297       {
    298         // not Boolean argument
    299         var actualTree = simplifier.Simplify(importer.Import("(not (and (> (variable 1.0 a) 0.0) (> (variable 1.0 a) 0.0)))"));
    300         var expectedTree = importer.Import("(not (and (> (variable 1.0 a) 0.0) (> (variable 1.0 a) 0.0)))");
    301         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    302       }
    303       #endregion
     113      // always true and
     114      AssertEqualAfterSimplification("(and 1.0 2.0)", "1.0");
     115      // always false and
     116      AssertEqualAfterSimplification("(and 1.0 -2.0)", "-1.0");
     117      // always true or
     118      AssertEqualAfterSimplification("(or -1.0 2.0)", "1.0");
     119      // always false or
     120      AssertEqualAfterSimplification("(or -1.0 -2.0)", "-1.0");
     121      // constant not
     122      AssertEqualAfterSimplification("(not -2.0)", "1.0");
     123      // constant not
     124      AssertEqualAfterSimplification("(not 2.0)", "-1.0");
     125      // constant not
     126      AssertEqualAfterSimplification("(not 0.0)", "1.0");
     127      // nested nots
     128      AssertEqualAfterSimplification("(not (not 1.0))", "1.0");
     129      // not of non-Boolean argument
     130      AssertEqualAfterSimplification("(not (variable 1.0 a))", "(not (> (variable 1.0 a) 0.0))");
     131      // not Boolean argument
     132      AssertEqualAfterSimplification("(not (and (> (variable 1.0 a) 0.0) (> (variable 1.0 a) 0.0)))", "(not (and (> (variable 1.0 a) 0.0) (> (variable 1.0 a) 0.0)))");
     133      #endregion
     134
    304135      #region conditionals
    305       {
    306         // always false
    307         var actualTree = simplifier.Simplify(importer.Import("(if -1.0 (variable 2.0 a) (variable 3.0 a))"));
    308         var expectedTree = importer.Import("(variable 3.0 a)");
    309         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    310       }
    311       {
    312         // always true
    313         var actualTree = simplifier.Simplify(importer.Import("(if 1.0 (variable 2.0 a) (variable 3.0 a))"));
    314         var expectedTree = importer.Import("(variable 2.0 a)");
    315         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    316       }
    317       {
    318         // always false (0.0)
    319         var actualTree = simplifier.Simplify(importer.Import("(if 0.0 (variable 2.0 a) (variable 3.0 a))"));
    320         var expectedTree = importer.Import("(variable 3.0 a)");
    321         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    322       }
    323       {
    324         // complex constant condition (always false)
    325         var actualTree = simplifier.Simplify(importer.Import("(if (* 1.0 -2.0) (variable 2.0 a) (variable 3.0 a))"));
    326         var expectedTree = importer.Import("(variable 3.0 a)");
    327         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    328       }
    329       {
    330         // complex constant condition (always false)
    331         var actualTree = simplifier.Simplify(importer.Import("(if (/ (variable 1.0 a) (variable -2.0 a)) (variable 2.0 a) (variable 3.0 a))"));
    332         var expectedTree = importer.Import("(variable 3.0 a)");
    333         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    334       }
    335       {
    336         // insertion of relational operator
    337         var actualTree = simplifier.Simplify(importer.Import("(if (variable 1.0 a) (variable 2.0 a) (variable 3.0 a))"));
    338         var expectedTree = importer.Import("(if (> (variable 1.0 a) 0.0) (variable 2.0 a) (variable 3.0 a))");
    339         Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    340       }
     136      // always false
     137      AssertEqualAfterSimplification("(if -1.0 (variable 2.0 a) (variable 3.0 a))", "(variable 3.0 a)");
     138      // always true
     139      AssertEqualAfterSimplification("(if 1.0 (variable 2.0 a) (variable 3.0 a))", "(variable 2.0 a)");
     140      // always false (0.0)
     141      AssertEqualAfterSimplification("(if 0.0 (variable 2.0 a) (variable 3.0 a))", "(variable 3.0 a)");
     142      // complex constant condition (always false)
     143      AssertEqualAfterSimplification("(if (* 1.0 -2.0) (variable 2.0 a) (variable 3.0 a))", "(variable 3.0 a)");
     144      // complex constant condition (always false)
     145      AssertEqualAfterSimplification("(if (/ (variable 1.0 a) (variable -2.0 a)) (variable 2.0 a) (variable 3.0 a))", "(variable 3.0 a)");
     146      // insertion of relational operator
     147      AssertEqualAfterSimplification("(if (variable 1.0 a) (variable 2.0 a) (variable 3.0 a))", "(if (> (variable 1.0 a) 0.0) (variable 2.0 a) (variable 3.0 a))");
     148      #endregion
     149
     150      #region factor variables
     151      AssertEqualAfterSimplification("(factor a 1.0)", "(factor a 1.0)");
     152      // factor folding
     153      AssertEqualAfterSimplification("(+ (factor a 1.0 1.0) (factor a 2.0 3.0))", "(factor a 3.0 4.0)");
     154      AssertEqualAfterSimplification("(- (factor a 1.0 1.0) (factor a 2.0 3.0))", "(factor a -1.0 -2.0)");
     155      AssertEqualAfterSimplification("(* (factor a 2.0 2.0) (factor a 2.0 3.0))", "(factor a 4.0 6.0)");
     156      AssertEqualAfterSimplification("(/ (factor a 2.0 5.0))", "(factor a 0.5 0.2)");
     157      AssertEqualAfterSimplification("(/ (factor a 4.0 6.0) (factor a 2.0 3.0))", "(factor a 2.0 2.0)");
     158      AssertEqualAfterSimplification("(+ 3.0 (factor a 4.0 6.0))", "(factor a 7.0 9.0)");
     159      AssertEqualAfterSimplification("(+ (factor a 4.0 6.0) 3.0)", "(factor a 7.0 9.0)");
     160      AssertEqualAfterSimplification("(- 3.0 (factor a 4.0 6.0))", "(factor a -1.0 -3.0)");
     161      AssertEqualAfterSimplification("(- (factor a 4.0 6.0) 3.0)", "(factor a 1.0 3.0)");
     162      AssertEqualAfterSimplification("(* 2.0 (factor a 4.0 6.0))", "(factor a 8.0 12.0)");
     163      AssertEqualAfterSimplification("(* (factor a 4.0 6.0) 2.0)", "(factor a 8.0 12.0)");
     164      AssertEqualAfterSimplification("(* (factor a 4.0 6.0) (variable 2.0 a))", "(* (factor a 8.0 12.0) (variable 1.0 a))"); // not possible (a is used as factor and double variable) interpreter will fail
     165      AssertEqualAfterSimplification(
     166        "(log (factor a 10.0 100.0))",
     167        string.Format(CultureInfo.InvariantCulture, "(factor a {0} {1})", Math.Log(10.0), Math.Log(100.0)));
     168      AssertEqualAfterSimplification(
     169        "(exp (factor a 2.0 3.0))",
     170        string.Format(CultureInfo.InvariantCulture, "(factor a {0} {1})", Math.Exp(2.0), Math.Exp(3.0)));
     171      AssertEqualAfterSimplification("(sqrt (factor a 9.0 16.0))", "(factor a 3.0 4.0))");
     172      AssertEqualAfterSimplification("(sqr (factor a 2.0 3.0))", "(factor a 4.0 9.0))");
     173      AssertEqualAfterSimplification("(root (factor a 8.0 27.0) 3)", "(factor a 2.0 3.0))");
     174      AssertEqualAfterSimplification("(pow (factor a 2.0 3.0) 3)", "(factor a 8.0 27.0))");
     175
     176      AssertEqualAfterSimplification("(sin (factor a 1.0 2.0) )",
     177        string.Format(CultureInfo.InvariantCulture, "(factor a {0} {1}))", Math.Sin(1.0), Math.Sin(2.0)));
     178      AssertEqualAfterSimplification("(cos (factor a 1.0 2.0) )",
     179        string.Format(CultureInfo.InvariantCulture, "(factor a {0} {1}))", Math.Cos(1.0), Math.Cos(2.0)));
     180      AssertEqualAfterSimplification("(tan (factor a 1.0 2.0) )",
     181        string.Format(CultureInfo.InvariantCulture, "(factor a {0} {1}))", Math.Tan(1.0), Math.Tan(2.0)));
     182
     183
     184      AssertEqualAfterSimplification("(binfactor a val 1.0)", "(binfactor a val 1.0)");
     185      // binfactor folding
     186      AssertEqualAfterSimplification("(+ (binfactor a val 1.0) (binfactor a val 2.0))", "(binfactor a val 3.0)");
     187      AssertEqualAfterSimplification("(+ (binfactor a val0 1.0) (binfactor a val1 2.0))", "(+ (binfactor a val0 1.0) (binfactor a val1 2.0))"); // cannot be simplified (different vals)
     188      AssertEqualAfterSimplification("(+ (binfactor a val 1.0) (binfactor b val 2.0))", "(+ (binfactor a val 1.0) (binfactor b val 2.0))"); // cannot be simplified (different vars)
     189      AssertEqualAfterSimplification("(- (binfactor a val 1.0) (binfactor a val 2.0))", "(binfactor a val -1.0)");
     190      AssertEqualAfterSimplification("(* (binfactor a val 2.0) (binfactor a val 3.0))", "(binfactor a val 6.0)");
     191      AssertEqualAfterSimplification("(/ (binfactor a val 6.0) (binfactor a val 3.0))", "(/ (binfactor a val 6.0) (binfactor a val 3.0))"); // not allowed! 0/0 for other values than 'val'
     192      AssertEqualAfterSimplification("(/ (binfactor a val 4.0))", "(/ 1.0 (binfactor a val 4.0))"); // not allowed!
     193
     194      AssertEqualAfterSimplification("(+ 3.0 (binfactor a val 4.0 ))", "(+ (binfactor a val 4.0 ) 3.0))"); // not allowed
     195      AssertEqualAfterSimplification("(- 3.0 (binfactor a val 4.0 ))", "(+ (binfactor a val -4.0 ) 3.0)");
     196      AssertEqualAfterSimplification("(+ (binfactor a val 4.0 ) 3.0)", "(+ (binfactor a val 4.0 ) 3.0)");  // not allowed
     197      AssertEqualAfterSimplification("(- (binfactor a val 4.0 ) 3.0)", "(+ (binfactor a val 4.0 ) -3.0)");
     198      AssertEqualAfterSimplification("(* 2.0 (binfactor a val 4.0))", "(binfactor a val 8.0 )");
     199      AssertEqualAfterSimplification("(* (binfactor a val 4.0) 2.0)", "(binfactor a val 8.0 )");
     200      AssertEqualAfterSimplification("(* (binfactor a val 4.0) (variable 2.0 a))", "(* (binfactor a val 1.0) (variable 1.0 a) 8.0)");   // not possible (a is used as factor and double variable) interpreter will fail
     201      AssertEqualAfterSimplification("(log (binfactor a val 10.0))", "(log (binfactor a val 10.0))"); // not allowed (log(0))
     202
     203      // exp( binfactor w val=a) = if(val=a) exp(w) else exp(0) = binfactor( (exp(w) - 1) val a) + 1
     204      AssertEqualAfterSimplification("(exp (binfactor a val 3.0))",
     205        string.Format(CultureInfo.InvariantCulture, "(+ (binfactor a val {0}) 1.0)", Math.Exp(3.0) - 1)
     206        );
     207      AssertEqualAfterSimplification("(sqrt (binfactor a val 16.0))", "(binfactor a val 4.0))"); // sqrt(0) = 0
     208      AssertEqualAfterSimplification("(sqr (binfactor a val 3.0))", "(binfactor a val 9.0))"); // 0*0 = 0
     209      AssertEqualAfterSimplification("(root (binfactor a val 27.0) 3)", "(binfactor a val 3.0))");
     210      AssertEqualAfterSimplification("(pow (binfactor a val 3.0) 3)", "(binfactor a val 27.0))");
     211
     212      AssertEqualAfterSimplification("(sin (binfactor a val 2.0) )",
     213        string.Format(CultureInfo.InvariantCulture, "(binfactor a val {0}))", Math.Sin(2.0))); // sin(0) = 0
     214      AssertEqualAfterSimplification("(cos (binfactor a val 2.0) )",
     215        string.Format(CultureInfo.InvariantCulture, "(+ (binfactor a val {0}) 1.0)", Math.Cos(2.0) - 1)); // cos(0) = 1
     216      AssertEqualAfterSimplification("(tan (binfactor a val 2.0) )",
     217        string.Format(CultureInfo.InvariantCulture, "(binfactor a val {0}))", Math.Tan(2.0))); // tan(0) = 0
     218
     219      // combination of factor and binfactor
     220      AssertEqualAfterSimplification("(+ (binfactor a x0 2.0) (factor a 2.0 3.0))", "(factor a 4.0 3.0)");
     221      AssertEqualAfterSimplification("(+ (factor a 2.0 3.0) (binfactor a x0 2.0))", "(factor a 4.0 3.0)");
     222      AssertEqualAfterSimplification("(* (binfactor a x1 2.0) (factor a 2.0 3.0))", "(binfactor a x1 6.0)"); // all other values have weight zero in binfactor
     223      AssertEqualAfterSimplification("(* (factor a 2.0 3.0) (binfactor a x1 2.0))", "(binfactor a x1 6.0)"); // all other values have weight zero in binfactor
     224      AssertEqualAfterSimplification("(/ (binfactor a x0 2.0) (factor a 2.0 3.0))", "(binfactor a x0 1.0)");
     225      AssertEqualAfterSimplification("(/ (factor a 2.0 3.0) (binfactor a x0 2.0))",
     226        string.Format(CultureInfo.InvariantCulture, "(factor a 1.0 {0})", 3.0 / 0.0));
     227      AssertEqualAfterSimplification("(- (binfactor a x0 2.0) (factor a 2.0 3.0))", "(factor a 0.0 -3.0)");
     228      AssertEqualAfterSimplification("(- (factor a 2.0 3.0) (binfactor a x0 2.0))", "(factor a 0.0 3.0)");
    341229      #endregion
    342230    }
    343231
    344     private void AssertEqualEnumerations(IEnumerable<double> expected, IEnumerable<double> actual) {
    345       var expectedEnumerator = expected.GetEnumerator();
    346       var actualEnumerator = actual.GetEnumerator();
    347       while (expectedEnumerator.MoveNext() & actualEnumerator.MoveNext()) {
    348         Assert.AreEqual(expectedEnumerator.Current, actualEnumerator.Current, Math.Abs(1E-6 * expectedEnumerator.Current));
    349       }
    350       if (expectedEnumerator.MoveNext() | actualEnumerator.MoveNext())
    351         Assert.Fail("Number of elements in enumerations do not match");
     232
     233    private void AssertEqualAfterSimplification(string original, string expected) {
     234      var formatter = new SymbolicExpressionTreeStringFormatter();
     235      var importer = new SymbolicExpressionImporter();
     236      var actualTree = TreeSimplifier.Simplify(importer.Import(original));
     237      var expectedTree = importer.Import(expected);
     238      Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
     239
    352240    }
    353241  }
    354242}
     243
Note: See TracChangeset for help on using the changeset viewer.