1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using HeuristicLab.Common;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Data;
|
---|
7 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
8 | using HeuristicLab.Problems.DataAnalysis;
|
---|
9 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
10 | using HEAL.Attic;
|
---|
11 |
|
---|
12 | namespace HeuristicLab.Problems.DynamicalSystemsModelling {
|
---|
13 | [StorableType("70AA254D-706E-474A-8129-E9A8A7067FE1")]
|
---|
14 | public class Solution : Item {
|
---|
15 | [Storable]
|
---|
16 | private ISymbolicExpressionTree[] trees;
|
---|
17 | public ISymbolicExpressionTree[] Trees {
|
---|
18 | get { return trees; }
|
---|
19 | }
|
---|
20 |
|
---|
21 | [Storable]
|
---|
22 | private IRegressionProblemData problemData;
|
---|
23 | public IRegressionProblemData ProblemData {
|
---|
24 | get { return problemData; }
|
---|
25 | }
|
---|
26 | [Storable]
|
---|
27 | private string[] targetVars;
|
---|
28 | public string[] TargetVariables {
|
---|
29 | get { return targetVars; }
|
---|
30 | }
|
---|
31 | [Storable]
|
---|
32 | private string[] latentVariables;
|
---|
33 | public string[] LatentVariables {
|
---|
34 | get { return latentVariables; }
|
---|
35 | }
|
---|
36 | [Storable]
|
---|
37 | private IEnumerable<IntRange> trainingEpisodes;
|
---|
38 | public IEnumerable<IntRange> TrainingEpisodes {
|
---|
39 | get { return trainingEpisodes; }
|
---|
40 | }
|
---|
41 | [Storable]
|
---|
42 | private string odeSolver;
|
---|
43 | [Storable]
|
---|
44 | private int numericIntegrationSteps;
|
---|
45 |
|
---|
46 | [StorableConstructor]
|
---|
47 | private Solution(StorableConstructorFlag _) : base(_) { }
|
---|
48 | [StorableHook(HookType.AfterDeserialization)]
|
---|
49 | private void AfterDeserialization() {
|
---|
50 | }
|
---|
51 |
|
---|
52 | // cloning
|
---|
53 | private Solution(Solution original, Cloner cloner)
|
---|
54 | : base(original, cloner) {
|
---|
55 | this.trees = new ISymbolicExpressionTree[original.trees.Length];
|
---|
56 | for (int i = 0; i < trees.Length; i++) this.trees[i] = cloner.Clone(original.trees[i]);
|
---|
57 | this.problemData = cloner.Clone(original.problemData);
|
---|
58 | this.targetVars = original.TargetVariables.ToArray();
|
---|
59 | this.latentVariables = original.LatentVariables.ToArray();
|
---|
60 | this.trainingEpisodes = original.TrainingEpisodes.Select(te => cloner.Clone(te)).ToArray();
|
---|
61 | this.odeSolver = original.odeSolver;
|
---|
62 | this.numericIntegrationSteps = original.numericIntegrationSteps;
|
---|
63 | }
|
---|
64 |
|
---|
65 | public Solution(ISymbolicExpressionTree[] trees,
|
---|
66 | IRegressionProblemData problemData,
|
---|
67 | string[] targetVars, string[] latentVariables, IEnumerable<IntRange> trainingEpisodes,
|
---|
68 | string odeSolver, int numericIntegrationSteps) : base() {
|
---|
69 | this.trees = trees;
|
---|
70 |
|
---|
71 | this.problemData = problemData;
|
---|
72 | this.targetVars = targetVars;
|
---|
73 | this.latentVariables = latentVariables;
|
---|
74 | this.trainingEpisodes = trainingEpisodes;
|
---|
75 | this.odeSolver = odeSolver;
|
---|
76 | this.numericIntegrationSteps = numericIntegrationSteps;
|
---|
77 | }
|
---|
78 |
|
---|
79 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
80 | return new Solution(this, cloner);
|
---|
81 | }
|
---|
82 |
|
---|
83 | public IEnumerable<double[]> Predict(IntRange episode, int forecastHorizon) {
|
---|
84 | var forecastEpisode = new IntRange(episode.Start, episode.End + forecastHorizon);
|
---|
85 |
|
---|
86 | var inputVariables = trees.SelectMany(t => t.IterateNodesPrefix().OfType<VariableTreeNode>().Select(n => n.VariableName))
|
---|
87 | .Except(targetVars)
|
---|
88 | .Except(latentVariables)
|
---|
89 | .Distinct();
|
---|
90 |
|
---|
91 | var optimizationData = new Problem.OptimizationData(trees, targetVars, inputVariables.ToArray(), problemData, null, new[] { forecastEpisode }, numericIntegrationSteps, latentVariables, odeSolver);
|
---|
92 | var fi = new double[forecastEpisode.Size * targetVars.Length];
|
---|
93 | var jac = new double[forecastEpisode.Size * targetVars.Length, optimizationData.nodeValueLookup.ParameterCount];
|
---|
94 | var latentValues = new double[forecastEpisode.Size, LatentVariables.Length];
|
---|
95 | Problem.Integrate(optimizationData, fi, jac, latentValues);
|
---|
96 | for (int i = 0; i < forecastEpisode.Size; i++) {
|
---|
97 | var res = new double[targetVars.Length + latentVariables.Length];
|
---|
98 | for (int j = 0; j < targetVars.Length; j++) {
|
---|
99 | res[j] = fi[i * targetVars.Length + j];
|
---|
100 | }
|
---|
101 | for (int j = 0; j < latentVariables.Length; j++) {
|
---|
102 | res[targetVars.Length + j] = latentValues[i, j];
|
---|
103 | }
|
---|
104 | yield return res;
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|
108 | } |
---|