Ignore:
Timestamp:
06/25/19 23:14:06 (14 months ago)
Author:
mkommend
Message:

#2435: Updated branch with most recent trunk changes.

Location:
branches/2435-alglib_3_15
Files:
1 deleted
27 edited
1 copied

Legend:

Unmodified
Added
Removed
  • branches/2435-alglib_3_15

  • branches/2435-alglib_3_15/HeuristicLab.Problems.DataAnalysis.Symbolic

  • branches/2435-alglib_3_15/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers

    • Property svn:ignore set to
      SymbolicDataAnalysisBuildingBlockAnalyzer.cs
      SymbolicDataAnalysisHashBasedDiversityAnalyzer.cs
  • branches/2435-alglib_3_15/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/DerivativeCalculator.cs

    r16565 r17034  
    8383        if (branch.SubtreeCount >= 2) {
    8484          var f = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone();
    85           var g = (ISymbolicExpressionTreeNode)branch.GetSubtree(1).Clone();
    8685          var fprime = Derive(f, variableName);
    87           var gprime = Derive(g, variableName);
    88           var fgPrime = Sum(Product(f, gprime), Product(fprime, g));
    89           for (int i = 2; i < branch.SubtreeCount; i++) {
     86          for (int i = 1; i < branch.SubtreeCount; i++) {
     87            var g = (ISymbolicExpressionTreeNode)branch.GetSubtree(i).Clone();
    9088            var fg = Product((ISymbolicExpressionTreeNode)f.Clone(), (ISymbolicExpressionTreeNode)g.Clone());
    91             var h = (ISymbolicExpressionTreeNode)branch.GetSubtree(i).Clone();
    92             var hPrime = Derive(h, variableName);
    93             fgPrime = Sum(Product(fgPrime, h), Product(fg, hPrime));
     89            var gPrime = Derive(g, variableName);
     90            var fgPrime = Sum(Product(fprime, g), Product(gPrime, f));
     91            // prepare for next iteration
     92            f = fg;
     93            fprime = fgPrime;
    9494          }
    95           return fgPrime;
     95          return fprime;
    9696        } else
    9797          // multiplication with only one argument has no effect -> derive the argument
     
    143143        var f = (ISymbolicExpressionTreeNode)branch.Clone();
    144144        var u = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone();
    145         return Product(Div(CreateConstant(1.0), Product(CreateConstant(3.0), Square(f))), Derive(u, variableName));
     145        return Product(Div(CreateConstant(1.0), Product(CreateConstant(3.0), Square(f))), Derive(u, variableName));  // 1/3 1/cbrt(f(x))^2 d/dx f(x)
    146146      }
    147147      if (branch.Symbol is Cube) {
  • branches/2435-alglib_3_15/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/LinearModelToTreeConverter.cs

    r16565 r17034  
    3838      double @const = 0) {
    3939
    40       if (factorCoefficients.Length == 0 && coefficients.Length == 0) throw new ArgumentException();
     40      if (factorCoefficients.Length == 0 && coefficients.Length == 0 && @const==0) throw new ArgumentException();
     41
     42      // Combine both trees
     43      ISymbolicExpressionTreeNode add = (new Addition()).CreateTreeNode();
    4144
    4245      // Create tree for double variables
    43       ISymbolicExpressionTree tree = null;     
    4446      if (coefficients.Length > 0) {
    45         tree = CreateTree(variableNames, new int[variableNames.Length], coefficients, @const);
    46         if (factorCoefficients.Length == 0) return tree;
     47        var varTree = CreateTree(variableNames, new int[variableNames.Length], coefficients);
     48        foreach (var varNode in varTree.IterateNodesPrefix().OfType<VariableTreeNode>())
     49          add.AddSubtree(varNode);
    4750      }
    4851
    4952      // Create tree for string variables
    50       ISymbolicExpressionTree factorTree = null;     
    5153      if (factorCoefficients.Length > 0) {
    52         factorTree = CreateTree(factors, factorCoefficients, @const);
    53         if (tree == null) return factorTree; 
     54        var factorTree = CreateTree(factors, factorCoefficients);
     55        foreach (var binFactorNode in factorTree.IterateNodesPrefix().OfType<BinaryFactorVariableTreeNode>())
     56          add.AddSubtree(binFactorNode);
    5457      }
    5558
    56       // Combine both trees
    57       ISymbolicExpressionTreeNode add = tree.Root.GetSubtree(0).GetSubtree(0);
    58       foreach (var binFactorNode in factorTree.IterateNodesPrefix().OfType<BinaryFactorVariableTreeNode>())
    59         add.InsertSubtree(add.SubtreeCount - 1, binFactorNode);
     59      if (@const!=0.0) {
     60        ConstantTreeNode cNode = (ConstantTreeNode)new Constant().CreateTreeNode();
     61        cNode.Value = @const;
     62        add.AddSubtree(cNode);
     63      }
     64
     65      ISymbolicExpressionTree tree = new SymbolicExpressionTree(new ProgramRootSymbol().CreateTreeNode());
     66      ISymbolicExpressionTreeNode startNode = new StartSymbol().CreateTreeNode();
     67      tree.Root.AddSubtree(startNode);
     68      startNode.AddSubtree(add);
    6069      return tree;
    61 
    62       throw new ArgumentException();
    6370    }
    6471
  • branches/2435-alglib_3_15/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/TreeToAutoDiffTermConverter.cs

    r16656 r17034  
    9292      );
    9393
     94    private static readonly Func<Term, UnaryFunc> cbrt = UnaryFunc.Factory(
     95      eval: x => x < 0 ? -Math.Pow(-x, 1.0 / 3) : Math.Pow(x, 1.0 / 3),
     96      diff: x => { var cbrt_x = x < 0 ? -Math.Pow(-x, 1.0 / 3) : Math.Pow(x, 1.0 / 3); return 1.0 / (3 * cbrt_x * cbrt_x); }
     97      );
     98
     99
     100
    94101    #endregion
    95102
     
    250257      }
    251258      if (node.Symbol is CubeRoot) {
    252         return AutoDiff.TermBuilder.Power(
    253           ConvertToAutoDiff(node.GetSubtree(0)), 1.0/3.0);
     259        return cbrt(ConvertToAutoDiff(node.GetSubtree(0)));
    254260      }
    255261      if (node.Symbol is Sine) {
  • branches/2435-alglib_3_15/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionDiversityPreservingCrossover.cs

    r16565 r17034  
    1 using System;
     1#region License Information
     2/* HeuristicLab
     3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     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
     22using System;
    223using System.Collections.Generic;
    324using System.Linq;
    4 
     25using HEAL.Attic;
    526using HeuristicLab.Common;
    627using HeuristicLab.Core;
     
    829using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    930using HeuristicLab.Parameters;
    10 using HEAL.Attic;
    1131using HeuristicLab.Random;
    1232using static HeuristicLab.Problems.DataAnalysis.Symbolic.SymbolicExpressionHashExtensions;
    1333
    1434namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    15   [Item("DiversityCrossover", "Simple crossover operator prioritizing internal nodes according to the given probability.")]
     35  [Item("DiversityCrossover", "Simple crossover operator preventing swap between subtrees with the same hash value.")]
    1636  [StorableType("ED35B0D9-9704-4D32-B10B-8F9870E76781")]
    1737  public sealed class SymbolicDataAnalysisExpressionDiversityPreservingCrossover<T> : SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData {
     
    2040    private const string WindowingParameterName = "Windowing";
    2141    private const string ProportionalSamplingParameterName = "ProportionalSampling";
     42    private const string StrictHashingParameterName = "StrictHashing";
    2243
    2344    private static readonly Func<byte[], ulong> hashFunction = HashUtil.JSHash;
     
    3556      get { return (IValueLookupParameter<BoolValue>)Parameters[ProportionalSamplingParameterName]; }
    3657    }
     58
     59    public IFixedValueParameter<BoolValue> StrictHashingParameter {
     60      get { return (IFixedValueParameter<BoolValue>)Parameters[StrictHashingParameterName]; }
     61    }
    3762    #endregion
    3863
     
    4974      get { return ProportionalSamplingParameter.ActualValue; }
    5075    }
     76
     77    bool StrictHashing {
     78      get { return StrictHashingParameter.Value.Value; }
     79    }
    5180    #endregion
     81
     82
     83    [StorableHook(HookType.AfterDeserialization)]
     84    private void AfterDeserialization() {
     85      if (!Parameters.ContainsKey(StrictHashingParameterName)) {
     86        Parameters.Add(new FixedValueParameter<BoolValue>(StrictHashingParameterName, "Use strict hashing when calculating subtree hash values."));
     87      }
     88    }
    5289
    5390    public SymbolicDataAnalysisExpressionDiversityPreservingCrossover() {
     
    5693      Parameters.Add(new ValueLookupParameter<BoolValue>(WindowingParameterName, "Use proportional sampling with windowing for cutpoint selection.", new BoolValue(false)));
    5794      Parameters.Add(new ValueLookupParameter<BoolValue>(ProportionalSamplingParameterName, "Select cutpoints proportionally using probabilities as weights instead of randomly.", new BoolValue(true)));
     95      Parameters.Add(new FixedValueParameter<BoolValue>(StrictHashingParameterName, "Use strict hashing when calculating subtree hash values."));
    5896    }
    5997
     
    72110    }
    73111
    74     public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, double internalCrossoverPointProbability, int maxLength, int maxDepth, bool windowing, bool proportionalSampling = false) {
    75       var leafCrossoverPointProbability = 1 - internalCrossoverPointProbability;
    76 
    77       var nodes0 = ActualRoot(parent0).MakeNodes().Sort(hashFunction);
    78       var nodes1 = ActualRoot(parent1).MakeNodes().Sort(hashFunction);
     112    public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, double internalCrossoverPointProbability, int maxLength, int maxDepth, bool windowing, bool proportionalSampling = false, bool strictHashing = false) {
     113      var nodes0 = ActualRoot(parent0).MakeNodes(strictHashing).Sort(hashFunction);
     114      var nodes1 = ActualRoot(parent1).MakeNodes(strictHashing).Sort(hashFunction);
    79115
    80116      IList<HashNode<ISymbolicExpressionTreeNode>> sampled0;
     
    126162      var proportionalSampling = ProportionalSampling.Value;
    127163
    128       return Cross(random, parent0, parent1, internalCrossoverPointProbability, maxLength, maxDepth, windowing, proportionalSampling);
     164      return Cross(random, parent0, parent1, internalCrossoverPointProbability, maxLength, maxDepth, windowing, proportionalSampling, StrictHashing);
    129165    }
    130166
  • branches/2435-alglib_3_15/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/InfixExpressionFormatter.cs

    r16565 r17034  
    5151    }
    5252
    53     public string Format(ISymbolicExpressionTree symbolicExpressionTree) {
     53    /// <summary>
     54    /// Produces an infix expression for a given expression tree.
     55    /// </summary>
     56    /// <param name="symbolicExpressionTree">The tree representation of the expression.</param>
     57    /// <param name="numberFormat">Number format that should be used for numeric parameters (e.g. NumberFormatInfo.InvariantInfo (default)).</param>
     58    /// <param name="formatString">The format string for numeric parameters (e.g. \"G4\" to limit to 4 digits, default is \"G\")</param>
     59    /// <returns>Infix expression</returns>
     60    public string Format(ISymbolicExpressionTree symbolicExpressionTree, NumberFormatInfo numberFormat, string formatString="G") {
    5461      // skip root and start symbols
    5562      StringBuilder strBuilder = new StringBuilder();
    56       FormatRecursively(symbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0), strBuilder);
     63      FormatRecursively(symbolicExpressionTree.Root.GetSubtree(0).GetSubtree(0), strBuilder, numberFormat, formatString);
    5764      return strBuilder.ToString();
    5865    }
    5966
    60     private void FormatRecursively(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
     67    public string Format(ISymbolicExpressionTree symbolicExpressionTree) {
     68      return Format(symbolicExpressionTree, NumberFormatInfo.InvariantInfo);
     69    }
     70
     71    private static void FormatRecursively(ISymbolicExpressionTreeNode node, StringBuilder strBuilder, NumberFormatInfo numberFormat, string formatString) {
    6172      if (node.SubtreeCount > 1) {
    6273        var token = GetToken(node.Symbol);
     
    6677            token == "^") {
    6778          strBuilder.Append("(");
    68           FormatRecursively(node.Subtrees.First(), strBuilder);
     79          FormatRecursively(node.Subtrees.First(), strBuilder, numberFormat, formatString);
    6980
    7081          foreach (var subtree in node.Subtrees.Skip(1)) {
    7182            strBuilder.Append(" ").Append(token).Append(" ");
    72             FormatRecursively(subtree, strBuilder);
     83            FormatRecursively(subtree, strBuilder, numberFormat, formatString);
    7384          }
    7485          strBuilder.Append(")");
     
    7687          // function with multiple arguments
    7788          strBuilder.Append(token).Append("(");
    78           FormatRecursively(node.Subtrees.First(), strBuilder);
     89          FormatRecursively(node.Subtrees.First(), strBuilder, numberFormat, formatString);
    7990          foreach (var subtree in node.Subtrees.Skip(1)) {
    8091            strBuilder.Append(", ");
    81             FormatRecursively(subtree, strBuilder);
     92            FormatRecursively(subtree, strBuilder, numberFormat, formatString);
    8293          }
    8394          strBuilder.Append(")");
     
    8798        if (token == "-" || token == "NOT") {
    8899          strBuilder.Append("(").Append(token).Append("(");
    89           FormatRecursively(node.GetSubtree(0), strBuilder);
     100          FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString);
    90101          strBuilder.Append("))");
    91102        } else if (token == "/") {
    92103          strBuilder.Append("1/");
    93           FormatRecursively(node.GetSubtree(0), strBuilder);
     104          FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString);
    94105        } else if (token == "+" || token == "*") {
    95           FormatRecursively(node.GetSubtree(0), strBuilder);
     106          FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString);
    96107        } else {
    97108          // function with only one argument
    98109          strBuilder.Append(token).Append("(");
    99           FormatRecursively(node.GetSubtree(0), strBuilder);
     110          FormatRecursively(node.GetSubtree(0), strBuilder, numberFormat, formatString);
    100111          strBuilder.Append(")");
    101112        }
     
    106117          if (!varNode.Weight.IsAlmost(1.0)) {
    107118            strBuilder.Append("(");
    108             strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", varNode.Weight);
     119            strBuilder.Append(varNode.Weight.ToString(formatString, numberFormat));
    109120            strBuilder.Append("*");
    110121          }
     
    116127          }
    117128          strBuilder.Append(", ")
    118             .AppendFormat(CultureInfo.InvariantCulture, "{0}", varNode.Lag)
     129            .AppendFormat(numberFormat, "{0}", varNode.Lag)
    119130            .Append(")");
    120131        } else if (node.Symbol is Variable) {
     
    122133          if (!varNode.Weight.IsAlmost(1.0)) {
    123134            strBuilder.Append("(");
    124             strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", varNode.Weight);
     135            strBuilder.Append(varNode.Weight.ToString(formatString, numberFormat));
    125136            strBuilder.Append("*");
    126137          }
     
    141152          }
    142153          strBuilder.AppendFormat("[{0}]",
    143             string.Join(", ", factorNode.Weights.Select(w => w.ToString(CultureInfo.InvariantCulture))));
     154            string.Join(", ", factorNode.Weights.Select(w => w.ToString(formatString, numberFormat))));
    144155        } else if (node.Symbol is BinaryFactorVariable) {
    145156          var factorNode = node as BinaryFactorVariableTreeNode;
    146157          if (!factorNode.Weight.IsAlmost(1.0)) {
    147158            strBuilder.Append("(");
    148             strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", factorNode.Weight);
     159            strBuilder.Append(factorNode.Weight.ToString(formatString, numberFormat));
    149160            strBuilder.Append("*");
    150161          }
     
    168179          var constNode = node as ConstantTreeNode;
    169180          if (constNode.Value >= 0.0)
    170             strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", constNode.Value);
     181            strBuilder.Append(constNode.Value.ToString(formatString, numberFormat));
    171182          else
    172             strBuilder.AppendFormat(CultureInfo.InvariantCulture, "({0})", constNode.Value); // (-1
     183            strBuilder.Append("(").Append(constNode.Value.ToString(formatString, numberFormat)).Append(")"); // (-1
    173184        }
    174185      }
    175186    }
    176187
    177     private string GetToken(ISymbol symbol) {
     188    private static string GetToken(ISymbol symbol) {
    178189      var tok = InfixExpressionParser.knownSymbols.GetBySecond(symbol).FirstOrDefault();
    179190      if (tok == null)
  • branches/2435-alglib_3_15/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionCSharpFormatter.cs

    r16565 r17034  
    2626using System.Text;
    2727using System.Text.RegularExpressions;
     28using HEAL.Attic;
    2829using HeuristicLab.Common;
    2930using HeuristicLab.Core;
    3031using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    31 using HEAL.Attic;
    3232
    3333namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
     
    5656    }
    5757
    58     private string VariableName2Identifier(string name) {     
     58    private string VariableName2Identifier(string name) {
    5959      /*
    6060       * identifier-start-character:
     
    131131        } else if (node.Symbol is Tangent) {
    132132          FormatFunction(node, "Math.Tan", strBuilder);
     133        } else if (node.Symbol is HyperbolicTangent) {
     134          FormatFunction(node, "Math.Tanh", strBuilder);
    133135        } else if (node.Symbol is Square) {
    134136          FormatSquare(node, strBuilder);
    135137        } else if (node.Symbol is SquareRoot) {
    136138          FormatFunction(node, "Math.Sqrt", strBuilder);
     139        } else if (node.Symbol is Cube) {
     140          FormatPower(node, strBuilder, "3");
     141        } else if (node.Symbol is CubeRoot) {
     142          strBuilder.Append("Cbrt(");
     143          FormatRecursively(node.GetSubtree(0), strBuilder);
     144          strBuilder.Append(")");
    137145        } else if (node.Symbol is Power) {
    138146          FormatFunction(node, "Math.Pow", strBuilder);
    139147        } else if (node.Symbol is Root) {
    140148          FormatRoot(node, strBuilder);
     149        } else if (node.Symbol is Absolute) {
     150          FormatFunction(node, "Math.Abs", strBuilder);
     151        } else if (node.Symbol is AnalyticQuotient) {
     152          strBuilder.Append("(");
     153          FormatRecursively(node.GetSubtree(0), strBuilder);
     154          strBuilder.Append(" / Math.Sqrt(1 + Math.Pow(");
     155          FormatRecursively(node.GetSubtree(1), strBuilder);
     156          strBuilder.Append(" , 2) )");
    141157        } else {
    142158          throw new NotSupportedException("Formatting of symbol: " + node.Symbol + " not supported for C# symbolic expression tree formatter.");
     
    171187
    172188    private void FormatSquare(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
     189      FormatPower(node, strBuilder, "2");
     190    }
     191    private void FormatPower(ISymbolicExpressionTreeNode node, StringBuilder strBuilder, string exponent) {
    173192      strBuilder.Append("Math.Pow(");
    174193      FormatRecursively(node.GetSubtree(0), strBuilder);
    175       strBuilder.Append(", 2)");
     194      strBuilder.Append($", {exponent})");
    176195    }
    177196
     
    235254      strBuilder.AppendLine("public static class Model {");
    236255      GenerateAverageSource(strBuilder);
     256      GenerateCbrtSource(strBuilder);
    237257      GenerateIfThenElseSource(strBuilder);
    238258      GenerateFactorSource(strBuilder);
     
    276296      strBuilder.AppendLine("private static double Average(params double[] values) {");
    277297      strBuilder.AppendLine("  return values.Average();");
     298      strBuilder.AppendLine("}");
     299    }
     300    private void GenerateCbrtSource(StringBuilder strBuilder) {
     301      strBuilder.AppendLine("private static double Cbrt(double x) {");
     302      strBuilder.AppendLine("  return x < 0 ? -Math.Pow(-x, 1.0 / 3.0) : Math.Pow(x, 1.0 / 3.0);");
    278303      strBuilder.AppendLine("}");
    279304    }
  • branches/2435-alglib_3_15/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionExcelFormatter.cs

    r16565 r17034  
    2525using System.Linq;
    2626using System.Text;
     27using HEAL.Attic;
    2728using HeuristicLab.Common;
    2829using HeuristicLab.Core;
    2930using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    30 using HEAL.Attic;
    3131
    3232namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
     
    119119        }
    120120        stringBuilder.Append(")");
     121      } else if (symbol is Absolute) {
     122        stringBuilder.Append($"ABS({FormatRecursively(node.GetSubtree(0))})");
     123      } else if (symbol is AnalyticQuotient) {
     124        stringBuilder.Append($"({FormatRecursively(node.GetSubtree(0))}) / SQRT(1 + POWER({FormatRecursively(node.GetSubtree(1))}, 2))");
    121125      } else if (symbol is Average) {
    122         stringBuilder.Append("(1/");
     126        stringBuilder.Append("(1/(");
    123127        stringBuilder.Append(node.SubtreeCount);
    124128        stringBuilder.Append(")*(");
     
    129133          stringBuilder.Append(")");
    130134        }
    131         stringBuilder.Append(")");
     135        stringBuilder.Append("))");
    132136      } else if (symbol is Constant) {
    133137        ConstantTreeNode constantTreeNode = node as ConstantTreeNode;
     
    137141        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    138142        stringBuilder.Append(")");
     143      } else if (symbol is Cube) {
     144        stringBuilder.Append($"POWER({FormatRecursively(node.GetSubtree(0))}, 3)");
     145      } else if (symbol is CubeRoot) {
     146        var arg_expr = FormatRecursively(node.GetSubtree(0));
     147        stringBuilder.Append($"IF({arg_expr} < 0, -POWER(-{arg_expr}, 1/3), POWER({arg_expr}, 1/3))");
    139148      } else if (symbol is Division) {
    140149        if (node.SubtreeCount == 1) {
    141           stringBuilder.Append("1/");
    142           stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
     150          stringBuilder.Append("1/(");
     151          stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
     152          stringBuilder.Append(")");
    143153        } else {
    144154          stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
     
    192202        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    193203        stringBuilder.Append(")");
    194 
     204      } else if (symbol is HyperbolicTangent) {
     205        stringBuilder.Append("TANH(");
     206        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
     207        stringBuilder.Append(")");
    195208      } else if (symbol is Variable) {
    196209        VariableTreeNode variableTreeNode = node as VariableTreeNode;
  • branches/2435-alglib_3_15/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionLatexFormatter.cs

    r16565 r17034  
    2424using System.Linq;
    2525using System.Text;
     26using HEAL.Attic;
    2627using HeuristicLab.Common;
    2728using HeuristicLab.Core;
    2829using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    29 using HEAL.Attic;
    3030
    3131namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
     
    117117          strBuilder.Append(@" \cfrac{ ");
    118118        }
     119      } else if (node.Symbol is Absolute) {
     120        strBuilder.Append(@"\operatorname{abs} \left( ");
     121      } else if (node.Symbol is AnalyticQuotient) {
     122        strBuilder.Append(@" \frac { ");
    119123      } else if (node.Symbol is Average) {
    120124        // skip output of (1/1) if only one subtree
     
    131135      } else if (node.Symbol is SquareRoot) {
    132136        strBuilder.Append(@"\sqrt{");
     137      } else if (node.Symbol is Cube) {
     138        strBuilder.Append(@"\left(");
     139      } else if (node.Symbol is CubeRoot) {
     140        strBuilder.Append(@"\operatorname{cbrt}\left(");
    133141      } else if (node.Symbol is Sine) {
    134142        strBuilder.Append(@"\sin \left( ");
     
    137145      } else if (node.Symbol is Tangent) {
    138146        strBuilder.Append(@"\tan \left( ");
     147      } else if (node.Symbol is HyperbolicTangent) {
     148        strBuilder.Append(@"\tanh \left( ");
    139149      } else if (node.Symbol is AiryA) {
    140150        strBuilder.Append(@"\operatorname{airy}_a \left( ");
     
    287297        else
    288298          strBuilder.Append(@" }{ \cfrac{ ");
     299      } else if (node.Symbol is Absolute) {
     300        throw new InvalidOperationException();
     301      } else if (node.Symbol is AnalyticQuotient) {
     302        strBuilder.Append(@"}{\sqrt{1 + \left( ");
    289303      } else if (node.Symbol is Average) {
    290304        strBuilder.Append(@" + ");
     
    297311      } else if (node.Symbol is SquareRoot) {
    298312        throw new InvalidOperationException();
     313      } else if (node.Symbol is Cube) {
     314        throw new InvalidOperationException();
     315      } else if (node.Symbol is CubeRoot) {
     316        throw new InvalidOperationException();
    299317      } else if (node.Symbol is Sine) {
    300318        throw new InvalidOperationException();
     
    302320        throw new InvalidOperationException();
    303321      } else if (node.Symbol is Tangent) {
     322        throw new InvalidOperationException();
     323      } else if (node.Symbol is HyperbolicTangent) {
    304324        throw new InvalidOperationException();
    305325      } else if (node.Symbol is AiryA) {
     
    383403        for (int i = 2; i < node.SubtreeCount; i++)
    384404          strBuilder.Append(" } ");
     405      } else if (node.Symbol is Absolute) {
     406        strBuilder.Append(@" \right)");
     407      } else if (node.Symbol is AnalyticQuotient) {
     408        strBuilder.Append(@" \right)^2}}");
    385409      } else if (node.Symbol is Average) {
    386410        strBuilder.Append(@" \right) ");
     
    393417      } else if (node.Symbol is SquareRoot) {
    394418        strBuilder.Append(@"}");
     419      } else if (node.Symbol is Cube) {
     420        strBuilder.Append(@"\right)^3");
     421      } else if (node.Symbol is CubeRoot) {
     422        strBuilder.Append(@"\right)");
    395423      } else if (node.Symbol is Sine) {
    396424        strBuilder.Append(@" \right) ");
     
    398426        strBuilder.Append(@" \right) ");
    399427      } else if (node.Symbol is Tangent) {
     428        strBuilder.Append(@" \right) ");
     429      } else if (node.Symbol is HyperbolicTangent) {
    400430        strBuilder.Append(@" \right) ");
    401431      } else if (node.Symbol is AiryA) {
  • branches/2435-alglib_3_15/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionMATLABFormatter.cs

    r16565 r17034  
    2323using System.Linq;
    2424using System.Text;
     25using HEAL.Attic;
    2526using HeuristicLab.Common;
    2627using HeuristicLab.Core;
    2728using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    28 using HEAL.Attic;
    2929
    3030namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
     
    126126        }
    127127        stringBuilder.Append(")");
     128      } else if (symbol is Absolute) {
     129        stringBuilder.Append($"abs({FormatRecursively(node.GetSubtree(0))})");
     130      } else if (symbol is AnalyticQuotient) {
     131        stringBuilder.Append($"({FormatRecursively(node.GetSubtree(0))}) / sqrt(1 + ({FormatRecursively(node.GetSubtree(1))}).^2)");
    128132      } else if (symbol is And) {
    129133        stringBuilder.Append("((");
     
    179183        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    180184        stringBuilder.Append(")");
     185      } else if (symbol is Cube) {
     186        stringBuilder.Append("(");
     187        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
     188        stringBuilder.Append(").^3");
     189      } else if (symbol is CubeRoot) {
     190        stringBuilder.Append("NTHROOT(");
     191        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
     192        stringBuilder.Append(", 3)");
    181193      } else if (symbol is GreaterThan) {
    182194        stringBuilder.Append("((");
     
    252264      } else if (symbol is Tangent) {
    253265        stringBuilder.Append("tan(");
     266        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
     267        stringBuilder.Append(")");
     268      } else if (symbol is HyperbolicTangent) {
     269        stringBuilder.Append("tanh(");
    254270        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    255271        stringBuilder.Append(")");
  • branches/2435-alglib_3_15/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionMathematicaFormatter.cs

    r16565 r17034  
    2424using System.Linq;
    2525using System.Text;
     26using HEAL.Attic;
    2627using HeuristicLab.Common;
    2728using HeuristicLab.Core;
    2829using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    29 using HEAL.Attic;
    3030
    3131namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
     
    5656        if (node.Symbol is Addition) {
    5757          FormatFunction(node, "Plus", strBuilder);
     58        } else if (node.Symbol is Absolute) {
     59          FormatFunction(node, "Abs", strBuilder);
     60        } else if (node.Symbol is AnalyticQuotient) {
     61          strBuilder.Append("[");
     62          FormatRecursively(node.GetSubtree(0), strBuilder);
     63          strBuilder.Append("]/Sqrt[ 1 + Power[");
     64          FormatRecursively(node.GetSubtree(1), strBuilder);
     65          strBuilder.Append(", 2]]");
    5866        } else if (node.Symbol is Average) {
    5967          FormatAverage(node, strBuilder);
     
    7078        } else if (node.Symbol is Tangent) {
    7179          FormatFunction(node, "Tan", strBuilder);
     80        } else if (node.Symbol is HyperbolicTangent) {
     81          FormatFunction(node, "Tanh", strBuilder);
    7282        } else if (node.Symbol is Exponential) {
    7383          FormatFunction(node, "Exp", strBuilder);
     
    102112        } else if (node.Symbol is SquareRoot) {
    103113          FormatFunction(node, "Sqrt", strBuilder);
     114        } else if (node.Symbol is Cube) {
     115          FormatPower(node, strBuilder, "3");
     116        } else if (node.Symbol is CubeRoot) {
     117          strBuilder.Append("CubeRoot[");
     118          FormatRecursively(node.GetSubtree(0), strBuilder);
     119          strBuilder.Append("]");
    104120        } else if (node.Symbol is Power) {
    105121          FormatFunction(node, "Power", strBuilder);
     
    203219
    204220    private void FormatSquare(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
     221      FormatPower(node, strBuilder, "2");
     222    }
     223
     224    private void FormatPower(ISymbolicExpressionTreeNode node, StringBuilder strBuilder, string exponent) {
    205225      strBuilder.Append("Power[");
    206226      FormatRecursively(node.GetSubtree(0), strBuilder);
    207       strBuilder.Append(", 2]");
     227      strBuilder.Append($", {exponent}]");
    208228    }
    209229
  • branches/2435-alglib_3_15/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionSmalltalkFormatter.cs

    r16565 r17034  
    2020#endregion
    2121
     22using System;
    2223using System.Globalization;
    2324using System.Text;
     
    6970        }
    7071        stringBuilder.Append(") ifTrue:[1] ifFalse:[-1]");
     72      } else if (symbol is Absolute) {
     73        stringBuilder.Append($"({FormatRecursively(node.GetSubtree(0))}) abs");
     74      } else if (symbol is AnalyticQuotient) {
     75        stringBuilder.Append($"({FormatRecursively(node.GetSubtree(0))}) / (1 + ({FormatPower(node.GetSubtree(1), "2")})) sqrt");
    7176      } else if (symbol is Average) {
    7277        stringBuilder.Append("(1/");
     
    8489        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    8590        stringBuilder.Append(" cos");
     91      } else if (symbol is Cube) {
     92        stringBuilder.Append(FormatPower(node.GetSubtree(0), "3"));
     93      } else if (symbol is CubeRoot) {
     94        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
     95        stringBuilder.Append(" cbrt");
    8696      } else if (symbol is Division) {
    8797        if (node.SubtreeCount == 1) {
     
    146156        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    147157        stringBuilder.Append(" sin");
     158      } else if (symbol is Square) {
     159        stringBuilder.Append(FormatPower(node.GetSubtree(0), "2"));
     160      } else if (symbol is SquareRoot) {
     161        stringBuilder.Append(FormatPower(node.GetSubtree(0), "(1/2)"));
    148162      } else if (symbol is Subtraction) {
    149163        if (node.SubtreeCount == 1) {
     
    160174        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    161175        stringBuilder.Append(" tan");
     176      } else if (symbol is HyperbolicTangent) {
     177        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
     178        stringBuilder.Append(" tanh");
    162179      } else if (symbol is Variable) {
    163180        VariableTreeNode variableTreeNode = node as VariableTreeNode;
     
    181198    }
    182199
     200    private string FormatPower(ISymbolicExpressionTreeNode node, string exponent) {
     201      return $"(({FormatRecursively(node)}) log * {exponent}) exp ";
     202    }
     203
    183204    public override IDeepCloneable Clone(Cloner cloner) {
    184205      return new SymbolicDataAnalysisExpressionSmalltalkFormatter(this, cloner);
  • branches/2435-alglib_3_15/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Hashing/HashExtensions.cs

    r16565 r17034  
    2121
    2222using System;
    23 using System.Collections.Generic;
     23using System.Linq;
    2424
    2525namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
     
    3838      public SimplifyAction Simplify;
    3939
    40       public IComparer<T> Comparer;
     40      //public IComparer<T> Comparer;
    4141
    4242      public bool IsLeaf => Arity == 0;
    4343
    44       public HashNode(IComparer<T> comparer) {
    45         Comparer = comparer;
    46       }
    47 
    48       private HashNode() { }
     44      //public HashNode(IComparer<T> comparer) {
     45      //  Comparer = comparer;
     46      //}
     47
     48      //public HashNode() { }
    4949
    5050      public int CompareTo(HashNode<T> other) {
    51         var res = Comparer.Compare(Data, other.Data);
    52         return res == 0 ? CalculatedHashValue.CompareTo(other.CalculatedHashValue) : res;
     51        return CalculatedHashValue.CompareTo(other.CalculatedHashValue);
    5352      }
    5453
     
    103102
    104103    public static HashNode<T>[] Simplify<T>(this HashNode<T>[] nodes, Func<byte[], ulong> hashFunction) where T : class {
    105       var reduced = nodes.UpdateNodeSizes().Reduce().Sort(hashFunction);
    106 
    107       for (int i = 0; i < reduced.Length; ++i) {
    108         var node = reduced[i];
    109         if (node.IsLeaf) {
    110           continue;
    111         }
    112         node.Simplify?.Invoke(ref reduced, i);
    113       }
    114       // detect if anything was simplified
    115       var count = 0;
    116       foreach (var node in reduced) {
    117         if (!node.Enabled) { ++count; }
    118       }
    119       if (count == 0) {
    120         return reduced;
    121       }
    122 
    123       var simplified = new HashNode<T>[reduced.Length - count];
    124       int j = 0;
    125       foreach (var node in reduced) {
    126         if (node.Enabled) {
    127           simplified[j++] = node;
    128         }
    129       }
    130       return simplified.UpdateNodeSizes().Reduce().Sort(hashFunction);
     104      bool simplified = false;
     105      nodes = nodes.UpdateNodeSizes().Reduce().Sort(hashFunction);
     106      do {
     107        if (simplified) {
     108          simplified = false;
     109          nodes = nodes.Where(x => x.Enabled).ToArray().UpdateNodeSizes().Reduce().Sort(hashFunction);
     110        }
     111
     112        for (int i = 0; i < nodes.Length; ++i) {
     113          var node = nodes[i];
     114          if (node.IsLeaf) {
     115            continue;
     116          }
     117          node.Simplify?.Invoke(ref nodes, i);
     118          for (int j = i - node.Size; j < i; ++j) {
     119            // detect if anything was simplified
     120            if (!nodes[j].Enabled) {
     121              simplified = true;
     122              break;
     123            }
     124          }
     125        }
     126      } while (simplified);
     127      return nodes.UpdateNodeSizes().Sort(hashFunction);
    131128    }
    132129
     
    207204    }
    208205
    209     private static HashNode<T>[] Reduce<T>(this HashNode<T>[] nodes) where T : class {
     206    public static HashNode<T>[] Reduce<T>(this HashNode<T>[] nodes) where T : class {
    210207      int count = 0;
    211208      for (int i = 0; i < nodes.Length; ++i) {
  • branches/2435-alglib_3_15/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Hashing/SymbolicExpressionTreeHash.cs

    r16478 r17034  
    3838    private static readonly Constant constant = new Constant();
    3939
    40     private static readonly ISymbolicExpressionTreeNodeComparer comparer = new SymbolicExpressionTreeNodeComparer();
    4140    private static ISymbolicExpressionTreeNode ActualRoot(this ISymbolicExpressionTree tree) => tree.Root.GetSubtree(0).GetSubtree(0);
    4241
     
    7473      }
    7574      var hash = (ulong)name.GetHashCode();
    76       var hashNode = new HashNode<ISymbolicExpressionTreeNode>(comparer) {
     75      var hashNode = new HashNode<ISymbolicExpressionTreeNode> {
    7776        Data = node,
    7877        Arity = node.SubtreeCount,
  • branches/2435-alglib_3_15/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj

    r16672 r17034  
    132132  <ItemGroup>
    133133    <Compile Include="Analyzers\SymbolicDataAnalysisBottomUpDiversityAnalyzer.cs" />
    134     <Compile Include="Analyzers\SymbolicDataAnalysisBuildingBlockAnalyzer.cs" />
    135134    <Compile Include="Analyzers\SymbolicDataAnalysisSingleObjectivePruningAnalyzer.cs" />
    136135    <Compile Include="Analyzers\SymbolicDataAnalysisSingleObjectiveValidationParetoBestSolutionAnalyzer.cs" />
     
    172171    <Compile Include="Interpreter\SymbolicDataAnalysisExpressionTreeNativeInterpreter.cs" />
    173172    <Compile Include="Selectors\DiversitySelector.cs" />
     173    <Compile Include="SymbolicDataAnalysisExpressionTreeAverageSimilarityCalculator.cs" />
    174174    <Compile Include="SymbolicDataAnalysisExpressionTreeSimplificationOperator.cs" />
    175175    <Compile Include="SymbolicDataAnalysisModelComplexityCalculator.cs" />
  • branches/2435-alglib_3_15/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/BatchOperations.cs

    r16656 r17034  
    101101    public static void CubeRoot(double[] a, double[] b) {
    102102      for (int i = 0; i < BATCHSIZE; ++i)
    103         a[i] = Math.Pow(b[i], 1d / 3d);
     103        a[i] = b[i] < 0 ? -Math.Pow(-b[i], 1d / 3d) : Math.Pow(b[i], 1d / 3d);
    104104    }
    105105
  • branches/2435-alglib_3_15/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/IntervalInterpreter.cs

    r16629 r17034  
    6969    #endregion
    7070
    71     public Interval GetSymbolicExressionTreeInterval(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows = null) {
     71    public Interval GetSymbolicExpressionTreeInterval(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows = null) {
    7272      var variableRanges = DatasetUtil.GetVariableRanges(dataset, rows);
    73       return GetSymbolicExressionTreeInterval(tree, variableRanges);
    74     }
    75 
    76     public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree, IDataset dataset,
    77       out Dictionary<ISymbolicExpressionTreeNode, Interval> nodeIntervals, IEnumerable<int> rows = null) {
     73      return GetSymbolicExpressionTreeInterval(tree, variableRanges);
     74    }
     75
     76    public Interval GetSymbolicExpressionTreeIntervals(ISymbolicExpressionTree tree, IDataset dataset,
     77      out IDictionary<ISymbolicExpressionTreeNode, Interval> nodeIntervals, IEnumerable<int> rows = null) {
    7878      var variableRanges = DatasetUtil.GetVariableRanges(dataset, rows);
    79       return GetSymbolicExressionTreeIntervals(tree, variableRanges, out nodeIntervals);
    80     }
    81 
    82     public Interval GetSymbolicExressionTreeInterval(ISymbolicExpressionTree tree, Dictionary<string, Interval> variableRanges) {
     79      return GetSymbolicExpressionTreeIntervals(tree, variableRanges, out nodeIntervals);
     80    }
     81
     82    public Interval GetSymbolicExpressionTreeInterval(ISymbolicExpressionTree tree, IDictionary<string, Interval> variableRanges) {
    8383      lock (syncRoot) {
    8484        EvaluatedSolutions++;
     
    9696
    9797
    98     public Interval GetSymbolicExressionTreeIntervals(ISymbolicExpressionTree tree,
    99       Dictionary<string, Interval> variableRanges, out Dictionary<ISymbolicExpressionTreeNode, Interval> nodeIntervals) {
     98    public Interval GetSymbolicExpressionTreeIntervals(ISymbolicExpressionTree tree,
     99      IDictionary<string, Interval> variableRanges, out IDictionary<ISymbolicExpressionTreeNode, Interval> nodeIntervals) {
    100100      lock (syncRoot) {
    101101        EvaluatedSolutions++;
     
    108108      // fix incorrect intervals if necessary (could occur because of numerical errors)
    109109      nodeIntervals = new Dictionary<ISymbolicExpressionTreeNode, Interval>();
    110       foreach(var kvp in intervals) {
     110      foreach (var kvp in intervals) {
    111111        var interval = kvp.Value;
    112112        if (interval.IsInfiniteOrUndefined || interval.LowerBound <= interval.UpperBound)
     
    124124
    125125
    126     private static Instruction[] PrepareInterpreterState(ISymbolicExpressionTree tree, Dictionary<string, Interval> variableRanges) {
     126    private static Instruction[] PrepareInterpreterState(ISymbolicExpressionTree tree, IDictionary<string, Interval> variableRanges) {
    127127      if (variableRanges == null)
    128128        throw new ArgumentNullException("No variablew ranges are present!", nameof(variableRanges));
     
    141141    }
    142142
    143     private Interval Evaluate(Instruction[] instructions, ref int instructionCounter, Dictionary<ISymbolicExpressionTreeNode, Interval> nodeIntervals = null) {
     143    private Interval Evaluate(Instruction[] instructions, ref int instructionCounter, IDictionary<ISymbolicExpressionTreeNode, Interval> nodeIntervals = null) {
    144144      Instruction currentInstr = instructions[instructionCounter];
    145145      //Use ref parameter, because the tree will be iterated through recursively from the left-side branch to the right side
     
    216216            var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
    217217            result = Interval.Tangens(argumentInterval);
     218            break;
     219          }
     220        case OpCodes.Tanh: {
     221            var argumentInterval = Evaluate(instructions, ref instructionCounter, nodeIntervals);
     222            result = Interval.HyperbolicTangent(argumentInterval);
    218223            break;
    219224          }
  • branches/2435-alglib_3_15/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/OpCodes.cs

    r16656 r17034  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Linq;
    2425using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2526
    2627namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
     28  public enum OpCode : byte {
     29    Add = 1,
     30    Sub = 2,
     31    Mul = 3,
     32    Div = 4,
     33    Sin = 5,
     34    Cos = 6,
     35    Tan = 7,
     36    Log = 8,
     37    Exp = 9,
     38    IfThenElse = 10,
     39    GT = 11,
     40    LT = 12,
     41    AND = 13,
     42    OR = 14,
     43    NOT = 15,
     44    Average = 16,
     45    Call = 17,
     46    Variable = 18,
     47    LagVariable = 19,
     48    Constant = 20,
     49    Arg = 21,
     50    Power = 22,
     51    Root = 23,
     52    TimeLag = 24,
     53    Integral = 25,
     54    Derivative = 26,
     55    VariableCondition = 27,
     56    Square = 28,
     57    SquareRoot = 29,
     58    Gamma = 30,
     59    Psi = 31,
     60    Dawson = 32,
     61    ExponentialIntegralEi = 33,
     62    CosineIntegral = 34,
     63    SineIntegral = 35,
     64    HyperbolicCosineIntegral = 36,
     65    HyperbolicSineIntegral = 37,
     66    FresnelCosineIntegral = 38,
     67    FresnelSineIntegral = 39,
     68    AiryA = 40,
     69    AiryB = 41,
     70    Norm = 42,
     71    Erf = 43,
     72    Bessel = 44,
     73    XOR = 45,
     74    FactorVariable = 46,
     75    BinaryFactorVariable = 47,
     76    Absolute = 48,
     77    AnalyticQuotient = 49,
     78    Cube = 50,
     79    CubeRoot = 51,
     80    Tanh = 52,
     81  };
    2782  public static class OpCodes {
    28     public const byte Add = 1;
    29     public const byte Sub = 2;
    30     public const byte Mul = 3;
    31     public const byte Div = 4;
    32 
    33     public const byte Sin = 5;
    34     public const byte Cos = 6;
    35     public const byte Tan = 7;
    36 
    37     public const byte Log = 8;
    38     public const byte Exp = 9;
    39 
    40     public const byte IfThenElse = 10;
    41 
    42     public const byte GT = 11;
    43     public const byte LT = 12;
    44 
    45     public const byte AND = 13;
    46     public const byte OR = 14;
    47     public const byte NOT = 15;
    48 
    49 
    50     public const byte Average = 16;
    51 
    52     public const byte Call = 17;
    53 
    54     public const byte Variable = 18;
    55     public const byte LagVariable = 19;
    56     public const byte Constant = 20;
    57     public const byte Arg = 21;
    58 
    59     public const byte Power = 22;
    60     public const byte Root = 23;
    61     public const byte TimeLag = 24;
    62     public const byte Integral = 25;
    63     public const byte Derivative = 26;
    64 
    65     public const byte VariableCondition = 27;
    66 
    67     public const byte Square = 28;
    68     public const byte SquareRoot = 29;
    69     public const byte Gamma = 30;
    70     public const byte Psi = 31;
    71     public const byte Dawson = 32;
    72     public const byte ExponentialIntegralEi = 33;
    73     public const byte CosineIntegral = 34;
    74     public const byte SineIntegral = 35;
    75     public const byte HyperbolicCosineIntegral = 36;
    76     public const byte HyperbolicSineIntegral = 37;
    77     public const byte FresnelCosineIntegral = 38;
    78     public const byte FresnelSineIntegral = 39;
    79     public const byte AiryA = 40;
    80     public const byte AiryB = 41;
    81     public const byte Norm = 42;
    82     public const byte Erf = 43;
    83     public const byte Bessel = 44;
    84     public const byte XOR = 45;
    85     public const byte FactorVariable = 46;
    86     public const byte BinaryFactorVariable = 47;
    87     public const byte Absolute = 48;
    88     public const byte AnalyticQuotient = 49;
    89     public const byte Cube = 50;
    90     public const byte CubeRoot = 51;
    91 
    92     public const byte Tanh = 52;
     83    // constants for API compatibility only
     84    public const byte Add = (byte)OpCode.Add;
     85    public const byte Sub =(byte)OpCode.Sub;
     86    public const byte Mul =(byte)OpCode.Mul;
     87    public const byte Div =(byte)OpCode.Div;
     88    public const byte Sin =(byte)OpCode.Sin;
     89    public const byte Cos =(byte)OpCode.Cos;
     90    public const byte Tan =(byte)OpCode.Tan;
     91    public const byte Log =(byte)OpCode.Log;
     92    public const byte Exp = (byte)OpCode.Exp;
     93    public const byte IfThenElse = (byte)OpCode.IfThenElse;
     94    public const byte GT = (byte)OpCode.GT;
     95    public const byte LT = (byte)OpCode.LT;
     96    public const byte AND = (byte)OpCode.AND;
     97    public const byte OR = (byte)OpCode.OR;
     98    public const byte NOT = (byte)OpCode.NOT;
     99    public const byte Average = (byte)OpCode.Average;
     100    public const byte Call = (byte)OpCode.Call;
     101    public const byte Variable = (byte)OpCode.Variable;
     102    public const byte LagVariable = (byte)OpCode.LagVariable;
     103    public const byte Constant = (byte)OpCode.Constant;
     104    public const byte Arg = (byte)OpCode.Arg;
     105    public const byte Power = (byte)OpCode.Power;
     106    public const byte Root = (byte)OpCode.Root;
     107    public const byte TimeLag = (byte)OpCode.TimeLag;
     108    public const byte Integral = (byte)OpCode.Integral;
     109    public const byte Derivative = (byte)OpCode.Derivative;
     110    public const byte VariableCondition = (byte)OpCode.VariableCondition;
     111    public const byte Square = (byte)OpCode.Square;
     112    public const byte SquareRoot = (byte)OpCode.SquareRoot;
     113    public const byte Gamma = (byte)OpCode.Gamma;
     114    public const byte Psi = (byte)OpCode.Psi;
     115    public const byte Dawson = (byte)OpCode.Dawson;
     116    public const byte ExponentialIntegralEi = (byte)OpCode.ExponentialIntegralEi;
     117    public const byte CosineIntegral = (byte)OpCode.CosineIntegral;
     118    public const byte SineIntegral = (byte)OpCode.SineIntegral;
     119    public const byte HyperbolicCosineIntegral = (byte)OpCode.HyperbolicCosineIntegral;
     120    public const byte HyperbolicSineIntegral = (byte)OpCode.HyperbolicSineIntegral;
     121    public const byte FresnelCosineIntegral = (byte)OpCode.FresnelCosineIntegral;
     122    public const byte FresnelSineIntegral = (byte)OpCode.FresnelSineIntegral;
     123    public const byte AiryA = (byte)OpCode.AiryA;
     124    public const byte AiryB = (byte)OpCode.AiryB;
     125    public const byte Norm = (byte)OpCode.Norm;
     126    public const byte Erf = (byte)OpCode.Erf;
     127    public const byte Bessel = (byte)OpCode.Bessel;
     128    public const byte XOR = (byte)OpCode.XOR;
     129    public const byte FactorVariable = (byte)OpCode.FactorVariable;
     130    public const byte BinaryFactorVariable = (byte)OpCode.BinaryFactorVariable;
     131    public const byte Absolute = (byte)OpCode.Absolute;
     132    public const byte AnalyticQuotient = (byte)OpCode.AnalyticQuotient;
     133    public const byte Cube = (byte)OpCode.Cube;
     134    public const byte CubeRoot = (byte)OpCode.CubeRoot;
     135    public const byte Tanh = (byte)OpCode.Tanh;
    93136
    94137
     
    150193
    151194    public static byte MapSymbolToOpCode(ISymbolicExpressionTreeNode treeNode) {
    152       byte opCode;
    153       if (symbolToOpcode.TryGetValue(treeNode.Symbol.GetType(), out opCode)) return opCode;
     195      if (symbolToOpcode.TryGetValue(treeNode.Symbol.GetType(), out byte opCode)) return opCode;
    154196      else throw new NotSupportedException("Symbol: " + treeNode.Symbol);
    155197    }
  • branches/2435-alglib_3_15/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionCompiledTreeInterpreter.cs

    r16668 r17034  
    247247        case OpCodes.CubeRoot: {
    248248            var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns);
    249             return Expression.Power(arg, Expression.Constant(1.0 / 3.0));
     249            return Expression.Condition(Expression.LessThan(arg, Expression.Constant(0.0)),
     250              Expression.Negate(Expression.Power(Expression.Negate(arg), Expression.Constant(1.0 / 3.0))),
     251              Expression.Power(arg, Expression.Constant(1.0 / 3.0)));
    250252          }
    251253        case OpCodes.Root: {
     
    514516            var x1 = MakeExpr(node.GetSubtree(0), variableIndices, row, columns);
    515517            var x2 = MakeExpr(node.GetSubtree(1), variableIndices, row, columns);
    516             return Expression.Divide(x1, 
    517               Expression.Call(Sqrt, 
     518            return Expression.Divide(x1,
     519              Expression.Call(Sqrt,
    518520              Expression.Add(
    519521                Expression.Constant(1.0),
  • branches/2435-alglib_3_15/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeBatchInterpreter.cs

    r16656 r17034  
    6363              break;
    6464            }
    65 
     65          case OpCodes.Constant: break; // nothing to do here, don't remove because we want to prevent falling into the default case here.
    6666          case OpCodes.Add: {
    6767              Load(instr.buf, code[c].buf);
     
    173173              break;
    174174            }
     175          default: throw new NotSupportedException($"This interpreter does not support {(OpCode)instr.opcode}");
    175176        }
    176177      }
  • branches/2435-alglib_3_15/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeILEmittingInterpreter.cs

    r16670 r17034  
    336336        case OpCodes.CubeRoot: {
    337337            CompileInstructions(il, state, ds);
     338            var c1 = il.DefineLabel();
     339            var end = il.DefineLabel();
     340
     341            il.Emit(System.Reflection.Emit.OpCodes.Dup); // x
     342            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 0.0);
     343            il.Emit(System.Reflection.Emit.OpCodes.Clt); // x < 0?
     344            il.Emit(System.Reflection.Emit.OpCodes.Brfalse, c1);
     345            il.Emit(System.Reflection.Emit.OpCodes.Neg); // x = -x
    338346            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0 / 3.0);
    339347            il.Emit(System.Reflection.Emit.OpCodes.Call, power);
     348            il.Emit(System.Reflection.Emit.OpCodes.Neg); // -Math.Pow(-x, 1/3)
     349            il.Emit(System.Reflection.Emit.OpCodes.Br, end);
     350            il.MarkLabel(c1);
     351            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0 / 3.0);
     352            il.Emit(System.Reflection.Emit.OpCodes.Call, power);
     353            il.MarkLabel(end);
    340354            return;
    341355          }
     
    422436            il.Emit(System.Reflection.Emit.OpCodes.Mul); // x2*x2
    423437            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0);
    424             il.Emit(System.Reflection.Emit.OpCodes.Mul); // 1+x2*x2
     438            il.Emit(System.Reflection.Emit.OpCodes.Add); // 1+x2*x2
    425439            il.Emit(System.Reflection.Emit.OpCodes.Call, sqrt);
    426440            il.Emit(System.Reflection.Emit.OpCodes.Div);
  • branches/2435-alglib_3_15/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeInterpreter.cs

    r16670 r17034  
    232232          }
    233233        case OpCodes.CubeRoot: {
    234             return Math.Pow(Evaluate(dataset, ref row, state), 1.0 / 3.0);
     234            var arg = Evaluate(dataset, ref row, state);
     235            return arg < 0 ? -Math.Pow(-arg, 1.0 / 3.0) : Math.Pow(arg, 1.0 / 3.0);
    235236          }
    236237        case OpCodes.Root: {
  • branches/2435-alglib_3_15/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeLinearInterpreter.cs

    r16656 r17034  
    245245          instr.value = Math.Sqrt(code[instr.childIndex].value);
    246246        } else if (instr.opCode == OpCodes.CubeRoot) {
    247           instr.value = Math.Pow(code[instr.childIndex].value, 1.0 / 3.0);
     247          var arg = code[instr.childIndex].value;
     248          instr.value = arg < 0 ? -Math.Pow(-arg, 1.0 / 3.0) : Math.Pow(arg, 1.0 / 3.0);
    248249        } else if (instr.opCode == OpCodes.Root) {
    249250          double x = code[instr.childIndex].value;
  • branches/2435-alglib_3_15/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeNativeInterpreter.cs

    r16565 r17034  
    101101    private IDataset dataset;
    102102
     103    private static readonly HashSet<byte> supportedOpCodes = new HashSet<byte>() {
     104      (byte)OpCode.Constant,
     105      (byte)OpCode.Variable,
     106      (byte)OpCode.Add,
     107      (byte)OpCode.Sub,
     108      (byte)OpCode.Mul,
     109      (byte)OpCode.Div,
     110      (byte)OpCode.Exp,
     111      (byte)OpCode.Log,
     112      (byte)OpCode.Sin,
     113      (byte)OpCode.Cos,
     114      (byte)OpCode.Tan,
     115      (byte)OpCode.Tanh,
     116      (byte)OpCode.Power,
     117      (byte)OpCode.Root,
     118      (byte)OpCode.SquareRoot,
     119      (byte)OpCode.Square,
     120      (byte)OpCode.CubeRoot,
     121      (byte)OpCode.Cube,
     122      (byte)OpCode.Absolute,
     123      (byte)OpCode.AnalyticQuotient
     124    };
     125
    103126    public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows) {
    104127      if (!rows.Any()) return Enumerable.Empty<double>();
     
    108131      }
    109132
    110       var code = Compile(tree, OpCodes.MapSymbolToOpCode);
     133      byte mapSupportedSymbols(ISymbolicExpressionTreeNode node) {       
     134        var opCode = OpCodes.MapSymbolToOpCode(node);
     135        if (supportedOpCodes.Contains(opCode)) return opCode;
     136        else throw new NotSupportedException($"The native interpreter does not support {node.Symbol.Name}");
     137      };
     138      var code = Compile(tree, mapSupportedSymbols);
    111139
    112140      var rowsArray = rows.ToArray();
  • branches/2435-alglib_3_15/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Selectors/DiversitySelector.cs

    r16623 r17034  
    100100    #region Events
    101101    private void RegisterParameterEventHandlers() {
    102       SelectorParameter.ValueChanged += new EventHandler(SelectorParameter_ValueChanged);
    103       CopySelected.ValueChanged += new EventHandler(CopySelected_ValueChanged);
     102      SelectorParameter.ValueChanged += SelectorParameter_ValueChanged;
     103      CopySelectedParameter.ValueChanged += CopySelectedParameter_ValueChanged;
     104      CopySelected.ValueChanged += CopySelected_ValueChanged;
     105    }
     106
     107    private void CopySelectedParameter_ValueChanged(object sender, EventArgs e) {
     108      if (CopySelected.Value != true) {
     109        CopySelected.Value = true;
     110      }
     111      CopySelected.ValueChanged += CopySelected_ValueChanged;
    104112    }
    105113
Note: See TracChangeset for help on using the changeset viewer.