Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/07/19 13:32:09 (5 years ago)
Author:
mkommend
Message:

#2974: Merged trunk changes into branch.

Location:
branches/2974_Constants_Optimization/HeuristicLab.Problems.DataAnalysis.Symbolic
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • branches/2974_Constants_Optimization/HeuristicLab.Problems.DataAnalysis.Symbolic

  • branches/2974_Constants_Optimization/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/BatchOperations.cs

    r16676 r17193  
    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/2974_Constants_Optimization/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/InterpreterState.cs

    r16676 r17193  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
  • branches/2974_Constants_Optimization/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/IntervalInterpreter.cs

    r16676 r17193  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    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/2974_Constants_Optimization/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/OpCodes.cs

    r16676 r17193  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    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/2974_Constants_Optimization/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionCompiledTreeInterpreter.cs

    r16676 r17193  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    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/2974_Constants_Optimization/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeBatchInterpreter.cs

    r16676 r17193  
    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/2974_Constants_Optimization/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeILEmittingInterpreter.cs

    r16676 r17193  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    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/2974_Constants_Optimization/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeInterpreter.cs

    r16676 r17193  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    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/2974_Constants_Optimization/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeLinearInterpreter.cs

    r16676 r17193  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    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/2974_Constants_Optimization/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeNativeInterpreter.cs

    r16676 r17193  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    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();
Note: See TracChangeset for help on using the changeset viewer.