Changeset 14811 for stable/HeuristicLab.Problems.DataAnalysis.Symbolic
- Timestamp:
- 03/31/17 14:49:21 (8 years ago)
- Location:
- stable
- Files:
-
- 8 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/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 }
Note: See TracChangeset
for help on using the changeset viewer.