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