[5571] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17209] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5571] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
[9739] | 24 | using System.Linq;
|
---|
[5571] | 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
[6740] | 27 | using HeuristicLab.Data;
|
---|
[5571] | 28 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[6740] | 29 | using HeuristicLab.Parameters;
|
---|
[16565] | 30 | using HEAL.Attic;
|
---|
[5571] | 31 |
|
---|
| 32 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
[16565] | 33 | [StorableType("EF325166-E03A-44C4-83CE-7F07B836285E")]
|
---|
[9815] | 34 | [Item("SymbolicDataAnalysisExpressionTreeLinearInterpreter", "Fast linear (non-recursive) interpreter for symbolic expression trees. Does not support ADFs.")]
|
---|
[9758] | 35 | public sealed class SymbolicDataAnalysisExpressionTreeLinearInterpreter : ParameterizedNamedItem, ISymbolicDataAnalysisExpressionTreeInterpreter {
|
---|
[5749] | 36 | private const string CheckExpressionsWithIntervalArithmeticParameterName = "CheckExpressionsWithIntervalArithmetic";
|
---|
[13248] | 37 | private const string CheckExpressionsWithIntervalArithmeticParameterDescription = "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.";
|
---|
[7615] | 38 | private const string EvaluatedSolutionsParameterName = "EvaluatedSolutions";
|
---|
[5571] | 39 |
|
---|
[14282] | 40 | private readonly SymbolicDataAnalysisExpressionTreeInterpreter interpreter;
|
---|
[9776] | 41 |
|
---|
[9732] | 42 | public override bool CanChangeName {
|
---|
| 43 | get { return false; }
|
---|
| 44 | }
|
---|
[5571] | 45 |
|
---|
[9732] | 46 | public override bool CanChangeDescription {
|
---|
| 47 | get { return false; }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[5749] | 50 | #region parameter properties
|
---|
[13248] | 51 | public IFixedValueParameter<BoolValue> CheckExpressionsWithIntervalArithmeticParameter {
|
---|
| 52 | get { return (IFixedValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName]; }
|
---|
[5749] | 53 | }
|
---|
[7615] | 54 |
|
---|
[13248] | 55 | public IFixedValueParameter<IntValue> EvaluatedSolutionsParameter {
|
---|
| 56 | get { return (IFixedValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; }
|
---|
[7615] | 57 | }
|
---|
[5749] | 58 | #endregion
|
---|
| 59 |
|
---|
| 60 | #region properties
|
---|
[13248] | 61 | public bool CheckExpressionsWithIntervalArithmetic {
|
---|
| 62 | get { return CheckExpressionsWithIntervalArithmeticParameter.Value.Value; }
|
---|
| 63 | set { CheckExpressionsWithIntervalArithmeticParameter.Value.Value = value; }
|
---|
[5749] | 64 | }
|
---|
[13248] | 65 | public int EvaluatedSolutions {
|
---|
| 66 | get { return EvaluatedSolutionsParameter.Value.Value; }
|
---|
| 67 | set { EvaluatedSolutionsParameter.Value.Value = value; }
|
---|
[7615] | 68 | }
|
---|
[5749] | 69 | #endregion
|
---|
| 70 |
|
---|
[5571] | 71 | [StorableConstructor]
|
---|
[16565] | 72 | private SymbolicDataAnalysisExpressionTreeLinearInterpreter(StorableConstructorFlag _) : base(_) {
|
---|
[14282] | 73 | interpreter = new SymbolicDataAnalysisExpressionTreeInterpreter();
|
---|
[9732] | 74 | }
|
---|
| 75 |
|
---|
[9828] | 76 | private SymbolicDataAnalysisExpressionTreeLinearInterpreter(SymbolicDataAnalysisExpressionTreeLinearInterpreter original, Cloner cloner)
|
---|
[9732] | 77 | : base(original, cloner) {
|
---|
[9828] | 78 | interpreter = cloner.Clone(original.interpreter);
|
---|
[9732] | 79 | }
|
---|
| 80 |
|
---|
[5571] | 81 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[9734] | 82 | return new SymbolicDataAnalysisExpressionTreeLinearInterpreter(this, cloner);
|
---|
[5571] | 83 | }
|
---|
| 84 |
|
---|
[9734] | 85 | public SymbolicDataAnalysisExpressionTreeLinearInterpreter()
|
---|
[9758] | 86 | : base("SymbolicDataAnalysisExpressionTreeLinearInterpreter", "Linear (non-recursive) interpreter for symbolic expression trees (does not support ADFs).") {
|
---|
[13248] | 87 | Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, CheckExpressionsWithIntervalArithmeticParameterDescription, 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)));
|
---|
[9776] | 89 | interpreter = new SymbolicDataAnalysisExpressionTreeInterpreter();
|
---|
[5571] | 90 | }
|
---|
| 91 |
|
---|
[13248] | 92 | public SymbolicDataAnalysisExpressionTreeLinearInterpreter(string name, string description)
|
---|
| 93 | : base(name, description) {
|
---|
| 94 | Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, CheckExpressionsWithIntervalArithmeticParameterDescription, new BoolValue(false)));
|
---|
| 95 | Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
|
---|
| 96 | interpreter = new SymbolicDataAnalysisExpressionTreeInterpreter();
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[7615] | 99 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 100 | private void AfterDeserialization() {
|
---|
[13248] | 101 | var evaluatedSolutions = new IntValue(0);
|
---|
| 102 | var checkExpressionsWithIntervalArithmetic = new BoolValue(false);
|
---|
| 103 | if (Parameters.ContainsKey(EvaluatedSolutionsParameterName)) {
|
---|
| 104 | var evaluatedSolutionsParameter = (IValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName];
|
---|
| 105 | evaluatedSolutions = evaluatedSolutionsParameter.Value;
|
---|
| 106 | Parameters.Remove(EvaluatedSolutionsParameterName);
|
---|
| 107 | }
|
---|
| 108 | Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", evaluatedSolutions));
|
---|
| 109 | if (Parameters.ContainsKey(CheckExpressionsWithIntervalArithmeticParameterName)) {
|
---|
| 110 | var checkExpressionsWithIntervalArithmeticParameter = (IValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName];
|
---|
| 111 | Parameters.Remove(CheckExpressionsWithIntervalArithmeticParameterName);
|
---|
| 112 | checkExpressionsWithIntervalArithmetic = checkExpressionsWithIntervalArithmeticParameter.Value;
|
---|
| 113 | }
|
---|
| 114 | Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, CheckExpressionsWithIntervalArithmeticParameterDescription, checkExpressionsWithIntervalArithmetic));
|
---|
[7615] | 115 | }
|
---|
| 116 |
|
---|
| 117 | #region IStatefulItem
|
---|
| 118 | public void InitializeState() {
|
---|
[13248] | 119 | EvaluatedSolutions = 0;
|
---|
[7615] | 120 | }
|
---|
| 121 |
|
---|
[9828] | 122 | public void ClearState() { }
|
---|
[7615] | 123 | #endregion
|
---|
| 124 |
|
---|
[13251] | 125 | private readonly object syncRoot = new object();
|
---|
[12509] | 126 | public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows) {
|
---|
[14345] | 127 | if (!rows.Any()) return Enumerable.Empty<double>();
|
---|
[13248] | 128 | if (CheckExpressionsWithIntervalArithmetic)
|
---|
[9734] | 129 | throw new NotSupportedException("Interval arithmetic is not yet supported in the symbolic data analysis interpreter.");
|
---|
[7120] | 130 |
|
---|
[13251] | 131 | lock (syncRoot) {
|
---|
[13248] | 132 | EvaluatedSolutions++; // increment the evaluated solutions counter
|
---|
[9004] | 133 | }
|
---|
[8436] | 134 |
|
---|
[9739] | 135 | var code = SymbolicExpressionTreeLinearCompiler.Compile(tree, OpCodes.MapSymbolToOpCode);
|
---|
[9758] | 136 | PrepareInstructions(code, dataset);
|
---|
[9818] | 137 | return rows.Select(row => Evaluate(dataset, row, code));
|
---|
[9739] | 138 | }
|
---|
[9732] | 139 |
|
---|
[12509] | 140 | private double Evaluate(IDataset dataset, int row, LinearInstruction[] code) {
|
---|
[9732] | 141 | for (int i = code.Length - 1; i >= 0; --i) {
|
---|
[9776] | 142 | if (code[i].skip) continue;
|
---|
[9871] | 143 | #region opcode if
|
---|
[9732] | 144 | var instr = code[i];
|
---|
[9871] | 145 | if (instr.opCode == OpCodes.Variable) {
|
---|
| 146 | if (row < 0 || row >= dataset.Rows) instr.value = double.NaN;
|
---|
[13268] | 147 | else {
|
---|
| 148 | var variableTreeNode = (VariableTreeNode)instr.dynamicNode;
|
---|
| 149 | instr.value = ((IList<double>)instr.data)[row] * variableTreeNode.Weight;
|
---|
| 150 | }
|
---|
[14826] | 151 | } else if (instr.opCode == OpCodes.BinaryFactorVariable) {
|
---|
| 152 | if (row < 0 || row >= dataset.Rows) instr.value = double.NaN;
|
---|
| 153 | else {
|
---|
| 154 | var factorTreeNode = instr.dynamicNode as BinaryFactorVariableTreeNode;
|
---|
| 155 | instr.value = ((IList<string>)instr.data)[row] == factorTreeNode.VariableValue ? factorTreeNode.Weight : 0;
|
---|
| 156 | }
|
---|
| 157 | } else if (instr.opCode == OpCodes.FactorVariable) {
|
---|
| 158 | if (row < 0 || row >= dataset.Rows) instr.value = double.NaN;
|
---|
| 159 | else {
|
---|
| 160 | var factorTreeNode = instr.dynamicNode as FactorVariableTreeNode;
|
---|
| 161 | instr.value = factorTreeNode.GetValue(((IList<string>)instr.data)[row]);
|
---|
| 162 | }
|
---|
[9871] | 163 | } else if (instr.opCode == OpCodes.LagVariable) {
|
---|
| 164 | var laggedVariableTreeNode = (LaggedVariableTreeNode)instr.dynamicNode;
|
---|
| 165 | int actualRow = row + laggedVariableTreeNode.Lag;
|
---|
| 166 | if (actualRow < 0 || actualRow >= dataset.Rows)
|
---|
| 167 | instr.value = double.NaN;
|
---|
| 168 | else
|
---|
| 169 | instr.value = ((IList<double>)instr.data)[actualRow] * laggedVariableTreeNode.Weight;
|
---|
| 170 | } else if (instr.opCode == OpCodes.VariableCondition) {
|
---|
| 171 | if (row < 0 || row >= dataset.Rows) instr.value = double.NaN;
|
---|
| 172 | var variableConditionTreeNode = (VariableConditionTreeNode)instr.dynamicNode;
|
---|
[14345] | 173 | if (!variableConditionTreeNode.Symbol.IgnoreSlope) {
|
---|
| 174 | double variableValue = ((IList<double>)instr.data)[row];
|
---|
| 175 | double x = variableValue - variableConditionTreeNode.Threshold;
|
---|
| 176 | double p = 1 / (1 + Math.Exp(-variableConditionTreeNode.Slope * x));
|
---|
[9738] | 177 |
|
---|
[14345] | 178 | double trueBranch = code[instr.childIndex].value;
|
---|
| 179 | double falseBranch = code[instr.childIndex + 1].value;
|
---|
[9738] | 180 |
|
---|
[14345] | 181 | instr.value = trueBranch * p + falseBranch * (1 - p);
|
---|
| 182 | } else {
|
---|
| 183 | double variableValue = ((IList<double>)instr.data)[row];
|
---|
| 184 | if (variableValue <= variableConditionTreeNode.Threshold) {
|
---|
| 185 | instr.value = code[instr.childIndex].value;
|
---|
| 186 | } else {
|
---|
| 187 | instr.value = code[instr.childIndex + 1].value;
|
---|
| 188 | }
|
---|
| 189 | }
|
---|
[9871] | 190 | } else if (instr.opCode == OpCodes.Add) {
|
---|
| 191 | double s = code[instr.childIndex].value;
|
---|
| 192 | for (int j = 1; j != instr.nArguments; ++j) {
|
---|
| 193 | s += code[instr.childIndex + j].value;
|
---|
| 194 | }
|
---|
| 195 | instr.value = s;
|
---|
| 196 | } else if (instr.opCode == OpCodes.Sub) {
|
---|
| 197 | double s = code[instr.childIndex].value;
|
---|
| 198 | for (int j = 1; j != instr.nArguments; ++j) {
|
---|
| 199 | s -= code[instr.childIndex + j].value;
|
---|
| 200 | }
|
---|
| 201 | if (instr.nArguments == 1) s = -s;
|
---|
| 202 | instr.value = s;
|
---|
| 203 | } else if (instr.opCode == OpCodes.Mul) {
|
---|
| 204 | double p = code[instr.childIndex].value;
|
---|
| 205 | for (int j = 1; j != instr.nArguments; ++j) {
|
---|
| 206 | p *= code[instr.childIndex + j].value;
|
---|
| 207 | }
|
---|
| 208 | instr.value = p;
|
---|
| 209 | } else if (instr.opCode == OpCodes.Div) {
|
---|
| 210 | double p = code[instr.childIndex].value;
|
---|
| 211 | for (int j = 1; j != instr.nArguments; ++j) {
|
---|
| 212 | p /= code[instr.childIndex + j].value;
|
---|
| 213 | }
|
---|
| 214 | if (instr.nArguments == 1) p = 1.0 / p;
|
---|
| 215 | instr.value = p;
|
---|
[16360] | 216 | } else if (instr.opCode == OpCodes.AnalyticQuotient) {
|
---|
[16356] | 217 | var x1 = code[instr.childIndex].value;
|
---|
| 218 | var x2 = code[instr.childIndex + 1].value;
|
---|
| 219 | instr.value = x1 / Math.Sqrt(1 + x2 * x2);
|
---|
[9871] | 220 | } else if (instr.opCode == OpCodes.Average) {
|
---|
| 221 | double s = code[instr.childIndex].value;
|
---|
| 222 | for (int j = 1; j != instr.nArguments; ++j) {
|
---|
| 223 | s += code[instr.childIndex + j].value;
|
---|
| 224 | }
|
---|
| 225 | instr.value = s / instr.nArguments;
|
---|
[16356] | 226 | } else if (instr.opCode == OpCodes.Absolute) {
|
---|
| 227 | instr.value = Math.Abs(code[instr.childIndex].value);
|
---|
[16656] | 228 | } else if (instr.opCode == OpCodes.Tanh) {
|
---|
| 229 | instr.value = Math.Tanh(code[instr.childIndex].value);
|
---|
[9871] | 230 | } else if (instr.opCode == OpCodes.Cos) {
|
---|
| 231 | instr.value = Math.Cos(code[instr.childIndex].value);
|
---|
| 232 | } else if (instr.opCode == OpCodes.Sin) {
|
---|
| 233 | instr.value = Math.Sin(code[instr.childIndex].value);
|
---|
| 234 | } else if (instr.opCode == OpCodes.Tan) {
|
---|
| 235 | instr.value = Math.Tan(code[instr.childIndex].value);
|
---|
| 236 | } else if (instr.opCode == OpCodes.Square) {
|
---|
| 237 | instr.value = Math.Pow(code[instr.childIndex].value, 2);
|
---|
[16356] | 238 | } else if (instr.opCode == OpCodes.Cube) {
|
---|
| 239 | instr.value = Math.Pow(code[instr.childIndex].value, 3);
|
---|
[9871] | 240 | } else if (instr.opCode == OpCodes.Power) {
|
---|
| 241 | double x = code[instr.childIndex].value;
|
---|
| 242 | double y = Math.Round(code[instr.childIndex + 1].value);
|
---|
| 243 | instr.value = Math.Pow(x, y);
|
---|
| 244 | } else if (instr.opCode == OpCodes.SquareRoot) {
|
---|
| 245 | instr.value = Math.Sqrt(code[instr.childIndex].value);
|
---|
[16356] | 246 | } else if (instr.opCode == OpCodes.CubeRoot) {
|
---|
[16911] | 247 | var arg = code[instr.childIndex].value;
|
---|
| 248 | instr.value = arg < 0 ? -Math.Pow(-arg, 1.0 / 3.0) : Math.Pow(arg, 1.0 / 3.0);
|
---|
[9871] | 249 | } else if (instr.opCode == OpCodes.Root) {
|
---|
| 250 | double x = code[instr.childIndex].value;
|
---|
[13254] | 251 | double y = Math.Round(code[instr.childIndex + 1].value);
|
---|
[9871] | 252 | instr.value = Math.Pow(x, 1 / y);
|
---|
| 253 | } else if (instr.opCode == OpCodes.Exp) {
|
---|
| 254 | instr.value = Math.Exp(code[instr.childIndex].value);
|
---|
| 255 | } else if (instr.opCode == OpCodes.Log) {
|
---|
| 256 | instr.value = Math.Log(code[instr.childIndex].value);
|
---|
| 257 | } else if (instr.opCode == OpCodes.Gamma) {
|
---|
| 258 | var x = code[instr.childIndex].value;
|
---|
| 259 | instr.value = double.IsNaN(x) ? double.NaN : alglib.gammafunction(x);
|
---|
| 260 | } else if (instr.opCode == OpCodes.Psi) {
|
---|
| 261 | var x = code[instr.childIndex].value;
|
---|
| 262 | if (double.IsNaN(x)) instr.value = double.NaN;
|
---|
| 263 | else if (x <= 0 && (Math.Floor(x) - x).IsAlmost(0)) instr.value = double.NaN;
|
---|
| 264 | else instr.value = alglib.psi(x);
|
---|
| 265 | } else if (instr.opCode == OpCodes.Dawson) {
|
---|
| 266 | var x = code[instr.childIndex].value;
|
---|
| 267 | instr.value = double.IsNaN(x) ? double.NaN : alglib.dawsonintegral(x);
|
---|
| 268 | } else if (instr.opCode == OpCodes.ExponentialIntegralEi) {
|
---|
| 269 | var x = code[instr.childIndex].value;
|
---|
| 270 | instr.value = double.IsNaN(x) ? double.NaN : alglib.exponentialintegralei(x);
|
---|
| 271 | } else if (instr.opCode == OpCodes.SineIntegral) {
|
---|
| 272 | double si, ci;
|
---|
| 273 | var x = code[instr.childIndex].value;
|
---|
| 274 | if (double.IsNaN(x)) instr.value = double.NaN;
|
---|
| 275 | else {
|
---|
| 276 | alglib.sinecosineintegrals(x, out si, out ci);
|
---|
| 277 | instr.value = si;
|
---|
| 278 | }
|
---|
| 279 | } else if (instr.opCode == OpCodes.CosineIntegral) {
|
---|
| 280 | double si, ci;
|
---|
| 281 | var x = code[instr.childIndex].value;
|
---|
| 282 | if (double.IsNaN(x)) instr.value = double.NaN;
|
---|
| 283 | else {
|
---|
| 284 | alglib.sinecosineintegrals(x, out si, out ci);
|
---|
| 285 | instr.value = ci;
|
---|
| 286 | }
|
---|
| 287 | } else if (instr.opCode == OpCodes.HyperbolicSineIntegral) {
|
---|
| 288 | double shi, chi;
|
---|
| 289 | var x = code[instr.childIndex].value;
|
---|
| 290 | if (double.IsNaN(x)) instr.value = double.NaN;
|
---|
| 291 | else {
|
---|
| 292 | alglib.hyperbolicsinecosineintegrals(x, out shi, out chi);
|
---|
| 293 | instr.value = shi;
|
---|
| 294 | }
|
---|
| 295 | } else if (instr.opCode == OpCodes.HyperbolicCosineIntegral) {
|
---|
| 296 | double shi, chi;
|
---|
| 297 | var x = code[instr.childIndex].value;
|
---|
| 298 | if (double.IsNaN(x)) instr.value = double.NaN;
|
---|
| 299 | else {
|
---|
| 300 | alglib.hyperbolicsinecosineintegrals(x, out shi, out chi);
|
---|
| 301 | instr.value = chi;
|
---|
| 302 | }
|
---|
| 303 | } else if (instr.opCode == OpCodes.FresnelCosineIntegral) {
|
---|
| 304 | double c = 0, s = 0;
|
---|
| 305 | var x = code[instr.childIndex].value;
|
---|
| 306 | if (double.IsNaN(x)) instr.value = double.NaN;
|
---|
| 307 | else {
|
---|
| 308 | alglib.fresnelintegral(x, ref c, ref s);
|
---|
| 309 | instr.value = c;
|
---|
| 310 | }
|
---|
| 311 | } else if (instr.opCode == OpCodes.FresnelSineIntegral) {
|
---|
| 312 | double c = 0, s = 0;
|
---|
| 313 | var x = code[instr.childIndex].value;
|
---|
| 314 | if (double.IsNaN(x)) instr.value = double.NaN;
|
---|
| 315 | else {
|
---|
| 316 | alglib.fresnelintegral(x, ref c, ref s);
|
---|
| 317 | instr.value = s;
|
---|
| 318 | }
|
---|
| 319 | } else if (instr.opCode == OpCodes.AiryA) {
|
---|
| 320 | double ai, aip, bi, bip;
|
---|
| 321 | var x = code[instr.childIndex].value;
|
---|
| 322 | if (double.IsNaN(x)) instr.value = double.NaN;
|
---|
| 323 | else {
|
---|
| 324 | alglib.airy(x, out ai, out aip, out bi, out bip);
|
---|
| 325 | instr.value = ai;
|
---|
| 326 | }
|
---|
| 327 | } else if (instr.opCode == OpCodes.AiryB) {
|
---|
| 328 | double ai, aip, bi, bip;
|
---|
| 329 | var x = code[instr.childIndex].value;
|
---|
| 330 | if (double.IsNaN(x)) instr.value = double.NaN;
|
---|
| 331 | else {
|
---|
| 332 | alglib.airy(x, out ai, out aip, out bi, out bip);
|
---|
| 333 | instr.value = bi;
|
---|
| 334 | }
|
---|
| 335 | } else if (instr.opCode == OpCodes.Norm) {
|
---|
| 336 | var x = code[instr.childIndex].value;
|
---|
| 337 | if (double.IsNaN(x)) instr.value = double.NaN;
|
---|
| 338 | else instr.value = alglib.normaldistribution(x);
|
---|
| 339 | } else if (instr.opCode == OpCodes.Erf) {
|
---|
| 340 | var x = code[instr.childIndex].value;
|
---|
| 341 | if (double.IsNaN(x)) instr.value = double.NaN;
|
---|
| 342 | else instr.value = alglib.errorfunction(x);
|
---|
| 343 | } else if (instr.opCode == OpCodes.Bessel) {
|
---|
| 344 | var x = code[instr.childIndex].value;
|
---|
| 345 | if (double.IsNaN(x)) instr.value = double.NaN;
|
---|
| 346 | else instr.value = alglib.besseli0(x);
|
---|
| 347 | } else if (instr.opCode == OpCodes.IfThenElse) {
|
---|
| 348 | double condition = code[instr.childIndex].value;
|
---|
| 349 | double result;
|
---|
| 350 | if (condition > 0.0) {
|
---|
| 351 | result = code[instr.childIndex + 1].value;
|
---|
| 352 | } else {
|
---|
| 353 | result = code[instr.childIndex + 2].value;
|
---|
| 354 | }
|
---|
| 355 | instr.value = result;
|
---|
| 356 | } else if (instr.opCode == OpCodes.AND) {
|
---|
| 357 | double result = code[instr.childIndex].value;
|
---|
| 358 | for (int j = 1; j < instr.nArguments; j++) {
|
---|
| 359 | if (result > 0.0) result = code[instr.childIndex + j].value;
|
---|
| 360 | else break;
|
---|
| 361 | }
|
---|
| 362 | instr.value = result > 0.0 ? 1.0 : -1.0;
|
---|
| 363 | } else if (instr.opCode == OpCodes.OR) {
|
---|
| 364 | double result = code[instr.childIndex].value;
|
---|
| 365 | for (int j = 1; j < instr.nArguments; j++) {
|
---|
| 366 | if (result <= 0.0) result = code[instr.childIndex + j].value;
|
---|
| 367 | else break;
|
---|
| 368 | }
|
---|
| 369 | instr.value = result > 0.0 ? 1.0 : -1.0;
|
---|
| 370 | } else if (instr.opCode == OpCodes.NOT) {
|
---|
| 371 | instr.value = code[instr.childIndex].value > 0.0 ? -1.0 : 1.0;
|
---|
[10774] | 372 | } else if (instr.opCode == OpCodes.XOR) {
|
---|
[10788] | 373 | int positiveSignals = 0;
|
---|
| 374 | for (int j = 0; j < instr.nArguments; j++) {
|
---|
| 375 | if (code[instr.childIndex + j].value > 0.0) positiveSignals++;
|
---|
[10774] | 376 | }
|
---|
[10788] | 377 | instr.value = positiveSignals % 2 != 0 ? 1.0 : -1.0;
|
---|
[9871] | 378 | } else if (instr.opCode == OpCodes.GT) {
|
---|
| 379 | double x = code[instr.childIndex].value;
|
---|
| 380 | double y = code[instr.childIndex + 1].value;
|
---|
| 381 | instr.value = x > y ? 1.0 : -1.0;
|
---|
| 382 | } else if (instr.opCode == OpCodes.LT) {
|
---|
| 383 | double x = code[instr.childIndex].value;
|
---|
| 384 | double y = code[instr.childIndex + 1].value;
|
---|
| 385 | instr.value = x < y ? 1.0 : -1.0;
|
---|
| 386 | } else if (instr.opCode == OpCodes.TimeLag || instr.opCode == OpCodes.Derivative || instr.opCode == OpCodes.Integral) {
|
---|
| 387 | var state = (InterpreterState)instr.data;
|
---|
| 388 | state.Reset();
|
---|
| 389 | instr.value = interpreter.Evaluate(dataset, ref row, state);
|
---|
| 390 | } else {
|
---|
| 391 | var errorText = string.Format("The {0} symbol is not supported by the linear interpreter. To support this symbol, please use the SymbolicDataAnalysisExpressionTreeInterpreter.", instr.dynamicNode.Symbol.Name);
|
---|
| 392 | throw new NotSupportedException(errorText);
|
---|
[9271] | 393 | }
|
---|
[9739] | 394 | #endregion
|
---|
[5571] | 395 | }
|
---|
[9739] | 396 | return code[0].value;
|
---|
[5571] | 397 | }
|
---|
[9815] | 398 |
|
---|
| 399 | private static LinearInstruction[] GetPrefixSequence(LinearInstruction[] code, int startIndex) {
|
---|
[9944] | 400 | var s = new Stack<int>();
|
---|
[9815] | 401 | var list = new List<LinearInstruction>();
|
---|
[9944] | 402 | s.Push(startIndex);
|
---|
| 403 | while (s.Any()) {
|
---|
| 404 | int i = s.Pop();
|
---|
[9815] | 405 | var instr = code[i];
|
---|
[9944] | 406 | // push instructions in reverse execution order
|
---|
| 407 | for (int j = instr.nArguments - 1; j >= 0; j--) s.Push(instr.childIndex + j);
|
---|
[9815] | 408 | list.Add(instr);
|
---|
| 409 | }
|
---|
| 410 | return list.ToArray();
|
---|
| 411 | }
|
---|
| 412 |
|
---|
[12509] | 413 | public static void PrepareInstructions(LinearInstruction[] code, IDataset dataset) {
|
---|
[9815] | 414 | for (int i = 0; i != code.Length; ++i) {
|
---|
| 415 | var instr = code[i];
|
---|
| 416 | #region opcode switch
|
---|
| 417 | switch (instr.opCode) {
|
---|
| 418 | case OpCodes.Constant: {
|
---|
| 419 | var constTreeNode = (ConstantTreeNode)instr.dynamicNode;
|
---|
| 420 | instr.value = constTreeNode.Value;
|
---|
| 421 | instr.skip = true; // the value is already set so this instruction should be skipped in the evaluation phase
|
---|
| 422 | }
|
---|
| 423 | break;
|
---|
| 424 | case OpCodes.Variable: {
|
---|
| 425 | var variableTreeNode = (VariableTreeNode)instr.dynamicNode;
|
---|
[9826] | 426 | instr.data = dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName);
|
---|
[9815] | 427 | }
|
---|
| 428 | break;
|
---|
[14826] | 429 | case OpCodes.BinaryFactorVariable: {
|
---|
| 430 | var factorVariableTreeNode = instr.dynamicNode as BinaryFactorVariableTreeNode;
|
---|
| 431 | instr.data = dataset.GetReadOnlyStringValues(factorVariableTreeNode.VariableName);
|
---|
| 432 | }
|
---|
| 433 | break;
|
---|
| 434 | case OpCodes.FactorVariable: {
|
---|
| 435 | var factorVariableTreeNode = instr.dynamicNode as FactorVariableTreeNode;
|
---|
| 436 | instr.data = dataset.GetReadOnlyStringValues(factorVariableTreeNode.VariableName);
|
---|
| 437 | }
|
---|
| 438 | break;
|
---|
[9815] | 439 | case OpCodes.LagVariable: {
|
---|
| 440 | var laggedVariableTreeNode = (LaggedVariableTreeNode)instr.dynamicNode;
|
---|
[9826] | 441 | instr.data = dataset.GetReadOnlyDoubleValues(laggedVariableTreeNode.VariableName);
|
---|
[9815] | 442 | }
|
---|
| 443 | break;
|
---|
| 444 | case OpCodes.VariableCondition: {
|
---|
| 445 | var variableConditionTreeNode = (VariableConditionTreeNode)instr.dynamicNode;
|
---|
[9826] | 446 | instr.data = dataset.GetReadOnlyDoubleValues(variableConditionTreeNode.VariableName);
|
---|
[9815] | 447 | }
|
---|
| 448 | break;
|
---|
| 449 | case OpCodes.TimeLag:
|
---|
| 450 | case OpCodes.Integral:
|
---|
| 451 | case OpCodes.Derivative: {
|
---|
| 452 | var seq = GetPrefixSequence(code, i);
|
---|
| 453 | var interpreterState = new InterpreterState(seq, 0);
|
---|
[9826] | 454 | instr.data = interpreterState;
|
---|
[9815] | 455 | for (int j = 1; j != seq.Length; ++j)
|
---|
| 456 | seq[j].skip = true;
|
---|
[14345] | 457 | break;
|
---|
[9815] | 458 | }
|
---|
| 459 | }
|
---|
| 460 | #endregion
|
---|
| 461 | }
|
---|
| 462 | }
|
---|
[5571] | 463 | }
|
---|
| 464 | }
|
---|