Changeset 16723 for branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter
- Timestamp:
- 03/28/19 16:54:20 (6 years ago)
- Location:
- branches/2521_ProblemRefactoring
- Files:
-
- 8 edited
- 5 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/2521_ProblemRefactoring
- Property svn:mergeinfo changed
-
branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic
- Property svn:mergeinfo changed
-
branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/InterpreterState.cs
r16692 r16723 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 8Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/OpCodes.cs
r16692 r16723 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 8Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 46 46 public const byte OR = 14; 47 47 public const byte NOT = 15; 48 public const byte XOR = 45;49 48 50 49 … … 83 82 public const byte Erf = 43; 84 83 public const byte Bessel = 44; 84 public const byte XOR = 45; 85 85 public const byte FactorVariable = 46; 86 86 public const byte BinaryFactorVariable = 47; 87 public const byte Absolute = 48; 88 public const byte AnalyticQuotient = 49; 89 public const byte Cube = 50; 90 public const byte CubeRoot = 51; 91 92 public const byte Tanh = 52; 87 93 88 94 … … 95 101 { typeof(Cosine), OpCodes.Cos }, 96 102 { typeof(Tangent), OpCodes.Tan }, 103 { typeof (HyperbolicTangent), OpCodes.Tanh}, 97 104 { typeof(Logarithm), OpCodes.Log }, 98 105 { typeof(Exponential), OpCodes.Exp }, … … 113 120 { typeof(Power),OpCodes.Power}, 114 121 { typeof(Root),OpCodes.Root}, 115 { typeof(TimeLag), OpCodes.TimeLag}, 122 { typeof(TimeLag), OpCodes.TimeLag}, 116 123 { typeof(Integral), OpCodes.Integral}, 117 124 { typeof(Derivative), OpCodes.Derivative}, … … 135 142 { typeof(Bessel), OpCodes.Bessel}, 136 143 { typeof(FactorVariable), OpCodes.FactorVariable }, 137 { typeof(BinaryFactorVariable), OpCodes.BinaryFactorVariable } 144 { typeof(BinaryFactorVariable), OpCodes.BinaryFactorVariable }, 145 { typeof(Absolute), OpCodes.Absolute }, 146 { typeof(AnalyticQuotient), OpCodes.AnalyticQuotient }, 147 { typeof(Cube), OpCodes.Cube }, 148 { typeof(CubeRoot), OpCodes.CubeRoot } 138 149 }; 139 150 -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionCompiledTreeInterpreter.cs
r16692 r16723 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 8Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 30 30 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 31 31 using HeuristicLab.Parameters; 32 using H euristicLab.Persistence.Default.CompositeSerializers.Storable;32 using HEAL.Attic; 33 33 34 34 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 35 [Storable Class]35 [StorableType("DFA06F28-E224-4D93-9907-69792D24D1F9")] 36 36 [Item("SymbolicDataAnalysisExpressionCompiledTreeInterpreter", "Interpreter that converts the tree into a Linq.Expression then compiles it.")] 37 37 public sealed class SymbolicDataAnalysisExpressionCompiledTreeInterpreter : ParameterizedNamedItem, ISymbolicDataAnalysisExpressionTreeInterpreter { … … 41 41 42 42 #region method info for the commonly called functions 43 private static readonly MethodInfo Abs = typeof(Math).GetMethod("Abs", new[] { typeof(double) }); 43 44 private static readonly MethodInfo Sin = typeof(Math).GetMethod("Sin", new[] { typeof(double) }); 44 45 private static readonly MethodInfo Cos = typeof(Math).GetMethod("Cos", new[] { typeof(double) }); 45 46 private static readonly MethodInfo Tan = typeof(Math).GetMethod("Tan", new[] { typeof(double) }); 47 private static readonly MethodInfo Tanh = typeof(Math).GetMethod("Tanh", new[] { typeof(double) }); 46 48 private static readonly MethodInfo Sqrt = typeof(Math).GetMethod("Sqrt", new[] { typeof(double) }); 47 49 private static readonly MethodInfo Floor = typeof(Math).GetMethod("Floor", new[] { typeof(double) }); … … 97 99 98 100 [StorableConstructor] 99 private SymbolicDataAnalysisExpressionCompiledTreeInterpreter(bool deserializing) 100 : base(deserializing) { 101 private SymbolicDataAnalysisExpressionCompiledTreeInterpreter(StorableConstructorFlag _) : base(_) { 101 102 } 102 103 … … 207 208 return Expression.Divide(result, Expression.Constant((double)node.SubtreeCount)); 208 209 } 210 case OpCodes.Absolute: { 211 var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns); 212 return Expression.Call(Abs, arg); 213 } 209 214 case OpCodes.Cos: { 210 215 var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns); … … 219 224 return Expression.Call(Tan, arg); 220 225 } 226 case OpCodes.Tanh: { 227 var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns); 228 return Expression.Call(Tanh, arg); 229 } 221 230 case OpCodes.Square: { 222 231 var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns); 223 232 return Expression.Power(arg, Expression.Constant(2.0)); 233 } 234 case OpCodes.Cube: { 235 var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns); 236 return Expression.Power(arg, Expression.Constant(3.0)); 224 237 } 225 238 case OpCodes.Power: { … … 231 244 var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns); 232 245 return Expression.Call(Sqrt, arg); 246 } 247 case OpCodes.CubeRoot: { 248 var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns); 249 return Expression.Power(arg, Expression.Constant(1.0 / 3.0)); 233 250 } 234 251 case OpCodes.Root: { … … 493 510 Expression.Assign(result, Expression.Call(Bessel, arg))), 494 511 result); 512 } 513 case OpCodes.AnalyticQuotient: { 514 var x1 = MakeExpr(node.GetSubtree(0), variableIndices, row, columns); 515 var x2 = MakeExpr(node.GetSubtree(1), variableIndices, row, columns); 516 return Expression.Divide(x1, 517 Expression.Call(Sqrt, 518 Expression.Add( 519 Expression.Constant(1.0), 520 Expression.Multiply(x2, x2)))); 495 521 } 496 522 case OpCodes.IfThenElse: { -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeILEmittingInterpreter.cs
r16692 r16723 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 8Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 30 30 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 31 31 using HeuristicLab.Parameters; 32 using H euristicLab.Persistence.Default.CompositeSerializers.Storable;32 using HEAL.Attic; 33 33 34 34 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 35 [Storable Class]35 [StorableType("426718E3-2A57-4CA4-98A1-65EDD0B0BDBF")] 36 36 [Item("SymbolicDataAnalysisExpressionTreeILEmittingInterpreter", "Interpreter for symbolic expression trees.")] 37 37 public sealed class SymbolicDataAnalysisExpressionTreeILEmittingInterpreter : ParameterizedNamedItem, ISymbolicDataAnalysisExpressionTreeInterpreter { … … 45 45 private static MethodInfo sin = typeof(Math).GetMethod("Sin", new Type[] { typeof(double) }); 46 46 private static MethodInfo tan = typeof(Math).GetMethod("Tan", new Type[] { typeof(double) }); 47 private static MethodInfo tanh = typeof(Math).GetMethod("Tanh", new Type[] { typeof(double) }); 47 48 private static MethodInfo exp = typeof(Math).GetMethod("Exp", new Type[] { typeof(double) }); 48 49 private static MethodInfo log = typeof(Math).GetMethod("Log", new Type[] { typeof(double) }); … … 50 51 private static MethodInfo round = typeof(Math).GetMethod("Round", new Type[] { typeof(double) }); 51 52 private static MethodInfo sqrt = typeof(Math).GetMethod("Sqrt", new Type[] { typeof(double) }); 53 private static MethodInfo abs = typeof(Math).GetMethod("Abs", new Type[] { typeof(double) }); 52 54 53 55 private static MethodInfo airyA = thisType.GetMethod("AiryA", new Type[] { typeof(double) }); … … 103 105 104 106 [StorableConstructor] 105 private SymbolicDataAnalysisExpressionTreeILEmittingInterpreter( bool deserializing) : base(deserializing) { }107 private SymbolicDataAnalysisExpressionTreeILEmittingInterpreter(StorableConstructorFlag _) : base(_) { } 106 108 107 109 private SymbolicDataAnalysisExpressionTreeILEmittingInterpreter(SymbolicDataAnalysisExpressionTreeILEmittingInterpreter original, Cloner cloner) : base(original, cloner) { } … … 264 266 return; 265 267 } 268 case OpCodes.Absolute: { 269 CompileInstructions(il, state, ds); 270 il.Emit(System.Reflection.Emit.OpCodes.Call, abs); 271 return; 272 } 266 273 case OpCodes.Cos: { 267 274 CompileInstructions(il, state, ds); … … 277 284 CompileInstructions(il, state, ds); 278 285 il.Emit(System.Reflection.Emit.OpCodes.Call, tan); 286 return; 287 } 288 case OpCodes.Tanh: { 289 CompileInstructions(il, state, ds); 290 il.Emit(System.Reflection.Emit.OpCodes.Call, tanh); 279 291 return; 280 292 } … … 311 323 return; 312 324 } 325 case OpCodes.Cube: { 326 CompileInstructions(il, state, ds); 327 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 3.0); 328 il.Emit(System.Reflection.Emit.OpCodes.Call, power); 329 return; 330 } 313 331 case OpCodes.SquareRoot: { 314 332 CompileInstructions(il, state, ds); … … 316 334 return; 317 335 } 336 case OpCodes.CubeRoot: { 337 CompileInstructions(il, state, ds); 338 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0 / 3.0); 339 il.Emit(System.Reflection.Emit.OpCodes.Call, power); 340 return; 341 } 318 342 case OpCodes.AiryA: { 319 343 CompileInstructions(il, state, ds); … … 389 413 CompileInstructions(il, state, ds); 390 414 il.Emit(System.Reflection.Emit.OpCodes.Call, sinIntegral); 415 return; 416 } 417 case OpCodes.AnalyticQuotient: { 418 CompileInstructions(il, state, ds); // x1 419 CompileInstructions(il, state, ds); // x2 420 421 il.Emit(System.Reflection.Emit.OpCodes.Dup); 422 il.Emit(System.Reflection.Emit.OpCodes.Mul); // x2*x2 423 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0); 424 il.Emit(System.Reflection.Emit.OpCodes.Mul); // 1+x2*x2 425 il.Emit(System.Reflection.Emit.OpCodes.Call, sqrt); 426 il.Emit(System.Reflection.Emit.OpCodes.Div); 391 427 return; 392 428 } … … 395 431 Label c1 = il.DefineLabel(); 396 432 CompileInstructions(il, state, ds); 397 il.Emit(System.Reflection.Emit.OpCodes.Ldc_ I4_0); // > 0433 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 0.0); // > 0 398 434 il.Emit(System.Reflection.Emit.OpCodes.Cgt); 399 435 il.Emit(System.Reflection.Emit.OpCodes.Brfalse, c1); … … 410 446 CompileInstructions(il, state, ds); 411 447 for (int i = 1; i < nArgs; i++) { 412 il.Emit(System.Reflection.Emit.OpCodes.Ldc_ I4_0); // > 0448 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 0.0); // > 0 413 449 il.Emit(System.Reflection.Emit.OpCodes.Cgt); 414 450 il.Emit(System.Reflection.Emit.OpCodes.Brfalse, falseBranch); 415 451 CompileInstructions(il, state, ds); 416 452 } 417 il.Emit(System.Reflection.Emit.OpCodes.Ldc_ I4_0); // > 0453 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 0.0); // > 0 418 454 il.Emit(System.Reflection.Emit.OpCodes.Cgt); 419 455 il.Emit(System.Reflection.Emit.OpCodes.Brfalse, falseBranch); … … 435 471 // complex definition because of special properties of NaN 436 472 il.Emit(System.Reflection.Emit.OpCodes.Dup); 437 il.Emit(System.Reflection.Emit.OpCodes.Ldc_ I4_0); // <= 0473 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 0.0); // <= 0 438 474 il.Emit(System.Reflection.Emit.OpCodes.Ble, nextArgBranch); 439 475 il.Emit(System.Reflection.Emit.OpCodes.Br, resultBranch); … … 443 479 } 444 480 il.MarkLabel(resultBranch); 445 il.Emit(System.Reflection.Emit.OpCodes.Ldc_ I4_0); // > 0481 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 0.0); // > 0 446 482 il.Emit(System.Reflection.Emit.OpCodes.Cgt); 447 483 il.Emit(System.Reflection.Emit.OpCodes.Brtrue, trueBranch); … … 456 492 case OpCodes.NOT: { 457 493 CompileInstructions(il, state, ds); 458 il.Emit(System.Reflection.Emit.OpCodes.Ldc_ I4_0); // > 0494 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 0.0); // > 0 459 495 il.Emit(System.Reflection.Emit.OpCodes.Cgt); 460 496 il.Emit(System.Reflection.Emit.OpCodes.Conv_R8); // convert to float64 … … 468 504 case OpCodes.XOR: { 469 505 CompileInstructions(il, state, ds); 470 il.Emit(System.Reflection.Emit.OpCodes.Ldc_ I4_0);506 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 0.0); 471 507 il.Emit(System.Reflection.Emit.OpCodes.Cgt);// > 0 472 508 473 509 for (int i = 1; i < nArgs; i++) { 474 510 CompileInstructions(il, state, ds); 475 il.Emit(System.Reflection.Emit.OpCodes.Ldc_ I4_0);511 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 0.0); 476 512 il.Emit(System.Reflection.Emit.OpCodes.Cgt);// > 0 477 513 il.Emit(System.Reflection.Emit.OpCodes.Xor); -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeInterpreter.cs
r16692 r16723 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 8Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Linq;25 24 using HeuristicLab.Common; 26 25 using HeuristicLab.Core; … … 28 27 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 29 28 using HeuristicLab.Parameters; 30 using H euristicLab.Persistence.Default.CompositeSerializers.Storable;29 using HEAL.Attic; 31 30 32 31 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 33 [Storable Class]32 [StorableType("FB94F333-B32A-44FB-A561-CBDE76693D20")] 34 33 [Item("SymbolicDataAnalysisExpressionTreeInterpreter", "Interpreter for symbolic expression trees including automatically defined functions.")] 35 34 public class SymbolicDataAnalysisExpressionTreeInterpreter : ParameterizedNamedItem, … … 70 69 71 70 [StorableConstructor] 72 protected SymbolicDataAnalysisExpressionTreeInterpreter( bool deserializing) : base(deserializing) { }71 protected SymbolicDataAnalysisExpressionTreeInterpreter(StorableConstructorFlag _) : base(_) { } 73 72 74 73 protected SymbolicDataAnalysisExpressionTreeInterpreter(SymbolicDataAnalysisExpressionTreeInterpreter original, … … 203 202 return sum / currentInstr.nArguments; 204 203 } 204 case OpCodes.Absolute: { 205 return Math.Abs(Evaluate(dataset, ref row, state)); 206 } 207 case OpCodes.Tanh: { 208 return Math.Tanh(Evaluate(dataset, ref row, state)); 209 } 205 210 case OpCodes.Cos: { 206 211 return Math.Cos(Evaluate(dataset, ref row, state)); … … 214 219 case OpCodes.Square: { 215 220 return Math.Pow(Evaluate(dataset, ref row, state), 2); 221 } 222 case OpCodes.Cube: { 223 return Math.Pow(Evaluate(dataset, ref row, state), 3); 216 224 } 217 225 case OpCodes.Power: { … … 223 231 return Math.Sqrt(Evaluate(dataset, ref row, state)); 224 232 } 233 case OpCodes.CubeRoot: { 234 return Math.Pow(Evaluate(dataset, ref row, state), 1.0 / 3.0); 235 } 225 236 case OpCodes.Root: { 226 237 double x = Evaluate(dataset, ref row, state); … … 340 351 if (double.IsNaN(x)) return double.NaN; 341 352 else return alglib.besseli0(x); 353 } 354 355 case OpCodes.AnalyticQuotient: { 356 var x1 = Evaluate(dataset, ref row, state); 357 var x2 = Evaluate(dataset, ref row, state); 358 return x1 / Math.Pow(1 + x2 * x2, 0.5); 342 359 } 343 360 case OpCodes.IfThenElse: { -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeLinearInterpreter.cs
r16692 r16723 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 8Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 28 28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 29 29 using HeuristicLab.Parameters; 30 using H euristicLab.Persistence.Default.CompositeSerializers.Storable;30 using HEAL.Attic; 31 31 32 32 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 33 [Storable Class]33 [StorableType("EF325166-E03A-44C4-83CE-7F07B836285E")] 34 34 [Item("SymbolicDataAnalysisExpressionTreeLinearInterpreter", "Fast linear (non-recursive) interpreter for symbolic expression trees. Does not support ADFs.")] 35 35 public sealed class SymbolicDataAnalysisExpressionTreeLinearInterpreter : ParameterizedNamedItem, ISymbolicDataAnalysisExpressionTreeInterpreter { … … 70 70 71 71 [StorableConstructor] 72 private SymbolicDataAnalysisExpressionTreeLinearInterpreter(bool deserializing) 73 : base(deserializing) { 72 private SymbolicDataAnalysisExpressionTreeLinearInterpreter(StorableConstructorFlag _) : base(_) { 74 73 interpreter = new SymbolicDataAnalysisExpressionTreeInterpreter(); 75 74 } … … 215 214 if (instr.nArguments == 1) p = 1.0 / p; 216 215 instr.value = p; 216 } else if (instr.opCode == OpCodes.AnalyticQuotient) { 217 var x1 = code[instr.childIndex].value; 218 var x2 = code[instr.childIndex + 1].value; 219 instr.value = x1 / Math.Sqrt(1 + x2 * x2); 217 220 } else if (instr.opCode == OpCodes.Average) { 218 221 double s = code[instr.childIndex].value; … … 221 224 } 222 225 instr.value = s / instr.nArguments; 226 } else if (instr.opCode == OpCodes.Absolute) { 227 instr.value = Math.Abs(code[instr.childIndex].value); 228 } else if (instr.opCode == OpCodes.Tanh) { 229 instr.value = Math.Tanh(code[instr.childIndex].value); 223 230 } else if (instr.opCode == OpCodes.Cos) { 224 231 instr.value = Math.Cos(code[instr.childIndex].value); … … 229 236 } else if (instr.opCode == OpCodes.Square) { 230 237 instr.value = Math.Pow(code[instr.childIndex].value, 2); 238 } else if (instr.opCode == OpCodes.Cube) { 239 instr.value = Math.Pow(code[instr.childIndex].value, 3); 231 240 } else if (instr.opCode == OpCodes.Power) { 232 241 double x = code[instr.childIndex].value; … … 235 244 } else if (instr.opCode == OpCodes.SquareRoot) { 236 245 instr.value = Math.Sqrt(code[instr.childIndex].value); 246 } else if (instr.opCode == OpCodes.CubeRoot) { 247 instr.value = Math.Pow(code[instr.childIndex].value, 1.0 / 3.0); 237 248 } else if (instr.opCode == OpCodes.Root) { 238 249 double x = code[instr.childIndex].value;
Note: See TracChangeset
for help on using the changeset viewer.