Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/15/11 10:34:38 (14 years ago)
Author:
gkronber
Message:

#1227 implemented transformations in simplifier to successfully run current set of test cases.

Location:
trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Symbolic/SymbolicSimplifier.cs

    r5455 r5461  
    3535  public class SymbolicSimplifier {
    3636    private Addition addSymbol = new Addition();
     37    private Subtraction subSymbol = new Subtraction();
    3738    private Multiplication mulSymbol = new Multiplication();
    3839    private Division divSymbol = new Division();
     
    151152    private bool IsLessThan(SymbolicExpressionTreeNode node) {
    152153      return node.Symbol is LessThan;
     154    }
     155
     156    private bool IsBoolean(SymbolicExpressionTreeNode node) {
     157      return
     158        node.Symbol is GreaterThan ||
     159        node.Symbol is LessThan ||
     160        node.Symbol is And ||
     161        node.Symbol is Or;
    153162    }
    154163
     
    439448      } else {
    440449        var ifNode = ifThenElseSymbol.CreateTreeNode();
    441         ifNode.AddSubTree(condition);
     450        if (IsBoolean(condition)) {
     451          ifNode.AddSubTree(condition);
     452        } else {
     453          var gtNode = gtSymbol.CreateTreeNode();
     454          gtNode.AddSubTree(condition); gtNode.AddSubTree(MakeConstant(0.0));
     455          ifNode.AddSubTree(gtNode);
     456        }
    442457        ifNode.AddSubTree(trueBranch);
    443458        ifNode.AddSubTree(falseBranch);
     
    445460      }
    446461    }
     462
    447463    private SymbolicExpressionTreeNode MakeSine(SymbolicExpressionTreeNode node) {
    448464      // todo implement more transformation rules
     
    483499        var constT = node as ConstantTreeNode;
    484500        return MakeConstant(Math.Exp(constT.Value));
     501      } else if (IsLog(node)) {
     502        return node.SubTrees[0];
    485503      } else {
    486504        var expNode = expSymbol.CreateTreeNode();
     
    494512        var constT = node as ConstantTreeNode;
    495513        return MakeConstant(Math.Log(constT.Value));
     514      } else if (IsExp(node)) {
     515        return node.SubTrees[0];
     516      } else if (IsMultiplication(node)) {
     517        return node.SubTrees.Select(s => MakeLog(s)).Aggregate((x, y) => MakeSum(x, y));
     518      } else if (IsDivision(node)) {
     519        var subtractionNode = subSymbol.CreateTreeNode();
     520        foreach (var subTree in node.SubTrees) {
     521          subtractionNode.AddSubTree(MakeLog(subTree));
     522        }
     523        return subtractionNode;
    496524      } else {
    497525        var logNode = logSymbol.CreateTreeNode();
     
    503531
    504532    // MakeFraction, MakeProduct and MakeSum take two already simplified trees and create a new simplified tree
    505 
    506533    private SymbolicExpressionTreeNode MakeFraction(SymbolicExpressionTreeNode a, SymbolicExpressionTreeNode b) {
    507534      if (IsConstant(a) && IsConstant(b)) {
     
    515542        ((VariableTreeNode)a).Weight /= constB;
    516543        return a;
     544      } else if (IsVariable(a) && IsVariable(b) && AreSameVariable(a, b)) {
     545        // cancel variables
     546        var aVar = a as VariableTreeNode;
     547        var bVar = b as VariableTreeNode;
     548        return MakeConstant(aVar.Weight / bVar.Weight);
    517549      } else if (IsAddition(a) && IsConstant(b)) {
    518550        return a.SubTrees
     
    574606        }
    575607        MergeVariablesInSum(add);
    576         return add;
     608        if (add.SubTrees.Count == 1) {
     609          return add.SubTrees[0];
     610        } else {
     611          return add;
     612        }
    577613      } else if (IsAddition(b)) {
    578614        return MakeSum(b, a);
     
    596632        }
    597633        MergeVariablesInSum(add);
    598         return add;
     634        if (add.SubTrees.Count == 1) {
     635          return add.SubTrees[0];
     636        } else {
     637          return add;
     638        }
    599639      } else {
    600640        var add = addSymbol.CreateTreeNode();
     
    602642        add.AddSubTree(b);
    603643        MergeVariablesInSum(add);
    604         return add;
     644        if (add.SubTrees.Count == 1) {
     645          return add.SubTrees[0];
     646        } else {
     647          return add;
     648        }
    605649      }
    606650    }
    607651
    608652    // makes sure variable symbols in sums are combined
    609     // possible improvment: combine sums of products where the products only reference the same variable
     653    // possible improvement: combine sums of products where the products only reference the same variable
    610654    private void MergeVariablesInSum(SymbolicExpressionTreeNode sum) {
    611655      var subtrees = new List<SymbolicExpressionTreeNode>(sum.SubTrees);
     
    683727    #endregion
    684728
     729
     730    #region helper functions
     731
     732    private bool AreSameVariable(SymbolicExpressionTreeNode a, SymbolicExpressionTreeNode b) {
     733      var aLaggedVar = a as LaggedVariableTreeNode;
     734      var bLaggedVar = b as LaggedVariableTreeNode;
     735      if (aLaggedVar != null && bLaggedVar != null) {
     736        return aLaggedVar.VariableName == bLaggedVar.VariableName &&
     737          aLaggedVar.Lag == bLaggedVar.Lag;
     738      }
     739      var aVar = a as VariableTreeNode;
     740      var bVar = b as VariableTreeNode;
     741      if (aVar != null && bVar != null) {
     742        return aVar.VariableName == bVar.VariableName;
     743      }
     744      return false;
     745    }
     746
    685747    // helper to combine the constant factors in products and to combine variables (powers of 2, 3...)
    686748    private void MergeVariablesAndConstantsInProduct(SymbolicExpressionTreeNode prod) {
     
    727789
    728790
    729     #region helper functions
    730791    /// <summary>
    731792    /// x => x * -1
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Tests/SymbolicSimplifierTest.cs

    r5460 r5461  
    140140      }
    141141      #endregion
     142      #region constant and variable folding
     143      {
     144        var actualTree = simplifier.Simplify(importer.Import("(+ 1.0 2.0)"));
     145        var expectedTree = importer.Import("3.0");
     146        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
     147      }
     148      {
     149        var actualTree = simplifier.Simplify(importer.Import("(+ (variable 2.0 a) (variable 2.0 a))"));
     150        var expectedTree = importer.Import("(variable 4.0 a)");
     151        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
     152      }
     153      {
     154        var actualTree = simplifier.Simplify(importer.Import("(- (variable 2.0 a) (variable 1.0 a))"));
     155        var expectedTree = importer.Import("(variable 1.0 a)");
     156        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
     157      }
     158      {
     159        var actualTree = simplifier.Simplify(importer.Import("(* (variable 2.0 a) (variable 2.0 a))"));
     160        var expectedTree = importer.Import("(* (* (variable 1.0 a) (variable 1.0 a)) 4.0)");
     161        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
     162      }
     163      {
     164        var actualTree = simplifier.Simplify(importer.Import("(/ (variable 1.0 a) (variable 2.0 a))"));
     165        var expectedTree = importer.Import("0.5");
     166        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
     167      }
     168      #endregion
    142169      #region logarithm rules
    143170      {
     
    155182      {
    156183        // 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)))");
     184        var actualTree = simplifier.Simplify(importer.Import("(log (* (variable 2.0 a) (variable 3.0 b)))"));
     185        var expectedTree = importer.Import("(+ (log (variable 1.0 a)) (log (variable 1.0 b)) 1.7918)");
    159186        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    160187      }
    161188      {
    162189        // log transformation
    163         var actualTree = simplifier.Simplify(importer.Import("(log (/ (variable 2.0 a) (variable 3.0 b))"));
     190        var actualTree = simplifier.Simplify(importer.Import("(log (/ (variable 2.0 a) (variable 3.0 b)))"));
    164191        var expectedTree = importer.Import("(- (log (variable 2.0 a)) (log (variable 3.0 b)))");
    165192        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
     
    194221        // constant not
    195222        var actualTree = simplifier.Simplify(importer.Import("(not -2.0)"));
    196         var expectedTree = importer.Import("1.0");
     223        var expectedTree = importer.Import("2.0");
    197224        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    198225      }
     
    200227        // constant not
    201228        var actualTree = simplifier.Simplify(importer.Import("(not 2.0)"));
    202         var expectedTree = importer.Import("-1.0");
     229        var expectedTree = importer.Import("-2.0");
    203230        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    204231      }
     
    206233        // constant not
    207234        var actualTree = simplifier.Simplify(importer.Import("(not 0.0)"));
    208         var expectedTree = importer.Import("1.0");
     235        var expectedTree = importer.Import("0.0");
    209236        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    210237      }
     
    245272        var actualTree = simplifier.Simplify(importer.Import("(if (variable 1.0 a) (variable 2.0 a) (variable 3.0 a))"));
    246273        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");
    274274        Assert.AreEqual(formatter.Format(expectedTree), formatter.Format(actualTree));
    275275      }
Note: See TracChangeset for help on using the changeset viewer.