[8798] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11171] | 3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[8798] | 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;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis {
|
---|
| 33 | [StorableClass]
|
---|
| 34 | [Item("SymbolicTimeSeriesPrognosisInterpreter", "Interpreter for symbolic expression trees including automatically defined functions.")]
|
---|
| 35 | public sealed class SymbolicTimeSeriesPrognosisExpressionTreeInterpreter : SymbolicDataAnalysisExpressionTreeInterpreter, ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter {
|
---|
| 36 | private const string TargetVariableParameterName = "TargetVariable";
|
---|
| 37 |
|
---|
| 38 | public IFixedValueParameter<StringValue> TargetVariableParameter {
|
---|
| 39 | get { return (IFixedValueParameter<StringValue>)Parameters[TargetVariableParameterName]; }
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | public string TargetVariable {
|
---|
| 43 | get { return TargetVariableParameter.Value.Value; }
|
---|
| 44 | set { TargetVariableParameter.Value.Value = value; }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | [ThreadStatic]
|
---|
| 48 | private static double[] targetVariableCache;
|
---|
| 49 | [ThreadStatic]
|
---|
| 50 | private static List<int> invalidateCacheIndexes;
|
---|
| 51 |
|
---|
| 52 | [StorableConstructor]
|
---|
| 53 | private SymbolicTimeSeriesPrognosisExpressionTreeInterpreter(bool deserializing) : base(deserializing) { }
|
---|
| 54 | private SymbolicTimeSeriesPrognosisExpressionTreeInterpreter(SymbolicTimeSeriesPrognosisExpressionTreeInterpreter original, Cloner cloner) : base(original, cloner) { }
|
---|
| 55 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 56 | return new SymbolicTimeSeriesPrognosisExpressionTreeInterpreter(this, cloner);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | internal SymbolicTimeSeriesPrognosisExpressionTreeInterpreter()
|
---|
| 60 | : base("SymbolicTimeSeriesPrognosisInterpreter", "Interpreter for symbolic expression trees including automatically defined functions.") {
|
---|
| 61 | Parameters.Add(new FixedValueParameter<StringValue>(TargetVariableParameterName));
|
---|
| 62 | TargetVariableParameter.Hidden = true;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | public SymbolicTimeSeriesPrognosisExpressionTreeInterpreter(string targetVariable)
|
---|
| 66 | : this() {
|
---|
| 67 | TargetVariable = targetVariable;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | // for each row several (=#horizon) future predictions
|
---|
| 71 | public IEnumerable<IEnumerable<double>> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, Dataset dataset, IEnumerable<int> rows, int horizon) {
|
---|
| 72 | return GetSymbolicExpressionTreeValues(tree, dataset, rows, rows.Select(row => horizon));
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | public IEnumerable<IEnumerable<double>> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, Dataset dataset, IEnumerable<int> rows, IEnumerable<int> horizons) {
|
---|
| 76 | if (CheckExpressionsWithIntervalArithmetic.Value)
|
---|
| 77 | throw new NotSupportedException("Interval arithmetic is not yet supported in the symbolic data analysis interpreter.");
|
---|
| 78 | if (targetVariableCache == null || targetVariableCache.GetLength(0) < dataset.Rows)
|
---|
| 79 | targetVariableCache = dataset.GetDoubleValues(TargetVariable).ToArray();
|
---|
| 80 | if (invalidateCacheIndexes == null)
|
---|
| 81 | invalidateCacheIndexes = new List<int>(10);
|
---|
| 82 |
|
---|
| 83 | string targetVariable = TargetVariable;
|
---|
[9004] | 84 | lock (EvaluatedSolutions) {
|
---|
| 85 | EvaluatedSolutions.Value++; // increment the evaluated solutions counter
|
---|
| 86 | }
|
---|
| 87 | var state = PrepareInterpreterState(tree, dataset, targetVariableCache, TargetVariable);
|
---|
[8798] | 88 | var rowsEnumerator = rows.GetEnumerator();
|
---|
| 89 | var horizonsEnumerator = horizons.GetEnumerator();
|
---|
| 90 |
|
---|
| 91 | // produce a n-step forecast for all rows
|
---|
| 92 | while (rowsEnumerator.MoveNext() & horizonsEnumerator.MoveNext()) {
|
---|
| 93 | int row = rowsEnumerator.Current;
|
---|
| 94 | int horizon = horizonsEnumerator.Current;
|
---|
| 95 | double[] vProgs = new double[horizon];
|
---|
| 96 |
|
---|
| 97 | for (int i = 0; i < horizon; i++) {
|
---|
| 98 | int localRow = i + row; // create a local variable for the ref parameter
|
---|
| 99 | vProgs[i] = Evaluate(dataset, ref localRow, state);
|
---|
| 100 | targetVariableCache[localRow] = vProgs[i];
|
---|
| 101 | invalidateCacheIndexes.Add(localRow);
|
---|
| 102 | state.Reset();
|
---|
| 103 | }
|
---|
| 104 | yield return vProgs;
|
---|
| 105 |
|
---|
| 106 | int j = 0;
|
---|
| 107 | foreach (var targetValue in dataset.GetDoubleValues(targetVariable, invalidateCacheIndexes)) {
|
---|
| 108 | targetVariableCache[invalidateCacheIndexes[j]] = targetValue;
|
---|
| 109 | j++;
|
---|
| 110 | }
|
---|
| 111 | invalidateCacheIndexes.Clear();
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | if (rowsEnumerator.MoveNext() || horizonsEnumerator.MoveNext())
|
---|
| 115 | throw new ArgumentException("Number of elements in rows and horizon enumerations doesn't match.");
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[9004] | 118 | private static InterpreterState PrepareInterpreterState(ISymbolicExpressionTree tree, Dataset dataset, double[] targetVariableCache, string targetVariable) {
|
---|
[8798] | 119 | Instruction[] code = SymbolicExpressionTreeCompiler.Compile(tree, OpCodes.MapSymbolToOpCode);
|
---|
| 120 | int necessaryArgStackSize = 0;
|
---|
| 121 | foreach (Instruction instr in code) {
|
---|
| 122 | if (instr.opCode == OpCodes.Variable) {
|
---|
| 123 | var variableTreeNode = (VariableTreeNode)instr.dynamicNode;
|
---|
[9004] | 124 | if (variableTreeNode.VariableName == targetVariable)
|
---|
[9828] | 125 | instr.data = targetVariableCache;
|
---|
[8798] | 126 | else
|
---|
[9828] | 127 | instr.data = dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName);
|
---|
[8798] | 128 | } else if (instr.opCode == OpCodes.LagVariable) {
|
---|
| 129 | var variableTreeNode = (LaggedVariableTreeNode)instr.dynamicNode;
|
---|
[9004] | 130 | if (variableTreeNode.VariableName == targetVariable)
|
---|
[9828] | 131 | instr.data = targetVariableCache;
|
---|
[8798] | 132 | else
|
---|
[9828] | 133 | instr.data = dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName);
|
---|
[8798] | 134 | } else if (instr.opCode == OpCodes.VariableCondition) {
|
---|
| 135 | var variableTreeNode = (VariableConditionTreeNode)instr.dynamicNode;
|
---|
[9004] | 136 | if (variableTreeNode.VariableName == targetVariable)
|
---|
[9828] | 137 | instr.data = targetVariableCache;
|
---|
[8798] | 138 | else
|
---|
[9828] | 139 | instr.data = dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName);
|
---|
[8798] | 140 | } else if (instr.opCode == OpCodes.Call) {
|
---|
| 141 | necessaryArgStackSize += instr.nArguments + 1;
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | return new InterpreterState(code, necessaryArgStackSize);
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 | }
|
---|