[5571] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14185] | 3 | * Copyright (C) 2002-2016 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;
|
---|
[5571] | 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
| 33 | [StorableClass]
|
---|
[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]
|
---|
[9758] | 72 | private SymbolicDataAnalysisExpressionTreeLinearInterpreter(bool deserializing)
|
---|
[9732] | 73 | : base(deserializing) {
|
---|
[14282] | 74 | interpreter = new SymbolicDataAnalysisExpressionTreeInterpreter();
|
---|
[9732] | 75 | }
|
---|
| 76 |
|
---|
[9828] | 77 | private SymbolicDataAnalysisExpressionTreeLinearInterpreter(SymbolicDataAnalysisExpressionTreeLinearInterpreter original, Cloner cloner)
|
---|
[9732] | 78 | : base(original, cloner) {
|
---|
[9828] | 79 | interpreter = cloner.Clone(original.interpreter);
|
---|
[9732] | 80 | }
|
---|
| 81 |
|
---|
[5571] | 82 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[9734] | 83 | return new SymbolicDataAnalysisExpressionTreeLinearInterpreter(this, cloner);
|
---|
[5571] | 84 | }
|
---|
| 85 |
|
---|
[9734] | 86 | public SymbolicDataAnalysisExpressionTreeLinearInterpreter()
|
---|
[9758] | 87 | : base("SymbolicDataAnalysisExpressionTreeLinearInterpreter", "Linear (non-recursive) interpreter for symbolic expression trees (does not support ADFs).") {
|
---|
[13248] | 88 | Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, CheckExpressionsWithIntervalArithmeticParameterDescription, new BoolValue(false)));
|
---|
| 89 | Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
|
---|
[9776] | 90 | interpreter = new SymbolicDataAnalysisExpressionTreeInterpreter();
|
---|
[5571] | 91 | }
|
---|
| 92 |
|
---|
[13248] | 93 | public SymbolicDataAnalysisExpressionTreeLinearInterpreter(string name, string description)
|
---|
| 94 | : base(name, description) {
|
---|
| 95 | Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, CheckExpressionsWithIntervalArithmeticParameterDescription, new BoolValue(false)));
|
---|
| 96 | Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
|
---|
| 97 | interpreter = new SymbolicDataAnalysisExpressionTreeInterpreter();
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[7615] | 100 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 101 | private void AfterDeserialization() {
|
---|
[13248] | 102 | var evaluatedSolutions = new IntValue(0);
|
---|
| 103 | var checkExpressionsWithIntervalArithmetic = new BoolValue(false);
|
---|
| 104 | if (Parameters.ContainsKey(EvaluatedSolutionsParameterName)) {
|
---|
| 105 | var evaluatedSolutionsParameter = (IValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName];
|
---|
| 106 | evaluatedSolutions = evaluatedSolutionsParameter.Value;
|
---|
| 107 | Parameters.Remove(EvaluatedSolutionsParameterName);
|
---|
| 108 | }
|
---|
| 109 | Parameters.Add(new FixedValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", evaluatedSolutions));
|
---|
| 110 | if (Parameters.ContainsKey(CheckExpressionsWithIntervalArithmeticParameterName)) {
|
---|
| 111 | var checkExpressionsWithIntervalArithmeticParameter = (IValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName];
|
---|
| 112 | Parameters.Remove(CheckExpressionsWithIntervalArithmeticParameterName);
|
---|
| 113 | checkExpressionsWithIntervalArithmetic = checkExpressionsWithIntervalArithmeticParameter.Value;
|
---|
| 114 | }
|
---|
| 115 | Parameters.Add(new FixedValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, CheckExpressionsWithIntervalArithmeticParameterDescription, checkExpressionsWithIntervalArithmetic));
|
---|
[7615] | 116 | }
|
---|
| 117 |
|
---|
| 118 | #region IStatefulItem
|
---|
| 119 | public void InitializeState() {
|
---|
[13248] | 120 | EvaluatedSolutions = 0;
|
---|
[7615] | 121 | }
|
---|
| 122 |
|
---|
[9828] | 123 | public void ClearState() { }
|
---|
[7615] | 124 | #endregion
|
---|
| 125 |
|
---|
[13251] | 126 | private readonly object syncRoot = new object();
|
---|
[12509] | 127 | public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows) {
|
---|
[14345] | 128 | if (!rows.Any()) return Enumerable.Empty<double>();
|
---|
[13248] | 129 | if (CheckExpressionsWithIntervalArithmetic)
|
---|
[9734] | 130 | throw new NotSupportedException("Interval arithmetic is not yet supported in the symbolic data analysis interpreter.");
|
---|
[7120] | 131 |
|
---|
[13251] | 132 | lock (syncRoot) {
|
---|
[13248] | 133 | EvaluatedSolutions++; // increment the evaluated solutions counter
|
---|
[9004] | 134 | }
|
---|
[8436] | 135 |
|
---|
[9739] | 136 | var code = SymbolicExpressionTreeLinearCompiler.Compile(tree, OpCodes.MapSymbolToOpCode);
|
---|
[9758] | 137 | PrepareInstructions(code, dataset);
|
---|
[9818] | 138 | return rows.Select(row => Evaluate(dataset, row, code));
|
---|
[9739] | 139 | }
|
---|
[9732] | 140 |
|
---|
[12509] | 141 | private double Evaluate(IDataset dataset, int row, LinearInstruction[] code) {
|
---|
[9732] | 142 | for (int i = code.Length - 1; i >= 0; --i) {
|
---|
[9776] | 143 | if (code[i].skip) continue;
|
---|
[9871] | 144 | #region opcode if
|
---|
[9732] | 145 | var instr = code[i];
|
---|
[9871] | 146 | if (instr.opCode == OpCodes.Variable) {
|
---|
| 147 | if (row < 0 || row >= dataset.Rows) instr.value = double.NaN;
|
---|
[13268] | 148 | else {
|
---|
| 149 | var variableTreeNode = (VariableTreeNode)instr.dynamicNode;
|
---|
| 150 | instr.value = ((IList<double>)instr.data)[row] * variableTreeNode.Weight;
|
---|
| 151 | }
|
---|
[9871] | 152 | } else if (instr.opCode == OpCodes.LagVariable) {
|
---|
| 153 | var laggedVariableTreeNode = (LaggedVariableTreeNode)instr.dynamicNode;
|
---|
| 154 | int actualRow = row + laggedVariableTreeNode.Lag;
|
---|
| 155 | if (actualRow < 0 || actualRow >= dataset.Rows)
|
---|
| 156 | instr.value = double.NaN;
|
---|
| 157 | else
|
---|
| 158 | instr.value = ((IList<double>)instr.data)[actualRow] * laggedVariableTreeNode.Weight;
|
---|
| 159 | } else if (instr.opCode == OpCodes.VariableCondition) {
|
---|
| 160 | if (row < 0 || row >= dataset.Rows) instr.value = double.NaN;
|
---|
| 161 | var variableConditionTreeNode = (VariableConditionTreeNode)instr.dynamicNode;
|
---|
[14345] | 162 | if (!variableConditionTreeNode.Symbol.IgnoreSlope) {
|
---|
| 163 | double variableValue = ((IList<double>)instr.data)[row];
|
---|
| 164 | double x = variableValue - variableConditionTreeNode.Threshold;
|
---|
| 165 | double p = 1 / (1 + Math.Exp(-variableConditionTreeNode.Slope * x));
|
---|
[9738] | 166 |
|
---|
[14345] | 167 | double trueBranch = code[instr.childIndex].value;
|
---|
| 168 | double falseBranch = code[instr.childIndex + 1].value;
|
---|
[9738] | 169 |
|
---|
[14345] | 170 | instr.value = trueBranch * p + falseBranch * (1 - p);
|
---|
| 171 | } else {
|
---|
| 172 | double variableValue = ((IList<double>)instr.data)[row];
|
---|
| 173 | if (variableValue <= variableConditionTreeNode.Threshold) {
|
---|
| 174 | instr.value = code[instr.childIndex].value;
|
---|
| 175 | } else {
|
---|
| 176 | instr.value = code[instr.childIndex + 1].value;
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
[9871] | 179 | } else if (instr.opCode == OpCodes.Add) {
|
---|
| 180 | double s = code[instr.childIndex].value;
|
---|
| 181 | for (int j = 1; j != instr.nArguments; ++j) {
|
---|
| 182 | s += code[instr.childIndex + j].value;
|
---|
| 183 | }
|
---|
| 184 | instr.value = s;
|
---|
| 185 | } else if (instr.opCode == OpCodes.Sub) {
|
---|
| 186 | double s = code[instr.childIndex].value;
|
---|
| 187 | for (int j = 1; j != instr.nArguments; ++j) {
|
---|
| 188 | s -= code[instr.childIndex + j].value;
|
---|
| 189 | }
|
---|
| 190 | if (instr.nArguments == 1) s = -s;
|
---|
| 191 | instr.value = s;
|
---|
| 192 | } else if (instr.opCode == OpCodes.Mul) {
|
---|
| 193 | double p = code[instr.childIndex].value;
|
---|
| 194 | for (int j = 1; j != instr.nArguments; ++j) {
|
---|
| 195 | p *= code[instr.childIndex + j].value;
|
---|
| 196 | }
|
---|
| 197 | instr.value = p;
|
---|
| 198 | } else if (instr.opCode == OpCodes.Div) {
|
---|
| 199 | double p = code[instr.childIndex].value;
|
---|
| 200 | for (int j = 1; j != instr.nArguments; ++j) {
|
---|
| 201 | p /= code[instr.childIndex + j].value;
|
---|
| 202 | }
|
---|
| 203 | if (instr.nArguments == 1) p = 1.0 / p;
|
---|
| 204 | instr.value = p;
|
---|
| 205 | } else if (instr.opCode == OpCodes.Average) {
|
---|
| 206 | double s = code[instr.childIndex].value;
|
---|
| 207 | for (int j = 1; j != instr.nArguments; ++j) {
|
---|
| 208 | s += code[instr.childIndex + j].value;
|
---|
| 209 | }
|
---|
| 210 | instr.value = s / instr.nArguments;
|
---|
| 211 | } else if (instr.opCode == OpCodes.Cos) {
|
---|
| 212 | instr.value = Math.Cos(code[instr.childIndex].value);
|
---|
| 213 | } else if (instr.opCode == OpCodes.Sin) {
|
---|
| 214 | instr.value = Math.Sin(code[instr.childIndex].value);
|
---|
| 215 | } else if (instr.opCode == OpCodes.Tan) {
|
---|
| 216 | instr.value = Math.Tan(code[instr.childIndex].value);
|
---|
| 217 | } else if (instr.opCode == OpCodes.Square) {
|
---|
| 218 | instr.value = Math.Pow(code[instr.childIndex].value, 2);
|
---|
| 219 | } else if (instr.opCode == OpCodes.Power) {
|
---|
| 220 | double x = code[instr.childIndex].value;
|
---|
| 221 | double y = Math.Round(code[instr.childIndex + 1].value);
|
---|
| 222 | instr.value = Math.Pow(x, y);
|
---|
| 223 | } else if (instr.opCode == OpCodes.SquareRoot) {
|
---|
| 224 | instr.value = Math.Sqrt(code[instr.childIndex].value);
|
---|
| 225 | } else if (instr.opCode == OpCodes.Root) {
|
---|
| 226 | double x = code[instr.childIndex].value;
|
---|
[13254] | 227 | double y = Math.Round(code[instr.childIndex + 1].value);
|
---|
[9871] | 228 | instr.value = Math.Pow(x, 1 / y);
|
---|
| 229 | } else if (instr.opCode == OpCodes.Exp) {
|
---|
| 230 | instr.value = Math.Exp(code[instr.childIndex].value);
|
---|
| 231 | } else if (instr.opCode == OpCodes.Log) {
|
---|
| 232 | instr.value = Math.Log(code[instr.childIndex].value);
|
---|
| 233 | } else if (instr.opCode == OpCodes.Gamma) {
|
---|
| 234 | var x = code[instr.childIndex].value;
|
---|
| 235 | instr.value = double.IsNaN(x) ? double.NaN : alglib.gammafunction(x);
|
---|
| 236 | } else if (instr.opCode == OpCodes.Psi) {
|
---|
| 237 | var x = code[instr.childIndex].value;
|
---|
| 238 | if (double.IsNaN(x)) instr.value = double.NaN;
|
---|
| 239 | else if (x <= 0 && (Math.Floor(x) - x).IsAlmost(0)) instr.value = double.NaN;
|
---|
| 240 | else instr.value = alglib.psi(x);
|
---|
| 241 | } else if (instr.opCode == OpCodes.Dawson) {
|
---|
| 242 | var x = code[instr.childIndex].value;
|
---|
| 243 | instr.value = double.IsNaN(x) ? double.NaN : alglib.dawsonintegral(x);
|
---|
| 244 | } else if (instr.opCode == OpCodes.ExponentialIntegralEi) {
|
---|
| 245 | var x = code[instr.childIndex].value;
|
---|
| 246 | instr.value = double.IsNaN(x) ? double.NaN : alglib.exponentialintegralei(x);
|
---|
| 247 | } else if (instr.opCode == OpCodes.SineIntegral) {
|
---|
| 248 | double si, ci;
|
---|
| 249 | var x = code[instr.childIndex].value;
|
---|
| 250 | if (double.IsNaN(x)) instr.value = double.NaN;
|
---|
| 251 | else {
|
---|
| 252 | alglib.sinecosineintegrals(x, out si, out ci);
|
---|
| 253 | instr.value = si;
|
---|
| 254 | }
|
---|
| 255 | } else if (instr.opCode == OpCodes.CosineIntegral) {
|
---|
| 256 | double si, ci;
|
---|
| 257 | var x = code[instr.childIndex].value;
|
---|
| 258 | if (double.IsNaN(x)) instr.value = double.NaN;
|
---|
| 259 | else {
|
---|
| 260 | alglib.sinecosineintegrals(x, out si, out ci);
|
---|
| 261 | instr.value = ci;
|
---|
| 262 | }
|
---|
| 263 | } else if (instr.opCode == OpCodes.HyperbolicSineIntegral) {
|
---|
| 264 | double shi, chi;
|
---|
| 265 | var x = code[instr.childIndex].value;
|
---|
| 266 | if (double.IsNaN(x)) instr.value = double.NaN;
|
---|
| 267 | else {
|
---|
| 268 | alglib.hyperbolicsinecosineintegrals(x, out shi, out chi);
|
---|
| 269 | instr.value = shi;
|
---|
| 270 | }
|
---|
| 271 | } else if (instr.opCode == OpCodes.HyperbolicCosineIntegral) {
|
---|
| 272 | double shi, chi;
|
---|
| 273 | var x = code[instr.childIndex].value;
|
---|
| 274 | if (double.IsNaN(x)) instr.value = double.NaN;
|
---|
| 275 | else {
|
---|
| 276 | alglib.hyperbolicsinecosineintegrals(x, out shi, out chi);
|
---|
| 277 | instr.value = chi;
|
---|
| 278 | }
|
---|
| 279 | } else if (instr.opCode == OpCodes.FresnelCosineIntegral) {
|
---|
| 280 | double c = 0, s = 0;
|
---|
| 281 | var x = code[instr.childIndex].value;
|
---|
| 282 | if (double.IsNaN(x)) instr.value = double.NaN;
|
---|
| 283 | else {
|
---|
| 284 | alglib.fresnelintegral(x, ref c, ref s);
|
---|
| 285 | instr.value = c;
|
---|
| 286 | }
|
---|
| 287 | } else if (instr.opCode == OpCodes.FresnelSineIntegral) {
|
---|
| 288 | double c = 0, s = 0;
|
---|
| 289 | var x = code[instr.childIndex].value;
|
---|
| 290 | if (double.IsNaN(x)) instr.value = double.NaN;
|
---|
| 291 | else {
|
---|
| 292 | alglib.fresnelintegral(x, ref c, ref s);
|
---|
| 293 | instr.value = s;
|
---|
| 294 | }
|
---|
| 295 | } else if (instr.opCode == OpCodes.AiryA) {
|
---|
| 296 | double ai, aip, bi, bip;
|
---|
| 297 | var x = code[instr.childIndex].value;
|
---|
| 298 | if (double.IsNaN(x)) instr.value = double.NaN;
|
---|
| 299 | else {
|
---|
| 300 | alglib.airy(x, out ai, out aip, out bi, out bip);
|
---|
| 301 | instr.value = ai;
|
---|
| 302 | }
|
---|
| 303 | } else if (instr.opCode == OpCodes.AiryB) {
|
---|
| 304 | double ai, aip, bi, bip;
|
---|
| 305 | var x = code[instr.childIndex].value;
|
---|
| 306 | if (double.IsNaN(x)) instr.value = double.NaN;
|
---|
| 307 | else {
|
---|
| 308 | alglib.airy(x, out ai, out aip, out bi, out bip);
|
---|
| 309 | instr.value = bi;
|
---|
| 310 | }
|
---|
| 311 | } else if (instr.opCode == OpCodes.Norm) {
|
---|
| 312 | var x = code[instr.childIndex].value;
|
---|
| 313 | if (double.IsNaN(x)) instr.value = double.NaN;
|
---|
| 314 | else instr.value = alglib.normaldistribution(x);
|
---|
| 315 | } else if (instr.opCode == OpCodes.Erf) {
|
---|
| 316 | var x = code[instr.childIndex].value;
|
---|
| 317 | if (double.IsNaN(x)) instr.value = double.NaN;
|
---|
| 318 | else instr.value = alglib.errorfunction(x);
|
---|
| 319 | } else if (instr.opCode == OpCodes.Bessel) {
|
---|
| 320 | var x = code[instr.childIndex].value;
|
---|
| 321 | if (double.IsNaN(x)) instr.value = double.NaN;
|
---|
| 322 | else instr.value = alglib.besseli0(x);
|
---|
| 323 | } else if (instr.opCode == OpCodes.IfThenElse) {
|
---|
| 324 | double condition = code[instr.childIndex].value;
|
---|
| 325 | double result;
|
---|
| 326 | if (condition > 0.0) {
|
---|
| 327 | result = code[instr.childIndex + 1].value;
|
---|
| 328 | } else {
|
---|
| 329 | result = code[instr.childIndex + 2].value;
|
---|
| 330 | }
|
---|
| 331 | instr.value = result;
|
---|
| 332 | } else if (instr.opCode == OpCodes.AND) {
|
---|
| 333 | double result = code[instr.childIndex].value;
|
---|
| 334 | for (int j = 1; j < instr.nArguments; j++) {
|
---|
| 335 | if (result > 0.0) result = code[instr.childIndex + j].value;
|
---|
| 336 | else break;
|
---|
| 337 | }
|
---|
| 338 | instr.value = result > 0.0 ? 1.0 : -1.0;
|
---|
| 339 | } else if (instr.opCode == OpCodes.OR) {
|
---|
| 340 | double result = code[instr.childIndex].value;
|
---|
| 341 | for (int j = 1; j < instr.nArguments; j++) {
|
---|
| 342 | if (result <= 0.0) result = code[instr.childIndex + j].value;
|
---|
| 343 | else break;
|
---|
| 344 | }
|
---|
| 345 | instr.value = result > 0.0 ? 1.0 : -1.0;
|
---|
| 346 | } else if (instr.opCode == OpCodes.NOT) {
|
---|
| 347 | instr.value = code[instr.childIndex].value > 0.0 ? -1.0 : 1.0;
|
---|
[10774] | 348 | } else if (instr.opCode == OpCodes.XOR) {
|
---|
[10788] | 349 | int positiveSignals = 0;
|
---|
| 350 | for (int j = 0; j < instr.nArguments; j++) {
|
---|
| 351 | if (code[instr.childIndex + j].value > 0.0) positiveSignals++;
|
---|
[10774] | 352 | }
|
---|
[10788] | 353 | instr.value = positiveSignals % 2 != 0 ? 1.0 : -1.0;
|
---|
[9871] | 354 | } else if (instr.opCode == OpCodes.GT) {
|
---|
| 355 | double x = code[instr.childIndex].value;
|
---|
| 356 | double y = code[instr.childIndex + 1].value;
|
---|
| 357 | instr.value = x > y ? 1.0 : -1.0;
|
---|
| 358 | } else if (instr.opCode == OpCodes.LT) {
|
---|
| 359 | double x = code[instr.childIndex].value;
|
---|
| 360 | double y = code[instr.childIndex + 1].value;
|
---|
| 361 | instr.value = x < y ? 1.0 : -1.0;
|
---|
| 362 | } else if (instr.opCode == OpCodes.TimeLag || instr.opCode == OpCodes.Derivative || instr.opCode == OpCodes.Integral) {
|
---|
| 363 | var state = (InterpreterState)instr.data;
|
---|
| 364 | state.Reset();
|
---|
| 365 | instr.value = interpreter.Evaluate(dataset, ref row, state);
|
---|
| 366 | } else {
|
---|
| 367 | 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);
|
---|
| 368 | throw new NotSupportedException(errorText);
|
---|
[9271] | 369 | }
|
---|
[9739] | 370 | #endregion
|
---|
[5571] | 371 | }
|
---|
[9739] | 372 | return code[0].value;
|
---|
[5571] | 373 | }
|
---|
[9815] | 374 |
|
---|
| 375 | private static LinearInstruction[] GetPrefixSequence(LinearInstruction[] code, int startIndex) {
|
---|
[9944] | 376 | var s = new Stack<int>();
|
---|
[9815] | 377 | var list = new List<LinearInstruction>();
|
---|
[9944] | 378 | s.Push(startIndex);
|
---|
| 379 | while (s.Any()) {
|
---|
| 380 | int i = s.Pop();
|
---|
[9815] | 381 | var instr = code[i];
|
---|
[9944] | 382 | // push instructions in reverse execution order
|
---|
| 383 | for (int j = instr.nArguments - 1; j >= 0; j--) s.Push(instr.childIndex + j);
|
---|
[9815] | 384 | list.Add(instr);
|
---|
| 385 | }
|
---|
| 386 | return list.ToArray();
|
---|
| 387 | }
|
---|
| 388 |
|
---|
[12509] | 389 | public static void PrepareInstructions(LinearInstruction[] code, IDataset dataset) {
|
---|
[9815] | 390 | for (int i = 0; i != code.Length; ++i) {
|
---|
| 391 | var instr = code[i];
|
---|
| 392 | #region opcode switch
|
---|
| 393 | switch (instr.opCode) {
|
---|
| 394 | case OpCodes.Constant: {
|
---|
| 395 | var constTreeNode = (ConstantTreeNode)instr.dynamicNode;
|
---|
| 396 | instr.value = constTreeNode.Value;
|
---|
| 397 | instr.skip = true; // the value is already set so this instruction should be skipped in the evaluation phase
|
---|
| 398 | }
|
---|
| 399 | break;
|
---|
| 400 | case OpCodes.Variable: {
|
---|
| 401 | var variableTreeNode = (VariableTreeNode)instr.dynamicNode;
|
---|
[9826] | 402 | instr.data = dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName);
|
---|
[9815] | 403 | }
|
---|
| 404 | break;
|
---|
| 405 | case OpCodes.LagVariable: {
|
---|
| 406 | var laggedVariableTreeNode = (LaggedVariableTreeNode)instr.dynamicNode;
|
---|
[9826] | 407 | instr.data = dataset.GetReadOnlyDoubleValues(laggedVariableTreeNode.VariableName);
|
---|
[9815] | 408 | }
|
---|
| 409 | break;
|
---|
| 410 | case OpCodes.VariableCondition: {
|
---|
| 411 | var variableConditionTreeNode = (VariableConditionTreeNode)instr.dynamicNode;
|
---|
[9826] | 412 | instr.data = dataset.GetReadOnlyDoubleValues(variableConditionTreeNode.VariableName);
|
---|
[9815] | 413 | }
|
---|
| 414 | break;
|
---|
| 415 | case OpCodes.TimeLag:
|
---|
| 416 | case OpCodes.Integral:
|
---|
| 417 | case OpCodes.Derivative: {
|
---|
| 418 | var seq = GetPrefixSequence(code, i);
|
---|
| 419 | var interpreterState = new InterpreterState(seq, 0);
|
---|
[9826] | 420 | instr.data = interpreterState;
|
---|
[9815] | 421 | for (int j = 1; j != seq.Length; ++j)
|
---|
| 422 | seq[j].skip = true;
|
---|
[14345] | 423 | break;
|
---|
[9815] | 424 | }
|
---|
| 425 | }
|
---|
| 426 | #endregion
|
---|
| 427 | }
|
---|
| 428 | }
|
---|
[5571] | 429 | }
|
---|
| 430 | }
|
---|