Changeset 13482 for branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeInterpreter.cs
- Timestamp:
- 12/17/15 20:43:07 (8 years ago)
- Location:
- branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic
- Property svn:mergeinfo changed
-
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeInterpreter.cs
r12891 r13482 32 32 [StorableClass] 33 33 [Item("SymbolicDataAnalysisExpressionTreeInterpreter", "Interpreter for symbolic expression trees including automatically defined functions.")] 34 public class SymbolicDataAnalysisExpressionTreeInterpreter : ParameterizedNamedItem, ISymbolicDataAnalysisExpressionTreeInterpreter { 34 public class SymbolicDataAnalysisExpressionTreeInterpreter : ParameterizedNamedItem, 35 ISymbolicDataAnalysisExpressionTreeInterpreter { 35 36 private const string CheckExpressionsWithIntervalArithmeticParameterName = "CheckExpressionsWithIntervalArithmetic"; 37 private const string CheckExpressionsWithIntervalArithmeticParameterDescription = "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression."; 36 38 private const string EvaluatedSolutionsParameterName = "EvaluatedSolutions"; 37 39 38 public override bool CanChangeName { get { return false; } } 39 public override bool CanChangeDescription { get { return false; } } 40 public override bool CanChangeName { 41 get { return false; } 42 } 43 44 public override bool CanChangeDescription { 45 get { return false; } 46 } 40 47 41 48 #region parameter properties 42 public I ValueParameter<BoolValue> CheckExpressionsWithIntervalArithmeticParameter {43 get { return (I ValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName]; }44 } 45 46 public I ValueParameter<IntValue> EvaluatedSolutionsParameter {47 get { return (I ValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; }49 public IFixedValueParameter<BoolValue> CheckExpressionsWithIntervalArithmeticParameter { 50 get { return (IFixedValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName]; } 51 } 52 53 public IFixedValueParameter<IntValue> EvaluatedSolutionsParameter { 54 get { return (IFixedValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; } 48 55 } 49 56 #endregion 50 57 51 58 #region properties 52 public BoolValueCheckExpressionsWithIntervalArithmetic {53 get { return CheckExpressionsWithIntervalArithmeticParameter.Value ; }54 set { CheckExpressionsWithIntervalArithmeticParameter.Value = value; }55 } 56 57 public IntValueEvaluatedSolutions {58 get { return EvaluatedSolutionsParameter.Value ; }59 set { EvaluatedSolutionsParameter.Value = value; }59 public bool CheckExpressionsWithIntervalArithmetic { 60 get { return CheckExpressionsWithIntervalArithmeticParameter.Value.Value; } 61 set { CheckExpressionsWithIntervalArithmeticParameter.Value.Value = value; } 62 } 63 64 public int EvaluatedSolutions { 65 get { return EvaluatedSolutionsParameter.Value.Value; } 66 set { EvaluatedSolutionsParameter.Value.Value = value; } 60 67 } 61 68 #endregion … … 63 70 [StorableConstructor] 64 71 protected SymbolicDataAnalysisExpressionTreeInterpreter(bool deserializing) : base(deserializing) { } 65 protected SymbolicDataAnalysisExpressionTreeInterpreter(SymbolicDataAnalysisExpressionTreeInterpreter original, Cloner cloner) : base(original, cloner) { } 72 73 protected SymbolicDataAnalysisExpressionTreeInterpreter(SymbolicDataAnalysisExpressionTreeInterpreter original, 74 Cloner cloner) 75 : base(original, cloner) { } 76 66 77 public override IDeepCloneable Clone(Cloner cloner) { 67 78 return new SymbolicDataAnalysisExpressionTreeInterpreter(this, cloner); … … 70 81 public SymbolicDataAnalysisExpressionTreeInterpreter() 71 82 : 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)));83 Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false))); 84 Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0))); 74 85 } 75 86 76 87 protected SymbolicDataAnalysisExpressionTreeInterpreter(string name, string description) 77 88 : 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)));89 Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false))); 90 Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0))); 80 91 } 81 92 82 93 [StorableHook(HookType.AfterDeserialization)] 83 94 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))); 95 var evaluatedSolutions = new IntValue(0); 96 var checkExpressionsWithIntervalArithmetic = new BoolValue(false); 97 if (Parameters.ContainsKey(EvaluatedSolutionsParameterName)) { 98 var evaluatedSolutionsParameter = (IValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; 99 evaluatedSolutions = evaluatedSolutionsParameter.Value; 100 Parameters.Remove(EvaluatedSolutionsParameterName); 101 } 102 Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", evaluatedSolutions)); 103 if (Parameters.ContainsKey(CheckExpressionsWithIntervalArithmeticParameterName)) { 104 var checkExpressionsWithIntervalArithmeticParameter = (IValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName]; 105 Parameters.Remove(CheckExpressionsWithIntervalArithmeticParameterName); 106 checkExpressionsWithIntervalArithmetic = checkExpressionsWithIntervalArithmeticParameter.Value; 107 } 108 Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, CheckExpressionsWithIntervalArithmeticParameterDescription, checkExpressionsWithIntervalArithmetic)); 86 109 } 87 110 88 111 #region IStatefulItem 89 112 public void InitializeState() { 90 EvaluatedSolutions.Value = 0; 91 } 92 93 public void ClearState() { 94 } 113 EvaluatedSolutions = 0; 114 } 115 116 public void ClearState() { } 95 117 #endregion 96 118 97 public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows) { 98 if (CheckExpressionsWithIntervalArithmetic.Value) 119 private readonly object syncRoot = new object(); 120 public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset, 121 IEnumerable<int> rows) { 122 if (CheckExpressionsWithIntervalArithmetic) { 99 123 throw new NotSupportedException("Interval arithmetic is not yet supported in the symbolic data analysis interpreter."); 100 101 lock (EvaluatedSolutions) { 102 EvaluatedSolutions.Value++; // increment the evaluated solutions counter 124 } 125 126 lock (syncRoot) { 127 EvaluatedSolutions++; // increment the evaluated solutions counter 103 128 } 104 129 var state = PrepareInterpreterState(tree, dataset); … … 131 156 } 132 157 133 134 158 public virtual double Evaluate(IDataset dataset, ref int row, InterpreterState state) { 135 159 Instruction currentInstr = state.NextInstruction(); … … 147 171 s -= Evaluate(dataset, ref row, state); 148 172 } 149 if (currentInstr.nArguments == 1) s = -s;173 if (currentInstr.nArguments == 1) { s = -s; } 150 174 return s; 151 175 } … … 162 186 p /= Evaluate(dataset, ref row, state); 163 187 } 164 if (currentInstr.nArguments == 1) p = 1.0 / p;188 if (currentInstr.nArguments == 1) { p = 1.0 / p; } 165 189 return p; 166 190 } … … 205 229 case OpCodes.Gamma: { 206 230 var x = Evaluate(dataset, ref row, state); 207 if (double.IsNaN(x)) return double.NaN; 208 else return alglib.gammafunction(x); 231 if (double.IsNaN(x)) { return double.NaN; } else { return alglib.gammafunction(x); } 209 232 } 210 233 case OpCodes.Psi: { … … 216 239 case OpCodes.Dawson: { 217 240 var x = Evaluate(dataset, ref row, state); 218 if (double.IsNaN(x)) return double.NaN;241 if (double.IsNaN(x)) { return double.NaN; } 219 242 return alglib.dawsonintegral(x); 220 243 } 221 244 case OpCodes.ExponentialIntegralEi: { 222 245 var x = Evaluate(dataset, ref row, state); 223 if (double.IsNaN(x)) return double.NaN;246 if (double.IsNaN(x)) { return double.NaN; } 224 247 return alglib.exponentialintegralei(x); 225 248 } … … 349 372 int positiveSignals = 0; 350 373 for (int i = 0; i < currentInstr.nArguments; i++) { 351 if (Evaluate(dataset, ref row, state) > 0.0) positiveSignals++;374 if (Evaluate(dataset, ref row, state) > 0.0) { positiveSignals++; } 352 375 } 353 376 return positiveSignals % 2 != 0 ? 1.0 : -1.0; … … 356 379 double x = Evaluate(dataset, ref row, state); 357 380 double y = Evaluate(dataset, ref row, state); 358 if (x > y) return 1.0; 359 else return -1.0; 381 if (x > y) { return 1.0; } else { return -1.0; } 360 382 } 361 383 case OpCodes.LT: { 362 384 double x = Evaluate(dataset, ref row, state); 363 385 double y = Evaluate(dataset, ref row, state); 364 if (x < y) return 1.0; 365 else return -1.0; 386 if (x < y) { return 1.0; } else { return -1.0; } 366 387 } 367 388 case OpCodes.TimeLag: { … … 437 458 var laggedVariableTreeNode = (LaggedVariableTreeNode)currentInstr.dynamicNode; 438 459 int actualRow = row + laggedVariableTreeNode.Lag; 439 if (actualRow < 0 || actualRow >= dataset.Rows) return double.NaN;460 if (actualRow < 0 || actualRow >= dataset.Rows) { return double.NaN; } 440 461 return ((IList<double>)currentInstr.data)[actualRow] * laggedVariableTreeNode.Weight; 441 462 } … … 459 480 return trueBranch * p + falseBranch * (1 - p); 460 481 } 461 default: throw new NotSupportedException(); 482 default: 483 throw new NotSupportedException(); 462 484 } 463 485 }
Note: See TracChangeset
for help on using the changeset viewer.