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.Persistence.Default.CompositeSerializers.Storable;
|
---|
9 | using HeuristicLab.Problems.DataAnalysis;
|
---|
10 | using HeuristicLab.Random;
|
---|
11 |
|
---|
12 | namespace HeuristicLab.Problems.DynamicalSystemsModelling {
|
---|
13 | [StorableClass]
|
---|
14 | public class Solution : Item {
|
---|
15 | [Storable]
|
---|
16 | private ISymbolicExpressionTree[] trees;
|
---|
17 | public ISymbolicExpressionTree[] Trees {
|
---|
18 | get { return trees; }
|
---|
19 | }
|
---|
20 | // [Storable]
|
---|
21 | // private double[] theta;
|
---|
22 |
|
---|
23 | [Storable]
|
---|
24 | private IRegressionProblemData problemData;
|
---|
25 | public IRegressionProblemData ProblemData {
|
---|
26 | get { return problemData; }
|
---|
27 | }
|
---|
28 | [Storable]
|
---|
29 | private string[] targetVars;
|
---|
30 | public string[] TargetVariables {
|
---|
31 | get { return targetVars; }
|
---|
32 | }
|
---|
33 | [Storable]
|
---|
34 | private string[] latentVariables;
|
---|
35 | public string[] LatentVariables {
|
---|
36 | get { return latentVariables; }
|
---|
37 | }
|
---|
38 | [Storable]
|
---|
39 | private IEnumerable<IntRange> trainingEpisodes;
|
---|
40 | public IEnumerable<IntRange> TrainingEpisodes {
|
---|
41 | get { return trainingEpisodes; }
|
---|
42 | }
|
---|
43 | [Storable]
|
---|
44 | private string odeSolver;
|
---|
45 | [Storable]
|
---|
46 | private int numericIntegrationSteps;
|
---|
47 |
|
---|
48 | [StorableConstructor]
|
---|
49 | private Solution(bool deserializing) : base(deserializing) { }
|
---|
50 | [StorableHook(HookType.AfterDeserialization)]
|
---|
51 | private void AfterDeserialization() {
|
---|
52 | }
|
---|
53 |
|
---|
54 | // cloning
|
---|
55 | private Solution(Solution original, Cloner cloner)
|
---|
56 | : base(original, cloner) {
|
---|
57 | this.trees = new ISymbolicExpressionTree[original.trees.Length];
|
---|
58 | for (int i = 0; i < trees.Length; i++) this.trees[i] = cloner.Clone(original.trees[i]);
|
---|
59 | // this.theta = new double[original.theta.Length];
|
---|
60 | // Array.Copy(original.theta, this.theta, this.theta.Length);
|
---|
61 | this.problemData = cloner.Clone(original.problemData);
|
---|
62 | this.targetVars = original.TargetVariables.ToArray();
|
---|
63 | this.latentVariables = original.LatentVariables.ToArray();
|
---|
64 | this.trainingEpisodes = original.TrainingEpisodes.Select(te => cloner.Clone(te)).ToArray();
|
---|
65 | this.odeSolver = original.odeSolver;
|
---|
66 | this.numericIntegrationSteps = original.numericIntegrationSteps;
|
---|
67 | }
|
---|
68 |
|
---|
69 | public Solution(ISymbolicExpressionTree[] trees,
|
---|
70 | IRegressionProblemData problemData,
|
---|
71 | string[] targetVars, string[] latentVariables, IEnumerable<IntRange> trainingEpisodes,
|
---|
72 | string odeSolver, int numericIntegrationSteps) : base() {
|
---|
73 | this.trees = trees;
|
---|
74 |
|
---|
75 | this.problemData = problemData;
|
---|
76 | this.targetVars = targetVars;
|
---|
77 | this.latentVariables = latentVariables;
|
---|
78 | this.trainingEpisodes = trainingEpisodes;
|
---|
79 | this.odeSolver = odeSolver;
|
---|
80 | this.numericIntegrationSteps = numericIntegrationSteps;
|
---|
81 | }
|
---|
82 |
|
---|
83 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
84 | return new Solution(this, cloner);
|
---|
85 | }
|
---|
86 |
|
---|
87 | public IEnumerable<double[]> Predict(IntRange episode, int forecastHorizon, out double snmse) {
|
---|
88 | var forecastEpisode = new IntRange(episode.Start, episode.End + forecastHorizon);
|
---|
89 |
|
---|
90 | double[] optL0;
|
---|
91 | var random = new FastRandom(12345);
|
---|
92 | Problem.OptimizeForEpisodes(trees, problemData, targetVars, latentVariables, random, new[] { forecastEpisode }, 100, numericIntegrationSteps, odeSolver, out optL0, out snmse);
|
---|
93 | var predictions = Problem.Integrate(
|
---|
94 | trees,
|
---|
95 | problemData.Dataset,
|
---|
96 | problemData.AllowedInputVariables.ToArray(),
|
---|
97 | targetVars,
|
---|
98 | latentVariables,
|
---|
99 | new[] { forecastEpisode },
|
---|
100 | optL0,
|
---|
101 | odeSolver,
|
---|
102 | numericIntegrationSteps).ToArray();
|
---|
103 | return predictions.Select(p => p.Select(pi => pi.Item1).ToArray()).ToArray();
|
---|
104 | }
|
---|
105 | }
|
---|
106 | } |
---|