Changeset 9758 for branches/HeuristicLab.DataAnalysis.Symbolic.LinearInterpreter/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeLinearInterpreter.cs
- Timestamp:
- 07/25/13 17:04:27 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.DataAnalysis.Symbolic.LinearInterpreter/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeLinearInterpreter.cs
r9739 r9758 32 32 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 33 33 [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 { 36 36 private const string CheckExpressionsWithIntervalArithmeticParameterName = "CheckExpressionsWithIntervalArithmetic"; 37 37 private const string EvaluatedSolutionsParameterName = "EvaluatedSolutions"; … … 46 46 47 47 #region parameter properties 48 49 48 public IValueParameter<BoolValue> CheckExpressionsWithIntervalArithmeticParameter { 50 49 get { return (IValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName]; } … … 54 53 get { return (IValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; } 55 54 } 56 57 55 #endregion 58 56 59 57 #region properties 60 61 58 public BoolValue CheckExpressionsWithIntervalArithmetic { 62 59 get { return CheckExpressionsWithIntervalArithmeticParameter.Value; } 63 60 set { CheckExpressionsWithIntervalArithmeticParameter.Value = value; } 64 61 } 65 66 62 public IntValue EvaluatedSolutions { 67 63 get { return EvaluatedSolutionsParameter.Value; } 68 64 set { EvaluatedSolutionsParameter.Value = value; } 69 65 } 70 71 66 #endregion 72 67 73 68 [StorableConstructor] 74 pr otectedSymbolicDataAnalysisExpressionTreeLinearInterpreter(bool deserializing)69 private SymbolicDataAnalysisExpressionTreeLinearInterpreter(bool deserializing) 75 70 : base(deserializing) { 76 71 } 77 72 78 pr otectedSymbolicDataAnalysisExpressionTreeLinearInterpreter(73 private SymbolicDataAnalysisExpressionTreeLinearInterpreter( 79 74 SymbolicDataAnalysisExpressionTreeLinearInterpreter original, Cloner cloner) 80 75 : base(original, cloner) { … … 86 81 87 82 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).") { 89 84 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))); 90 85 Parameters.Add(new ValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0))); 91 86 } 92 87 93 pr otectedSymbolicDataAnalysisExpressionTreeLinearInterpreter(string name, string description)88 private SymbolicDataAnalysisExpressionTreeLinearInterpreter(string name, string description) 94 89 : base(name, description) { 95 90 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))); … … 123 118 124 119 var code = SymbolicExpressionTreeLinearCompiler.Compile(tree, OpCodes.MapSymbolToOpCode); 125 PrepareIn terpreterState(code, dataset);120 PrepareInstructions(code, dataset); 126 121 return rows.Select(row => Evaluate(dataset, ref row, code)); 127 122 } 128 123 129 private static void PrepareIn terpreterState(LinearInstruction[] code, Dataset dataset) {124 private static void PrepareInstructions(LinearInstruction[] code, Dataset dataset) { 130 125 for (int i = code.Length - 1; i >= 0; --i) { 131 126 var instr = code[i]; … … 159 154 private static double Evaluate(Dataset dataset, ref int row, LinearInstruction[] code) { 160 155 for (int i = code.Length - 1; i >= 0; --i) { 156 if (code[i].opCode == OpCodes.Constant) continue; 157 #region opcode switch 161 158 var instr = code[i]; 162 if (instr.opCode == OpCodes.Constant) continue;163 #region opcode switch164 159 switch (instr.opCode) { 165 160 case OpCodes.Variable: { … … 241 236 case OpCodes.Tan: { 242 237 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); 243 252 } 244 253 break; … … 422 431 } 423 432 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 }436 433 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); 438 436 } 439 437 #endregion
Note: See TracChangeset
for help on using the changeset viewer.