Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis/3.4/SymbolicTimeSeriesPrognosisExpressionTreeInterpreter.cs @ 18190

Last change on this file since 18190 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 6.4 KB
RevLine 
[8798]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) 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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Parameters;
[16565]30using HEAL.Attic;
[8798]31
32namespace HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis {
[16565]33  [StorableType("45710F01-2B76-4780-B04C-A457C289F33A")]
[8798]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    [StorableConstructor]
[16565]48    private SymbolicTimeSeriesPrognosisExpressionTreeInterpreter(StorableConstructorFlag _) : base(_) { }
[8798]49    private SymbolicTimeSeriesPrognosisExpressionTreeInterpreter(SymbolicTimeSeriesPrognosisExpressionTreeInterpreter original, Cloner cloner) : base(original, cloner) { }
50    public override IDeepCloneable Clone(Cloner cloner) {
51      return new SymbolicTimeSeriesPrognosisExpressionTreeInterpreter(this, cloner);
52    }
53
54    internal SymbolicTimeSeriesPrognosisExpressionTreeInterpreter()
55      : base("SymbolicTimeSeriesPrognosisInterpreter", "Interpreter for symbolic expression trees including automatically defined functions.") {
56      Parameters.Add(new FixedValueParameter<StringValue>(TargetVariableParameterName));
57      TargetVariableParameter.Hidden = true;
58    }
59
60    public SymbolicTimeSeriesPrognosisExpressionTreeInterpreter(string targetVariable)
61      : this() {
62      TargetVariable = targetVariable;
63    }
64
65    // for each row several (=#horizon) future predictions
[12509]66    public IEnumerable<IEnumerable<double>> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows, int horizon) {
[8798]67      return GetSymbolicExpressionTreeValues(tree, dataset, rows, rows.Select(row => horizon));
68    }
69
[13248]70    private readonly object syncRoot = new object();
[12509]71    public IEnumerable<IEnumerable<double>> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows, IEnumerable<int> horizons) {
[13248]72      if (CheckExpressionsWithIntervalArithmetic)
[8798]73        throw new NotSupportedException("Interval arithmetic is not yet supported in the symbolic data analysis interpreter.");
74
75      string targetVariable = TargetVariable;
[14423]76      double[] targetVariableCache = dataset.GetDoubleValues(targetVariable).ToArray();
[13248]77      lock (syncRoot) {
78        EvaluatedSolutions++; // increment the evaluated solutions counter
[9004]79      }
80      var state = PrepareInterpreterState(tree, dataset, targetVariableCache, TargetVariable);
[8798]81      var rowsEnumerator = rows.GetEnumerator();
82      var horizonsEnumerator = horizons.GetEnumerator();
83
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;
88        double[] vProgs = new double[horizon];
89
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          state.Reset();
95        }
96        yield return vProgs;
97      }
98
99      if (rowsEnumerator.MoveNext() || horizonsEnumerator.MoveNext())
100        throw new ArgumentException("Number of elements in rows and horizon enumerations doesn't match.");
101    }
102
[12509]103    private static InterpreterState PrepareInterpreterState(ISymbolicExpressionTree tree, IDataset dataset, double[] targetVariableCache, string targetVariable) {
[8798]104      Instruction[] code = SymbolicExpressionTreeCompiler.Compile(tree, OpCodes.MapSymbolToOpCode);
105      int necessaryArgStackSize = 0;
106      foreach (Instruction instr in code) {
107        if (instr.opCode == OpCodes.Variable) {
108          var variableTreeNode = (VariableTreeNode)instr.dynamicNode;
[9004]109          if (variableTreeNode.VariableName == targetVariable)
[9828]110            instr.data = targetVariableCache;
[8798]111          else
[9828]112            instr.data = dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName);
[8798]113        } else if (instr.opCode == OpCodes.LagVariable) {
114          var variableTreeNode = (LaggedVariableTreeNode)instr.dynamicNode;
[9004]115          if (variableTreeNode.VariableName == targetVariable)
[9828]116            instr.data = targetVariableCache;
[8798]117          else
[9828]118            instr.data = dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName);
[8798]119        } else if (instr.opCode == OpCodes.VariableCondition) {
120          var variableTreeNode = (VariableConditionTreeNode)instr.dynamicNode;
[9004]121          if (variableTreeNode.VariableName == targetVariable)
[9828]122            instr.data = targetVariableCache;
[8798]123          else
[9828]124            instr.data = dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName);
[8798]125        } else if (instr.opCode == OpCodes.Call) {
126          necessaryArgStackSize += instr.nArguments + 1;
127        }
128      }
129
130      return new InterpreterState(code, necessaryArgStackSize);
131    }
132  }
133}
Note: See TracBrowser for help on using the repository browser.