Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/27/21 14:10:56 (4 years ago)
Author:
pfleck
Message:

#3040 Merged trunk into branch.

Location:
branches/3040_VectorBasedGP
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/3040_VectorBasedGP

  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic

  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/TreeSimplifier.cs

    r17180 r17825  
    933933        var constB = b as ConstantTreeNode;
    934934        var constBValue = Math.Round(constB.Value);
    935         if (constBValue.IsAlmost(1.0)) {
     935        if (constBValue == 1.0) {
     936          // root(a, 1) => a
    936937          return a;
    937         } else if (constBValue.IsAlmost(0.0)) {
    938           return MakeConstant(1.0);
    939         } else if (constBValue.IsAlmost(-1.0)) {
     938        } else if (constBValue == 0.0) {
     939          // root(a, 0) is not defined
     940          //return MakeConstant(1.0);
     941          return MakeConstant(double.NaN);
     942        } else if (constBValue == -1.0) {
     943          // root(a, -1) => a^(-1/1) => 1/a
    940944          return MakeFraction(MakeConstant(1.0), a);
    941945        } else if (constBValue < 0) {
     946          // root(a, -b) => a^(-1/b) => (1/a)^(1/b) => root(1, b) / root(a, b) => 1 / root(a, b)
    942947          var rootNode = rootSymbol.CreateTreeNode();
    943948          rootNode.AddSubtree(a);
     
    987992        var constB = b as ConstantTreeNode;
    988993        double exponent = Math.Round(constB.Value);
    989         if (exponent.IsAlmost(0.0)) {
     994        if (exponent == 0.0) {
     995          // a^0 => 1
    990996          return MakeConstant(1.0);
    991         } else if (exponent.IsAlmost(1.0)) {
     997        } else if (exponent == 1.0) {
     998          // a^1 => a
    992999          return a;
    993         } else if (exponent.IsAlmost(-1.0)) {
     1000        } else if (exponent == -1.0) {
     1001          // a^-1 => 1/a
    9941002          return MakeFraction(MakeConstant(1.0), a);
    9951003        } else if (exponent < 0) {
     1004          // a^-b => (1/a)^b => 1/(a^b)
    9961005          var powNode = powSymbol.CreateTreeNode();
    9971006          powNode.AddSubtree(a);
     
    10181027        // fold constants
    10191028        return MakeConstant(((ConstantTreeNode)a).Value / ((ConstantTreeNode)b).Value);
    1020       } else if ((IsConstant(a) && !((ConstantTreeNode)a).Value.IsAlmost(1.0))) {
     1029      } else if ((IsConstant(a) && ((ConstantTreeNode)a).Value != 1.0)) {
     1030        // a / x => (a * 1/a) / (x * 1/a) => 1 / (x * 1/a)
    10211031        return MakeFraction(MakeConstant(1.0), MakeProduct(b, Invert(a)));
    10221032      } else if (IsVariableBase(a) && IsConstant(b)) {
     
    10941104        // b is not constant => make sure constant is on the right
    10951105        return MakeSum(b, a);
    1096       } else if (IsConstant(b) && ((ConstantTreeNode)b).Value.IsAlmost(0.0)) {
     1106      } else if (IsConstant(b) && ((ConstantTreeNode)b).Value == 0.0) {
    10971107        // x + 0 => x
    10981108        return a;
     
    12101220      foreach (var unchangedSubtree in unchangedSubtrees)
    12111221        sum.AddSubtree(unchangedSubtree);
    1212       if (!constant.IsAlmost(0.0)) {
     1222      if (constant != 0.0) {
    12131223        sum.AddSubtree(MakeConstant(constant));
    12141224      }
     
    12681278        if (wi < 0) throw new ArgumentException();
    12691279        return MakeBinFactor(node1.Symbol, node1.VariableName, node1.VariableValue, node1.Weight * node0.Weights[wi]);
    1270       } else if (IsConstant(b) && ((ConstantTreeNode)b).Value.IsAlmost(1.0)) {
     1280      } else if (IsConstant(b) && ((ConstantTreeNode)b).Value == 1.0) {
    12711281        // $ * 1.0 => $
    12721282        return a;
    1273       } else if (IsConstant(b) && ((ConstantTreeNode)b).Value.IsAlmost(0.0)) {
     1283      } else if (IsConstant(b) && ((ConstantTreeNode)b).Value == 0.0) {
    12741284        return MakeConstant(0);
    12751285      } else if (IsConstant(b) && IsVariableBase(a)) {
     
    14191429        prod.AddSubtree(unchangedSubtree);
    14201430
    1421       if (!constantProduct.IsAlmost(1.0)) {
     1431      if (constantProduct != 1.0) {
    14221432        prod.AddSubtree(MakeConstant(constantProduct));
    14231433      }
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/TreeToAutoDiffTermConverter.cs

    r17180 r17825  
    258258      if (node.Symbol is CubeRoot) {
    259259        return cbrt(ConvertToAutoDiff(node.GetSubtree(0)));
     260      }
     261      if (node.Symbol is Power) {
     262        var powerNode = node.GetSubtree(1) as ConstantTreeNode;
     263        if (powerNode == null)
     264          throw new NotSupportedException("Only integer powers are allowed in parameter optimization. Try to use exp() and log() instead of the power symbol.");
     265        var intPower = Math.Truncate(powerNode.Value);
     266        if (intPower != powerNode.Value)
     267          throw new NotSupportedException("Only integer powers are allowed in parameter optimization. Try to use exp() and log() instead of the power symbol.");
     268        return AutoDiff.TermBuilder.Power(ConvertToAutoDiff(node.GetSubtree(0)), intPower);
    260269      }
    261270      if (node.Symbol is Sine) {
     
    340349          !(n.Symbol is AnalyticQuotient) &&
    341350          !(n.Symbol is Cube) &&
    342           !(n.Symbol is CubeRoot)
     351          !(n.Symbol is CubeRoot) &&
     352          !(n.Symbol is Power)
    343353        select n).Any();
    344354      return !containsUnknownSymbol;
Note: See TracChangeset for help on using the changeset viewer.