using System; using System.Collections.Generic; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Problems.DataAnalysis; using HeuristicLab.Random; namespace HeuristicLab.Problems.DynamicalSystemsModelling { [StorableClass] public class Solution : Item { [Storable] private ISymbolicExpressionTree[] trees; public ISymbolicExpressionTree[] Trees { get { return trees; } } // [Storable] // private double[] theta; [Storable] private IRegressionProblemData problemData; public IRegressionProblemData ProblemData { get { return problemData; } } [Storable] private string[] targetVars; public string[] TargetVariables { get { return targetVars; } } [Storable] private string[] latentVariables; public string[] LatentVariables { get { return latentVariables; } } [Storable] private IEnumerable trainingEpisodes; public IEnumerable TrainingEpisodes { get { return trainingEpisodes; } } [Storable] private string odeSolver; [Storable] private int numericIntegrationSteps; [StorableConstructor] private Solution(bool deserializing) : base(deserializing) { } [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { } // cloning private Solution(Solution original, Cloner cloner) : base(original, cloner) { this.trees = new ISymbolicExpressionTree[original.trees.Length]; for (int i = 0; i < trees.Length; i++) this.trees[i] = cloner.Clone(original.trees[i]); // this.theta = new double[original.theta.Length]; // Array.Copy(original.theta, this.theta, this.theta.Length); this.problemData = cloner.Clone(original.problemData); this.targetVars = original.TargetVariables.ToArray(); this.latentVariables = original.LatentVariables.ToArray(); this.trainingEpisodes = original.TrainingEpisodes.Select(te => cloner.Clone(te)).ToArray(); this.odeSolver = original.odeSolver; this.numericIntegrationSteps = original.numericIntegrationSteps; } public Solution(ISymbolicExpressionTree[] trees, IRegressionProblemData problemData, string[] targetVars, string[] latentVariables, IEnumerable trainingEpisodes, string odeSolver, int numericIntegrationSteps) : base() { this.trees = trees; this.problemData = problemData; this.targetVars = targetVars; this.latentVariables = latentVariables; this.trainingEpisodes = trainingEpisodes; this.odeSolver = odeSolver; this.numericIntegrationSteps = numericIntegrationSteps; } public override IDeepCloneable Clone(Cloner cloner) { return new Solution(this, cloner); } public IEnumerable Predict(IntRange episode, int forecastHorizon, out double snmse) { var forecastEpisode = new IntRange(episode.Start, episode.End + forecastHorizon); double[] optL0; var random = new FastRandom(12345); Problem.OptimizeForEpisodes(trees, problemData, targetVars, latentVariables, random, new[] { forecastEpisode }, 100, numericIntegrationSteps, odeSolver, out optL0, out snmse); var predictions = Problem.Integrate( trees, problemData.Dataset, problemData.AllowedInputVariables.ToArray(), targetVars, latentVariables, new[] { forecastEpisode }, optL0, odeSolver, numericIntegrationSteps).ToArray(); return predictions.Select(p => p.Select(pi => pi.Item1).ToArray()).ToArray(); } } }