Changeset 9271 for branches/HeuristicLab.DataAnalysis.Symbolic.LinearInterpreter/HeuristicLab.Problems.DataAnalysis.Symbolic
- Timestamp:
- 03/04/13 14:43:44 (12 years ago)
- Location:
- branches/HeuristicLab.DataAnalysis.Symbolic.LinearInterpreter
- Files:
-
- 2 added
- 3 edited
- 2 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.DataAnalysis.Symbolic.LinearInterpreter/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj
r8946 r9271 41 41 <DebugType>full</DebugType> 42 42 <Optimize>false</Optimize> 43 <OutputPath> $(SolutionDir)\bin\</OutputPath>43 <OutputPath>..\..\..\..\trunk\sources\bin\</OutputPath> 44 44 <DefineConstants>DEBUG;TRACE</DefineConstants> 45 45 <ErrorReport>prompt</ErrorReport> … … 50 50 <DebugType>pdbonly</DebugType> 51 51 <Optimize>true</Optimize> 52 <OutputPath> $(SolutionDir)\bin\</OutputPath>52 <OutputPath>..\..\..\..\trunk\sources\bin\</OutputPath> 53 53 <DefineConstants>TRACE</DefineConstants> 54 54 <ErrorReport>prompt</ErrorReport> … … 58 58 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' "> 59 59 <DebugSymbols>true</DebugSymbols> 60 <OutputPath> $(SolutionDir)\bin\</OutputPath>60 <OutputPath>..\..\..\..\trunk\sources\bin\</OutputPath> 61 61 <DefineConstants>DEBUG;TRACE</DefineConstants> 62 62 <DebugType>full</DebugType> … … 66 66 </PropertyGroup> 67 67 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x64' "> 68 <OutputPath> $(SolutionDir)\bin\</OutputPath>68 <OutputPath>..\..\..\..\trunk\sources\bin\</OutputPath> 69 69 <DefineConstants>TRACE</DefineConstants> 70 70 <Optimize>true</Optimize> … … 76 76 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' "> 77 77 <DebugSymbols>true</DebugSymbols> 78 <OutputPath> $(SolutionDir)\bin\</OutputPath>78 <OutputPath>..\..\..\..\trunk\sources\bin\</OutputPath> 79 79 <DefineConstants>DEBUG;TRACE</DefineConstants> 80 80 <DebugType>full</DebugType> … … 84 84 </PropertyGroup> 85 85 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' "> 86 <OutputPath> $(SolutionDir)\bin\</OutputPath>86 <OutputPath>..\..\..\..\trunk\sources\bin\</OutputPath> 87 87 <DefineConstants>TRACE</DefineConstants> 88 88 <Optimize>true</Optimize> … … 96 96 <HintPath>..\..\bin\ALGLIB-3.6.0.dll</HintPath> 97 97 <Private>False</Private> 98 </Reference> 99 <Reference Include="HeuristicLab.Analysis-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86"> 100 <SpecificVersion>False</SpecificVersion> 101 <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Analysis-3.3.dll</HintPath> 102 </Reference> 103 <Reference Include="HeuristicLab.Collections-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86"> 104 <SpecificVersion>False</SpecificVersion> 105 <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Collections-3.3.dll</HintPath> 106 </Reference> 107 <Reference Include="HeuristicLab.Common-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86"> 108 <SpecificVersion>False</SpecificVersion> 109 <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Common-3.3.dll</HintPath> 110 </Reference> 111 <Reference Include="HeuristicLab.Common.Resources-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86"> 112 <SpecificVersion>False</SpecificVersion> 113 <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Common.Resources-3.3.dll</HintPath> 114 </Reference> 115 <Reference Include="HeuristicLab.Core-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86"> 116 <SpecificVersion>False</SpecificVersion> 117 <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Core-3.3.dll</HintPath> 118 </Reference> 119 <Reference Include="HeuristicLab.Data-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86"> 120 <SpecificVersion>False</SpecificVersion> 121 <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Data-3.3.dll</HintPath> 122 </Reference> 123 <Reference Include="HeuristicLab.Data.Views-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86"> 124 <SpecificVersion>False</SpecificVersion> 125 <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Data.Views-3.3.dll</HintPath> 126 </Reference> 127 <Reference Include="HeuristicLab.Operators-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86"> 128 <SpecificVersion>False</SpecificVersion> 129 <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Operators-3.3.dll</HintPath> 130 </Reference> 131 <Reference Include="HeuristicLab.Optimization-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86"> 132 <SpecificVersion>False</SpecificVersion> 133 <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Optimization-3.3.dll</HintPath> 134 </Reference> 135 <Reference Include="HeuristicLab.Parameters-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86"> 136 <SpecificVersion>False</SpecificVersion> 137 <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Parameters-3.3.dll</HintPath> 138 </Reference> 139 <Reference Include="HeuristicLab.Persistence-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86"> 140 <SpecificVersion>False</SpecificVersion> 141 <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Persistence-3.3.dll</HintPath> 142 </Reference> 143 <Reference Include="HeuristicLab.PluginInfrastructure-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86"> 144 <SpecificVersion>False</SpecificVersion> 145 <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.PluginInfrastructure-3.3.dll</HintPath> 146 </Reference> 147 <Reference Include="HeuristicLab.Problems.DataAnalysis-3.4, Version=3.4.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86"> 148 <SpecificVersion>False</SpecificVersion> 149 <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Problems.DataAnalysis-3.4.dll</HintPath> 150 </Reference> 151 <Reference Include="HeuristicLab.Problems.Instances-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86"> 152 <SpecificVersion>False</SpecificVersion> 153 <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Problems.Instances-3.3.dll</HintPath> 154 </Reference> 155 <Reference Include="HeuristicLab.Random-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86"> 156 <SpecificVersion>False</SpecificVersion> 157 <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Random-3.3.dll</HintPath> 98 158 </Reference> 99 159 <Reference Include="System" /> … … 141 201 <Compile Include="Interfaces\ISymbolicDataAnalysisExpressionCrossover.cs" /> 142 202 <Compile Include="Interfaces\ISymbolicDataAnalysisImpactValuesCalculator.cs" /> 203 <Compile Include="Interpreter\SymbolicDataAnalysisExpressionTreeLinearInterpreter.cs" /> 143 204 <Compile Include="Interpreter\InterpreterState.cs" /> 144 205 <Compile Include="Interpreter\OpCodes.cs" /> … … 250 311 </ItemGroup> 251 312 <ItemGroup> 252 <ProjectReference Include="..\..\HeuristicLab.Analysis\3.3\HeuristicLab.Analysis-3.3.csproj">253 <Project>{887425B4-4348-49ED-A457-B7D2C26DDBF9}</Project>254 <Name>HeuristicLab.Analysis-3.3</Name>255 <Private>False</Private>256 </ProjectReference>257 <ProjectReference Include="..\..\HeuristicLab.Collections\3.3\HeuristicLab.Collections-3.3.csproj">258 <Project>{958B43BC-CC5C-4FA2-8628-2B3B01D890B6}</Project>259 <Name>HeuristicLab.Collections-3.3</Name>260 <Private>False</Private>261 </ProjectReference>262 <ProjectReference Include="..\..\HeuristicLab.Common.Resources\3.3\HeuristicLab.Common.Resources-3.3.csproj">263 <Project>{0E27A536-1C4A-4624-A65E-DC4F4F23E3E1}</Project>264 <Name>HeuristicLab.Common.Resources-3.3</Name>265 <Private>False</Private>266 </ProjectReference>267 <ProjectReference Include="..\..\HeuristicLab.Common\3.3\HeuristicLab.Common-3.3.csproj">268 <Project>{A9AD58B9-3EF9-4CC1-97E5-8D909039FF5C}</Project>269 <Name>HeuristicLab.Common-3.3</Name>270 <Private>False</Private>271 </ProjectReference>272 <ProjectReference Include="..\..\HeuristicLab.Core\3.3\HeuristicLab.Core-3.3.csproj">273 <Project>{C36BD924-A541-4A00-AFA8-41701378DDC5}</Project>274 <Name>HeuristicLab.Core-3.3</Name>275 <Private>False</Private>276 </ProjectReference>277 <ProjectReference Include="..\..\HeuristicLab.Data\3.3\HeuristicLab.Data-3.3.csproj">278 <Project>{BBAB9DF5-5EF3-4BA8-ADE9-B36E82114937}</Project>279 <Name>HeuristicLab.Data-3.3</Name>280 <Private>False</Private>281 </ProjectReference>282 313 <ProjectReference Include="..\..\HeuristicLab.Encodings.SymbolicExpressionTreeEncoding\3.4\HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.csproj"> 283 314 <Project>{06D4A186-9319-48A0-BADE-A2058D462EEA}</Project> 284 315 <Name>HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4</Name> 285 <Private>False</Private>286 </ProjectReference>287 <ProjectReference Include="..\..\HeuristicLab.Operators\3.3\HeuristicLab.Operators-3.3.csproj">288 <Project>{23DA7FF4-D5B8-41B6-AA96-F0561D24F3EE}</Project>289 <Name>HeuristicLab.Operators-3.3</Name>290 <Private>False</Private>291 </ProjectReference>292 <ProjectReference Include="..\..\HeuristicLab.Optimization.Operators\3.3\HeuristicLab.Optimization.Operators-3.3.csproj">293 <Project>{25087811-F74C-4128-BC86-8324271DA13E}</Project>294 <Name>HeuristicLab.Optimization.Operators-3.3</Name>295 <Private>False</Private>296 </ProjectReference>297 <ProjectReference Include="..\..\HeuristicLab.Optimization\3.3\HeuristicLab.Optimization-3.3.csproj">298 <Project>{14AB8D24-25BC-400C-A846-4627AA945192}</Project>299 <Name>HeuristicLab.Optimization-3.3</Name>300 </ProjectReference>301 <ProjectReference Include="..\..\HeuristicLab.Parameters\3.3\HeuristicLab.Parameters-3.3.csproj">302 <Project>{56F9106A-079F-4C61-92F6-86A84C2D84B7}</Project>303 <Name>HeuristicLab.Parameters-3.3</Name>304 <Private>False</Private>305 </ProjectReference>306 <ProjectReference Include="..\..\HeuristicLab.Persistence\3.3\HeuristicLab.Persistence-3.3.csproj">307 <Project>{102BC7D3-0EF9-439C-8F6D-96FF0FDB8E1B}</Project>308 <Name>HeuristicLab.Persistence-3.3</Name>309 <Private>False</Private>310 </ProjectReference>311 <ProjectReference Include="..\..\HeuristicLab.PluginInfrastructure\3.3\HeuristicLab.PluginInfrastructure-3.3.csproj">312 <Project>{94186A6A-5176-4402-AE83-886557B53CCA}</Project>313 <Name>HeuristicLab.PluginInfrastructure-3.3</Name>314 <Private>False</Private>315 </ProjectReference>316 <ProjectReference Include="..\..\HeuristicLab.Problems.DataAnalysis\3.4\HeuristicLab.Problems.DataAnalysis-3.4.csproj">317 <Project>{DF87C13E-A889-46FF-8153-66DCAA8C5674}</Project>318 <Name>HeuristicLab.Problems.DataAnalysis-3.4</Name>319 <Private>False</Private>320 </ProjectReference>321 <ProjectReference Include="..\..\HeuristicLab.Problems.Instances\3.3\HeuristicLab.Problems.Instances-3.3.csproj">322 <Project>{3540E29E-4793-49E7-8EE2-FEA7F61C3994}</Project>323 <Name>HeuristicLab.Problems.Instances-3.3</Name>324 <Private>False</Private>325 </ProjectReference>326 <ProjectReference Include="..\..\HeuristicLab.Random\3.3\HeuristicLab.Random-3.3.csproj">327 <Project>{F4539FB6-4708-40C9-BE64-0A1390AEA197}</Project>328 <Name>HeuristicLab.Random-3.3</Name>329 316 <Private>False</Private> 330 317 </ProjectReference> -
branches/HeuristicLab.DataAnalysis.Symbolic.LinearInterpreter/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeILEmittingInterpreter.cs
r8798 r9271 162 162 163 163 private InterpreterState PrepareInterpreterState(ISymbolicExpressionTree tree, Dataset dataset) { 164 Instruction[] code = SymbolicExpressionTreeCompiler.Compile (tree, OpCodes.MapSymbolToOpCode);164 Instruction[] code = SymbolicExpressionTreeCompiler.CompilePrefix(tree, OpCodes.MapSymbolToOpCode); 165 165 Dictionary<string, int> doubleVariableNames = dataset.DoubleVariables.Select((x, i) => new { x, i }).ToDictionary(e => e.x, e => e.i); 166 166 int necessaryArgStackSize = 0; -
branches/HeuristicLab.DataAnalysis.Symbolic.LinearInterpreter/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeInterpreter.cs
r9004 r9271 112 112 113 113 private static InterpreterState PrepareInterpreterState(ISymbolicExpressionTree tree, Dataset dataset) { 114 Instruction[] code = SymbolicExpressionTreeCompiler.Compile (tree, OpCodes.MapSymbolToOpCode);114 Instruction[] code = SymbolicExpressionTreeCompiler.CompilePrefix(tree, OpCodes.MapSymbolToOpCode); 115 115 int necessaryArgStackSize = 0; 116 116 foreach (Instruction instr in code) { -
branches/HeuristicLab.DataAnalysis.Symbolic.LinearInterpreter/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeLinearInterpreter.cs
r9257 r9271 31 31 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 32 32 [StorableClass] 33 [Item("SymbolicDataAnalysisExpressionTree Interpreter", "Interpreter for symbolic expression trees including automatically defined functions.")]34 public class SymbolicDataAnalysisExpressionTree Interpreter : ParameterizedNamedItem, ISymbolicDataAnalysisExpressionTreeInterpreter {33 [Item("SymbolicDataAnalysisExpressionTreeLinearInterpreter", "Interpreter for symbolic expression trees including automatically defined functions.")] 34 public class SymbolicDataAnalysisExpressionTreeLinearInterpreter : ParameterizedNamedItem, ISymbolicDataAnalysisExpressionTreeInterpreter { 35 35 private const string CheckExpressionsWithIntervalArithmeticParameterName = "CheckExpressionsWithIntervalArithmetic"; 36 36 private const string EvaluatedSolutionsParameterName = "EvaluatedSolutions"; … … 62 62 63 63 [StorableConstructor] 64 protected SymbolicDataAnalysisExpressionTree Interpreter(bool deserializing) : base(deserializing) { }65 protected SymbolicDataAnalysisExpressionTree Interpreter(SymbolicDataAnalysisExpressionTreeInterpreter original, Cloner cloner) : base(original, cloner) { }64 protected SymbolicDataAnalysisExpressionTreeLinearInterpreter(bool deserializing) : base(deserializing) { } 65 protected SymbolicDataAnalysisExpressionTreeLinearInterpreter(SymbolicDataAnalysisExpressionTreeLinearInterpreter original, Cloner cloner) : base(original, cloner) { } 66 66 public override IDeepCloneable Clone(Cloner cloner) { 67 return new SymbolicDataAnalysisExpressionTree Interpreter(this, cloner);68 } 69 70 public SymbolicDataAnalysisExpressionTree Interpreter()71 : base("SymbolicDataAnalysisExpressionTree Interpreter", "Interpreter for symbolic expression trees including automatically defined functions.") {67 return new SymbolicDataAnalysisExpressionTreeLinearInterpreter(this, cloner); 68 } 69 70 public SymbolicDataAnalysisExpressionTreeLinearInterpreter() 71 : base("SymbolicDataAnalysisExpressionTreeLinearInterpreter", "Interpreter for symbolic expression trees including automatically defined functions.") { 72 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 73 Parameters.Add(new ValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0))); 74 74 } 75 75 76 protected SymbolicDataAnalysisExpressionTree Interpreter(string name, string description)76 protected SymbolicDataAnalysisExpressionTreeLinearInterpreter(string name, string description) 77 77 : base(name, description) { 78 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))); … … 102 102 EvaluatedSolutions.Value++; // increment the evaluated solutions counter 103 103 } 104 var state = PrepareInterpreterState(tree, dataset); 104 105 Instruction[] code = SymbolicExpressionTreeCompiler.CompilePostfix(tree, OpCodes.MapSymbolToOpCode); 106 foreach (Instruction instr in code) { 107 switch (instr.opCode) { 108 case OpCodes.Variable: { 109 var variableTreeNode = (VariableTreeNode)instr.dynamicNode; 110 instr.iArg0 = dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName); 111 } 112 break; 113 case OpCodes.LagVariable: { 114 var laggedVariableTreeNode = (LaggedVariableTreeNode)instr.dynamicNode; 115 instr.iArg0 = dataset.GetReadOnlyDoubleValues(laggedVariableTreeNode.VariableName); 116 } 117 break; 118 case OpCodes.VariableCondition: { 119 var variableConditionTreeNode = (VariableConditionTreeNode)instr.dynamicNode; 120 instr.iArg0 = dataset.GetReadOnlyDoubleValues(variableConditionTreeNode.VariableName); 121 } 122 break; 123 } 124 } 105 125 106 126 foreach (var rowEnum in rows) { 107 127 int row = rowEnum; 108 yield return Evaluate(dataset, ref row, state); 109 state.Reset(); 128 yield return Evaluate(dataset, ref row, code); 110 129 } 111 130 } 112 131 113 private static InterpreterState PrepareInterpreterState(ISymbolicExpressionTree tree, Dataset dataset) { 114 Instruction[] code = SymbolicExpressionTreeCompiler.Compile(tree, OpCodes.MapSymbolToOpCode); 115 int necessaryArgStackSize = 0; 116 foreach (Instruction instr in code) { 117 if (instr.opCode == OpCodes.Variable) { 118 var variableTreeNode = (VariableTreeNode)instr.dynamicNode; 119 instr.iArg0 = dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName); 120 } else if (instr.opCode == OpCodes.LagVariable) { 121 var laggedVariableTreeNode = (LaggedVariableTreeNode)instr.dynamicNode; 122 instr.iArg0 = dataset.GetReadOnlyDoubleValues(laggedVariableTreeNode.VariableName); 123 } else if (instr.opCode == OpCodes.VariableCondition) { 124 var variableConditionTreeNode = (VariableConditionTreeNode)instr.dynamicNode; 125 instr.iArg0 = dataset.GetReadOnlyDoubleValues(variableConditionTreeNode.VariableName); 126 } else if (instr.opCode == OpCodes.Call) { 127 necessaryArgStackSize += instr.nArguments + 1; 132 protected virtual double Evaluate(Dataset dataset, ref int row, Instruction[] code) { 133 int count = 0; 134 for (int j = 0; j != code.Length; ++j) { 135 Instruction currentInstr = code[j]; 136 137 switch (currentInstr.opCode) { 138 case OpCodes.Add: { 139 double s = code[j - currentInstr.nArguments].value; 140 for (int i = j - currentInstr.nArguments + 1; i < j; i++) { 141 s += code[i].value; 142 } 143 currentInstr.value = s; 144 } 145 break; 146 case OpCodes.Sub: { 147 double s = code[j - currentInstr.nArguments].value; 148 for (int i = j - currentInstr.nArguments + 1; i < j; i++) { 149 s -= code[i].value; 150 } 151 if (currentInstr.nArguments == 1) s = -s; 152 currentInstr.value = s; 153 } 154 break; 155 case OpCodes.Mul: { 156 double p = code[j - currentInstr.nArguments].value; 157 for (int i = j - currentInstr.nArguments + 1; i < j; i++) { 158 p *= code[i].value; 159 } 160 currentInstr.value = p; 161 } 162 break; 163 case OpCodes.Div: { 164 double p = code[j - currentInstr.nArguments].value; 165 for (int i = j - currentInstr.nArguments + 1; i < j; i++) { 166 p /= code[i].value; 167 } 168 if (currentInstr.nArguments == 1) p = 1.0 / p; 169 currentInstr.value = p; 170 } 171 break; 172 case OpCodes.Average: { 173 double sum = code[j - currentInstr.nArguments].value; 174 for (int i = j - currentInstr.nArguments + 1; i < j; i++) { 175 sum += code[i].value; 176 } 177 currentInstr.value = sum / currentInstr.nArguments; 178 } 179 break; 180 case OpCodes.Cos: { 181 currentInstr.value = Math.Cos(code[j - 1].value); 182 } 183 break; 184 case OpCodes.Sin: { 185 currentInstr.value = Math.Sin(code[j - 1].value); 186 break; 187 } 188 case OpCodes.Tan: { 189 currentInstr.value = Math.Tan(code[j - 1].value); 190 } 191 break; 192 case OpCodes.Square: { 193 currentInstr.value = Math.Pow(code[j - 1].value, 2); 194 } 195 break; 196 case OpCodes.Power: { 197 double x = code[j - 2].value; 198 double y = Math.Round(code[j - 1].value); 199 currentInstr.value = Math.Pow(x, y); 200 } 201 break; 202 case OpCodes.SquareRoot: { 203 currentInstr.value = Math.Sqrt(code[j - 1].value); 204 } 205 break; 206 case OpCodes.Root: { 207 double x = code[j - 2].value; 208 double y = Math.Round(code[j - 1].value); 209 currentInstr.value = Math.Pow(x, 1 / y); 210 } 211 break; 212 case OpCodes.Exp: { 213 currentInstr.value = Math.Exp(code[j - 1].value); 214 } 215 break; 216 case OpCodes.Log: { 217 currentInstr.value = Math.Log(code[j - 1].value); 218 } 219 break; 220 case OpCodes.Gamma: { 221 currentInstr.value = double.IsNaN(code[j - 1].value) ? double.NaN : alglib.gammafunction(code[j - 1].value); 222 } 223 break; 224 case OpCodes.Psi: { 225 var x = code[j - 1].value; 226 if (double.IsNaN(x)) currentInstr.value = double.NaN; 227 else if (x <= 0 && (Math.Floor(x) - x).IsAlmost(0)) currentInstr.value = double.NaN; 228 else currentInstr.value = alglib.psi(x); 229 } 230 break; 231 case OpCodes.Dawson: { 232 var x = code[j - 1].value; 233 currentInstr.value = double.IsNaN(x) ? double.NaN : alglib.dawsonintegral(x); 234 } 235 break; 236 case OpCodes.ExponentialIntegralEi: { 237 var x = code[j - 1].value; 238 currentInstr.value = double.IsNaN(x) ? double.NaN : alglib.exponentialintegralei(x); 239 } 240 break; 241 case OpCodes.SineIntegral: { 242 double si, ci; 243 var x = code[j - 1].value; 244 if (double.IsNaN(x)) currentInstr.value = double.NaN; 245 else { 246 alglib.sinecosineintegrals(x, out si, out ci); 247 currentInstr.value = si; 248 } 249 } 250 break; 251 case OpCodes.CosineIntegral: { 252 double si, ci; 253 var x = code[j - 1].value; 254 if (double.IsNaN(x)) currentInstr.value = double.NaN; 255 else { 256 alglib.sinecosineintegrals(x, out si, out ci); 257 currentInstr.value = ci; 258 } 259 } 260 break; 261 case OpCodes.HyperbolicSineIntegral: { 262 double shi, chi; 263 var x = code[j - 1].value; 264 if (double.IsNaN(x)) currentInstr.value = double.NaN; 265 else { 266 alglib.hyperbolicsinecosineintegrals(x, out shi, out chi); 267 currentInstr.value = shi; 268 } 269 } 270 break; 271 case OpCodes.HyperbolicCosineIntegral: { 272 double shi, chi; 273 var x = code[j - 1].value; 274 if (double.IsNaN(x)) currentInstr.value = double.NaN; 275 else { 276 alglib.hyperbolicsinecosineintegrals(x, out shi, out chi); 277 currentInstr.value = chi; 278 } 279 } 280 break; 281 case OpCodes.FresnelCosineIntegral: { 282 double c = 0, s = 0; 283 var x = code[j - 1].value; 284 if (double.IsNaN(x)) currentInstr.value = double.NaN; 285 else { 286 alglib.fresnelintegral(x, ref c, ref s); 287 currentInstr.value = c; 288 } 289 } 290 break; 291 case OpCodes.FresnelSineIntegral: { 292 double c = 0, s = 0; 293 var x = code[j - 1].value; 294 if (double.IsNaN(x)) currentInstr.value = double.NaN; 295 else { 296 alglib.fresnelintegral(x, ref c, ref s); 297 currentInstr.value = s; 298 } 299 } 300 break; 301 case OpCodes.AiryA: { 302 double ai, aip, bi, bip; 303 var x = code[j - 1].value; 304 if (double.IsNaN(x)) currentInstr.value = double.NaN; 305 else { 306 alglib.airy(x, out ai, out aip, out bi, out bip); 307 currentInstr.value = ai; 308 } 309 } 310 break; 311 case OpCodes.AiryB: { 312 double ai, aip, bi, bip; 313 var x = code[j - 1].value; 314 if (double.IsNaN(x)) currentInstr.value = double.NaN; 315 else { 316 alglib.airy(x, out ai, out aip, out bi, out bip); 317 currentInstr.value = bi; 318 } 319 } 320 break; 321 case OpCodes.Norm: { 322 var x = code[j - 1].value; 323 currentInstr.value = double.IsNaN(x) ? double.NaN : alglib.normaldistribution(x); 324 } 325 break; 326 case OpCodes.Erf: { 327 var x = code[j - 1].value; 328 currentInstr.value = double.IsNaN(x) ? double.NaN : alglib.errorfunction(x); 329 } 330 break; 331 case OpCodes.Bessel: { 332 var x = code[j - 1].value; 333 currentInstr.value = double.IsNaN(x) ? double.NaN : alglib.besseli0(x); 334 } 335 break; 336 case OpCodes.IfThenElse: { 337 double condition = code[j - currentInstr.nArguments].value; 338 double result = condition > 0.0 ? code[j - 2].value : code[j - 1].value; 339 currentInstr.value = result; 340 } 341 break; 342 case OpCodes.AND: { 343 double result = code[j - currentInstr.nArguments].value; 344 for (int i = j - currentInstr.nArguments + 1; i < j; i++) { 345 if (result > 0.0) result = code[i].value; 346 } 347 currentInstr.value = result > 0.0 ? 1.0 : -1.0; 348 } 349 break; 350 case OpCodes.OR: { 351 double result = code[j - currentInstr.nArguments].value; 352 for (int i = 1; i < currentInstr.nArguments; i++) { 353 if (result <= 0.0) result = code[i].value; ; 354 } 355 currentInstr.value = result > 0.0 ? 1.0 : -1.0; 356 } 357 break; 358 case OpCodes.NOT: { 359 currentInstr.value = code[j - 1].value > 0.0 ? -1.0 : 1.0; 360 } 361 break; 362 case OpCodes.GT: { 363 double x = code[j - 2].value; 364 double y = code[j - 1].value; 365 currentInstr.value = x > y ? 1.0 : -1.0; 366 } 367 break; 368 case OpCodes.LT: { 369 double x = code[j - 2].value; 370 double y = code[j - 1].value; 371 currentInstr.value = x < y ? 1.0 : -1.0; 372 } 373 break; 374 case OpCodes.TimeLag: { 375 throw new NotSupportedException(); 376 } 377 case OpCodes.Integral: { 378 throw new NotSupportedException(); 379 } 380 381 //mkommend: derivate calculation taken from: 382 //http://www.holoborodko.com/pavel/numerical-methods/numerical-derivative/smooth-low-noise-differentiators/ 383 //one sided smooth differentiatior, N = 4 384 // y' = 1/8h (f_i + 2f_i-1, -2 f_i-3 - f_i-4) 385 case OpCodes.Derivative: { 386 throw new NotSupportedException(); 387 } 388 case OpCodes.Call: { 389 throw new NotSupportedException(); 390 } 391 case OpCodes.Arg: { 392 throw new NotSupportedException(); 393 } 394 case OpCodes.Variable: { 395 if (row < 0 || row >= dataset.Rows) currentInstr.value = double.NaN; 396 else { 397 var variableTreeNode = (VariableTreeNode)currentInstr.dynamicNode; 398 currentInstr.value = ((IList<double>)currentInstr.iArg0)[row] * variableTreeNode.Weight; 399 } 400 } 401 break; 402 case OpCodes.LagVariable: { 403 var laggedVariableTreeNode = (LaggedVariableTreeNode)currentInstr.dynamicNode; 404 int actualRow = row + laggedVariableTreeNode.Lag; 405 if (actualRow < 0 || actualRow >= dataset.Rows) currentInstr.value = double.NaN; 406 else { 407 currentInstr.value = ((IList<double>)currentInstr.iArg0)[actualRow] * laggedVariableTreeNode.Weight; 408 } 409 } 410 break; 411 case OpCodes.Constant: { 412 var constTreeNode = (ConstantTreeNode)currentInstr.dynamicNode; 413 currentInstr.value = constTreeNode.Value; 414 } 415 break; 416 417 //mkommend: this symbol uses the logistic function f(x) = 1 / (1 + e^(-alpha * x) ) 418 //to determine the relative amounts of the true and false branch see http://en.wikipedia.org/wiki/Logistic_function 419 case OpCodes.VariableCondition: { 420 if (row < 0 || row >= dataset.Rows) currentInstr.value = double.NaN; 421 else { 422 var variableConditionTreeNode = (VariableConditionTreeNode)currentInstr.dynamicNode; 423 double variableValue = ((IList<double>)currentInstr.iArg0)[row]; 424 double x = variableValue - variableConditionTreeNode.Threshold; 425 double p = 1 / (1 + Math.Exp(-variableConditionTreeNode.Slope * x)); 426 427 double trueBranch = code[j - currentInstr.nArguments].value; 428 double falseBranch = code[j - currentInstr.nArguments + 1].value; 429 430 currentInstr.value = trueBranch * p + falseBranch * (1 - p); 431 } 432 } 433 break; 434 default: 435 throw new NotSupportedException(); 128 436 } 437 438 // book-keeping in order to keep all the values correct 439 int narg = currentInstr.nArguments; 440 441 // we count the values before the next function is encountered 442 if (narg == 0) { ++count; continue; } 443 444 if (count > narg) { 445 for (int i = 1; i <= count - narg; ++i) 446 code[j - i].value = code[j - i - narg].value; 447 } 448 449 count -= (narg - 1); // because after being evaluated the current instruction becomes a 'value' 129 450 } 130 return new InterpreterState(code, necessaryArgStackSize); 131 } 132 133 134 protected virtual double Evaluate(Dataset dataset, ref int row, InterpreterState state) { 135 Instruction currentInstr = state.NextInstruction(); 136 switch (currentInstr.opCode) { 137 case OpCodes.Add: { 138 double s = Evaluate(dataset, ref row, state); 139 for (int i = 1; i < currentInstr.nArguments; i++) { 140 s += Evaluate(dataset, ref row, state); 141 } 142 return s; 143 } 144 case OpCodes.Sub: { 145 double s = Evaluate(dataset, ref row, state); 146 for (int i = 1; i < currentInstr.nArguments; i++) { 147 s -= Evaluate(dataset, ref row, state); 148 } 149 if (currentInstr.nArguments == 1) s = -s; 150 return s; 151 } 152 case OpCodes.Mul: { 153 double p = Evaluate(dataset, ref row, state); 154 for (int i = 1; i < currentInstr.nArguments; i++) { 155 p *= Evaluate(dataset, ref row, state); 156 } 157 return p; 158 } 159 case OpCodes.Div: { 160 double p = Evaluate(dataset, ref row, state); 161 for (int i = 1; i < currentInstr.nArguments; i++) { 162 p /= Evaluate(dataset, ref row, state); 163 } 164 if (currentInstr.nArguments == 1) p = 1.0 / p; 165 return p; 166 } 167 case OpCodes.Average: { 168 double sum = Evaluate(dataset, ref row, state); 169 for (int i = 1; i < currentInstr.nArguments; i++) { 170 sum += Evaluate(dataset, ref row, state); 171 } 172 return sum / currentInstr.nArguments; 173 } 174 case OpCodes.Cos: { 175 return Math.Cos(Evaluate(dataset, ref row, state)); 176 } 177 case OpCodes.Sin: { 178 return Math.Sin(Evaluate(dataset, ref row, state)); 179 } 180 case OpCodes.Tan: { 181 return Math.Tan(Evaluate(dataset, ref row, state)); 182 } 183 case OpCodes.Square: { 184 return Math.Pow(Evaluate(dataset, ref row, state), 2); 185 } 186 case OpCodes.Power: { 187 double x = Evaluate(dataset, ref row, state); 188 double y = Math.Round(Evaluate(dataset, ref row, state)); 189 return Math.Pow(x, y); 190 } 191 case OpCodes.SquareRoot: { 192 return Math.Sqrt(Evaluate(dataset, ref row, state)); 193 } 194 case OpCodes.Root: { 195 double x = Evaluate(dataset, ref row, state); 196 double y = Math.Round(Evaluate(dataset, ref row, state)); 197 return Math.Pow(x, 1 / y); 198 } 199 case OpCodes.Exp: { 200 return Math.Exp(Evaluate(dataset, ref row, state)); 201 } 202 case OpCodes.Log: { 203 return Math.Log(Evaluate(dataset, ref row, state)); 204 } 205 case OpCodes.Gamma: { 206 var x = Evaluate(dataset, ref row, state); 207 if (double.IsNaN(x)) return double.NaN; 208 else return alglib.gammafunction(x); 209 } 210 case OpCodes.Psi: { 211 var x = Evaluate(dataset, ref row, state); 212 if (double.IsNaN(x)) return double.NaN; 213 else if (x <= 0 && (Math.Floor(x) - x).IsAlmost(0)) return double.NaN; 214 return alglib.psi(x); 215 } 216 case OpCodes.Dawson: { 217 var x = Evaluate(dataset, ref row, state); 218 if (double.IsNaN(x)) return double.NaN; 219 return alglib.dawsonintegral(x); 220 } 221 case OpCodes.ExponentialIntegralEi: { 222 var x = Evaluate(dataset, ref row, state); 223 if (double.IsNaN(x)) return double.NaN; 224 return alglib.exponentialintegralei(x); 225 } 226 case OpCodes.SineIntegral: { 227 double si, ci; 228 var x = Evaluate(dataset, ref row, state); 229 if (double.IsNaN(x)) return double.NaN; 230 else { 231 alglib.sinecosineintegrals(x, out si, out ci); 232 return si; 233 } 234 } 235 case OpCodes.CosineIntegral: { 236 double si, ci; 237 var x = Evaluate(dataset, ref row, state); 238 if (double.IsNaN(x)) return double.NaN; 239 else { 240 alglib.sinecosineintegrals(x, out si, out ci); 241 return ci; 242 } 243 } 244 case OpCodes.HyperbolicSineIntegral: { 245 double shi, chi; 246 var x = Evaluate(dataset, ref row, state); 247 if (double.IsNaN(x)) return double.NaN; 248 else { 249 alglib.hyperbolicsinecosineintegrals(x, out shi, out chi); 250 return shi; 251 } 252 } 253 case OpCodes.HyperbolicCosineIntegral: { 254 double shi, chi; 255 var x = Evaluate(dataset, ref row, state); 256 if (double.IsNaN(x)) return double.NaN; 257 else { 258 alglib.hyperbolicsinecosineintegrals(x, out shi, out chi); 259 return chi; 260 } 261 } 262 case OpCodes.FresnelCosineIntegral: { 263 double c = 0, s = 0; 264 var x = Evaluate(dataset, ref row, state); 265 if (double.IsNaN(x)) return double.NaN; 266 else { 267 alglib.fresnelintegral(x, ref c, ref s); 268 return c; 269 } 270 } 271 case OpCodes.FresnelSineIntegral: { 272 double c = 0, s = 0; 273 var x = Evaluate(dataset, ref row, state); 274 if (double.IsNaN(x)) return double.NaN; 275 else { 276 alglib.fresnelintegral(x, ref c, ref s); 277 return s; 278 } 279 } 280 case OpCodes.AiryA: { 281 double ai, aip, bi, bip; 282 var x = Evaluate(dataset, ref row, state); 283 if (double.IsNaN(x)) return double.NaN; 284 else { 285 alglib.airy(x, out ai, out aip, out bi, out bip); 286 return ai; 287 } 288 } 289 case OpCodes.AiryB: { 290 double ai, aip, bi, bip; 291 var x = Evaluate(dataset, ref row, state); 292 if (double.IsNaN(x)) return double.NaN; 293 else { 294 alglib.airy(x, out ai, out aip, out bi, out bip); 295 return bi; 296 } 297 } 298 case OpCodes.Norm: { 299 var x = Evaluate(dataset, ref row, state); 300 if (double.IsNaN(x)) return double.NaN; 301 else return alglib.normaldistribution(x); 302 } 303 case OpCodes.Erf: { 304 var x = Evaluate(dataset, ref row, state); 305 if (double.IsNaN(x)) return double.NaN; 306 else return alglib.errorfunction(x); 307 } 308 case OpCodes.Bessel: { 309 var x = Evaluate(dataset, ref row, state); 310 if (double.IsNaN(x)) return double.NaN; 311 else return alglib.besseli0(x); 312 } 313 case OpCodes.IfThenElse: { 314 double condition = Evaluate(dataset, ref row, state); 315 double result; 316 if (condition > 0.0) { 317 result = Evaluate(dataset, ref row, state); state.SkipInstructions(); 318 } else { 319 state.SkipInstructions(); result = Evaluate(dataset, ref row, state); 320 } 321 return result; 322 } 323 case OpCodes.AND: { 324 double result = Evaluate(dataset, ref row, state); 325 for (int i = 1; i < currentInstr.nArguments; i++) { 326 if (result > 0.0) result = Evaluate(dataset, ref row, state); 327 else { 328 state.SkipInstructions(); 329 } 330 } 331 return result > 0.0 ? 1.0 : -1.0; 332 } 333 case OpCodes.OR: { 334 double result = Evaluate(dataset, ref row, state); 335 for (int i = 1; i < currentInstr.nArguments; i++) { 336 if (result <= 0.0) result = Evaluate(dataset, ref row, state); 337 else { 338 state.SkipInstructions(); 339 } 340 } 341 return result > 0.0 ? 1.0 : -1.0; 342 } 343 case OpCodes.NOT: { 344 return Evaluate(dataset, ref row, state) > 0.0 ? -1.0 : 1.0; 345 } 346 case OpCodes.GT: { 347 double x = Evaluate(dataset, ref row, state); 348 double y = Evaluate(dataset, ref row, state); 349 if (x > y) return 1.0; 350 else return -1.0; 351 } 352 case OpCodes.LT: { 353 double x = Evaluate(dataset, ref row, state); 354 double y = Evaluate(dataset, ref row, state); 355 if (x < y) return 1.0; 356 else return -1.0; 357 } 358 case OpCodes.TimeLag: { 359 var timeLagTreeNode = (LaggedTreeNode)currentInstr.dynamicNode; 360 row += timeLagTreeNode.Lag; 361 double result = Evaluate(dataset, ref row, state); 362 row -= timeLagTreeNode.Lag; 363 return result; 364 } 365 case OpCodes.Integral: { 366 int savedPc = state.ProgramCounter; 367 var timeLagTreeNode = (LaggedTreeNode)currentInstr.dynamicNode; 368 double sum = 0.0; 369 for (int i = 0; i < Math.Abs(timeLagTreeNode.Lag); i++) { 370 row += Math.Sign(timeLagTreeNode.Lag); 371 sum += Evaluate(dataset, ref row, state); 372 state.ProgramCounter = savedPc; 373 } 374 row -= timeLagTreeNode.Lag; 375 sum += Evaluate(dataset, ref row, state); 376 return sum; 377 } 378 379 //mkommend: derivate calculation taken from: 380 //http://www.holoborodko.com/pavel/numerical-methods/numerical-derivative/smooth-low-noise-differentiators/ 381 //one sided smooth differentiatior, N = 4 382 // y' = 1/8h (f_i + 2f_i-1, -2 f_i-3 - f_i-4) 383 case OpCodes.Derivative: { 384 int savedPc = state.ProgramCounter; 385 double f_0 = Evaluate(dataset, ref row, state); row--; 386 state.ProgramCounter = savedPc; 387 double f_1 = Evaluate(dataset, ref row, state); row -= 2; 388 state.ProgramCounter = savedPc; 389 double f_3 = Evaluate(dataset, ref row, state); row--; 390 state.ProgramCounter = savedPc; 391 double f_4 = Evaluate(dataset, ref row, state); 392 row += 4; 393 394 return (f_0 + 2 * f_1 - 2 * f_3 - f_4) / 8; // h = 1 395 } 396 case OpCodes.Call: { 397 // evaluate sub-trees 398 double[] argValues = new double[currentInstr.nArguments]; 399 for (int i = 0; i < currentInstr.nArguments; i++) { 400 argValues[i] = Evaluate(dataset, ref row, state); 401 } 402 // push on argument values on stack 403 state.CreateStackFrame(argValues); 404 405 // save the pc 406 int savedPc = state.ProgramCounter; 407 // set pc to start of function 408 state.ProgramCounter = (ushort)currentInstr.iArg0; 409 // evaluate the function 410 double v = Evaluate(dataset, ref row, state); 411 412 // delete the stack frame 413 state.RemoveStackFrame(); 414 415 // restore the pc => evaluation will continue at point after my subtrees 416 state.ProgramCounter = savedPc; 417 return v; 418 } 419 case OpCodes.Arg: { 420 return state.GetStackFrameValue((ushort)currentInstr.iArg0); 421 } 422 case OpCodes.Variable: { 423 if (row < 0 || row >= dataset.Rows) return double.NaN; 424 var variableTreeNode = (VariableTreeNode)currentInstr.dynamicNode; 425 return ((IList<double>)currentInstr.iArg0)[row] * variableTreeNode.Weight; 426 } 427 case OpCodes.LagVariable: { 428 var laggedVariableTreeNode = (LaggedVariableTreeNode)currentInstr.dynamicNode; 429 int actualRow = row + laggedVariableTreeNode.Lag; 430 if (actualRow < 0 || actualRow >= dataset.Rows) return double.NaN; 431 return ((IList<double>)currentInstr.iArg0)[actualRow] * laggedVariableTreeNode.Weight; 432 } 433 case OpCodes.Constant: { 434 var constTreeNode = (ConstantTreeNode)currentInstr.dynamicNode; 435 return constTreeNode.Value; 436 } 437 438 //mkommend: this symbol uses the logistic function f(x) = 1 / (1 + e^(-alpha * x) ) 439 //to determine the relative amounts of the true and false branch see http://en.wikipedia.org/wiki/Logistic_function 440 case OpCodes.VariableCondition: { 441 if (row < 0 || row >= dataset.Rows) return double.NaN; 442 var variableConditionTreeNode = (VariableConditionTreeNode)currentInstr.dynamicNode; 443 double variableValue = ((IList<double>)currentInstr.iArg0)[row]; 444 double x = variableValue - variableConditionTreeNode.Threshold; 445 double p = 1 / (1 + Math.Exp(-variableConditionTreeNode.Slope * x)); 446 447 double trueBranch = Evaluate(dataset, ref row, state); 448 double falseBranch = Evaluate(dataset, ref row, state); 449 450 return trueBranch * p + falseBranch * (1 - p); 451 } 452 default: throw new NotSupportedException(); 453 } 451 452 return code[code.Length - 1].value; 454 453 } 455 454 }
Note: See TracChangeset
for help on using the changeset viewer.