source: branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DynamicalSystemsModelling/3.3/Solution.cs @ 16663

Last change on this file since 16663 was 16663, checked in by gkronber, 2 years ago

#2925: adapted to work with new persistence

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