Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/25/13 17:04:27 (11 years ago)
Author:
bburlacu
Message:

#2021:

  • Derived the LinearInstruction class from Instruction.
  • Added missing symbols to the linear interpreter
  • Changed description for the linear interpreter
  • Added more helpful exception message when a symbol is not supported.
  • Added evaluation test for the linear interpreter
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.DataAnalysis.Symbolic.LinearInterpreter/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeLinearInterpreter.cs

    r9739 r9758  
    3232namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    3333  [StorableClass]
    34   [Item("SymbolicDataAnalysisExpressionTreeLinearInterpreter", "Linear (non-recursive) interpreter for symbolic expression trees (does not support ADFs).")]
    35   public class SymbolicDataAnalysisExpressionTreeLinearInterpreter : ParameterizedNamedItem, ISymbolicDataAnalysisExpressionTreeInterpreter {
     34  [Item("SymbolicDataAnalysisExpressionTreeLinearInterpreter", "Linear (non-recursive) interpreter for symbolic expression trees. This interpreter is faster but does not support Integral, Derivative, TimeLag or ADF function nodes.")]
     35  public sealed class SymbolicDataAnalysisExpressionTreeLinearInterpreter : ParameterizedNamedItem, ISymbolicDataAnalysisExpressionTreeInterpreter {
    3636    private const string CheckExpressionsWithIntervalArithmeticParameterName = "CheckExpressionsWithIntervalArithmetic";
    3737    private const string EvaluatedSolutionsParameterName = "EvaluatedSolutions";
     
    4646
    4747    #region parameter properties
    48 
    4948    public IValueParameter<BoolValue> CheckExpressionsWithIntervalArithmeticParameter {
    5049      get { return (IValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName]; }
     
    5453      get { return (IValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; }
    5554    }
    56 
    5755    #endregion
    5856
    5957    #region properties
    60 
    6158    public BoolValue CheckExpressionsWithIntervalArithmetic {
    6259      get { return CheckExpressionsWithIntervalArithmeticParameter.Value; }
    6360      set { CheckExpressionsWithIntervalArithmeticParameter.Value = value; }
    6461    }
    65 
    6662    public IntValue EvaluatedSolutions {
    6763      get { return EvaluatedSolutionsParameter.Value; }
    6864      set { EvaluatedSolutionsParameter.Value = value; }
    6965    }
    70 
    7166    #endregion
    7267
    7368    [StorableConstructor]
    74     protected SymbolicDataAnalysisExpressionTreeLinearInterpreter(bool deserializing)
     69    private SymbolicDataAnalysisExpressionTreeLinearInterpreter(bool deserializing)
    7570      : base(deserializing) {
    7671    }
    7772
    78     protected SymbolicDataAnalysisExpressionTreeLinearInterpreter(
     73    private SymbolicDataAnalysisExpressionTreeLinearInterpreter(
    7974      SymbolicDataAnalysisExpressionTreeLinearInterpreter original, Cloner cloner)
    8075      : base(original, cloner) {
     
    8681
    8782    public SymbolicDataAnalysisExpressionTreeLinearInterpreter()
    88       : base("SymbolicDataAnalysisExpressionTreeLinearInterpreter", "Interpreter for symbolic expression trees including automatically defined functions.") {
     83      : base("SymbolicDataAnalysisExpressionTreeLinearInterpreter", "Linear (non-recursive) interpreter for symbolic expression trees (does not support ADFs).") {
    8984      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)));
    9085      Parameters.Add(new ValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
    9186    }
    9287
    93     protected SymbolicDataAnalysisExpressionTreeLinearInterpreter(string name, string description)
     88    private SymbolicDataAnalysisExpressionTreeLinearInterpreter(string name, string description)
    9489      : base(name, description) {
    9590      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)));
     
    123118
    124119      var code = SymbolicExpressionTreeLinearCompiler.Compile(tree, OpCodes.MapSymbolToOpCode);
    125       PrepareInterpreterState(code, dataset);
     120      PrepareInstructions(code, dataset);
    126121      return rows.Select(row => Evaluate(dataset, ref row, code));
    127122    }
    128123
    129     private static void PrepareInterpreterState(LinearInstruction[] code, Dataset dataset) {
     124    private static void PrepareInstructions(LinearInstruction[] code, Dataset dataset) {
    130125      for (int i = code.Length - 1; i >= 0; --i) {
    131126        var instr = code[i];
     
    159154    private static double Evaluate(Dataset dataset, ref int row, LinearInstruction[] code) {
    160155      for (int i = code.Length - 1; i >= 0; --i) {
     156        if (code[i].opCode == OpCodes.Constant) continue;
     157        #region opcode switch
    161158        var instr = code[i];
    162         if (instr.opCode == OpCodes.Constant) continue;
    163         #region opcode switch
    164159        switch (instr.opCode) {
    165160          case OpCodes.Variable: {
     
    241236          case OpCodes.Tan: {
    242237              instr.value = Math.Tan(code[instr.childIndex].value);
     238            }
     239            break;
     240          case OpCodes.Square: {
     241              instr.value = Math.Pow(code[instr.childIndex].value, 2);
     242            }
     243            break;
     244          case OpCodes.Power: {
     245              double x = code[instr.childIndex].value;
     246              double y = Math.Round(code[instr.childIndex + 1].value);
     247              instr.value = Math.Pow(x, y);
     248            }
     249            break;
     250          case OpCodes.SquareRoot: {
     251              instr.value = Math.Sqrt(code[instr.childIndex].value);
    243252            }
    244253            break;
     
    422431            }
    423432            break;
    424           case OpCodes.TimeLag: {
    425               throw new NotSupportedException();
    426             }
    427           case OpCodes.Integral: {
    428               throw new NotSupportedException();
    429             }
    430           case OpCodes.Derivative: {
    431               throw new NotSupportedException();
    432             }
    433           case OpCodes.Arg: {
    434               throw new NotSupportedException();
    435             }
    436433          default:
    437             throw new NotSupportedException();
     434            var errorText = string.Format("The {0} symbol is not supported by the linear interpreter. To support this symbol, please use another interpreter.", instr.dynamicNode.Symbol.Name);
     435            throw new NotSupportedException(errorText);
    438436        }
    439437        #endregion
Note: See TracChangeset for help on using the changeset viewer.