Free cookie consent management tool by TermsFeed Policy Generator

Changeset 13141


Ignore:
Timestamp:
11/13/15 14:47:00 (9 years ago)
Author:
bburlacu
Message:

#2442: Merged files from trunk and updated project file. Implemented missing operations in the CompiledTreeInterpreter: Integral, Derivative, Lag, TimeLag. Adapted lambda signature to accept an array of List<double> in order to make it easier to work with compiled trees. Changed value parameters to fixed value parameters and adjusted interpreter constructors and after serialization hooks. Removed function symbol.

From the performance point of view, compiling the tree into a lambda accepting a double[][] parameter (an array of arrays for the values of each double variable), accessed with Expression.ArrayIndex is the fastest, but it can be cumbersome to provide the data as a double[][]. Therefore the variant with List<double>[] was chosen. Internally, for each variable node the List's underlying double array is used, result in an overall decent speed compromise.

Location:
branches/HeuristicLab.LinqExpressionTreeInterpreter/HeuristicLab.Problems.DataAnalysis.Symbolic
Files:
1 deleted
10 edited
1 copied

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.LinqExpressionTreeInterpreter/HeuristicLab.Problems.DataAnalysis.Symbolic

  • branches/HeuristicLab.LinqExpressionTreeInterpreter/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionCSharpFormatter.cs

    r12645 r13141  
    5656
    5757    private void FormatRecursively(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
     58      // TODO: adapt to interpreter semantics. The HL interpreter also allows Boolean operations on reals
    5859      if (node.Subtrees.Any()) {
    5960        if (node.Symbol is Addition) {
     
    8384        } else if (node.Symbol is Or) {
    8485          FormatOperator(node, "||", strBuilder);
     86        } else if (node.Symbol is Xor) {
     87          FormatOperator(node, "^", strBuilder);
    8588        } else if (node.Symbol is Sine) {
    8689          FormatFunction(node, "Math.Sin", strBuilder);
  • branches/HeuristicLab.LinqExpressionTreeInterpreter/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj

    r12807 r13141  
    9999  </PropertyGroup>
    100100  <ItemGroup>
    101     <Reference Include="ALGLIB-3.9.0">
    102       <HintPath>..\..\..\..\trunk\sources\bin\ALGLIB-3.9.0.dll</HintPath>
     101    <Reference Include="ALGLIB-3.7.0, Version=3.7.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL">
     102      <SpecificVersion>False</SpecificVersion>
     103      <HintPath>..\..\..\..\trunk\sources\bin\ALGLIB-3.7.0.dll</HintPath>
    103104      <Private>False</Private>
    104105    </Reference>
     
    198199    </Compile>
    199200    <Compile Include="Formatters\SymbolicDataAnalysisExpressionCSharpFormatter.cs" />
     201    <Compile Include="Formatters\SymbolicDataAnalysisExpressionMathematicaFormatter.cs" />
    200202    <Compile Include="Importer\SymbolicExpressionImporter.cs" />
    201203    <Compile Include="Importer\Token.cs" />
     
    257259    <Compile Include="Symbols\AiryB.cs" />
    258260    <Compile Include="Symbols\Bessel.cs" />
    259     <Compile Include="Symbols\FunctionSymbol.cs" />
    260261    <Compile Include="Symbols\Xor.cs" />
    261262    <Compile Include="Symbols\Erf.cs" />
  • branches/HeuristicLab.LinqExpressionTreeInterpreter/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interfaces/ISymbolicDataAnalysisExpressionTreeInterpreter.cs

    r12509 r13141  
    2222using System.Collections.Generic;
    2323using HeuristicLab.Core;
    24 using HeuristicLab.Data;
    2524using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2625
     
    2827  public interface ISymbolicDataAnalysisExpressionTreeInterpreter : INamedItem, IStatefulItem {
    2928    IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows);
    30     IntValue EvaluatedSolutions { get; set; }
     29    int EvaluatedSolutions { get; set; }
    3130  }
    3231}
  • branches/HeuristicLab.LinqExpressionTreeInterpreter/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/OpCodes.cs

    r12807 r13141  
    8383    public const byte Erf = 43;
    8484    public const byte Bessel = 44;
    85     public const byte UserDefinedFunction = 46;
    8685
    8786    private static Dictionary<Type, byte> symbolToOpcode = new Dictionary<Type, byte>() {
     
    132131      { typeof(Erf), OpCodes.Erf},
    133132      { typeof(Bessel), OpCodes.Bessel},
    134       { typeof(FunctionSymbol), OpCodes.UserDefinedFunction }
    135133    };
    136134
  • branches/HeuristicLab.LinqExpressionTreeInterpreter/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionCompiledTreeInterpreter.cs

    r13140 r13141  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Collections.ObjectModel;
    2425using System.Linq;
    2526using System.Linq.Expressions;
     
    6162    #endregion
    6263
    63     private static readonly MethodInfo ListGetValue = typeof(IList<double>).GetProperty("Item", new Type[] { typeof(int) }).GetGetMethod();
     64    private static readonly Func<ReadOnlyCollection<double>, IList<double>> ReadOnlyCollectionRetrieveList = GetField<ReadOnlyCollection<double>, IList<double>>("list"); // retrieve underlying field of type IList<double> from a ReadOnlyCollection<double>
     65    private static readonly Func<List<double>, double[]> ListRetrieveItems = GetField<List<double>, double[]>("_items"); // retrieve underlying field of type double[] from a List<double>
     66    private static MethodInfo listGetValue = typeof(IList<double>).GetProperty("Item", new Type[] { typeof(int) }).GetGetMethod();
    6467
    6568    public override bool CanChangeName { get { return false; } }
     
    6871
    6972    #region parameter properties
    70     public IValueParameter<BoolValue> CheckExpressionsWithIntervalArithmeticParameter {
    71       get { return (IValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName]; }
    72     }
    73 
    74     public IValueParameter<IntValue> EvaluatedSolutionsParameter {
    75       get { return (IValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; }
     73    public IFixedValueParameter<BoolValue> CheckExpressionsWithIntervalArithmeticParameter {
     74      get { return (IFixedValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName]; }
     75    }
     76
     77    public IFixedValueParameter<IntValue> EvaluatedSolutionsParameter {
     78      get { return (IFixedValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; }
    7679    }
    7780    #endregion
    7881
    7982    #region properties
    80     public BoolValue CheckExpressionsWithIntervalArithmetic {
    81       get { return CheckExpressionsWithIntervalArithmeticParameter.Value; }
    82       set { CheckExpressionsWithIntervalArithmeticParameter.Value = value; }
    83     }
    84     public IntValue EvaluatedSolutions {
    85       get { return EvaluatedSolutionsParameter.Value; }
    86       set { EvaluatedSolutionsParameter.Value = value; }
     83    public bool CheckExpressionsWithIntervalArithmetic {
     84      get { return CheckExpressionsWithIntervalArithmeticParameter.Value.Value; }
     85      set { CheckExpressionsWithIntervalArithmeticParameter.Value.Value = value; }
     86    }
     87    public int EvaluatedSolutions {
     88      get { return EvaluatedSolutionsParameter.Value.Value; }
     89      set { EvaluatedSolutionsParameter.Value.Value = value; }
    8790    }
    8891    #endregion
     
    101104    }
    102105
    103     public SymbolicDataAnalysisExpressionCompiledTreeInterpreter() {
    104       Parameters.Add(new ValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false)));
    105       Parameters.Add(new ValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
     106    public SymbolicDataAnalysisExpressionCompiledTreeInterpreter() :
     107      base("SymbolicDataAnalysisExpressionCompiledTreeInterpreter", "Interpreter which compiles the tree into a lambda") {
     108      Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName,
     109        "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false)));
     110      Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
     111    }
     112
     113    public SymbolicDataAnalysisExpressionCompiledTreeInterpreter(string name, string description) :
     114      base(name, description) {
     115      Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName,
     116        "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false)));
     117      Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
     118    }
     119
     120    [StorableHook(HookType.AfterDeserialization)]
     121    private void AfterDeserialization() {
     122      Parameters.Remove(EvaluatedSolutionsParameterName);
     123      Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
     124
     125      Parameters.Remove(CheckExpressionsWithIntervalArithmeticParameterName);
     126      Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName,
     127        "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false)));
    106128    }
    107129
    108130    public void InitializeState() {
    109       EvaluatedSolutions.Value = 0;
     131      EvaluatedSolutions = 0;
    110132    }
    111133
    112134    public void ClearState() {
    113       EvaluatedSolutions.Value = 0;
    114135    }
    115136
    116137    public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows) {
    117       if (CheckExpressionsWithIntervalArithmetic.Value)
     138      if (CheckExpressionsWithIntervalArithmetic)
    118139        throw new NotSupportedException("Interval arithmetic is not yet supported in the symbolic data analysis interpreter.");
    119140
    120       lock (EvaluatedSolutions) {
    121         EvaluatedSolutions.Value++; // increment the evaluated solutions counter
     141      lock (EvaluatedSolutionsParameter.Value) {
     142        EvaluatedSolutions++; // increment the evaluated solutions counter
    122143      }
    123 
    124       var columns = dataset.DoubleVariables.Select(x => (IList<double>)dataset.GetReadOnlyDoubleValues(x)).ToArray();
     144      //      var columns = dataset.DoubleVariables.Select(x => ListRetrieveItems((List<double>)ReadOnlyCollectionRetrieveList(dataset.GetReadOnlyDoubleValues(x)))).ToArray();
     145      var columns = dataset.DoubleVariables.Select(x => (List<double>)dataset.GetDoubleValues(x)).ToArray();
    125146      var compiled = CompileTree(tree, dataset);
    126147      return rows.Select(x => compiled(x, columns));
    127148    }
    128149
    129     public static Func<int, IList<double>[], double> CompileTree(ISymbolicExpressionTree tree, IDataset dataset) {
     150    public static Func<int, List<double>[], double> CompileTree(ISymbolicExpressionTree tree, IDataset dataset) {
    130151      var row = Expression.Parameter(typeof(int));
    131       var columns = Expression.Parameter(typeof(IList<double>[]));
     152      var columns = Expression.Parameter(typeof(List<double>[]));
    132153      var variableIndices = dataset.DoubleVariables.Select((x, i) => new { x, i }).ToDictionary(e => e.x, e => e.i);
    133154      var expr = MakeExpr(tree, variableIndices, row, columns);
    134       var lambda = Expression.Lambda<Func<int, IList<double>[], double>>(expr, row, columns);
     155      var lambda = Expression.Lambda<Func<int, List<double>[], double>>(expr, row, columns);
    135156      return lambda.Compile();
    136157    }
    137158
    138     private static Expression MakeExpr(ISymbolicExpressionTree tree, Dictionary<string, int> variableIndices, ParameterExpression row, ParameterExpression columns) {
     159    private static Expression MakeExpr(ISymbolicExpressionTree tree, Dictionary<string, int> variableIndices, Expression row, Expression columns) {
    139160      var actualRoot = tree.Root.GetSubtree(0).GetSubtree(0);
    140161      return MakeExpr(actualRoot, variableIndices, row, columns);
    141162    }
    142163
    143     private static Expression MakeExpr(ISymbolicExpressionTreeNode node, Dictionary<string, int> variableIndices, ParameterExpression row, ParameterExpression columns) {
     164    private static Expression MakeExpr(ISymbolicExpressionTreeNode node, Dictionary<string, int> variableIndices, Expression row, Expression columns) {
    144165      var opcode = OpCodes.MapSymbolToOpCode(node);
     166      #region switch opcode
    145167      switch (opcode) {
    146         case OpCodes.UserDefinedFunction:
    147           {
    148             var functionSymbol = (FunctionSymbol)node.Symbol;
    149             return Expression.Call(functionSymbol.MethodInfo, node.Subtrees.Select(x => MakeExpr(x, variableIndices, row, columns)));
    150           }
    151168        case OpCodes.Constant:
    152169          {
     
    161178            var indexExpr = Expression.Constant(variableIndices[variableName]);
    162179            var valuesExpr = Expression.ArrayIndex(columns, indexExpr);
    163             var variableValue = Expression.Call(valuesExpr, ListGetValue, row);
     180            var variableValue = Expression.ArrayIndex(Expression.Field(valuesExpr, "_items"), row);
     181            //            var variableValue = Expression.Property(valuesExpr, Indexer, row);
    164182            return Expression.Multiply(variableWeight, variableValue);
    165183          }
     
    667685            var indexExpr = Expression.Constant(variableIndices[variableName]);
    668686            var valuesExpr = Expression.ArrayIndex(columns, indexExpr);
    669             var variableValue = Expression.Call(valuesExpr, ListGetValue, row);
     687            var variableValue = Expression.ArrayIndex(valuesExpr, row);
    670688            var variableThreshold = Expression.Constant(variableConditionTreeNode.Threshold);
    671689            var variableSlope = Expression.Constant(variableConditionTreeNode.Slope);
     
    682700              );
    683701          }
     702        case OpCodes.LagVariable:
     703          {
     704            var laggedVariableTreeNode = (LaggedVariableTreeNode)node;
     705            var lag = Expression.Constant(laggedVariableTreeNode.Lag);
     706            var variableWeight = Expression.Constant(laggedVariableTreeNode.Weight);
     707            var variableName = laggedVariableTreeNode.VariableName;
     708            var indexExpr = Expression.Constant(variableIndices[variableName]);
     709            var valuesExpr = Expression.ArrayIndex(columns, indexExpr);
     710            var variableValue = Expression.ArrayIndex(Expression.Field(valuesExpr, "_items"), Expression.Add(row, lag));
     711            return Expression.Multiply(variableWeight, variableValue);
     712          }
     713        case OpCodes.TimeLag:
     714          {
     715            var timeLagTreeNode = (LaggedTreeNode)node;
     716            var lag = Expression.Constant(timeLagTreeNode.Lag);
     717            return MakeExpr(timeLagTreeNode.GetSubtree(0), variableIndices, Expression.Add(row, lag), columns);
     718          }
     719        case OpCodes.Integral:
     720          {
     721            var timeLagTreeNode = (LaggedTreeNode)node;
     722            var subtree = node.GetSubtree(0);
     723            var sum = MakeExpr(subtree, variableIndices, row, columns);
     724            var sign = Expression.Constant(Math.Sign(timeLagTreeNode.Lag));
     725            var lag = Expression.Add(row, sign);
     726            for (int i = 0; i < Math.Abs(timeLagTreeNode.Lag); ++i) {
     727              sum = Expression.Add(sum, MakeExpr(subtree, variableIndices, lag, columns));
     728              lag = Expression.Add(lag, sign);
     729            }
     730            return sum;
     731          }
     732        case OpCodes.Derivative:
     733          {
     734            var subtree = node.GetSubtree(0);
     735            var f0 = MakeExpr(subtree, variableIndices, row, columns);
     736            var f1 = MakeExpr(subtree, variableIndices, Expression.Subtract(row, Expression.Constant(1)), columns);
     737            var f3 = MakeExpr(subtree, variableIndices, Expression.Subtract(row, Expression.Constant(3)), columns);
     738            var f4 = MakeExpr(subtree, variableIndices, Expression.Subtract(row, Expression.Constant(4)), columns);
     739
     740            var result = f0;
     741            result = Expression.Add(result, Expression.Multiply(Expression.Constant(2.0), f1));
     742            result = Expression.Subtract(result, Expression.Multiply(Expression.Constant(2.0), f3));
     743            result = Expression.Subtract(result, f4);
     744            return Expression.Divide(result, Expression.Constant(8.0));
     745          }
    684746        default:
    685747          throw new NotSupportedException("Unsupported symbol: " + node.Symbol);
    686748      }
     749      #endregion
    687750    }
    688751    // util stuff
  • branches/HeuristicLab.LinqExpressionTreeInterpreter/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeILEmittingInterpreter.cs

    r12509 r13141  
    8080
    8181    #region parameter properties
    82 
    83     public IValueParameter<BoolValue> CheckExpressionsWithIntervalArithmeticParameter {
    84       get { return (IValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName]; }
    85     }
    86 
    87     public IValueParameter<IntValue> EvaluatedSolutionsParameter {
    88       get { return (IValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; }
    89     }
    90 
     82    public IFixedValueParameter<BoolValue> CheckExpressionsWithIntervalArithmeticParameter {
     83      get { return (IFixedValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName]; }
     84    }
     85
     86    public IFixedValueParameter<IntValue> EvaluatedSolutionsParameter {
     87      get { return (IFixedValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; }
     88    }
    9189    #endregion
    9290
    9391    #region properties
    94 
    95     public BoolValue CheckExpressionsWithIntervalArithmetic {
    96       get { return CheckExpressionsWithIntervalArithmeticParameter.Value; }
    97       set { CheckExpressionsWithIntervalArithmeticParameter.Value = value; }
    98     }
    99 
    100     public IntValue EvaluatedSolutions {
    101       get { return EvaluatedSolutionsParameter.Value; }
    102       set { EvaluatedSolutionsParameter.Value = value; }
    103     }
    104 
     92    public bool CheckExpressionsWithIntervalArithmetic {
     93      get { return CheckExpressionsWithIntervalArithmeticParameter.Value.Value; }
     94      set { CheckExpressionsWithIntervalArithmeticParameter.Value.Value = value; }
     95    }
     96    public int EvaluatedSolutions {
     97      get { return EvaluatedSolutionsParameter.Value.Value; }
     98      set { EvaluatedSolutionsParameter.Value.Value = value; }
     99    }
    105100    #endregion
    106 
    107101
    108102    [StorableConstructor]
     
    116110    public SymbolicDataAnalysisExpressionTreeILEmittingInterpreter()
    117111      : base("SymbolicDataAnalysisExpressionTreeILEmittingInterpreter", "Interpreter for symbolic expression trees.") {
    118       Parameters.Add(new ValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false)));
    119       Parameters.Add(new ValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
     112      Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName,
     113        "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false)));
     114      Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
     115    }
     116
     117    public SymbolicDataAnalysisExpressionTreeILEmittingInterpreter(string name, string description)
     118      : base(name, description) {
     119      Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName,
     120        "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false)));
     121      Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
    120122    }
    121123
    122124    [StorableHook(HookType.AfterDeserialization)]
    123125    private void AfterDeserialization() {
    124       if (!Parameters.ContainsKey(EvaluatedSolutionsParameterName))
    125         Parameters.Add(new ValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
     126      Parameters.Remove(EvaluatedSolutionsParameterName);
     127      Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
     128
     129      Parameters.Remove(CheckExpressionsWithIntervalArithmeticParameterName);
     130      Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName,
     131        "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false)));
    126132    }
    127133
     
    129135
    130136    public void InitializeState() {
    131       EvaluatedSolutions.Value = 0;
     137      EvaluatedSolutions = 0;
    132138    }
    133139
    134140    public void ClearState() {
    135       EvaluatedSolutions.Value = 0;
    136141    }
    137142
     
    139144
    140145    public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows) {
    141       if (CheckExpressionsWithIntervalArithmetic.Value)
     146      if (CheckExpressionsWithIntervalArithmetic)
    142147        throw new NotSupportedException("Interval arithmetic is not yet supported in the symbolic data analysis interpreter.");
    143148
    144       EvaluatedSolutions.Value++; // increment the evaluated solutions counter
     149      EvaluatedSolutions++; // increment the evaluated solutions counter
    145150      var state = PrepareInterpreterState(tree, dataset);
    146151
     
    187192
    188193      switch (currentInstr.opCode) {
    189         case OpCodes.Add: {
     194        case OpCodes.Add:
     195          {
    190196            if (nArgs > 0) {
    191197              CompileInstructions(il, state, ds);
     
    197203            return;
    198204          }
    199         case OpCodes.Sub: {
     205        case OpCodes.Sub:
     206          {
    200207            if (nArgs == 1) {
    201208              CompileInstructions(il, state, ds);
     
    212219            return;
    213220          }
    214         case OpCodes.Mul: {
     221        case OpCodes.Mul:
     222          {
    215223            if (nArgs > 0) {
    216224              CompileInstructions(il, state, ds);
     
    222230            return;
    223231          }
    224         case OpCodes.Div: {
     232        case OpCodes.Div:
     233          {
    225234            if (nArgs == 1) {
    226235              il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0);
     
    238247            return;
    239248          }
    240         case OpCodes.Average: {
     249        case OpCodes.Average:
     250          {
    241251            CompileInstructions(il, state, ds);
    242252            for (int i = 1; i < nArgs; i++) {
     
    248258            return;
    249259          }
    250         case OpCodes.Cos: {
     260        case OpCodes.Cos:
     261          {
    251262            CompileInstructions(il, state, ds);
    252263            il.Emit(System.Reflection.Emit.OpCodes.Call, cos);
    253264            return;
    254265          }
    255         case OpCodes.Sin: {
     266        case OpCodes.Sin:
     267          {
    256268            CompileInstructions(il, state, ds);
    257269            il.Emit(System.Reflection.Emit.OpCodes.Call, sin);
    258270            return;
    259271          }
    260         case OpCodes.Tan: {
     272        case OpCodes.Tan:
     273          {
    261274            CompileInstructions(il, state, ds);
    262275            il.Emit(System.Reflection.Emit.OpCodes.Call, tan);
    263276            return;
    264277          }
    265         case OpCodes.Power: {
     278        case OpCodes.Power:
     279          {
    266280            CompileInstructions(il, state, ds);
    267281            CompileInstructions(il, state, ds);
     
    270284            return;
    271285          }
    272         case OpCodes.Root: {
     286        case OpCodes.Root:
     287          {
    273288            CompileInstructions(il, state, ds);
    274289            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0); // 1 / round(...)
     
    279294            return;
    280295          }
    281         case OpCodes.Exp: {
     296        case OpCodes.Exp:
     297          {
    282298            CompileInstructions(il, state, ds);
    283299            il.Emit(System.Reflection.Emit.OpCodes.Call, exp);
    284300            return;
    285301          }
    286         case OpCodes.Log: {
     302        case OpCodes.Log:
     303          {
    287304            CompileInstructions(il, state, ds);
    288305            il.Emit(System.Reflection.Emit.OpCodes.Call, log);
    289306            return;
    290307          }
    291         case OpCodes.Square: {
     308        case OpCodes.Square:
     309          {
    292310            CompileInstructions(il, state, ds);
    293311            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 2.0);
     
    295313            return;
    296314          }
    297         case OpCodes.SquareRoot: {
     315        case OpCodes.SquareRoot:
     316          {
    298317            CompileInstructions(il, state, ds);
    299318            il.Emit(System.Reflection.Emit.OpCodes.Call, sqrt);
    300319            return;
    301320          }
    302         case OpCodes.AiryA: {
     321        case OpCodes.AiryA:
     322          {
    303323            CompileInstructions(il, state, ds);
    304324            il.Emit(System.Reflection.Emit.OpCodes.Call, airyA);
    305325            return;
    306326          }
    307         case OpCodes.AiryB: {
     327        case OpCodes.AiryB:
     328          {
    308329            CompileInstructions(il, state, ds);
    309330            il.Emit(System.Reflection.Emit.OpCodes.Call, airyB);
    310331            return;
    311332          }
    312         case OpCodes.Bessel: {
     333        case OpCodes.Bessel:
     334          {
    313335            CompileInstructions(il, state, ds);
    314336            il.Emit(System.Reflection.Emit.OpCodes.Call, bessel);
    315337            return;
    316338          }
    317         case OpCodes.CosineIntegral: {
     339        case OpCodes.CosineIntegral:
     340          {
    318341            CompileInstructions(il, state, ds);
    319342            il.Emit(System.Reflection.Emit.OpCodes.Call, cosIntegral);
    320343            return;
    321344          }
    322         case OpCodes.Dawson: {
     345        case OpCodes.Dawson:
     346          {
    323347            CompileInstructions(il, state, ds);
    324348            il.Emit(System.Reflection.Emit.OpCodes.Call, dawson);
    325349            return;
    326350          }
    327         case OpCodes.Erf: {
     351        case OpCodes.Erf:
     352          {
    328353            CompileInstructions(il, state, ds);
    329354            il.Emit(System.Reflection.Emit.OpCodes.Call, erf);
    330355            return;
    331356          }
    332         case OpCodes.ExponentialIntegralEi: {
     357        case OpCodes.ExponentialIntegralEi:
     358          {
    333359            CompileInstructions(il, state, ds);
    334360            il.Emit(System.Reflection.Emit.OpCodes.Call, expIntegralEi);
    335361            return;
    336362          }
    337         case OpCodes.FresnelCosineIntegral: {
     363        case OpCodes.FresnelCosineIntegral:
     364          {
    338365            CompileInstructions(il, state, ds);
    339366            il.Emit(System.Reflection.Emit.OpCodes.Call, fresnelCosIntegral);
    340367            return;
    341368          }
    342         case OpCodes.FresnelSineIntegral: {
     369        case OpCodes.FresnelSineIntegral:
     370          {
    343371            CompileInstructions(il, state, ds);
    344372            il.Emit(System.Reflection.Emit.OpCodes.Call, fresnelSinIntegral);
    345373            return;
    346374          }
    347         case OpCodes.Gamma: {
     375        case OpCodes.Gamma:
     376          {
    348377            CompileInstructions(il, state, ds);
    349378            il.Emit(System.Reflection.Emit.OpCodes.Call, gamma);
    350379            return;
    351380          }
    352         case OpCodes.HyperbolicCosineIntegral: {
     381        case OpCodes.HyperbolicCosineIntegral:
     382          {
    353383            CompileInstructions(il, state, ds);
    354384            il.Emit(System.Reflection.Emit.OpCodes.Call, hypCosIntegral);
    355385            return;
    356386          }
    357         case OpCodes.HyperbolicSineIntegral: {
     387        case OpCodes.HyperbolicSineIntegral:
     388          {
    358389            CompileInstructions(il, state, ds);
    359390            il.Emit(System.Reflection.Emit.OpCodes.Call, hypSinIntegral);
    360391            return;
    361392          }
    362         case OpCodes.Norm: {
     393        case OpCodes.Norm:
     394          {
    363395            CompileInstructions(il, state, ds);
    364396            il.Emit(System.Reflection.Emit.OpCodes.Call, norm);
    365397            return;
    366398          }
    367         case OpCodes.Psi: {
     399        case OpCodes.Psi:
     400          {
    368401            CompileInstructions(il, state, ds);
    369402            il.Emit(System.Reflection.Emit.OpCodes.Call, psi);
    370403            return;
    371404          }
    372         case OpCodes.SineIntegral: {
     405        case OpCodes.SineIntegral:
     406          {
    373407            CompileInstructions(il, state, ds);
    374408            il.Emit(System.Reflection.Emit.OpCodes.Call, sinIntegral);
    375409            return;
    376410          }
    377         case OpCodes.IfThenElse: {
     411        case OpCodes.IfThenElse:
     412          {
    378413            Label end = il.DefineLabel();
    379414            Label c1 = il.DefineLabel();
     
    389424            return;
    390425          }
    391         case OpCodes.AND: {
     426        case OpCodes.AND:
     427          {
    392428            Label falseBranch = il.DefineLabel();
    393429            Label end = il.DefineLabel();
     
    410446            return;
    411447          }
    412         case OpCodes.OR: {
     448        case OpCodes.OR:
     449          {
    413450            Label trueBranch = il.DefineLabel();
    414451            Label end = il.DefineLabel();
     
    438475            return;
    439476          }
    440         case OpCodes.NOT: {
     477        case OpCodes.NOT:
     478          {
    441479            CompileInstructions(il, state, ds);
    442480            il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_0); // > 0
     
    449487            return;
    450488          }
    451         case OpCodes.XOR: {
     489        case OpCodes.XOR:
     490          {
    452491            CompileInstructions(il, state, ds);
    453492            il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_0);
     
    466505            return;
    467506          }
    468         case OpCodes.GT: {
     507        case OpCodes.GT:
     508          {
    469509            CompileInstructions(il, state, ds);
    470510            CompileInstructions(il, state, ds);
     
    477517            return;
    478518          }
    479         case OpCodes.LT: {
     519        case OpCodes.LT:
     520          {
    480521            CompileInstructions(il, state, ds);
    481522            CompileInstructions(il, state, ds);
     
    487528            return;
    488529          }
    489         case OpCodes.TimeLag: {
     530        case OpCodes.TimeLag:
     531          {
    490532            LaggedTreeNode laggedTreeNode = (LaggedTreeNode)currentInstr.dynamicNode;
    491533            il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // row -= lag
     
    503545            return;
    504546          }
    505         case OpCodes.Integral: {
     547        case OpCodes.Integral:
     548          {
    506549            int savedPc = state.ProgramCounter;
    507550            LaggedTreeNode laggedTreeNode = (LaggedTreeNode)currentInstr.dynamicNode;
     
    530573        //one sided smooth differentiatior, N = 4
    531574        // y' = 1/8h (f_i + 2f_i-1, -2 f_i-3 - f_i-4)
    532         case OpCodes.Derivative: {
     575        case OpCodes.Derivative:
     576          {
    533577            int savedPc = state.ProgramCounter;
    534578            CompileInstructions(il, state, ds);
     
    572616            return;
    573617          }
    574         case OpCodes.Call: {
     618        case OpCodes.Call:
     619          {
    575620            throw new NotSupportedException(
    576621              "Automatically defined functions are not supported by the SymbolicDataAnalysisTreeILEmittingInterpreter. Either turn of ADFs or change the interpeter.");
    577622          }
    578         case OpCodes.Arg: {
     623        case OpCodes.Arg:
     624          {
    579625            throw new NotSupportedException(
    580626              "Automatically defined functions are not supported by the SymbolicDataAnalysisTreeILEmittingInterpreter. Either turn of ADFs or change the interpeter.");
    581627          }
    582         case OpCodes.Variable: {
     628        case OpCodes.Variable:
     629          {
    583630            VariableTreeNode varNode = (VariableTreeNode)currentInstr.dynamicNode;
    584631            il.Emit(System.Reflection.Emit.OpCodes.Ldarg_1); // load columns array
     
    612659            return;
    613660          }
    614         case OpCodes.LagVariable: {
     661        case OpCodes.LagVariable:
     662          {
    615663            var nanResult = il.DefineLabel();
    616664            var normalResult = il.DefineLabel();
     
    640688            return;
    641689          }
    642         case OpCodes.Constant: {
     690        case OpCodes.Constant:
     691          {
    643692            ConstantTreeNode constNode = (ConstantTreeNode)currentInstr.dynamicNode;
    644693            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, constNode.Value);
     
    648697        //mkommend: this symbol uses the logistic function f(x) = 1 / (1 + e^(-alpha * x) )
    649698        //to determine the relative amounts of the true and false branch see http://en.wikipedia.org/wiki/Logistic_function
    650         case OpCodes.VariableCondition: {
     699        case OpCodes.VariableCondition:
     700          {
    651701            throw new NotSupportedException("Interpretation of symbol " + currentInstr.dynamicNode.Symbol.Name +
    652702                                            " is not supported by the SymbolicDataAnalysisTreeILEmittingInterpreter");
  • branches/HeuristicLab.LinqExpressionTreeInterpreter/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeInterpreter.cs

    r12509 r13141  
    4040
    4141    #region parameter properties
    42     public IValueParameter<BoolValue> CheckExpressionsWithIntervalArithmeticParameter {
    43       get { return (IValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName]; }
    44     }
    45 
    46     public IValueParameter<IntValue> EvaluatedSolutionsParameter {
    47       get { return (IValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; }
     42    public IFixedValueParameter<BoolValue> CheckExpressionsWithIntervalArithmeticParameter {
     43      get { return (IFixedValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName]; }
     44    }
     45
     46    public IFixedValueParameter<IntValue> EvaluatedSolutionsParameter {
     47      get { return (IFixedValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; }
    4848    }
    4949    #endregion
    5050
    5151    #region properties
    52     public BoolValue CheckExpressionsWithIntervalArithmetic {
    53       get { return CheckExpressionsWithIntervalArithmeticParameter.Value; }
    54       set { CheckExpressionsWithIntervalArithmeticParameter.Value = value; }
    55     }
    56 
    57     public IntValue EvaluatedSolutions {
    58       get { return EvaluatedSolutionsParameter.Value; }
    59       set { EvaluatedSolutionsParameter.Value = value; }
     52    public bool CheckExpressionsWithIntervalArithmetic {
     53      get { return CheckExpressionsWithIntervalArithmeticParameter.Value.Value; }
     54      set { CheckExpressionsWithIntervalArithmeticParameter.Value.Value = value; }
     55    }
     56    public int EvaluatedSolutions {
     57      get { return EvaluatedSolutionsParameter.Value.Value; }
     58      set { EvaluatedSolutionsParameter.Value.Value = value; }
    6059    }
    6160    #endregion
     
    7069    public SymbolicDataAnalysisExpressionTreeInterpreter()
    7170      : base("SymbolicDataAnalysisExpressionTreeInterpreter", "Interpreter for symbolic expression trees including automatically defined functions.") {
    72       Parameters.Add(new ValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false)));
    73       Parameters.Add(new ValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
     71      Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName,
     72        "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false)));
     73      Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
    7474    }
    7575
    7676    protected SymbolicDataAnalysisExpressionTreeInterpreter(string name, string description)
    7777      : base(name, description) {
    78       Parameters.Add(new ValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false)));
    79       Parameters.Add(new ValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
     78      Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName,
     79        "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false)));
     80      Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
    8081    }
    8182
    8283    [StorableHook(HookType.AfterDeserialization)]
    8384    private void AfterDeserialization() {
    84       if (!Parameters.ContainsKey(EvaluatedSolutionsParameterName))
    85         Parameters.Add(new ValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
     85      Parameters.Remove(EvaluatedSolutionsParameterName);
     86      Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
     87
     88      Parameters.Remove(CheckExpressionsWithIntervalArithmeticParameterName);
     89      Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName,
     90        "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false)));
    8691    }
    8792
    8893    #region IStatefulItem
    8994    public void InitializeState() {
    90       EvaluatedSolutions.Value = 0;
     95      EvaluatedSolutions = 0;
    9196    }
    9297
     
    96101
    97102    public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows) {
    98       if (CheckExpressionsWithIntervalArithmetic.Value)
     103      if (CheckExpressionsWithIntervalArithmetic)
    99104        throw new NotSupportedException("Interval arithmetic is not yet supported in the symbolic data analysis interpreter.");
    100105
    101       lock (EvaluatedSolutions) {
    102         EvaluatedSolutions.Value++; // increment the evaluated solutions counter
     106      lock (EvaluatedSolutionsParameter.Value) {
     107        EvaluatedSolutions++; // increment the evaluated solutions counter
    103108      }
    104109      var state = PrepareInterpreterState(tree, dataset);
     
    135140      Instruction currentInstr = state.NextInstruction();
    136141      switch (currentInstr.opCode) {
    137         case OpCodes.Add: {
     142        case OpCodes.Add:
     143          {
    138144            double s = Evaluate(dataset, ref row, state);
    139145            for (int i = 1; i < currentInstr.nArguments; i++) {
     
    142148            return s;
    143149          }
    144         case OpCodes.Sub: {
     150        case OpCodes.Sub:
     151          {
    145152            double s = Evaluate(dataset, ref row, state);
    146153            for (int i = 1; i < currentInstr.nArguments; i++) {
     
    150157            return s;
    151158          }
    152         case OpCodes.Mul: {
     159        case OpCodes.Mul:
     160          {
    153161            double p = Evaluate(dataset, ref row, state);
    154162            for (int i = 1; i < currentInstr.nArguments; i++) {
     
    157165            return p;
    158166          }
    159         case OpCodes.Div: {
     167        case OpCodes.Div:
     168          {
    160169            double p = Evaluate(dataset, ref row, state);
    161170            for (int i = 1; i < currentInstr.nArguments; i++) {
     
    165174            return p;
    166175          }
    167         case OpCodes.Average: {
     176        case OpCodes.Average:
     177          {
    168178            double sum = Evaluate(dataset, ref row, state);
    169179            for (int i = 1; i < currentInstr.nArguments; i++) {
     
    172182            return sum / currentInstr.nArguments;
    173183          }
    174         case OpCodes.Cos: {
     184        case OpCodes.Cos:
     185          {
    175186            return Math.Cos(Evaluate(dataset, ref row, state));
    176187          }
    177         case OpCodes.Sin: {
     188        case OpCodes.Sin:
     189          {
    178190            return Math.Sin(Evaluate(dataset, ref row, state));
    179191          }
    180         case OpCodes.Tan: {
     192        case OpCodes.Tan:
     193          {
    181194            return Math.Tan(Evaluate(dataset, ref row, state));
    182195          }
    183         case OpCodes.Square: {
     196        case OpCodes.Square:
     197          {
    184198            return Math.Pow(Evaluate(dataset, ref row, state), 2);
    185199          }
    186         case OpCodes.Power: {
     200        case OpCodes.Power:
     201          {
    187202            double x = Evaluate(dataset, ref row, state);
    188203            double y = Math.Round(Evaluate(dataset, ref row, state));
    189204            return Math.Pow(x, y);
    190205          }
    191         case OpCodes.SquareRoot: {
     206        case OpCodes.SquareRoot:
     207          {
    192208            return Math.Sqrt(Evaluate(dataset, ref row, state));
    193209          }
    194         case OpCodes.Root: {
     210        case OpCodes.Root:
     211          {
    195212            double x = Evaluate(dataset, ref row, state);
    196213            double y = Math.Round(Evaluate(dataset, ref row, state));
    197214            return Math.Pow(x, 1 / y);
    198215          }
    199         case OpCodes.Exp: {
     216        case OpCodes.Exp:
     217          {
    200218            return Math.Exp(Evaluate(dataset, ref row, state));
    201219          }
    202         case OpCodes.Log: {
     220        case OpCodes.Log:
     221          {
    203222            return Math.Log(Evaluate(dataset, ref row, state));
    204223          }
    205         case OpCodes.Gamma: {
     224        case OpCodes.Gamma:
     225          {
    206226            var x = Evaluate(dataset, ref row, state);
    207227            if (double.IsNaN(x)) return double.NaN;
    208228            else return alglib.gammafunction(x);
    209229          }
    210         case OpCodes.Psi: {
     230        case OpCodes.Psi:
     231          {
    211232            var x = Evaluate(dataset, ref row, state);
    212233            if (double.IsNaN(x)) return double.NaN;
     
    214235            return alglib.psi(x);
    215236          }
    216         case OpCodes.Dawson: {
     237        case OpCodes.Dawson:
     238          {
    217239            var x = Evaluate(dataset, ref row, state);
    218240            if (double.IsNaN(x)) return double.NaN;
    219241            return alglib.dawsonintegral(x);
    220242          }
    221         case OpCodes.ExponentialIntegralEi: {
     243        case OpCodes.ExponentialIntegralEi:
     244          {
    222245            var x = Evaluate(dataset, ref row, state);
    223246            if (double.IsNaN(x)) return double.NaN;
    224247            return alglib.exponentialintegralei(x);
    225248          }
    226         case OpCodes.SineIntegral: {
     249        case OpCodes.SineIntegral:
     250          {
    227251            double si, ci;
    228252            var x = Evaluate(dataset, ref row, state);
     
    233257            }
    234258          }
    235         case OpCodes.CosineIntegral: {
     259        case OpCodes.CosineIntegral:
     260          {
    236261            double si, ci;
    237262            var x = Evaluate(dataset, ref row, state);
     
    242267            }
    243268          }
    244         case OpCodes.HyperbolicSineIntegral: {
     269        case OpCodes.HyperbolicSineIntegral:
     270          {
    245271            double shi, chi;
    246272            var x = Evaluate(dataset, ref row, state);
     
    251277            }
    252278          }
    253         case OpCodes.HyperbolicCosineIntegral: {
     279        case OpCodes.HyperbolicCosineIntegral:
     280          {
    254281            double shi, chi;
    255282            var x = Evaluate(dataset, ref row, state);
     
    260287            }
    261288          }
    262         case OpCodes.FresnelCosineIntegral: {
     289        case OpCodes.FresnelCosineIntegral:
     290          {
    263291            double c = 0, s = 0;
    264292            var x = Evaluate(dataset, ref row, state);
     
    269297            }
    270298          }
    271         case OpCodes.FresnelSineIntegral: {
     299        case OpCodes.FresnelSineIntegral:
     300          {
    272301            double c = 0, s = 0;
    273302            var x = Evaluate(dataset, ref row, state);
     
    278307            }
    279308          }
    280         case OpCodes.AiryA: {
     309        case OpCodes.AiryA:
     310          {
    281311            double ai, aip, bi, bip;
    282312            var x = Evaluate(dataset, ref row, state);
     
    287317            }
    288318          }
    289         case OpCodes.AiryB: {
     319        case OpCodes.AiryB:
     320          {
    290321            double ai, aip, bi, bip;
    291322            var x = Evaluate(dataset, ref row, state);
     
    296327            }
    297328          }
    298         case OpCodes.Norm: {
     329        case OpCodes.Norm:
     330          {
    299331            var x = Evaluate(dataset, ref row, state);
    300332            if (double.IsNaN(x)) return double.NaN;
    301333            else return alglib.normaldistribution(x);
    302334          }
    303         case OpCodes.Erf: {
     335        case OpCodes.Erf:
     336          {
    304337            var x = Evaluate(dataset, ref row, state);
    305338            if (double.IsNaN(x)) return double.NaN;
    306339            else return alglib.errorfunction(x);
    307340          }
    308         case OpCodes.Bessel: {
     341        case OpCodes.Bessel:
     342          {
    309343            var x = Evaluate(dataset, ref row, state);
    310344            if (double.IsNaN(x)) return double.NaN;
    311345            else return alglib.besseli0(x);
    312346          }
    313         case OpCodes.IfThenElse: {
     347        case OpCodes.IfThenElse:
     348          {
    314349            double condition = Evaluate(dataset, ref row, state);
    315350            double result;
     
    321356            return result;
    322357          }
    323         case OpCodes.AND: {
     358        case OpCodes.AND:
     359          {
    324360            double result = Evaluate(dataset, ref row, state);
    325361            for (int i = 1; i < currentInstr.nArguments; i++) {
     
    331367            return result > 0.0 ? 1.0 : -1.0;
    332368          }
    333         case OpCodes.OR: {
     369        case OpCodes.OR:
     370          {
    334371            double result = Evaluate(dataset, ref row, state);
    335372            for (int i = 1; i < currentInstr.nArguments; i++) {
     
    341378            return result > 0.0 ? 1.0 : -1.0;
    342379          }
    343         case OpCodes.NOT: {
     380        case OpCodes.NOT:
     381          {
    344382            return Evaluate(dataset, ref row, state) > 0.0 ? -1.0 : 1.0;
    345383          }
    346         case OpCodes.XOR: {
     384        case OpCodes.XOR:
     385          {
    347386            //mkommend: XOR on multiple inputs is defined as true if the number of positive signals is odd
    348387            // this is equal to a consecutive execution of binary XOR operations.
     
    353392            return positiveSignals % 2 != 0 ? 1.0 : -1.0;
    354393          }
    355         case OpCodes.GT: {
     394        case OpCodes.GT:
     395          {
    356396            double x = Evaluate(dataset, ref row, state);
    357397            double y = Evaluate(dataset, ref row, state);
     
    359399            else return -1.0;
    360400          }
    361         case OpCodes.LT: {
     401        case OpCodes.LT:
     402          {
    362403            double x = Evaluate(dataset, ref row, state);
    363404            double y = Evaluate(dataset, ref row, state);
     
    365406            else return -1.0;
    366407          }
    367         case OpCodes.TimeLag: {
     408        case OpCodes.TimeLag:
     409          {
    368410            var timeLagTreeNode = (LaggedTreeNode)currentInstr.dynamicNode;
    369411            row += timeLagTreeNode.Lag;
     
    372414            return result;
    373415          }
    374         case OpCodes.Integral: {
     416        case OpCodes.Integral:
     417          {
    375418            int savedPc = state.ProgramCounter;
    376419            var timeLagTreeNode = (LaggedTreeNode)currentInstr.dynamicNode;
     
    390433        //one sided smooth differentiatior, N = 4
    391434        // y' = 1/8h (f_i + 2f_i-1, -2 f_i-3 - f_i-4)
    392         case OpCodes.Derivative: {
     435        case OpCodes.Derivative:
     436          {
    393437            int savedPc = state.ProgramCounter;
    394438            double f_0 = Evaluate(dataset, ref row, state); row--;
     
    403447            return (f_0 + 2 * f_1 - 2 * f_3 - f_4) / 8; // h = 1
    404448          }
    405         case OpCodes.Call: {
     449        case OpCodes.Call:
     450          {
    406451            // evaluate sub-trees
    407452            double[] argValues = new double[currentInstr.nArguments];
     
    426471            return v;
    427472          }
    428         case OpCodes.Arg: {
     473        case OpCodes.Arg:
     474          {
    429475            return state.GetStackFrameValue((ushort)currentInstr.data);
    430476          }
    431         case OpCodes.Variable: {
     477        case OpCodes.Variable:
     478          {
    432479            if (row < 0 || row >= dataset.Rows) return double.NaN;
    433480            var variableTreeNode = (VariableTreeNode)currentInstr.dynamicNode;
    434481            return ((IList<double>)currentInstr.data)[row] * variableTreeNode.Weight;
    435482          }
    436         case OpCodes.LagVariable: {
     483        case OpCodes.LagVariable:
     484          {
    437485            var laggedVariableTreeNode = (LaggedVariableTreeNode)currentInstr.dynamicNode;
    438486            int actualRow = row + laggedVariableTreeNode.Lag;
     
    440488            return ((IList<double>)currentInstr.data)[actualRow] * laggedVariableTreeNode.Weight;
    441489          }
    442         case OpCodes.Constant: {
     490        case OpCodes.Constant:
     491          {
    443492            var constTreeNode = (ConstantTreeNode)currentInstr.dynamicNode;
    444493            return constTreeNode.Value;
     
    447496        //mkommend: this symbol uses the logistic function f(x) = 1 / (1 + e^(-alpha * x) )
    448497        //to determine the relative amounts of the true and false branch see http://en.wikipedia.org/wiki/Logistic_function
    449         case OpCodes.VariableCondition: {
     498        case OpCodes.VariableCondition:
     499          {
    450500            if (row < 0 || row >= dataset.Rows) return double.NaN;
    451501            var variableConditionTreeNode = (VariableConditionTreeNode)currentInstr.dynamicNode;
  • branches/HeuristicLab.LinqExpressionTreeInterpreter/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeLinearInterpreter.cs

    r12509 r13141  
    4848
    4949    #region parameter properties
    50     public IValueParameter<BoolValue> CheckExpressionsWithIntervalArithmeticParameter {
    51       get { return (IValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName]; }
    52     }
    53 
    54     public IValueParameter<IntValue> EvaluatedSolutionsParameter {
    55       get { return (IValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; }
     50    public IFixedValueParameter<BoolValue> CheckExpressionsWithIntervalArithmeticParameter {
     51      get { return (IFixedValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName]; }
     52    }
     53
     54    public IFixedValueParameter<IntValue> EvaluatedSolutionsParameter {
     55      get { return (IFixedValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; }
    5656    }
    5757    #endregion
    5858
    5959    #region properties
    60     public BoolValue CheckExpressionsWithIntervalArithmetic {
    61       get { return CheckExpressionsWithIntervalArithmeticParameter.Value; }
    62       set { CheckExpressionsWithIntervalArithmeticParameter.Value = value; }
    63     }
    64     public IntValue EvaluatedSolutions {
    65       get { return EvaluatedSolutionsParameter.Value; }
    66       set { EvaluatedSolutionsParameter.Value = value; }
     60    public bool CheckExpressionsWithIntervalArithmetic {
     61      get { return CheckExpressionsWithIntervalArithmeticParameter.Value.Value; }
     62      set { CheckExpressionsWithIntervalArithmeticParameter.Value.Value = value; }
     63    }
     64    public int EvaluatedSolutions {
     65      get { return EvaluatedSolutionsParameter.Value.Value; }
     66      set { EvaluatedSolutionsParameter.Value.Value = value; }
    6767    }
    6868    #endregion
     
    8484    public SymbolicDataAnalysisExpressionTreeLinearInterpreter()
    8585      : base("SymbolicDataAnalysisExpressionTreeLinearInterpreter", "Linear (non-recursive) interpreter for symbolic expression trees (does not support ADFs).") {
    86       Parameters.Add(new ValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false)));
    87       Parameters.Add(new ValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
     86      Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName,
     87        "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false)));
     88      Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
     89      interpreter = new SymbolicDataAnalysisExpressionTreeInterpreter();
     90    }
     91
     92    public SymbolicDataAnalysisExpressionTreeLinearInterpreter(string name, string description)
     93      : base(name, description) {
     94      Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName,
     95        "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false)));
     96      Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
    8897      interpreter = new SymbolicDataAnalysisExpressionTreeInterpreter();
    8998    }
     
    92101    private void AfterDeserialization() {
    93102      if (interpreter == null) interpreter = new SymbolicDataAnalysisExpressionTreeInterpreter();
     103      Parameters.Remove(EvaluatedSolutionsParameterName);
     104      Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
     105
     106      Parameters.Remove(CheckExpressionsWithIntervalArithmeticParameterName);
     107      Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName,
     108        "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false)));
    94109    }
    95110
    96111    #region IStatefulItem
    97112    public void InitializeState() {
    98       EvaluatedSolutions.Value = 0;
     113      EvaluatedSolutions = 0;
    99114    }
    100115
     
    103118
    104119    public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows) {
    105       if (CheckExpressionsWithIntervalArithmetic.Value)
     120      if (CheckExpressionsWithIntervalArithmetic)
    106121        throw new NotSupportedException("Interval arithmetic is not yet supported in the symbolic data analysis interpreter.");
    107122
    108       lock (EvaluatedSolutions) {
    109         EvaluatedSolutions.Value++; // increment the evaluated solutions counter
     123      lock (EvaluatedSolutionsParameter.Value) {
     124        EvaluatedSolutions++; // increment the evaluated solutions counter
    110125      }
    111126
     
    357372        #region opcode switch
    358373        switch (instr.opCode) {
    359           case OpCodes.Constant: {
     374          case OpCodes.Constant:
     375            {
    360376              var constTreeNode = (ConstantTreeNode)instr.dynamicNode;
    361377              instr.value = constTreeNode.Value;
     
    363379            }
    364380            break;
    365           case OpCodes.Variable: {
     381          case OpCodes.Variable:
     382            {
    366383              var variableTreeNode = (VariableTreeNode)instr.dynamicNode;
    367384              instr.data = dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName);
    368385            }
    369386            break;
    370           case OpCodes.LagVariable: {
     387          case OpCodes.LagVariable:
     388            {
    371389              var laggedVariableTreeNode = (LaggedVariableTreeNode)instr.dynamicNode;
    372390              instr.data = dataset.GetReadOnlyDoubleValues(laggedVariableTreeNode.VariableName);
    373391            }
    374392            break;
    375           case OpCodes.VariableCondition: {
     393          case OpCodes.VariableCondition:
     394            {
    376395              var variableConditionTreeNode = (VariableConditionTreeNode)instr.dynamicNode;
    377396              instr.data = dataset.GetReadOnlyDoubleValues(variableConditionTreeNode.VariableName);
     
    380399          case OpCodes.TimeLag:
    381400          case OpCodes.Integral:
    382           case OpCodes.Derivative: {
     401          case OpCodes.Derivative:
     402            {
    383403              var seq = GetPrefixSequence(code, i);
    384404              var interpreterState = new InterpreterState(seq, 0);
  • branches/HeuristicLab.LinqExpressionTreeInterpreter/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Plugin.cs.frame

    r12790 r13141  
    2828  [Plugin("HeuristicLab.Problems.DataAnalysis.Symbolic","Provides base classes for symbolic data analysis tasks.", "3.4.8.$WCREV$")]
    2929  [PluginFile("HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.dll", PluginFileType.Assembly)]
    30   [PluginDependency("HeuristicLab.ALGLIB", "3.9")]
     30  [PluginDependency("HeuristicLab.ALGLIB", "3.7.0")]
    3131  [PluginDependency("HeuristicLab.Analysis", "3.3")]
    3232  [PluginDependency("HeuristicLab.Collections", "3.3")]
Note: See TracChangeset for help on using the changeset viewer.