Changeset 13141
- Timestamp:
- 11/13/15 14:47:00 (9 years ago)
- Location:
- branches/HeuristicLab.LinqExpressionTreeInterpreter/HeuristicLab.Problems.DataAnalysis.Symbolic
- Files:
-
- 1 deleted
- 10 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.LinqExpressionTreeInterpreter/HeuristicLab.Problems.DataAnalysis.Symbolic
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic (added) merged: 12817,12839,12982
- Property svn:mergeinfo changed
-
branches/HeuristicLab.LinqExpressionTreeInterpreter/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionCSharpFormatter.cs
r12645 r13141 56 56 57 57 private void FormatRecursively(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) { 58 // TODO: adapt to interpreter semantics. The HL interpreter also allows Boolean operations on reals 58 59 if (node.Subtrees.Any()) { 59 60 if (node.Symbol is Addition) { … … 83 84 } else if (node.Symbol is Or) { 84 85 FormatOperator(node, "||", strBuilder); 86 } else if (node.Symbol is Xor) { 87 FormatOperator(node, "^", strBuilder); 85 88 } else if (node.Symbol is Sine) { 86 89 FormatFunction(node, "Math.Sin", strBuilder); -
branches/HeuristicLab.LinqExpressionTreeInterpreter/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj
r12807 r13141 99 99 </PropertyGroup> 100 100 <ItemGroup> 101 <Reference Include="ALGLIB-3.9.0"> 102 <HintPath>..\..\..\..\trunk\sources\bin\ALGLIB-3.9.0.dll</HintPath> 101 <Reference Include="ALGLIB-3.7.0, Version=3.7.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 102 <SpecificVersion>False</SpecificVersion> 103 <HintPath>..\..\..\..\trunk\sources\bin\ALGLIB-3.7.0.dll</HintPath> 103 104 <Private>False</Private> 104 105 </Reference> … … 198 199 </Compile> 199 200 <Compile Include="Formatters\SymbolicDataAnalysisExpressionCSharpFormatter.cs" /> 201 <Compile Include="Formatters\SymbolicDataAnalysisExpressionMathematicaFormatter.cs" /> 200 202 <Compile Include="Importer\SymbolicExpressionImporter.cs" /> 201 203 <Compile Include="Importer\Token.cs" /> … … 257 259 <Compile Include="Symbols\AiryB.cs" /> 258 260 <Compile Include="Symbols\Bessel.cs" /> 259 <Compile Include="Symbols\FunctionSymbol.cs" />260 261 <Compile Include="Symbols\Xor.cs" /> 261 262 <Compile Include="Symbols\Erf.cs" /> -
branches/HeuristicLab.LinqExpressionTreeInterpreter/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interfaces/ISymbolicDataAnalysisExpressionTreeInterpreter.cs
r12509 r13141 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 } -
branches/HeuristicLab.LinqExpressionTreeInterpreter/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/OpCodes.cs
r12807 r13141 83 83 public const byte Erf = 43; 84 84 public const byte Bessel = 44; 85 public const byte UserDefinedFunction = 46;86 85 87 86 private static Dictionary<Type, byte> symbolToOpcode = new Dictionary<Type, byte>() { … … 132 131 { typeof(Erf), OpCodes.Erf}, 133 132 { typeof(Bessel), OpCodes.Bessel}, 134 { typeof(FunctionSymbol), OpCodes.UserDefinedFunction }135 133 }; 136 134 -
branches/HeuristicLab.LinqExpressionTreeInterpreter/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionCompiledTreeInterpreter.cs
r13140 r13141 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Collections.ObjectModel; 24 25 using System.Linq; 25 26 using System.Linq.Expressions; … … 61 62 #endregion 62 63 63 private static readonly MethodInfo ListGetValue = typeof(IList<double>).GetProperty("Item", new Type[] { typeof(int) }).GetGetMethod(); 64 private static readonly Func<ReadOnlyCollection<double>, IList<double>> ReadOnlyCollectionRetrieveList = GetField<ReadOnlyCollection<double>, IList<double>>("list"); // retrieve underlying field of type IList<double> from a ReadOnlyCollection<double> 65 private static readonly Func<List<double>, double[]> ListRetrieveItems = GetField<List<double>, double[]>("_items"); // retrieve underlying field of type double[] from a List<double> 66 private static MethodInfo listGetValue = typeof(IList<double>).GetProperty("Item", new Type[] { typeof(int) }).GetGetMethod(); 64 67 65 68 public override bool CanChangeName { get { return false; } } … … 68 71 69 72 #region parameter properties 70 public I ValueParameter<BoolValue> CheckExpressionsWithIntervalArithmeticParameter {71 get { return (I ValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName]; }72 } 73 74 public I ValueParameter<IntValue> EvaluatedSolutionsParameter {75 get { return (I ValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; }73 public IFixedValueParameter<BoolValue> CheckExpressionsWithIntervalArithmeticParameter { 74 get { return (IFixedValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName]; } 75 } 76 77 public IFixedValueParameter<IntValue> EvaluatedSolutionsParameter { 78 get { return (IFixedValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; } 76 79 } 77 80 #endregion 78 81 79 82 #region properties 80 public BoolValueCheckExpressionsWithIntervalArithmetic {81 get { return CheckExpressionsWithIntervalArithmeticParameter.Value ; }82 set { CheckExpressionsWithIntervalArithmeticParameter.Value = value; }83 } 84 public IntValueEvaluatedSolutions {85 get { return EvaluatedSolutionsParameter.Value ; }86 set { EvaluatedSolutionsParameter.Value = value; }83 public bool CheckExpressionsWithIntervalArithmetic { 84 get { return CheckExpressionsWithIntervalArithmeticParameter.Value.Value; } 85 set { CheckExpressionsWithIntervalArithmeticParameter.Value.Value = value; } 86 } 87 public int EvaluatedSolutions { 88 get { return EvaluatedSolutionsParameter.Value.Value; } 89 set { EvaluatedSolutionsParameter.Value.Value = value; } 87 90 } 88 91 #endregion … … 101 104 } 102 105 103 public SymbolicDataAnalysisExpressionCompiledTreeInterpreter() { 104 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))); 105 Parameters.Add(new ValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0))); 106 public SymbolicDataAnalysisExpressionCompiledTreeInterpreter() : 107 base("SymbolicDataAnalysisExpressionCompiledTreeInterpreter", "Interpreter which compiles the tree into a lambda") { 108 Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, 109 "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false))); 110 Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0))); 111 } 112 113 public SymbolicDataAnalysisExpressionCompiledTreeInterpreter(string name, string description) : 114 base(name, description) { 115 Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, 116 "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false))); 117 Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0))); 118 } 119 120 [StorableHook(HookType.AfterDeserialization)] 121 private void AfterDeserialization() { 122 Parameters.Remove(EvaluatedSolutionsParameterName); 123 Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0))); 124 125 Parameters.Remove(CheckExpressionsWithIntervalArithmeticParameterName); 126 Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, 127 "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false))); 106 128 } 107 129 108 130 public void InitializeState() { 109 EvaluatedSolutions .Value= 0;131 EvaluatedSolutions = 0; 110 132 } 111 133 112 134 public void ClearState() { 113 EvaluatedSolutions.Value = 0;114 135 } 115 136 116 137 public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows) { 117 if (CheckExpressionsWithIntervalArithmetic .Value)138 if (CheckExpressionsWithIntervalArithmetic) 118 139 throw new NotSupportedException("Interval arithmetic is not yet supported in the symbolic data analysis interpreter."); 119 140 120 lock (EvaluatedSolutions ) {121 EvaluatedSolutions .Value++; // increment the evaluated solutions counter141 lock (EvaluatedSolutionsParameter.Value) { 142 EvaluatedSolutions++; // increment the evaluated solutions counter 122 143 } 123 124 var columns = dataset.DoubleVariables.Select(x => ( IList<double>)dataset.GetReadOnlyDoubleValues(x)).ToArray();144 // var columns = dataset.DoubleVariables.Select(x => ListRetrieveItems((List<double>)ReadOnlyCollectionRetrieveList(dataset.GetReadOnlyDoubleValues(x)))).ToArray(); 145 var columns = dataset.DoubleVariables.Select(x => (List<double>)dataset.GetDoubleValues(x)).ToArray(); 125 146 var compiled = CompileTree(tree, dataset); 126 147 return rows.Select(x => compiled(x, columns)); 127 148 } 128 149 129 public static Func<int, IList<double>[], double> CompileTree(ISymbolicExpressionTree tree, IDataset dataset) {150 public static Func<int, List<double>[], double> CompileTree(ISymbolicExpressionTree tree, IDataset dataset) { 130 151 var row = Expression.Parameter(typeof(int)); 131 var columns = Expression.Parameter(typeof( IList<double>[]));152 var columns = Expression.Parameter(typeof(List<double>[])); 132 153 var variableIndices = dataset.DoubleVariables.Select((x, i) => new { x, i }).ToDictionary(e => e.x, e => e.i); 133 154 var expr = MakeExpr(tree, variableIndices, row, columns); 134 var lambda = Expression.Lambda<Func<int, IList<double>[], double>>(expr, row, columns);155 var lambda = Expression.Lambda<Func<int, List<double>[], double>>(expr, row, columns); 135 156 return lambda.Compile(); 136 157 } 137 158 138 private static Expression MakeExpr(ISymbolicExpressionTree tree, Dictionary<string, int> variableIndices, ParameterExpression row, ParameterExpression columns) {159 private static Expression MakeExpr(ISymbolicExpressionTree tree, Dictionary<string, int> variableIndices, Expression row, Expression columns) { 139 160 var actualRoot = tree.Root.GetSubtree(0).GetSubtree(0); 140 161 return MakeExpr(actualRoot, variableIndices, row, columns); 141 162 } 142 163 143 private static Expression MakeExpr(ISymbolicExpressionTreeNode node, Dictionary<string, int> variableIndices, ParameterExpression row, ParameterExpression columns) {164 private static Expression MakeExpr(ISymbolicExpressionTreeNode node, Dictionary<string, int> variableIndices, Expression row, Expression columns) { 144 165 var opcode = OpCodes.MapSymbolToOpCode(node); 166 #region switch opcode 145 167 switch (opcode) { 146 case OpCodes.UserDefinedFunction:147 {148 var functionSymbol = (FunctionSymbol)node.Symbol;149 return Expression.Call(functionSymbol.MethodInfo, node.Subtrees.Select(x => MakeExpr(x, variableIndices, row, columns)));150 }151 168 case OpCodes.Constant: 152 169 { … … 161 178 var indexExpr = Expression.Constant(variableIndices[variableName]); 162 179 var valuesExpr = Expression.ArrayIndex(columns, indexExpr); 163 var variableValue = Expression.Call(valuesExpr, ListGetValue, row); 180 var variableValue = Expression.ArrayIndex(Expression.Field(valuesExpr, "_items"), row); 181 // var variableValue = Expression.Property(valuesExpr, Indexer, row); 164 182 return Expression.Multiply(variableWeight, variableValue); 165 183 } … … 667 685 var indexExpr = Expression.Constant(variableIndices[variableName]); 668 686 var valuesExpr = Expression.ArrayIndex(columns, indexExpr); 669 var variableValue = Expression. Call(valuesExpr, ListGetValue, row);687 var variableValue = Expression.ArrayIndex(valuesExpr, row); 670 688 var variableThreshold = Expression.Constant(variableConditionTreeNode.Threshold); 671 689 var variableSlope = Expression.Constant(variableConditionTreeNode.Slope); … … 682 700 ); 683 701 } 702 case OpCodes.LagVariable: 703 { 704 var laggedVariableTreeNode = (LaggedVariableTreeNode)node; 705 var lag = Expression.Constant(laggedVariableTreeNode.Lag); 706 var variableWeight = Expression.Constant(laggedVariableTreeNode.Weight); 707 var variableName = laggedVariableTreeNode.VariableName; 708 var indexExpr = Expression.Constant(variableIndices[variableName]); 709 var valuesExpr = Expression.ArrayIndex(columns, indexExpr); 710 var variableValue = Expression.ArrayIndex(Expression.Field(valuesExpr, "_items"), Expression.Add(row, lag)); 711 return Expression.Multiply(variableWeight, variableValue); 712 } 713 case OpCodes.TimeLag: 714 { 715 var timeLagTreeNode = (LaggedTreeNode)node; 716 var lag = Expression.Constant(timeLagTreeNode.Lag); 717 return MakeExpr(timeLagTreeNode.GetSubtree(0), variableIndices, Expression.Add(row, lag), columns); 718 } 719 case OpCodes.Integral: 720 { 721 var timeLagTreeNode = (LaggedTreeNode)node; 722 var subtree = node.GetSubtree(0); 723 var sum = MakeExpr(subtree, variableIndices, row, columns); 724 var sign = Expression.Constant(Math.Sign(timeLagTreeNode.Lag)); 725 var lag = Expression.Add(row, sign); 726 for (int i = 0; i < Math.Abs(timeLagTreeNode.Lag); ++i) { 727 sum = Expression.Add(sum, MakeExpr(subtree, variableIndices, lag, columns)); 728 lag = Expression.Add(lag, sign); 729 } 730 return sum; 731 } 732 case OpCodes.Derivative: 733 { 734 var subtree = node.GetSubtree(0); 735 var f0 = MakeExpr(subtree, variableIndices, row, columns); 736 var f1 = MakeExpr(subtree, variableIndices, Expression.Subtract(row, Expression.Constant(1)), columns); 737 var f3 = MakeExpr(subtree, variableIndices, Expression.Subtract(row, Expression.Constant(3)), columns); 738 var f4 = MakeExpr(subtree, variableIndices, Expression.Subtract(row, Expression.Constant(4)), columns); 739 740 var result = f0; 741 result = Expression.Add(result, Expression.Multiply(Expression.Constant(2.0), f1)); 742 result = Expression.Subtract(result, Expression.Multiply(Expression.Constant(2.0), f3)); 743 result = Expression.Subtract(result, f4); 744 return Expression.Divide(result, Expression.Constant(8.0)); 745 } 684 746 default: 685 747 throw new NotSupportedException("Unsupported symbol: " + node.Symbol); 686 748 } 749 #endregion 687 750 } 688 751 // util stuff -
branches/HeuristicLab.LinqExpressionTreeInterpreter/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeILEmittingInterpreter.cs
r12509 r13141 80 80 81 81 #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 82 public IFixedValueParameter<BoolValue> CheckExpressionsWithIntervalArithmeticParameter { 83 get { return (IFixedValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName]; } 84 } 85 86 public IFixedValueParameter<IntValue> EvaluatedSolutionsParameter { 87 get { return (IFixedValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; } 88 } 91 89 #endregion 92 90 93 91 #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 92 public bool CheckExpressionsWithIntervalArithmetic { 93 get { return CheckExpressionsWithIntervalArithmeticParameter.Value.Value; } 94 set { CheckExpressionsWithIntervalArithmeticParameter.Value.Value = value; } 95 } 96 public int EvaluatedSolutions { 97 get { return EvaluatedSolutionsParameter.Value.Value; } 98 set { EvaluatedSolutionsParameter.Value.Value = value; } 99 } 105 100 #endregion 106 107 101 108 102 [StorableConstructor] … … 116 110 public SymbolicDataAnalysisExpressionTreeILEmittingInterpreter() 117 111 : 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))); 112 Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, 113 "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false))); 114 Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0))); 115 } 116 117 public SymbolicDataAnalysisExpressionTreeILEmittingInterpreter(string name, string description) 118 : base(name, description) { 119 Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, 120 "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false))); 121 Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0))); 120 122 } 121 123 122 124 [StorableHook(HookType.AfterDeserialization)] 123 125 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))); 126 Parameters.Remove(EvaluatedSolutionsParameterName); 127 Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0))); 128 129 Parameters.Remove(CheckExpressionsWithIntervalArithmeticParameterName); 130 Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, 131 "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false))); 126 132 } 127 133 … … 129 135 130 136 public void InitializeState() { 131 EvaluatedSolutions .Value= 0;137 EvaluatedSolutions = 0; 132 138 } 133 139 134 140 public void ClearState() { 135 EvaluatedSolutions.Value = 0;136 141 } 137 142 … … 139 144 140 145 public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows) { 141 if (CheckExpressionsWithIntervalArithmetic .Value)146 if (CheckExpressionsWithIntervalArithmetic) 142 147 throw new NotSupportedException("Interval arithmetic is not yet supported in the symbolic data analysis interpreter."); 143 148 144 EvaluatedSolutions .Value++; // increment the evaluated solutions counter149 EvaluatedSolutions++; // increment the evaluated solutions counter 145 150 var state = PrepareInterpreterState(tree, dataset); 146 151 … … 187 192 188 193 switch (currentInstr.opCode) { 189 case OpCodes.Add: { 194 case OpCodes.Add: 195 { 190 196 if (nArgs > 0) { 191 197 CompileInstructions(il, state, ds); … … 197 203 return; 198 204 } 199 case OpCodes.Sub: { 205 case OpCodes.Sub: 206 { 200 207 if (nArgs == 1) { 201 208 CompileInstructions(il, state, ds); … … 212 219 return; 213 220 } 214 case OpCodes.Mul: { 221 case OpCodes.Mul: 222 { 215 223 if (nArgs > 0) { 216 224 CompileInstructions(il, state, ds); … … 222 230 return; 223 231 } 224 case OpCodes.Div: { 232 case OpCodes.Div: 233 { 225 234 if (nArgs == 1) { 226 235 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0); … … 238 247 return; 239 248 } 240 case OpCodes.Average: { 249 case OpCodes.Average: 250 { 241 251 CompileInstructions(il, state, ds); 242 252 for (int i = 1; i < nArgs; i++) { … … 248 258 return; 249 259 } 250 case OpCodes.Cos: { 260 case OpCodes.Cos: 261 { 251 262 CompileInstructions(il, state, ds); 252 263 il.Emit(System.Reflection.Emit.OpCodes.Call, cos); 253 264 return; 254 265 } 255 case OpCodes.Sin: { 266 case OpCodes.Sin: 267 { 256 268 CompileInstructions(il, state, ds); 257 269 il.Emit(System.Reflection.Emit.OpCodes.Call, sin); 258 270 return; 259 271 } 260 case OpCodes.Tan: { 272 case OpCodes.Tan: 273 { 261 274 CompileInstructions(il, state, ds); 262 275 il.Emit(System.Reflection.Emit.OpCodes.Call, tan); 263 276 return; 264 277 } 265 case OpCodes.Power: { 278 case OpCodes.Power: 279 { 266 280 CompileInstructions(il, state, ds); 267 281 CompileInstructions(il, state, ds); … … 270 284 return; 271 285 } 272 case OpCodes.Root: { 286 case OpCodes.Root: 287 { 273 288 CompileInstructions(il, state, ds); 274 289 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0); // 1 / round(...) … … 279 294 return; 280 295 } 281 case OpCodes.Exp: { 296 case OpCodes.Exp: 297 { 282 298 CompileInstructions(il, state, ds); 283 299 il.Emit(System.Reflection.Emit.OpCodes.Call, exp); 284 300 return; 285 301 } 286 case OpCodes.Log: { 302 case OpCodes.Log: 303 { 287 304 CompileInstructions(il, state, ds); 288 305 il.Emit(System.Reflection.Emit.OpCodes.Call, log); 289 306 return; 290 307 } 291 case OpCodes.Square: { 308 case OpCodes.Square: 309 { 292 310 CompileInstructions(il, state, ds); 293 311 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 2.0); … … 295 313 return; 296 314 } 297 case OpCodes.SquareRoot: { 315 case OpCodes.SquareRoot: 316 { 298 317 CompileInstructions(il, state, ds); 299 318 il.Emit(System.Reflection.Emit.OpCodes.Call, sqrt); 300 319 return; 301 320 } 302 case OpCodes.AiryA: { 321 case OpCodes.AiryA: 322 { 303 323 CompileInstructions(il, state, ds); 304 324 il.Emit(System.Reflection.Emit.OpCodes.Call, airyA); 305 325 return; 306 326 } 307 case OpCodes.AiryB: { 327 case OpCodes.AiryB: 328 { 308 329 CompileInstructions(il, state, ds); 309 330 il.Emit(System.Reflection.Emit.OpCodes.Call, airyB); 310 331 return; 311 332 } 312 case OpCodes.Bessel: { 333 case OpCodes.Bessel: 334 { 313 335 CompileInstructions(il, state, ds); 314 336 il.Emit(System.Reflection.Emit.OpCodes.Call, bessel); 315 337 return; 316 338 } 317 case OpCodes.CosineIntegral: { 339 case OpCodes.CosineIntegral: 340 { 318 341 CompileInstructions(il, state, ds); 319 342 il.Emit(System.Reflection.Emit.OpCodes.Call, cosIntegral); 320 343 return; 321 344 } 322 case OpCodes.Dawson: { 345 case OpCodes.Dawson: 346 { 323 347 CompileInstructions(il, state, ds); 324 348 il.Emit(System.Reflection.Emit.OpCodes.Call, dawson); 325 349 return; 326 350 } 327 case OpCodes.Erf: { 351 case OpCodes.Erf: 352 { 328 353 CompileInstructions(il, state, ds); 329 354 il.Emit(System.Reflection.Emit.OpCodes.Call, erf); 330 355 return; 331 356 } 332 case OpCodes.ExponentialIntegralEi: { 357 case OpCodes.ExponentialIntegralEi: 358 { 333 359 CompileInstructions(il, state, ds); 334 360 il.Emit(System.Reflection.Emit.OpCodes.Call, expIntegralEi); 335 361 return; 336 362 } 337 case OpCodes.FresnelCosineIntegral: { 363 case OpCodes.FresnelCosineIntegral: 364 { 338 365 CompileInstructions(il, state, ds); 339 366 il.Emit(System.Reflection.Emit.OpCodes.Call, fresnelCosIntegral); 340 367 return; 341 368 } 342 case OpCodes.FresnelSineIntegral: { 369 case OpCodes.FresnelSineIntegral: 370 { 343 371 CompileInstructions(il, state, ds); 344 372 il.Emit(System.Reflection.Emit.OpCodes.Call, fresnelSinIntegral); 345 373 return; 346 374 } 347 case OpCodes.Gamma: { 375 case OpCodes.Gamma: 376 { 348 377 CompileInstructions(il, state, ds); 349 378 il.Emit(System.Reflection.Emit.OpCodes.Call, gamma); 350 379 return; 351 380 } 352 case OpCodes.HyperbolicCosineIntegral: { 381 case OpCodes.HyperbolicCosineIntegral: 382 { 353 383 CompileInstructions(il, state, ds); 354 384 il.Emit(System.Reflection.Emit.OpCodes.Call, hypCosIntegral); 355 385 return; 356 386 } 357 case OpCodes.HyperbolicSineIntegral: { 387 case OpCodes.HyperbolicSineIntegral: 388 { 358 389 CompileInstructions(il, state, ds); 359 390 il.Emit(System.Reflection.Emit.OpCodes.Call, hypSinIntegral); 360 391 return; 361 392 } 362 case OpCodes.Norm: { 393 case OpCodes.Norm: 394 { 363 395 CompileInstructions(il, state, ds); 364 396 il.Emit(System.Reflection.Emit.OpCodes.Call, norm); 365 397 return; 366 398 } 367 case OpCodes.Psi: { 399 case OpCodes.Psi: 400 { 368 401 CompileInstructions(il, state, ds); 369 402 il.Emit(System.Reflection.Emit.OpCodes.Call, psi); 370 403 return; 371 404 } 372 case OpCodes.SineIntegral: { 405 case OpCodes.SineIntegral: 406 { 373 407 CompileInstructions(il, state, ds); 374 408 il.Emit(System.Reflection.Emit.OpCodes.Call, sinIntegral); 375 409 return; 376 410 } 377 case OpCodes.IfThenElse: { 411 case OpCodes.IfThenElse: 412 { 378 413 Label end = il.DefineLabel(); 379 414 Label c1 = il.DefineLabel(); … … 389 424 return; 390 425 } 391 case OpCodes.AND: { 426 case OpCodes.AND: 427 { 392 428 Label falseBranch = il.DefineLabel(); 393 429 Label end = il.DefineLabel(); … … 410 446 return; 411 447 } 412 case OpCodes.OR: { 448 case OpCodes.OR: 449 { 413 450 Label trueBranch = il.DefineLabel(); 414 451 Label end = il.DefineLabel(); … … 438 475 return; 439 476 } 440 case OpCodes.NOT: { 477 case OpCodes.NOT: 478 { 441 479 CompileInstructions(il, state, ds); 442 480 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_0); // > 0 … … 449 487 return; 450 488 } 451 case OpCodes.XOR: { 489 case OpCodes.XOR: 490 { 452 491 CompileInstructions(il, state, ds); 453 492 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_0); … … 466 505 return; 467 506 } 468 case OpCodes.GT: { 507 case OpCodes.GT: 508 { 469 509 CompileInstructions(il, state, ds); 470 510 CompileInstructions(il, state, ds); … … 477 517 return; 478 518 } 479 case OpCodes.LT: { 519 case OpCodes.LT: 520 { 480 521 CompileInstructions(il, state, ds); 481 522 CompileInstructions(il, state, ds); … … 487 528 return; 488 529 } 489 case OpCodes.TimeLag: { 530 case OpCodes.TimeLag: 531 { 490 532 LaggedTreeNode laggedTreeNode = (LaggedTreeNode)currentInstr.dynamicNode; 491 533 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // row -= lag … … 503 545 return; 504 546 } 505 case OpCodes.Integral: { 547 case OpCodes.Integral: 548 { 506 549 int savedPc = state.ProgramCounter; 507 550 LaggedTreeNode laggedTreeNode = (LaggedTreeNode)currentInstr.dynamicNode; … … 530 573 //one sided smooth differentiatior, N = 4 531 574 // y' = 1/8h (f_i + 2f_i-1, -2 f_i-3 - f_i-4) 532 case OpCodes.Derivative: { 575 case OpCodes.Derivative: 576 { 533 577 int savedPc = state.ProgramCounter; 534 578 CompileInstructions(il, state, ds); … … 572 616 return; 573 617 } 574 case OpCodes.Call: { 618 case OpCodes.Call: 619 { 575 620 throw new NotSupportedException( 576 621 "Automatically defined functions are not supported by the SymbolicDataAnalysisTreeILEmittingInterpreter. Either turn of ADFs or change the interpeter."); 577 622 } 578 case OpCodes.Arg: { 623 case OpCodes.Arg: 624 { 579 625 throw new NotSupportedException( 580 626 "Automatically defined functions are not supported by the SymbolicDataAnalysisTreeILEmittingInterpreter. Either turn of ADFs or change the interpeter."); 581 627 } 582 case OpCodes.Variable: { 628 case OpCodes.Variable: 629 { 583 630 VariableTreeNode varNode = (VariableTreeNode)currentInstr.dynamicNode; 584 631 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_1); // load columns array … … 612 659 return; 613 660 } 614 case OpCodes.LagVariable: { 661 case OpCodes.LagVariable: 662 { 615 663 var nanResult = il.DefineLabel(); 616 664 var normalResult = il.DefineLabel(); … … 640 688 return; 641 689 } 642 case OpCodes.Constant: { 690 case OpCodes.Constant: 691 { 643 692 ConstantTreeNode constNode = (ConstantTreeNode)currentInstr.dynamicNode; 644 693 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, constNode.Value); … … 648 697 //mkommend: this symbol uses the logistic function f(x) = 1 / (1 + e^(-alpha * x) ) 649 698 //to determine the relative amounts of the true and false branch see http://en.wikipedia.org/wiki/Logistic_function 650 case OpCodes.VariableCondition: { 699 case OpCodes.VariableCondition: 700 { 651 701 throw new NotSupportedException("Interpretation of symbol " + currentInstr.dynamicNode.Symbol.Name + 652 702 " is not supported by the SymbolicDataAnalysisTreeILEmittingInterpreter"); -
branches/HeuristicLab.LinqExpressionTreeInterpreter/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeInterpreter.cs
r12509 r13141 40 40 41 41 #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]; }42 public IFixedValueParameter<BoolValue> CheckExpressionsWithIntervalArithmeticParameter { 43 get { return (IFixedValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName]; } 44 } 45 46 public IFixedValueParameter<IntValue> EvaluatedSolutionsParameter { 47 get { return (IFixedValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; } 48 48 } 49 49 #endregion 50 50 51 51 #region properties 52 public BoolValue CheckExpressionsWithIntervalArithmetic { 53 get { return CheckExpressionsWithIntervalArithmeticParameter.Value; } 54 set { CheckExpressionsWithIntervalArithmeticParameter.Value = value; } 55 } 56 57 public IntValue EvaluatedSolutions { 58 get { return EvaluatedSolutionsParameter.Value; } 59 set { EvaluatedSolutionsParameter.Value = value; } 52 public bool CheckExpressionsWithIntervalArithmetic { 53 get { return CheckExpressionsWithIntervalArithmeticParameter.Value.Value; } 54 set { CheckExpressionsWithIntervalArithmeticParameter.Value.Value = value; } 55 } 56 public int EvaluatedSolutions { 57 get { return EvaluatedSolutionsParameter.Value.Value; } 58 set { EvaluatedSolutionsParameter.Value.Value = value; } 60 59 } 61 60 #endregion … … 70 69 public SymbolicDataAnalysisExpressionTreeInterpreter() 71 70 : 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))); 71 Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, 72 "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 FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0))); 74 74 } 75 75 76 76 protected SymbolicDataAnalysisExpressionTreeInterpreter(string name, string description) 77 77 : 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))); 78 Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, 79 "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false))); 80 Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0))); 80 81 } 81 82 82 83 [StorableHook(HookType.AfterDeserialization)] 83 84 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))); 85 Parameters.Remove(EvaluatedSolutionsParameterName); 86 Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0))); 87 88 Parameters.Remove(CheckExpressionsWithIntervalArithmeticParameterName); 89 Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, 90 "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false))); 86 91 } 87 92 88 93 #region IStatefulItem 89 94 public void InitializeState() { 90 EvaluatedSolutions .Value= 0;95 EvaluatedSolutions = 0; 91 96 } 92 97 … … 96 101 97 102 public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows) { 98 if (CheckExpressionsWithIntervalArithmetic .Value)103 if (CheckExpressionsWithIntervalArithmetic) 99 104 throw new NotSupportedException("Interval arithmetic is not yet supported in the symbolic data analysis interpreter."); 100 105 101 lock (EvaluatedSolutions ) {102 EvaluatedSolutions .Value++; // increment the evaluated solutions counter106 lock (EvaluatedSolutionsParameter.Value) { 107 EvaluatedSolutions++; // increment the evaluated solutions counter 103 108 } 104 109 var state = PrepareInterpreterState(tree, dataset); … … 135 140 Instruction currentInstr = state.NextInstruction(); 136 141 switch (currentInstr.opCode) { 137 case OpCodes.Add: { 142 case OpCodes.Add: 143 { 138 144 double s = Evaluate(dataset, ref row, state); 139 145 for (int i = 1; i < currentInstr.nArguments; i++) { … … 142 148 return s; 143 149 } 144 case OpCodes.Sub: { 150 case OpCodes.Sub: 151 { 145 152 double s = Evaluate(dataset, ref row, state); 146 153 for (int i = 1; i < currentInstr.nArguments; i++) { … … 150 157 return s; 151 158 } 152 case OpCodes.Mul: { 159 case OpCodes.Mul: 160 { 153 161 double p = Evaluate(dataset, ref row, state); 154 162 for (int i = 1; i < currentInstr.nArguments; i++) { … … 157 165 return p; 158 166 } 159 case OpCodes.Div: { 167 case OpCodes.Div: 168 { 160 169 double p = Evaluate(dataset, ref row, state); 161 170 for (int i = 1; i < currentInstr.nArguments; i++) { … … 165 174 return p; 166 175 } 167 case OpCodes.Average: { 176 case OpCodes.Average: 177 { 168 178 double sum = Evaluate(dataset, ref row, state); 169 179 for (int i = 1; i < currentInstr.nArguments; i++) { … … 172 182 return sum / currentInstr.nArguments; 173 183 } 174 case OpCodes.Cos: { 184 case OpCodes.Cos: 185 { 175 186 return Math.Cos(Evaluate(dataset, ref row, state)); 176 187 } 177 case OpCodes.Sin: { 188 case OpCodes.Sin: 189 { 178 190 return Math.Sin(Evaluate(dataset, ref row, state)); 179 191 } 180 case OpCodes.Tan: { 192 case OpCodes.Tan: 193 { 181 194 return Math.Tan(Evaluate(dataset, ref row, state)); 182 195 } 183 case OpCodes.Square: { 196 case OpCodes.Square: 197 { 184 198 return Math.Pow(Evaluate(dataset, ref row, state), 2); 185 199 } 186 case OpCodes.Power: { 200 case OpCodes.Power: 201 { 187 202 double x = Evaluate(dataset, ref row, state); 188 203 double y = Math.Round(Evaluate(dataset, ref row, state)); 189 204 return Math.Pow(x, y); 190 205 } 191 case OpCodes.SquareRoot: { 206 case OpCodes.SquareRoot: 207 { 192 208 return Math.Sqrt(Evaluate(dataset, ref row, state)); 193 209 } 194 case OpCodes.Root: { 210 case OpCodes.Root: 211 { 195 212 double x = Evaluate(dataset, ref row, state); 196 213 double y = Math.Round(Evaluate(dataset, ref row, state)); 197 214 return Math.Pow(x, 1 / y); 198 215 } 199 case OpCodes.Exp: { 216 case OpCodes.Exp: 217 { 200 218 return Math.Exp(Evaluate(dataset, ref row, state)); 201 219 } 202 case OpCodes.Log: { 220 case OpCodes.Log: 221 { 203 222 return Math.Log(Evaluate(dataset, ref row, state)); 204 223 } 205 case OpCodes.Gamma: { 224 case OpCodes.Gamma: 225 { 206 226 var x = Evaluate(dataset, ref row, state); 207 227 if (double.IsNaN(x)) return double.NaN; 208 228 else return alglib.gammafunction(x); 209 229 } 210 case OpCodes.Psi: { 230 case OpCodes.Psi: 231 { 211 232 var x = Evaluate(dataset, ref row, state); 212 233 if (double.IsNaN(x)) return double.NaN; … … 214 235 return alglib.psi(x); 215 236 } 216 case OpCodes.Dawson: { 237 case OpCodes.Dawson: 238 { 217 239 var x = Evaluate(dataset, ref row, state); 218 240 if (double.IsNaN(x)) return double.NaN; 219 241 return alglib.dawsonintegral(x); 220 242 } 221 case OpCodes.ExponentialIntegralEi: { 243 case OpCodes.ExponentialIntegralEi: 244 { 222 245 var x = Evaluate(dataset, ref row, state); 223 246 if (double.IsNaN(x)) return double.NaN; 224 247 return alglib.exponentialintegralei(x); 225 248 } 226 case OpCodes.SineIntegral: { 249 case OpCodes.SineIntegral: 250 { 227 251 double si, ci; 228 252 var x = Evaluate(dataset, ref row, state); … … 233 257 } 234 258 } 235 case OpCodes.CosineIntegral: { 259 case OpCodes.CosineIntegral: 260 { 236 261 double si, ci; 237 262 var x = Evaluate(dataset, ref row, state); … … 242 267 } 243 268 } 244 case OpCodes.HyperbolicSineIntegral: { 269 case OpCodes.HyperbolicSineIntegral: 270 { 245 271 double shi, chi; 246 272 var x = Evaluate(dataset, ref row, state); … … 251 277 } 252 278 } 253 case OpCodes.HyperbolicCosineIntegral: { 279 case OpCodes.HyperbolicCosineIntegral: 280 { 254 281 double shi, chi; 255 282 var x = Evaluate(dataset, ref row, state); … … 260 287 } 261 288 } 262 case OpCodes.FresnelCosineIntegral: { 289 case OpCodes.FresnelCosineIntegral: 290 { 263 291 double c = 0, s = 0; 264 292 var x = Evaluate(dataset, ref row, state); … … 269 297 } 270 298 } 271 case OpCodes.FresnelSineIntegral: { 299 case OpCodes.FresnelSineIntegral: 300 { 272 301 double c = 0, s = 0; 273 302 var x = Evaluate(dataset, ref row, state); … … 278 307 } 279 308 } 280 case OpCodes.AiryA: { 309 case OpCodes.AiryA: 310 { 281 311 double ai, aip, bi, bip; 282 312 var x = Evaluate(dataset, ref row, state); … … 287 317 } 288 318 } 289 case OpCodes.AiryB: { 319 case OpCodes.AiryB: 320 { 290 321 double ai, aip, bi, bip; 291 322 var x = Evaluate(dataset, ref row, state); … … 296 327 } 297 328 } 298 case OpCodes.Norm: { 329 case OpCodes.Norm: 330 { 299 331 var x = Evaluate(dataset, ref row, state); 300 332 if (double.IsNaN(x)) return double.NaN; 301 333 else return alglib.normaldistribution(x); 302 334 } 303 case OpCodes.Erf: { 335 case OpCodes.Erf: 336 { 304 337 var x = Evaluate(dataset, ref row, state); 305 338 if (double.IsNaN(x)) return double.NaN; 306 339 else return alglib.errorfunction(x); 307 340 } 308 case OpCodes.Bessel: { 341 case OpCodes.Bessel: 342 { 309 343 var x = Evaluate(dataset, ref row, state); 310 344 if (double.IsNaN(x)) return double.NaN; 311 345 else return alglib.besseli0(x); 312 346 } 313 case OpCodes.IfThenElse: { 347 case OpCodes.IfThenElse: 348 { 314 349 double condition = Evaluate(dataset, ref row, state); 315 350 double result; … … 321 356 return result; 322 357 } 323 case OpCodes.AND: { 358 case OpCodes.AND: 359 { 324 360 double result = Evaluate(dataset, ref row, state); 325 361 for (int i = 1; i < currentInstr.nArguments; i++) { … … 331 367 return result > 0.0 ? 1.0 : -1.0; 332 368 } 333 case OpCodes.OR: { 369 case OpCodes.OR: 370 { 334 371 double result = Evaluate(dataset, ref row, state); 335 372 for (int i = 1; i < currentInstr.nArguments; i++) { … … 341 378 return result > 0.0 ? 1.0 : -1.0; 342 379 } 343 case OpCodes.NOT: { 380 case OpCodes.NOT: 381 { 344 382 return Evaluate(dataset, ref row, state) > 0.0 ? -1.0 : 1.0; 345 383 } 346 case OpCodes.XOR: { 384 case OpCodes.XOR: 385 { 347 386 //mkommend: XOR on multiple inputs is defined as true if the number of positive signals is odd 348 387 // this is equal to a consecutive execution of binary XOR operations. … … 353 392 return positiveSignals % 2 != 0 ? 1.0 : -1.0; 354 393 } 355 case OpCodes.GT: { 394 case OpCodes.GT: 395 { 356 396 double x = Evaluate(dataset, ref row, state); 357 397 double y = Evaluate(dataset, ref row, state); … … 359 399 else return -1.0; 360 400 } 361 case OpCodes.LT: { 401 case OpCodes.LT: 402 { 362 403 double x = Evaluate(dataset, ref row, state); 363 404 double y = Evaluate(dataset, ref row, state); … … 365 406 else return -1.0; 366 407 } 367 case OpCodes.TimeLag: { 408 case OpCodes.TimeLag: 409 { 368 410 var timeLagTreeNode = (LaggedTreeNode)currentInstr.dynamicNode; 369 411 row += timeLagTreeNode.Lag; … … 372 414 return result; 373 415 } 374 case OpCodes.Integral: { 416 case OpCodes.Integral: 417 { 375 418 int savedPc = state.ProgramCounter; 376 419 var timeLagTreeNode = (LaggedTreeNode)currentInstr.dynamicNode; … … 390 433 //one sided smooth differentiatior, N = 4 391 434 // y' = 1/8h (f_i + 2f_i-1, -2 f_i-3 - f_i-4) 392 case OpCodes.Derivative: { 435 case OpCodes.Derivative: 436 { 393 437 int savedPc = state.ProgramCounter; 394 438 double f_0 = Evaluate(dataset, ref row, state); row--; … … 403 447 return (f_0 + 2 * f_1 - 2 * f_3 - f_4) / 8; // h = 1 404 448 } 405 case OpCodes.Call: { 449 case OpCodes.Call: 450 { 406 451 // evaluate sub-trees 407 452 double[] argValues = new double[currentInstr.nArguments]; … … 426 471 return v; 427 472 } 428 case OpCodes.Arg: { 473 case OpCodes.Arg: 474 { 429 475 return state.GetStackFrameValue((ushort)currentInstr.data); 430 476 } 431 case OpCodes.Variable: { 477 case OpCodes.Variable: 478 { 432 479 if (row < 0 || row >= dataset.Rows) return double.NaN; 433 480 var variableTreeNode = (VariableTreeNode)currentInstr.dynamicNode; 434 481 return ((IList<double>)currentInstr.data)[row] * variableTreeNode.Weight; 435 482 } 436 case OpCodes.LagVariable: { 483 case OpCodes.LagVariable: 484 { 437 485 var laggedVariableTreeNode = (LaggedVariableTreeNode)currentInstr.dynamicNode; 438 486 int actualRow = row + laggedVariableTreeNode.Lag; … … 440 488 return ((IList<double>)currentInstr.data)[actualRow] * laggedVariableTreeNode.Weight; 441 489 } 442 case OpCodes.Constant: { 490 case OpCodes.Constant: 491 { 443 492 var constTreeNode = (ConstantTreeNode)currentInstr.dynamicNode; 444 493 return constTreeNode.Value; … … 447 496 //mkommend: this symbol uses the logistic function f(x) = 1 / (1 + e^(-alpha * x) ) 448 497 //to determine the relative amounts of the true and false branch see http://en.wikipedia.org/wiki/Logistic_function 449 case OpCodes.VariableCondition: { 498 case OpCodes.VariableCondition: 499 { 450 500 if (row < 0 || row >= dataset.Rows) return double.NaN; 451 501 var variableConditionTreeNode = (VariableConditionTreeNode)currentInstr.dynamicNode; -
branches/HeuristicLab.LinqExpressionTreeInterpreter/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeLinearInterpreter.cs
r12509 r13141 48 48 49 49 #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]; }50 public IFixedValueParameter<BoolValue> CheckExpressionsWithIntervalArithmeticParameter { 51 get { return (IFixedValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName]; } 52 } 53 54 public IFixedValueParameter<IntValue> EvaluatedSolutionsParameter { 55 get { return (IFixedValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; } 56 56 } 57 57 #endregion 58 58 59 59 #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; }60 public bool CheckExpressionsWithIntervalArithmetic { 61 get { return CheckExpressionsWithIntervalArithmeticParameter.Value.Value; } 62 set { CheckExpressionsWithIntervalArithmeticParameter.Value.Value = value; } 63 } 64 public int EvaluatedSolutions { 65 get { return EvaluatedSolutionsParameter.Value.Value; } 66 set { EvaluatedSolutionsParameter.Value.Value = value; } 67 67 } 68 68 #endregion … … 84 84 public SymbolicDataAnalysisExpressionTreeLinearInterpreter() 85 85 : 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))); 86 Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, 87 "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false))); 88 Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0))); 89 interpreter = new SymbolicDataAnalysisExpressionTreeInterpreter(); 90 } 91 92 public SymbolicDataAnalysisExpressionTreeLinearInterpreter(string name, string description) 93 : base(name, description) { 94 Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, 95 "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", 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 } … … 92 101 private void AfterDeserialization() { 93 102 if (interpreter == null) interpreter = new SymbolicDataAnalysisExpressionTreeInterpreter(); 103 Parameters.Remove(EvaluatedSolutionsParameterName); 104 Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0))); 105 106 Parameters.Remove(CheckExpressionsWithIntervalArithmeticParameterName); 107 Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, 108 "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false))); 94 109 } 95 110 96 111 #region IStatefulItem 97 112 public void InitializeState() { 98 EvaluatedSolutions .Value= 0;113 EvaluatedSolutions = 0; 99 114 } 100 115 … … 103 118 104 119 public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows) { 105 if (CheckExpressionsWithIntervalArithmetic .Value)120 if (CheckExpressionsWithIntervalArithmetic) 106 121 throw new NotSupportedException("Interval arithmetic is not yet supported in the symbolic data analysis interpreter."); 107 122 108 lock (EvaluatedSolutions ) {109 EvaluatedSolutions .Value++; // increment the evaluated solutions counter123 lock (EvaluatedSolutionsParameter.Value) { 124 EvaluatedSolutions++; // increment the evaluated solutions counter 110 125 } 111 126 … … 357 372 #region opcode switch 358 373 switch (instr.opCode) { 359 case OpCodes.Constant: { 374 case OpCodes.Constant: 375 { 360 376 var constTreeNode = (ConstantTreeNode)instr.dynamicNode; 361 377 instr.value = constTreeNode.Value; … … 363 379 } 364 380 break; 365 case OpCodes.Variable: { 381 case OpCodes.Variable: 382 { 366 383 var variableTreeNode = (VariableTreeNode)instr.dynamicNode; 367 384 instr.data = dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName); 368 385 } 369 386 break; 370 case OpCodes.LagVariable: { 387 case OpCodes.LagVariable: 388 { 371 389 var laggedVariableTreeNode = (LaggedVariableTreeNode)instr.dynamicNode; 372 390 instr.data = dataset.GetReadOnlyDoubleValues(laggedVariableTreeNode.VariableName); 373 391 } 374 392 break; 375 case OpCodes.VariableCondition: { 393 case OpCodes.VariableCondition: 394 { 376 395 var variableConditionTreeNode = (VariableConditionTreeNode)instr.dynamicNode; 377 396 instr.data = dataset.GetReadOnlyDoubleValues(variableConditionTreeNode.VariableName); … … 380 399 case OpCodes.TimeLag: 381 400 case OpCodes.Integral: 382 case OpCodes.Derivative: { 401 case OpCodes.Derivative: 402 { 383 403 var seq = GetPrefixSequence(code, i); 384 404 var interpreterState = new InterpreterState(seq, 0); -
branches/HeuristicLab.LinqExpressionTreeInterpreter/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Plugin.cs.frame
r12790 r13141 28 28 [Plugin("HeuristicLab.Problems.DataAnalysis.Symbolic","Provides base classes for symbolic data analysis tasks.", "3.4.8.$WCREV$")] 29 29 [PluginFile("HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.dll", PluginFileType.Assembly)] 30 [PluginDependency("HeuristicLab.ALGLIB", "3. 9")]30 [PluginDependency("HeuristicLab.ALGLIB", "3.7.0")] 31 31 [PluginDependency("HeuristicLab.Analysis", "3.3")] 32 32 [PluginDependency("HeuristicLab.Collections", "3.3")]
Note: See TracChangeset
for help on using the changeset viewer.