Changeset 14811
- Timestamp:
- 03/31/17 14:49:21 (8 years ago)
- Location:
- stable
- Files:
-
- 12 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
stable
- Property svn:mergeinfo changed
/trunk/sources merged: 13248,13251,13254-13256,13288,13313-13315,13318,14282,14809-14810
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Problems.DataAnalysis.Symbolic
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic merged: 13248,13251,13254-13255,13288,13313,14282,14809
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis merged: 13248
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis/3.4/SymbolicTimeSeriesPrognosisExpressionTreeInterpreter.cs
r14186 r14811 73 73 } 74 74 75 private readonly object syncRoot = new object(); 75 76 public IEnumerable<IEnumerable<double>> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows, IEnumerable<int> horizons) { 76 if (CheckExpressionsWithIntervalArithmetic .Value)77 if (CheckExpressionsWithIntervalArithmetic) 77 78 throw new NotSupportedException("Interval arithmetic is not yet supported in the symbolic data analysis interpreter."); 78 79 if (targetVariableCache == null || targetVariableCache.GetLength(0) < dataset.Rows) … … 82 83 83 84 string targetVariable = TargetVariable; 84 lock ( EvaluatedSolutions) {85 EvaluatedSolutions .Value++; // increment the evaluated solutions counter85 lock (syncRoot) { 86 EvaluatedSolutions++; // increment the evaluated solutions counter 86 87 } 87 88 var state = PrepareInterpreterState(tree, dataset, targetVariableCache, TargetVariable); -
stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj
r14116 r14811 140 140 <Compile Include="Importer\Token.cs" /> 141 141 <Compile Include="Interfaces\IModelBacktransformator.cs" /> 142 <Compile Include="Interpreter\SymbolicDataAnalysisExpressionCompiledTreeInterpreter.cs" /> 142 143 <Compile Include="SymbolicDataAnalysisExpressionTreeSimplificationOperator.cs" /> 143 144 <Compile Include="SymbolicDataAnalysisModelComplexityCalculator.cs" /> -
stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interfaces/ISymbolicDataAnalysisExpressionTreeInterpreter.cs
r14186 r14811 22 22 using System.Collections.Generic; 23 23 using HeuristicLab.Core; 24 using HeuristicLab.Data;25 24 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 26 25 … … 28 27 public interface ISymbolicDataAnalysisExpressionTreeInterpreter : INamedItem, IStatefulItem { 29 28 IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows); 30 IntValueEvaluatedSolutions { get; set; }29 int EvaluatedSolutions { get; set; } 31 30 } 32 31 } -
stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionCompiledTreeInterpreter.cs
r13248 r14811 46 46 private static readonly MethodInfo Sqrt = typeof(Math).GetMethod("Sqrt", new[] { typeof(double) }); 47 47 private static readonly MethodInfo Floor = typeof(Math).GetMethod("Floor", new[] { typeof(double) }); 48 private static readonly MethodInfo Round = typeof(Math).GetMethod("Round", new[] { typeof(double) }); 48 49 private static readonly MethodInfo Exp = typeof(Math).GetMethod("Exp", new[] { typeof(double) }); 49 50 private static readonly MethodInfo Log = typeof(Math).GetMethod("Log", new[] { typeof(double) }); 50 51 private static readonly MethodInfo IsNaN = typeof(double).GetMethod("IsNaN"); 52 private static readonly MethodInfo IsAlmost = typeof(DoubleExtensions).GetMethod("IsAlmost"); 51 53 private static readonly MethodInfo Gamma = typeof(alglib).GetMethod("gammafunction", new[] { typeof(double) }); 52 54 private static readonly MethodInfo Psi = typeof(alglib).GetMethod("psi", new[] { typeof(double) }); … … 117 119 public void ClearState() { } 118 120 121 private readonly object syncRoot = new object(); 119 122 public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows) { 120 123 if (CheckExpressionsWithIntervalArithmetic) 121 124 throw new NotSupportedException("Interval arithmetic is not yet supported in the symbolic data analysis interpreter."); 122 125 123 lock ( EvaluatedSolutionsParameter.Value) {126 lock (syncRoot) { 124 127 EvaluatedSolutions++; // increment the evaluated solutions counter 125 128 } … … 130 133 131 134 public static Func<int, IList<double>[], double> CompileTree(ISymbolicExpressionTree tree, IDataset dataset) { 135 var lambda = CreateDelegate(tree, dataset); 136 return lambda.Compile(); 137 } 138 139 public static Expression<Func<int, IList<double>[], double>> CreateDelegate(ISymbolicExpressionTree tree, IDataset dataset) { 132 140 var row = Expression.Parameter(typeof(int)); 133 141 var columns = Expression.Parameter(typeof(IList<double>[])); … … 135 143 var expr = MakeExpr(tree, variableIndices, row, columns); 136 144 var lambda = Expression.Lambda<Func<int, IList<double>[], double>>(expr, row, columns); 137 return lambda .Compile();145 return lambda; 138 146 } 139 147 … … 158 166 var indexExpr = Expression.Constant(variableIndices[variableName]); 159 167 var valuesExpr = Expression.ArrayIndex(columns, indexExpr); 160 var variableValue = Expression.Property(valuesExpr, Indexer, row); 161 return Expression.Multiply(variableWeight, variableValue); 168 return Expression.Multiply(variableWeight, Expression.Property(valuesExpr, Indexer, row)); 162 169 } 163 170 case OpCodes.Add: { … … 214 221 case OpCodes.Square: { 215 222 var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns); 216 return Expression.Power(arg, Expression.Constant(2 ));223 return Expression.Power(arg, Expression.Constant(2.0)); 217 224 } 218 225 case OpCodes.Power: { 219 226 var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns); 220 227 var power = MakeExpr(node.GetSubtree(1), variableIndices, row, columns); 221 return Expression.Power(arg, Expression.Call( Floor, power));228 return Expression.Power(arg, Expression.Call(Round, power)); 222 229 } 223 230 case OpCodes.SquareRoot: { … … 228 235 var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns); 229 236 var power = MakeExpr(node.GetSubtree(1), variableIndices, row, columns); 230 return Expression.Power(arg, Expression.Divide(Expression.Constant(1.0), power));237 return Expression.Power(arg, Expression.Divide(Expression.Constant(1.0), Expression.Call(Round, power))); 231 238 } 232 239 case OpCodes.Exp: { … … 241 248 var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns); 242 249 var isNaN = Expression.Call(IsNaN, arg); 243 var gamma = Expression.Call(Gamma, arg);244 250 245 251 var result = Expression.Variable(typeof(double)); … … 249 255 isNaN, 250 256 Expression.Assign(result, Expression.Constant(double.NaN)), 251 Expression.Assign(result, gamma)257 Expression.Assign(result, Expression.Call(Gamma, arg)) 252 258 ), 253 259 result … … 258 264 var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns); 259 265 var isNaN = Expression.Call(IsNaN, arg); 260 var psi = Expression.Call(Psi, arg);261 266 262 267 var result = Expression.Variable(typeof(double)); … … 268 273 Expression.Assign(result, Expression.Constant(double.NaN)), 269 274 Expression.IfThenElse( 270 Expression.AndAlso(Expression.LessThanOrEqual(arg, Expression.Constant(0.0)), 271 Expression.Equal(Expression.Subtract(floor, arg), Expression.Constant(0.0))), 275 Expression.AndAlso( 276 Expression.LessThanOrEqual(arg, Expression.Constant(0.0)), 277 Expression.Call(IsAlmost, Expression.Subtract(floor, arg), Expression.Constant(0.0))), 272 278 Expression.Assign(result, Expression.Constant(double.NaN)), 273 Expression.Assign(result, psi))279 Expression.Assign(result, Expression.Call(Psi, arg))) 274 280 ), 275 281 result); … … 280 286 var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns); 281 287 var isNaN = Expression.Call(IsNaN, arg); 282 var exprDawsonIntegral = Expression.Call(DawsonIntegral, arg); 283 var result = Expression.Variable(typeof(double)); 284 288 var result = Expression.Variable(typeof(double)); 285 289 var expr = Expression.Block( 286 290 new[] { result }, 287 291 Expression.IfThenElse(isNaN, 288 292 Expression.Assign(result, Expression.Constant(double.NaN)), 289 Expression.Assign(result, exprDawsonIntegral)),293 Expression.Assign(result, Expression.Call(DawsonIntegral, arg))), 290 294 result 291 295 ); … … 296 300 var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns); 297 301 var isNaN = Expression.Call(IsNaN, arg); 298 var expIntegrapEi =299 Expression.Call(ExponentialIntegralEi, arg);300 302 var result = Expression.Variable(typeof(double)); 301 303 var expr = Expression.Block( … … 303 305 Expression.IfThenElse(isNaN, 304 306 Expression.Assign(result, Expression.Constant(double.NaN)), 305 Expression.Assign(result, expIntegrapEi)),307 Expression.Assign(result, Expression.Call(ExponentialIntegralEi, arg))), 306 308 result 307 309 ); … … 314 316 var si = Expression.Variable(typeof(double)); 315 317 var ci = Expression.Variable(typeof(double)); 316 var sinCosIntegrals = Expression.Call(SineCosineIntegrals, arg, si, ci);317 318 var block = Expression.Block( 318 319 new[] { si, ci }, 319 sinCosIntegrals,320 Expression.Call(SineCosineIntegrals, arg, si, ci), 320 321 si 321 322 ); … … 335 336 var si = Expression.Variable(typeof(double)); 336 337 var ci = Expression.Variable(typeof(double)); 337 var sinCosIntegrals = Expression.Call(SineCosineIntegrals, arg, si, ci);338 338 var block = Expression.Block( 339 339 new[] { si, ci }, 340 sinCosIntegrals,340 Expression.Call(SineCosineIntegrals, arg, si, ci), 341 341 ci 342 342 ); … … 356 356 var shi = Expression.Variable(typeof(double)); 357 357 var chi = Expression.Variable(typeof(double)); 358 var hypSinCosIntegrals = Expression.Call(HyperbolicSineCosineIntegrals, arg, shi, chi);359 358 var block = Expression.Block( 360 359 new[] { shi, chi }, 361 hypSinCosIntegrals,360 Expression.Call(HyperbolicSineCosineIntegrals, arg, shi, chi), 362 361 shi 363 362 ); … … 377 376 var shi = Expression.Variable(typeof(double)); 378 377 var chi = Expression.Variable(typeof(double)); 379 var hypSinCosIntegrals = Expression.Call(HyperbolicSineCosineIntegrals, arg, shi, chi);380 378 var block = Expression.Block( 381 379 new[] { shi, chi }, 382 hypSinCosIntegrals,380 Expression.Call(HyperbolicSineCosineIntegrals, arg, shi, chi), 383 381 chi 384 382 ); … … 398 396 var s = Expression.Variable(typeof(double)); 399 397 var c = Expression.Variable(typeof(double)); 400 var fresnel = Expression.Call(FresnelIntegral, arg, c, s); 401 var block = Expression.Block(new[] { s, c }, fresnel, s); 398 var block = Expression.Block(new[] { s, c }, Expression.Call(FresnelIntegral, arg, c, s), s); 402 399 var result = Expression.Variable(typeof(double)); 403 400 var expr = Expression.Block(new[] { result }, … … 415 412 var s = Expression.Variable(typeof(double)); 416 413 var c = Expression.Variable(typeof(double)); 417 var fresnel = Expression.Call(FresnelIntegral, arg, c, s); 418 var block = Expression.Block(new[] { s, c }, fresnel, c); 414 var block = Expression.Block(new[] { s, c }, Expression.Call(FresnelIntegral, arg, c, s), c); 419 415 var result = Expression.Variable(typeof(double)); 420 416 var expr = Expression.Block(new[] { result }, … … 434 430 var bi = Expression.Variable(typeof(double)); 435 431 var bip = Expression.Variable(typeof(double)); 436 var airy = Expression.Call(Airy, arg, ai, aip, bi, bip); 437 var block = Expression.Block(new[] { ai, aip, bi, bip }, airy, ai); 432 var block = Expression.Block(new[] { ai, aip, bi, bip }, Expression.Call(Airy, arg, ai, aip, bi, bip), ai); 438 433 var result = Expression.Variable(typeof(double)); 439 434 var expr = Expression.Block(new[] { result }, … … 453 448 var bi = Expression.Variable(typeof(double)); 454 449 var bip = Expression.Variable(typeof(double)); 455 var airy = Expression.Call(Airy, arg, ai, aip, bi, bip); 456 var block = Expression.Block(new[] { ai, aip, bi, bip }, airy, bi); 450 var block = Expression.Block(new[] { ai, aip, bi, bip }, Expression.Call(Airy, arg, ai, aip, bi, bip), bi); 457 451 var result = Expression.Variable(typeof(double)); 458 452 var expr = Expression.Block(new[] { result }, … … 467 461 case OpCodes.Norm: { 468 462 var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns); 469 var isNaN = Expression.Call(IsNaN, arg); 470 var result = Expression.Variable(typeof(double)); 471 var norm = Expression.Call(NormalDistribution, arg); 472 473 var expr = Expression.Block(new[] { result }, 474 Expression.IfThenElse(isNaN, Expression.Assign(result, Expression.Constant(double.NaN)), 475 Expression.Assign(result, norm)), result); 476 477 return expr; 463 var result = Expression.Variable(typeof(double)); 464 return Expression.Block( 465 new[] { result }, 466 Expression.IfThenElse( 467 Expression.Call(IsNaN, arg), 468 Expression.Assign(result, arg), 469 Expression.Assign(result, Expression.Call(NormalDistribution, arg))), 470 result); 478 471 } 479 472 case OpCodes.Erf: { … … 481 474 var isNaN = Expression.Call(IsNaN, arg); 482 475 var result = Expression.Variable(typeof(double)); 483 var erf = Expression.Call(ErrorFunction, arg);484 485 var expr = Expression.Block(new[] { result },486 Expression.IfThenElse(isNaN, Expression.Assign(result, Expression.Constant(double.NaN)),487 Expression.Assign(result, erf)), result);488 489 return expr;476 return Expression.Block( 477 new[] { result }, 478 Expression.IfThenElse( 479 isNaN, 480 Expression.Assign(result, Expression.Constant(double.NaN)), 481 Expression.Assign(result, Expression.Call(ErrorFunction, arg))), 482 result); 490 483 } 491 484 case OpCodes.Bessel: { … … 493 486 var isNaN = Expression.Call(IsNaN, arg); 494 487 var result = Expression.Variable(typeof(double)); 495 var bessel = Expression.Call(Bessel, arg); 496 var expr = Expression.Block( 488 return Expression.Block( 497 489 new[] { result }, 498 490 Expression.IfThenElse( 499 491 isNaN, 500 492 Expression.Assign(result, Expression.Constant(double.NaN)), 501 Expression.Assign(result, bessel)),493 Expression.Assign(result, Expression.Call(Bessel, arg))), 502 494 result); 503 504 return expr;505 495 } 506 496 case OpCodes.IfThenElse: { … … 627 617 var indexExpr = Expression.Constant(variableIndices[variableName]); 628 618 var valuesExpr = Expression.ArrayIndex(columns, indexExpr); 629 var variableValue = Expression. ArrayIndex(valuesExpr, row);619 var variableValue = Expression.Property(valuesExpr, Indexer, row); 630 620 var variableThreshold = Expression.Constant(variableConditionTreeNode.Threshold); 631 621 var variableSlope = Expression.Constant(variableConditionTreeNode.Slope); … … 642 632 ); 643 633 } 644 case OpCodes.LagVariable: {645 var laggedVariableTreeNode = (LaggedVariableTreeNode)node;646 var lag = Expression.Constant(laggedVariableTreeNode.Lag);647 var variableWeight = Expression.Constant(laggedVariableTreeNode.Weight);648 var variableName = laggedVariableTreeNode.VariableName;649 var indexExpr = Expression.Constant(variableIndices[variableName]);650 var valuesExpr = Expression.ArrayIndex(columns, indexExpr);651 var variableValue = Expression.Property(valuesExpr, Indexer, Expression.Add(row, lag));652 return Expression.Multiply(variableWeight, variableValue);653 }654 case OpCodes.TimeLag: {655 var timeLagTreeNode = (LaggedTreeNode)node;656 var lag = Expression.Constant(timeLagTreeNode.Lag);657 return MakeExpr(timeLagTreeNode.GetSubtree(0), variableIndices, Expression.Add(row, lag), columns);658 }659 case OpCodes.Integral: {660 var timeLagTreeNode = (LaggedTreeNode)node;661 var subtree = node.GetSubtree(0);662 var sum = MakeExpr(subtree, variableIndices, row, columns);663 var sign = Expression.Constant(Math.Sign(timeLagTreeNode.Lag));664 var lag = Expression.Add(row, sign);665 for (int i = 0; i < Math.Abs(timeLagTreeNode.Lag); ++i) {666 sum = Expression.Add(sum, MakeExpr(subtree, variableIndices, lag, columns));667 lag = Expression.Add(lag, sign);668 }669 return sum;670 }671 case OpCodes.Derivative: {672 var subtree = node.GetSubtree(0);673 var f0 = MakeExpr(subtree, variableIndices, row, columns);674 var f1 = MakeExpr(subtree, variableIndices, Expression.Subtract(row, Expression.Constant(1)), columns);675 var f3 = MakeExpr(subtree, variableIndices, Expression.Subtract(row, Expression.Constant(3)), columns);676 var f4 = MakeExpr(subtree, variableIndices, Expression.Subtract(row, Expression.Constant(4)), columns);677 678 var result = f0;679 result = Expression.Add(result, Expression.Multiply(Expression.Constant(2.0), f1));680 result = Expression.Subtract(result, Expression.Multiply(Expression.Constant(2.0), f3));681 result = Expression.Subtract(result, f4);682 return Expression.Divide(result, Expression.Constant(8.0));683 }684 634 default: 685 635 throw new NotSupportedException("Unsupported symbol: " + node.Symbol); … … 687 637 #endregion 688 638 } 689 // util stuff690 private static Func<T, R> GetField<T, R>(string fieldName) {691 ParameterExpression param = Expression.Parameter(typeof(T), "arg");692 MemberExpression member = Expression.Field(param, fieldName);693 LambdaExpression lambda = Expression.Lambda(typeof(Func<T, R>), member, param);694 Func<T, R> compiled = (Func<T, R>)lambda.Compile();695 return compiled;696 }697 639 } 698 640 } -
stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeILEmittingInterpreter.cs
r14186 r14811 69 69 70 70 private const string CheckExpressionsWithIntervalArithmeticParameterName = "CheckExpressionsWithIntervalArithmetic"; 71 private const string CheckExpressionsWithIntervalArithmeticParameterDescription = "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression."; 71 72 private const string EvaluatedSolutionsParameterName = "EvaluatedSolutions"; 72 73 … … 80 81 81 82 #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 83 public IFixedValueParameter<BoolValue> CheckExpressionsWithIntervalArithmeticParameter { 84 get { return (IFixedValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName]; } 85 } 86 87 public IFixedValueParameter<IntValue> EvaluatedSolutionsParameter { 88 get { return (IFixedValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; } 89 } 91 90 #endregion 92 91 93 92 #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 93 public bool CheckExpressionsWithIntervalArithmetic { 94 get { return CheckExpressionsWithIntervalArithmeticParameter.Value.Value; } 95 set { CheckExpressionsWithIntervalArithmeticParameter.Value.Value = value; } 96 } 97 public int EvaluatedSolutions { 98 get { return EvaluatedSolutionsParameter.Value.Value; } 99 set { EvaluatedSolutionsParameter.Value.Value = value; } 100 } 105 101 #endregion 106 107 102 108 103 [StorableConstructor] … … 116 111 public SymbolicDataAnalysisExpressionTreeILEmittingInterpreter() 117 112 : 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))); 113 Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, 114 "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false))); 115 Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0))); 116 } 117 118 public SymbolicDataAnalysisExpressionTreeILEmittingInterpreter(string name, string description) 119 : base(name, description) { 120 Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, 121 "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false))); 122 Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0))); 120 123 } 121 124 122 125 [StorableHook(HookType.AfterDeserialization)] 123 126 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))); 127 var evaluatedSolutions = new IntValue(0); 128 var checkExpressionsWithIntervalArithmetic = new BoolValue(false); 129 if (Parameters.ContainsKey(EvaluatedSolutionsParameterName)) { 130 var evaluatedSolutionsParameter = (IValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; 131 evaluatedSolutions = evaluatedSolutionsParameter.Value; 132 Parameters.Remove(EvaluatedSolutionsParameterName); 133 } 134 Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", evaluatedSolutions)); 135 if (Parameters.ContainsKey(CheckExpressionsWithIntervalArithmeticParameterName)) { 136 var checkExpressionsWithIntervalArithmeticParameter = (IValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName]; 137 Parameters.Remove(CheckExpressionsWithIntervalArithmeticParameterName); 138 checkExpressionsWithIntervalArithmetic = checkExpressionsWithIntervalArithmeticParameter.Value; 139 } 140 Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, CheckExpressionsWithIntervalArithmeticParameterDescription, checkExpressionsWithIntervalArithmetic)); 126 141 } 127 142 128 143 #region IStatefulItem 129 130 144 public void InitializeState() { 131 EvaluatedSolutions .Value= 0;145 EvaluatedSolutions = 0; 132 146 } 133 147 134 148 public void ClearState() { 135 EvaluatedSolutions.Value = 0; 136 } 137 149 } 138 150 #endregion 139 151 152 private readonly object syncRoot = new object(); 140 153 public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows) { 141 if (CheckExpressionsWithIntervalArithmetic .Value)154 if (CheckExpressionsWithIntervalArithmetic) 142 155 throw new NotSupportedException("Interval arithmetic is not yet supported in the symbolic data analysis interpreter."); 143 156 144 EvaluatedSolutions.Value++; // increment the evaluated solutions counter 157 lock (syncRoot) { 158 EvaluatedSolutions++; // increment the evaluated solutions counter 159 } 145 160 var state = PrepareInterpreterState(tree, dataset); 146 161 … … 244 259 il.Emit(System.Reflection.Emit.OpCodes.Add); 245 260 } 246 il.Emit(System.Reflection.Emit.OpCodes.Ldc_ I4,nArgs);261 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, (double)nArgs); 247 262 il.Emit(System.Reflection.Emit.OpCodes.Div); 248 263 return; … … 442 457 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_0); // > 0 443 458 il.Emit(System.Reflection.Emit.OpCodes.Cgt); 459 il.Emit(System.Reflection.Emit.OpCodes.Conv_R8); // convert to float64 444 460 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 2.0); // * 2 445 461 il.Emit(System.Reflection.Emit.OpCodes.Mul); … … 460 476 il.Emit(System.Reflection.Emit.OpCodes.Xor); 461 477 } 478 il.Emit(System.Reflection.Emit.OpCodes.Conv_R8); // convert to float64 479 462 480 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 2.0); // * 2 463 481 il.Emit(System.Reflection.Emit.OpCodes.Mul); … … 471 489 472 490 il.Emit(System.Reflection.Emit.OpCodes.Cgt); // 1 (>) / 0 (otherwise) 491 il.Emit(System.Reflection.Emit.OpCodes.Conv_R8); // convert to float64 473 492 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 2.0); // * 2 474 493 il.Emit(System.Reflection.Emit.OpCodes.Mul); … … 481 500 CompileInstructions(il, state, ds); 482 501 il.Emit(System.Reflection.Emit.OpCodes.Clt); 502 il.Emit(System.Reflection.Emit.OpCodes.Conv_R8); // convert to float64 483 503 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 2.0); // * 2 484 504 il.Emit(System.Reflection.Emit.OpCodes.Mul); -
stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeInterpreter.cs
r14186 r14811 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 } -
stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeLinearInterpreter.cs
r14306 r14811 35 35 public sealed class SymbolicDataAnalysisExpressionTreeLinearInterpreter : ParameterizedNamedItem, ISymbolicDataAnalysisExpressionTreeInterpreter { 36 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."; 37 38 private const string EvaluatedSolutionsParameterName = "EvaluatedSolutions"; 38 39 39 private SymbolicDataAnalysisExpressionTreeInterpreter interpreter;40 private readonly SymbolicDataAnalysisExpressionTreeInterpreter interpreter; 40 41 41 42 public override bool CanChangeName { … … 48 49 49 50 #region parameter properties 50 public I ValueParameter<BoolValue> CheckExpressionsWithIntervalArithmeticParameter {51 get { return (I ValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName]; }52 } 53 54 public I ValueParameter<IntValue> EvaluatedSolutionsParameter {55 get { return (I ValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; }51 public IFixedValueParameter<BoolValue> CheckExpressionsWithIntervalArithmeticParameter { 52 get { return (IFixedValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName]; } 53 } 54 55 public IFixedValueParameter<IntValue> EvaluatedSolutionsParameter { 56 get { return (IFixedValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; } 56 57 } 57 58 #endregion 58 59 59 60 #region properties 60 public BoolValueCheckExpressionsWithIntervalArithmetic {61 get { return CheckExpressionsWithIntervalArithmeticParameter.Value ; }62 set { CheckExpressionsWithIntervalArithmeticParameter.Value = value; }63 } 64 public IntValueEvaluatedSolutions {65 get { return EvaluatedSolutionsParameter.Value ; }66 set { EvaluatedSolutionsParameter.Value = value; }61 public bool CheckExpressionsWithIntervalArithmetic { 62 get { return CheckExpressionsWithIntervalArithmeticParameter.Value.Value; } 63 set { CheckExpressionsWithIntervalArithmeticParameter.Value.Value = value; } 64 } 65 public int EvaluatedSolutions { 66 get { return EvaluatedSolutionsParameter.Value.Value; } 67 set { EvaluatedSolutionsParameter.Value.Value = value; } 67 68 } 68 69 #endregion … … 71 72 private SymbolicDataAnalysisExpressionTreeLinearInterpreter(bool deserializing) 72 73 : base(deserializing) { 74 interpreter = new SymbolicDataAnalysisExpressionTreeInterpreter(); 73 75 } 74 76 … … 84 86 public SymbolicDataAnalysisExpressionTreeLinearInterpreter() 85 87 : 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))); 88 Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, CheckExpressionsWithIntervalArithmeticParameterDescription, new BoolValue(false))); 89 Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0))); 90 interpreter = new SymbolicDataAnalysisExpressionTreeInterpreter(); 91 } 92 93 public SymbolicDataAnalysisExpressionTreeLinearInterpreter(string name, string description) 94 : base(name, description) { 95 Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, CheckExpressionsWithIntervalArithmeticParameterDescription, 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))); 88 97 interpreter = new SymbolicDataAnalysisExpressionTreeInterpreter(); 89 98 } … … 91 100 [StorableHook(HookType.AfterDeserialization)] 92 101 private void AfterDeserialization() { 93 if (interpreter == null) interpreter = new SymbolicDataAnalysisExpressionTreeInterpreter(); 102 var evaluatedSolutions = new IntValue(0); 103 var checkExpressionsWithIntervalArithmetic = new BoolValue(false); 104 if (Parameters.ContainsKey(EvaluatedSolutionsParameterName)) { 105 var evaluatedSolutionsParameter = (IValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; 106 evaluatedSolutions = evaluatedSolutionsParameter.Value; 107 Parameters.Remove(EvaluatedSolutionsParameterName); 108 } 109 Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", evaluatedSolutions)); 110 if (Parameters.ContainsKey(CheckExpressionsWithIntervalArithmeticParameterName)) { 111 var checkExpressionsWithIntervalArithmeticParameter = (IValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName]; 112 Parameters.Remove(CheckExpressionsWithIntervalArithmeticParameterName); 113 checkExpressionsWithIntervalArithmetic = checkExpressionsWithIntervalArithmeticParameter.Value; 114 } 115 Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, CheckExpressionsWithIntervalArithmeticParameterDescription, checkExpressionsWithIntervalArithmetic)); 94 116 } 95 117 96 118 #region IStatefulItem 97 119 public void InitializeState() { 98 EvaluatedSolutions .Value= 0;120 EvaluatedSolutions = 0; 99 121 } 100 122 … … 102 124 #endregion 103 125 126 private readonly object syncRoot = new object(); 104 127 public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows) { 105 if (CheckExpressionsWithIntervalArithmetic .Value)128 if (CheckExpressionsWithIntervalArithmetic) 106 129 throw new NotSupportedException("Interval arithmetic is not yet supported in the symbolic data analysis interpreter."); 107 130 108 lock ( EvaluatedSolutions) {109 EvaluatedSolutions .Value++; // increment the evaluated solutions counter131 lock (syncRoot) { 132 EvaluatedSolutions++; // increment the evaluated solutions counter 110 133 } 111 134 … … 190 213 } else if (instr.opCode == OpCodes.Root) { 191 214 double x = code[instr.childIndex].value; 192 double y = code[instr.childIndex + 1].value;215 double y = Math.Round(code[instr.childIndex + 1].value); 193 216 instr.value = Math.Pow(x, 1 / y); 194 217 } else if (instr.opCode == OpCodes.Exp) { -
stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/VariableCondition.cs
r14186 r14811 148 148 OnChanged(EventArgs.Empty); 149 149 } 150 } 151 } 152 153 public override bool Enabled { 154 get { 155 if (variableNames.Count == 0) return false; 156 return base.Enabled; 157 } 158 set { 159 if (variableNames.Count == 0) base.Enabled = false; 160 else base.Enabled = value; 150 161 } 151 162 } -
stable/HeuristicLab.Tests
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Tests merged: 13256,13313-13315,13318,14810
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/SymbolicDataAnalysisExpressionTreeInterpreterTest.cs
r14186 r14811 70 70 } 71 71 72 [TestMethod] 73 [TestCategory("Problems.DataAnalysis.Symbolic")] 74 [TestProperty("Time", "long")] 75 public void CompiledInterpreterTestTypeCoherentGrammarPerformance() { 76 TestTypeCoherentGrammarPerformance(new SymbolicDataAnalysisExpressionCompiledTreeInterpreter(), 12.5e6); 77 } 78 [TestMethod] 79 [TestCategory("Problems.DataAnalysis.Symbolic")] 80 [TestProperty("Time", "long")] 81 public void CompiledInterpreterTestFullGrammarPerformance() { 82 TestFullGrammarPerformance(new SymbolicDataAnalysisExpressionCompiledTreeInterpreter(), 12.5e6); 83 } 84 [TestMethod] 85 [TestCategory("Problems.DataAnalysis.Symbolic")] 86 [TestProperty("Time", "long")] 87 public void CompiledInterpreterTestArithmeticGrammarPerformance() { 88 TestArithmeticGrammarPerformance(new SymbolicDataAnalysisExpressionCompiledTreeInterpreter(), 12.5e6); 89 } 72 90 73 91 [TestMethod] … … 80 98 [TestCategory("Problems.DataAnalysis.Symbolic")] 81 99 [TestProperty("Time", "long")] 82 public void ILEmittingInterpreterTestFullGrammarPerformance() {83 TestFullGrammarPerformance(new SymbolicDataAnalysisExpressionTreeILEmittingInterpreter(), 7.5e6);84 }85 [TestMethod]86 [TestCategory("Problems.DataAnalysis.Symbolic")]87 [TestProperty("Time", "long")]88 100 public void ILEmittingInterpreterTestArithmeticGrammarPerformance() { 89 101 TestArithmeticGrammarPerformance(new SymbolicDataAnalysisExpressionTreeILEmittingInterpreter(), 7.5e6); 90 102 } 91 92 103 93 104 [TestMethod] … … 113 124 var twister = new MersenneTwister(31415); 114 125 var dataset = Util.CreateRandomDataset(twister, Rows, Columns); 126 115 127 var grammar = new TypeCoherentExpressionGrammar(); 116 128 grammar.ConfigureAsDefaultRegressionGrammar(); 117 grammar.MaximumFunctionArguments = 0; 118 grammar.MaximumFunctionDefinitions = 0; 119 grammar.MinimumFunctionArguments = 0; 120 grammar.MinimumFunctionDefinitions = 0; 129 121 130 var randomTrees = Util.CreateRandomTrees(twister, dataset, grammar, N, 1, 100, 0, 0); 122 131 foreach (ISymbolicExpressionTree tree in randomTrees) { … … 131 140 var twister = new MersenneTwister(31415); 132 141 var dataset = Util.CreateRandomDataset(twister, Rows, Columns); 142 133 143 var grammar = new FullFunctionalExpressionGrammar(); 134 grammar.MaximumFunctionArguments = 0;135 grammar.MaximumFunctionDefinitions = 0;136 grammar.MinimumFunctionArguments = 0;137 grammar.MinimumFunctionDefinitions = 0;138 144 var randomTrees = Util.CreateRandomTrees(twister, dataset, grammar, N, 1, 100, 0, 0); 139 145 foreach (ISymbolicExpressionTree tree in randomTrees) { … … 148 154 var twister = new MersenneTwister(31415); 149 155 var dataset = Util.CreateRandomDataset(twister, Rows, Columns); 156 150 157 var grammar = new ArithmeticExpressionGrammar(); 151 //grammar.Symbols.OfType<Variable>().First().Enabled = false;152 grammar.MaximumFunctionArguments = 0;153 grammar.MaximumFunctionDefinitions = 0;154 grammar.MinimumFunctionArguments = 0;155 grammar.MinimumFunctionDefinitions = 0;156 158 var randomTrees = Util.CreateRandomTrees(twister, dataset, grammar, N, 1, 100, 0, 0); 157 159 foreach (SymbolicExpressionTree tree in randomTrees) { … … 175 177 EvaluateTerminals(interpreter, ds); 176 178 EvaluateOperations(interpreter, ds); 179 EvaluateLaggedOperations(interpreter, ds); 180 EvaluateSpecialFunctions(interpreter, ds); 177 181 EvaluateAdf(interpreter, ds); 178 182 } 179 183 184 /// <summary> 185 ///A test for Evaluate 186 ///</summary> 180 187 [TestMethod] 181 188 [TestCategory("Problems.DataAnalysis.Symbolic")] … … 185 192 EvaluateTerminals(interpreter, ds); 186 193 EvaluateOperations(interpreter, ds); 194 EvaluateLaggedOperations(interpreter, ds); 195 EvaluateSpecialFunctions(interpreter, ds); 196 } 197 198 [TestMethod] 199 [TestCategory("Problems.DataAnalysis.Symbolic")] 200 [TestProperty("Time", "short")] 201 public void CompiledInterpreterTestEvaluation() { 202 var interpreter = new SymbolicDataAnalysisExpressionCompiledTreeInterpreter(); 203 EvaluateTerminals(interpreter, ds); 204 EvaluateOperations(interpreter, ds); 205 EvaluateSpecialFunctions(interpreter, ds); 187 206 } 188 207 … … 192 211 public void LinearInterpreterTestEvaluation() { 193 212 var interpreter = new SymbolicDataAnalysisExpressionTreeLinearInterpreter(); 194 195 213 //ADFs are not supported by the linear interpreter 196 214 EvaluateTerminals(interpreter, ds); 197 215 EvaluateOperations(interpreter, ds); 198 } 199 200 private void EvaluateTerminals(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, Dataset ds) { 216 EvaluateLaggedOperations(interpreter, ds); 217 EvaluateSpecialFunctions(interpreter, ds); 218 } 219 220 [TestMethod] 221 [TestCategory("Problems.DataAnalysis.Symbolic")] 222 [TestProperty("Time", "long")] 223 public void TestInterpretersEstimatedValuesConsistency() { 224 var twister = new MersenneTwister(); 225 int seed = twister.Next(0, int.MaxValue); 226 twister.Seed((uint)seed); 227 const int numRows = 100; 228 var dataset = Util.CreateRandomDataset(twister, numRows, Columns); 229 230 var grammar = new TypeCoherentExpressionGrammar(); 231 232 var interpreters = new ISymbolicDataAnalysisExpressionTreeInterpreter[] { 233 new SymbolicDataAnalysisExpressionTreeLinearInterpreter(), 234 new SymbolicDataAnalysisExpressionTreeInterpreter(), 235 }; 236 237 var rows = Enumerable.Range(0, numRows).ToList(); 238 var randomTrees = Util.CreateRandomTrees(twister, dataset, grammar, N, 1, 10, 0, 0); 239 foreach (ISymbolicExpressionTree tree in randomTrees) { 240 Util.InitTree(tree, twister, new List<string>(dataset.VariableNames)); 241 } 242 243 for (int i = 0; i < randomTrees.Length; ++i) { 244 var tree = randomTrees[i]; 245 var valuesMatrix = interpreters.Select(x => x.GetSymbolicExpressionTreeValues(tree, dataset, rows)).ToList(); 246 for (int m = 0; m < interpreters.Length - 1; ++m) { 247 var sum = valuesMatrix[m].Sum(); 248 for (int n = m + 1; n < interpreters.Length; ++n) { 249 var s = valuesMatrix[n].Sum(); 250 if (double.IsNaN(sum) && double.IsNaN(s)) continue; 251 252 string errorMessage = string.Format("Interpreters {0} and {1} do not agree on tree {2} (seed = {3}).", interpreters[m].Name, interpreters[n].Name, i, seed); 253 Assert.AreEqual(sum, s, 1e-12, errorMessage); 254 } 255 } 256 } 257 } 258 259 [TestMethod] 260 [TestCategory("Problems.DataAnalysis.Symbolic")] 261 [TestProperty("Time", "long")] 262 public void TestCompiledInterpreterEstimatedValuesConsistency() { 263 const double delta = 1e-12; 264 265 var twister = new MersenneTwister(); 266 int seed = twister.Next(0, int.MaxValue); 267 twister.Seed((uint)seed); 268 269 Console.WriteLine(seed); 270 271 const int numRows = 100; 272 var dataset = Util.CreateRandomDataset(twister, numRows, Columns); 273 274 var grammar = new TypeCoherentExpressionGrammar(); 275 grammar.ConfigureAsDefaultRegressionGrammar(); 276 grammar.Symbols.First(x => x.Name == "Power Functions").Enabled = true; 277 278 var randomTrees = Util.CreateRandomTrees(twister, dataset, grammar, N, 1, 10, 0, 0); 279 foreach (ISymbolicExpressionTree tree in randomTrees) { 280 Util.InitTree(tree, twister, new List<string>(dataset.VariableNames)); 281 } 282 283 var interpreters = new ISymbolicDataAnalysisExpressionTreeInterpreter[] { 284 new SymbolicDataAnalysisExpressionCompiledTreeInterpreter(), 285 new SymbolicDataAnalysisExpressionTreeInterpreter(), 286 new SymbolicDataAnalysisExpressionTreeLinearInterpreter(), 287 }; 288 var rows = Enumerable.Range(0, numRows).ToList(); 289 var formatter = new SymbolicExpressionTreeHierarchicalFormatter(); 290 291 for (int i = 0; i < randomTrees.Length; ++i) { 292 var tree = randomTrees[i]; 293 var valuesMatrix = interpreters.Select(x => x.GetSymbolicExpressionTreeValues(tree, dataset, rows).ToList()).ToList(); 294 for (int m = 0; m < interpreters.Length - 1; ++m) { 295 for (int n = m + 1; n < interpreters.Length; ++n) { 296 for (int row = 0; row < numRows; ++row) { 297 var v1 = valuesMatrix[m][row]; 298 var v2 = valuesMatrix[n][row]; 299 if (double.IsNaN(v1) && double.IsNaN(v2)) continue; 300 if (Math.Abs(v1 - v2) > delta) { 301 Console.WriteLine(formatter.Format(tree)); 302 foreach (var node in tree.Root.GetSubtree(0).GetSubtree(0).IterateNodesPrefix().ToList()) { 303 var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode(); 304 if (rootNode.HasLocalParameters) rootNode.ResetLocalParameters(twister); 305 rootNode.SetGrammar(grammar.CreateExpressionTreeGrammar()); 306 307 var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode(); 308 if (startNode.HasLocalParameters) startNode.ResetLocalParameters(twister); 309 startNode.SetGrammar(grammar.CreateExpressionTreeGrammar()); 310 311 rootNode.AddSubtree(startNode); 312 var t = new SymbolicExpressionTree(rootNode); 313 var start = t.Root.GetSubtree(0); 314 var p = node.Parent; 315 start.AddSubtree(node); 316 Console.WriteLine(node); 317 318 var y1 = interpreters[m].GetSymbolicExpressionTreeValues(t, dataset, new[] { row }).First(); 319 var y2 = interpreters[n].GetSymbolicExpressionTreeValues(t, dataset, new[] { row }).First(); 320 321 if (double.IsNaN(y1) && double.IsNaN(y2)) continue; 322 string prefix = Math.Abs(y1 - y2) > delta ? "++" : "=="; 323 Console.WriteLine("\t{0} Row {1}: {2} {3}, Deviation = {4}", prefix, row, y1, y2, Math.Abs(y1 - y2)); 324 node.Parent = p; 325 } 326 } 327 string errorMessage = string.Format("Interpreters {0} and {1} do not agree on tree {2} and row {3} (seed = {4}).", interpreters[m].Name, interpreters[n].Name, i, row, seed); 328 Assert.AreEqual(v1, v2, delta, errorMessage); 329 } 330 } 331 } 332 } 333 } 334 335 private void EvaluateTerminals(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, IDataset ds) { 201 336 // constants 202 337 Evaluate(interpreter, ds, "(+ 1.5 3.5)", 0, 5.0); … … 207 342 } 208 343 209 private void EvaluateAdf(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, Dataset ds) {344 private void EvaluateAdf(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, IDataset ds) { 210 345 211 346 // ADF … … 246 381 } 247 382 248 private void EvaluateOperations(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, Dataset ds) { 383 private void EvaluateSpecialFunctions(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, IDataset ds) { 384 // special functions 385 Action<double> checkAiry = (x) => { 386 double ai, aip, bi, bip; 387 alglib.airy(x, out ai, out aip, out bi, out bip); 388 Evaluate(interpreter, ds, "(airya " + x + ")", 0, ai); 389 Evaluate(interpreter, ds, "(airyb " + x + ")", 0, bi); 390 }; 391 392 Action<double> checkBessel = (x) => { 393 Evaluate(interpreter, ds, "(bessel " + x + ")", 0, alglib.besseli0(x)); 394 }; 395 396 Action<double> checkSinCosIntegrals = (x) => { 397 double si, ci; 398 alglib.sinecosineintegrals(x, out si, out ci); 399 Evaluate(interpreter, ds, "(cosint " + x + ")", 0, ci); 400 Evaluate(interpreter, ds, "(sinint " + x + ")", 0, si); 401 }; 402 Action<double> checkHypSinCosIntegrals = (x) => { 403 double shi, chi; 404 alglib.hyperbolicsinecosineintegrals(x, out shi, out chi); 405 Evaluate(interpreter, ds, "(hypcosint " + x + ")", 0, chi); 406 Evaluate(interpreter, ds, "(hypsinint " + x + ")", 0, shi); 407 }; 408 Action<double> checkFresnelSinCosIntegrals = (x) => { 409 double c = 0, s = 0; 410 alglib.fresnelintegral(x, ref c, ref s); 411 Evaluate(interpreter, ds, "(fresnelcosint " + x + ")", 0, c); 412 Evaluate(interpreter, ds, "(fresnelsinint " + x + ")", 0, s); 413 }; 414 Action<double> checkNormErf = (x) => { 415 Evaluate(interpreter, ds, "(norm " + x + ")", 0, alglib.normaldistribution(x)); 416 Evaluate(interpreter, ds, "(erf " + x + ")", 0, alglib.errorfunction(x)); 417 }; 418 419 Action<double> checkGamma = (x) => { 420 Evaluate(interpreter, ds, "(gamma " + x + ")", 0, alglib.gammafunction(x)); 421 }; 422 Action<double> checkPsi = (x) => { 423 try { 424 Evaluate(interpreter, ds, "(psi " + x + ")", 0, alglib.psi(x)); 425 } catch (alglib.alglibexception) { // ignore cases where alglib throws an exception 426 } 427 }; 428 Action<double> checkDawson = (x) => { 429 Evaluate(interpreter, ds, "(dawson " + x + ")", 0, alglib.dawsonintegral(x)); 430 }; 431 Action<double> checkExpInt = (x) => { 432 Evaluate(interpreter, ds, "(expint " + x + ")", 0, alglib.exponentialintegralei(x)); 433 }; 434 435 foreach (var e in new[] { -2.0, -1.0, 0.0, 1.0, 2.0 }) { 436 checkAiry(e); 437 checkBessel(e); 438 checkSinCosIntegrals(e); 439 checkGamma(e); 440 checkExpInt(e); 441 checkDawson(e); 442 checkPsi(e); 443 checkNormErf(e); 444 checkFresnelSinCosIntegrals(e); 445 checkHypSinCosIntegrals(e); 446 } 447 } 448 449 private void EvaluateLaggedOperations(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, IDataset ds) { 450 // lag 451 Evaluate(interpreter, ds, "(lagVariable 1.0 a -1) ", 1, ds.GetDoubleValue("A", 0)); 452 Evaluate(interpreter, ds, "(lagVariable 1.0 a -1) ", 2, ds.GetDoubleValue("A", 1)); 453 Evaluate(interpreter, ds, "(lagVariable 1.0 a 0) ", 2, ds.GetDoubleValue("A", 2)); 454 Evaluate(interpreter, ds, "(lagVariable 1.0 a 1) ", 0, ds.GetDoubleValue("A", 1)); 455 456 // integral 457 Evaluate(interpreter, ds, "(integral -1.0 (variable 1.0 a)) ", 1, ds.GetDoubleValue("A", 0) + ds.GetDoubleValue("A", 1)); 458 Evaluate(interpreter, ds, "(integral -1.0 (lagVariable 1.0 a 1)) ", 1, ds.GetDoubleValue("A", 1) + ds.GetDoubleValue("A", 2)); 459 Evaluate(interpreter, ds, "(integral -2.0 (variable 1.0 a)) ", 2, ds.GetDoubleValue("A", 0) + ds.GetDoubleValue("A", 1) + ds.GetDoubleValue("A", 2)); 460 Evaluate(interpreter, ds, "(integral -1.0 (* (variable 1.0 a) (variable 1.0 b)))", 1, ds.GetDoubleValue("A", 0) * ds.GetDoubleValue("B", 0) + ds.GetDoubleValue("A", 1) * ds.GetDoubleValue("B", 1)); 461 Evaluate(interpreter, ds, "(integral -2.0 3.0)", 1, 9.0); 462 463 // derivative 464 // (f_0 + 2 * f_1 - 2 * f_3 - f_4) / 8; // h = 1 465 Evaluate(interpreter, ds, "(diff (variable 1.0 a)) ", 5, (ds.GetDoubleValue("A", 5) + 2 * ds.GetDoubleValue("A", 4) - 2 * ds.GetDoubleValue("A", 2) - ds.GetDoubleValue("A", 1)) / 8.0); 466 Evaluate(interpreter, ds, "(diff (variable 1.0 b)) ", 5, (ds.GetDoubleValue("B", 5) + 2 * ds.GetDoubleValue("B", 4) - 2 * ds.GetDoubleValue("B", 2) - ds.GetDoubleValue("B", 1)) / 8.0); 467 Evaluate(interpreter, ds, "(diff (* (variable 1.0 a) (variable 1.0 b)))", 5, + 468 (ds.GetDoubleValue("A", 5) * ds.GetDoubleValue("B", 5) + 469 2 * ds.GetDoubleValue("A", 4) * ds.GetDoubleValue("B", 4) - 470 2 * ds.GetDoubleValue("A", 2) * ds.GetDoubleValue("B", 2) - 471 ds.GetDoubleValue("A", 1) * ds.GetDoubleValue("B", 1)) / 8.0); 472 Evaluate(interpreter, ds, "(diff -2.0 3.0)", 5, 0.0); 473 474 // timelag 475 Evaluate(interpreter, ds, "(lag -1.0 (lagVariable 1.0 a 2)) ", 1, ds.GetDoubleValue("A", 2)); 476 Evaluate(interpreter, ds, "(lag -2.0 (lagVariable 1.0 a 2)) ", 2, ds.GetDoubleValue("A", 2)); 477 Evaluate(interpreter, ds, "(lag -1.0 (* (lagVariable 1.0 a 1) (lagVariable 1.0 b 2)))", 1, ds.GetDoubleValue("A", 1) * ds.GetDoubleValue("B", 2)); 478 Evaluate(interpreter, ds, "(lag -2.0 3.0)", 1, 3.0); 479 } 480 481 private void EvaluateOperations(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, IDataset ds) { 249 482 // addition 250 483 Evaluate(interpreter, ds, "(+ (variable 2.0 a ))", 1, 4.0); … … 369 602 // mean 370 603 Evaluate(interpreter, ds, "(mean -1.0 1.0 -1.0)", 0, -1.0 / 3.0); 371 372 // lag 373 Evaluate(interpreter, ds, "(lagVariable 1.0 a -1) ", 1, ds.GetDoubleValue("A", 0)); 374 Evaluate(interpreter, ds, "(lagVariable 1.0 a -1) ", 2, ds.GetDoubleValue("A", 1)); 375 Evaluate(interpreter, ds, "(lagVariable 1.0 a 0) ", 2, ds.GetDoubleValue("A", 2)); 376 Evaluate(interpreter, ds, "(lagVariable 1.0 a 1) ", 0, ds.GetDoubleValue("A", 1)); 377 378 // integral 379 Evaluate(interpreter, ds, "(integral -1.0 (variable 1.0 a)) ", 1, ds.GetDoubleValue("A", 0) + ds.GetDoubleValue("A", 1)); 380 Evaluate(interpreter, ds, "(integral -1.0 (lagVariable 1.0 a 1)) ", 1, ds.GetDoubleValue("A", 1) + ds.GetDoubleValue("A", 2)); 381 Evaluate(interpreter, ds, "(integral -2.0 (variable 1.0 a)) ", 2, ds.GetDoubleValue("A", 0) + ds.GetDoubleValue("A", 1) + ds.GetDoubleValue("A", 2)); 382 Evaluate(interpreter, ds, "(integral -1.0 (* (variable 1.0 a) (variable 1.0 b)))", 1, ds.GetDoubleValue("A", 0) * ds.GetDoubleValue("B", 0) + ds.GetDoubleValue("A", 1) * ds.GetDoubleValue("B", 1)); 383 Evaluate(interpreter, ds, "(integral -2.0 3.0)", 1, 9.0); 384 385 // derivative 386 // (f_0 + 2 * f_1 - 2 * f_3 - f_4) / 8; // h = 1 387 Evaluate(interpreter, ds, "(diff (variable 1.0 a)) ", 5, (ds.GetDoubleValue("A", 5) + 2 * ds.GetDoubleValue("A", 4) - 2 * ds.GetDoubleValue("A", 2) - ds.GetDoubleValue("A", 1)) / 8.0); 388 Evaluate(interpreter, ds, "(diff (variable 1.0 b)) ", 5, (ds.GetDoubleValue("B", 5) + 2 * ds.GetDoubleValue("B", 4) - 2 * ds.GetDoubleValue("B", 2) - ds.GetDoubleValue("B", 1)) / 8.0); 389 Evaluate(interpreter, ds, "(diff (* (variable 1.0 a) (variable 1.0 b)))", 5, + 390 (ds.GetDoubleValue("A", 5) * ds.GetDoubleValue("B", 5) + 391 2 * ds.GetDoubleValue("A", 4) * ds.GetDoubleValue("B", 4) - 392 2 * ds.GetDoubleValue("A", 2) * ds.GetDoubleValue("B", 2) - 393 ds.GetDoubleValue("A", 1) * ds.GetDoubleValue("B", 1)) / 8.0); 394 Evaluate(interpreter, ds, "(diff -2.0 3.0)", 5, 0.0); 395 396 // timelag 397 Evaluate(interpreter, ds, "(lag -1.0 (lagVariable 1.0 a 2)) ", 1, ds.GetDoubleValue("A", 2)); 398 Evaluate(interpreter, ds, "(lag -2.0 (lagVariable 1.0 a 2)) ", 2, ds.GetDoubleValue("A", 2)); 399 Evaluate(interpreter, ds, "(lag -1.0 (* (lagVariable 1.0 a 1) (lagVariable 1.0 b 2)))", 1, ds.GetDoubleValue("A", 1) * ds.GetDoubleValue("B", 2)); 400 Evaluate(interpreter, ds, "(lag -2.0 3.0)", 1, 3.0); 401 402 { 403 // special functions 404 Action<double> checkAiry = (x) => { 405 double ai, aip, bi, bip; 406 alglib.airy(x, out ai, out aip, out bi, out bip); 407 Evaluate(interpreter, ds, "(airya " + x + ")", 0, ai); 408 Evaluate(interpreter, ds, "(airyb " + x + ")", 0, bi); 409 }; 410 411 Action<double> checkBessel = (x) => { 412 Evaluate(interpreter, ds, "(bessel " + x + ")", 0, alglib.besseli0(x)); 413 }; 414 415 Action<double> checkSinCosIntegrals = (x) => { 416 double si, ci; 417 alglib.sinecosineintegrals(x, out si, out ci); 418 Evaluate(interpreter, ds, "(cosint " + x + ")", 0, ci); 419 Evaluate(interpreter, ds, "(sinint " + x + ")", 0, si); 420 }; 421 Action<double> checkHypSinCosIntegrals = (x) => { 422 double shi, chi; 423 alglib.hyperbolicsinecosineintegrals(x, out shi, out chi); 424 Evaluate(interpreter, ds, "(hypcosint " + x + ")", 0, chi); 425 Evaluate(interpreter, ds, "(hypsinint " + x + ")", 0, shi); 426 }; 427 Action<double> checkFresnelSinCosIntegrals = (x) => { 428 double c = 0, s = 0; 429 alglib.fresnelintegral(x, ref c, ref s); 430 Evaluate(interpreter, ds, "(fresnelcosint " + x + ")", 0, c); 431 Evaluate(interpreter, ds, "(fresnelsinint " + x + ")", 0, s); 432 }; 433 Action<double> checkNormErf = (x) => { 434 Evaluate(interpreter, ds, "(norm " + x + ")", 0, alglib.normaldistribution(x)); 435 Evaluate(interpreter, ds, "(erf " + x + ")", 0, alglib.errorfunction(x)); 436 }; 437 438 Action<double> checkGamma = (x) => { 439 Evaluate(interpreter, ds, "(gamma " + x + ")", 0, alglib.gammafunction(x)); 440 }; 441 Action<double> checkPsi = (x) => { 442 try { 443 Evaluate(interpreter, ds, "(psi " + x + ")", 0, alglib.psi(x)); 444 } 445 catch (alglib.alglibexception) { // ignore cases where alglib throws an exception 446 } 447 }; 448 Action<double> checkDawson = (x) => { 449 Evaluate(interpreter, ds, "(dawson " + x + ")", 0, alglib.dawsonintegral(x)); 450 }; 451 Action<double> checkExpInt = (x) => { 452 Evaluate(interpreter, ds, "(expint " + x + ")", 0, alglib.exponentialintegralei(x)); 453 }; 454 455 456 457 foreach (var e in new[] { -2.0, -1.0, 0.0, 1.0, 2.0 }) { 458 checkAiry(e); 459 checkBessel(e); 460 checkSinCosIntegrals(e); 461 checkGamma(e); 462 checkExpInt(e); 463 checkDawson(e); 464 checkPsi(e); 465 checkNormErf(e); 466 checkFresnelSinCosIntegrals(e); 467 checkHypSinCosIntegrals(e); 468 } 469 } 470 } 471 472 private void Evaluate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, Dataset ds, string expr, int index, double expected) { 604 } 605 606 private void Evaluate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, IDataset ds, string expr, int index, double expected) { 473 607 var importer = new SymbolicExpressionImporter(); 474 608 ISymbolicExpressionTree tree = importer.Import(expr);
Note: See TracChangeset
for help on using the changeset viewer.