Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceOverhaul/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis/3.4/SingleObjective/SymbolicTimeSeriesPrognosisSingleObjectiveProblem.cs

Last change on this file was 14711, checked in by gkronber, 8 years ago

#2520

  • renamed StorableClass -> StorableType
  • changed persistence to use GUIDs instead of type names
File size: 7.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis {
30  [Item("Symbolic Time-Series Prognosis Problem (single-objective)", "Represents a single-objective symbolic time-series prognosis problem.")]
31  [StorableType("478FF590-DC38-4C50-932F-CC462D91AE22")]
32  [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 140)]
33  public class SymbolicTimeSeriesPrognosisSingleObjectiveProblem : SymbolicDataAnalysisSingleObjectiveProblem<ITimeSeriesPrognosisProblemData, ISymbolicTimeSeriesPrognosisSingleObjectiveEvaluator, ISymbolicDataAnalysisSolutionCreator>, ITimeSeriesPrognosisProblem {
34    private const double PunishmentFactor = 10;
35    private const int InitialMaximumTreeDepth = 8;
36    private const int InitialMaximumTreeLength = 25;
37    private const string EstimationLimitsParameterName = "EstimationLimits";
38    private const string EstimationLimitsParameterDescription = "The limits for the estimated value that can be returned by the symbolic regression model.";
39
40    #region parameter properties
41    public IFixedValueParameter<DoubleLimit> EstimationLimitsParameter {
42      get { return (IFixedValueParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
43    }
44    #endregion
45    #region properties
46    public DoubleLimit EstimationLimits {
47      get { return EstimationLimitsParameter.Value; }
48    }
49    #endregion
50    [StorableConstructor]
51    protected SymbolicTimeSeriesPrognosisSingleObjectiveProblem(bool deserializing) : base(deserializing) { }
52    protected SymbolicTimeSeriesPrognosisSingleObjectiveProblem(SymbolicTimeSeriesPrognosisSingleObjectiveProblem original, Cloner cloner) : base(original, cloner) { }
53    public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicTimeSeriesPrognosisSingleObjectiveProblem(this, cloner); }
54
55    public SymbolicTimeSeriesPrognosisSingleObjectiveProblem()
56      : base(new TimeSeriesPrognosisProblemData(), new SymbolicTimeSeriesPrognosisSingleObjectiveMeanSquaredErrorEvaluator(), new SymbolicDataAnalysisExpressionTreeCreator()) {
57      Parameters.Add(new FixedValueParameter<DoubleLimit>(EstimationLimitsParameterName, EstimationLimitsParameterDescription));
58      EstimationLimitsParameter.Hidden = true;
59
60      Maximization.Value = false;
61      MaximumSymbolicExpressionTreeDepth.Value = InitialMaximumTreeDepth;
62      MaximumSymbolicExpressionTreeLength.Value = InitialMaximumTreeLength;
63
64      var interpeter = new SymbolicTimeSeriesPrognosisExpressionTreeInterpreter();
65      interpeter.TargetVariable = ProblemData.TargetVariable;
66      SymbolicExpressionTreeInterpreter = interpeter;
67
68      SymbolicExpressionTreeGrammarParameter.ValueChanged += (o, e) => ConfigureGrammarSymbols();
69      ConfigureGrammarSymbols();
70
71      InitializeOperators();
72      UpdateEstimationLimits();
73    }
74
75    private void ConfigureGrammarSymbols() {
76      var grammar = SymbolicExpressionTreeGrammar as TypeCoherentExpressionGrammar;
77      if (grammar != null) grammar.ConfigureAsDefaultTimeSeriesPrognosisGrammar();
78      UpdateGrammar();
79    }
80    protected override void UpdateGrammar() {
81      base.UpdateGrammar();
82      foreach (var autoregressiveSymbol in SymbolicExpressionTreeGrammar.Symbols.OfType<AutoregressiveTargetVariable>()) {
83        if (!autoregressiveSymbol.Fixed) autoregressiveSymbol.VariableNames = ProblemData.TargetVariable.ToEnumerable();
84      }
85    }
86
87    private void InitializeOperators() {
88      Operators.Add(new SymbolicTimeSeriesPrognosisSingleObjectiveTrainingBestSolutionAnalyzer());
89      Operators.Add(new SymbolicTimeSeriesPrognosisSingleObjectiveValidationBestSolutionAnalyzer());
90      Operators.Add(new SymbolicTimeSeriesPrognosisSingleObjectiveOverfittingAnalyzer());
91      ParameterizeOperators();
92    }
93
94    private void UpdateEstimationLimits() {
95      if (ProblemData.TrainingIndices.Any()) {
96        var targetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).ToList();
97        var mean = targetValues.Average();
98        var range = targetValues.Max() - targetValues.Min();
99        EstimationLimits.Upper = mean + PunishmentFactor * range;
100        EstimationLimits.Lower = mean - PunishmentFactor * range;
101      } else {
102        EstimationLimits.Upper = double.MaxValue;
103        EstimationLimits.Lower = double.MinValue;
104      }
105    }
106
107    protected override void OnProblemDataChanged() {
108      base.OnProblemDataChanged();
109      var interpreter = SymbolicExpressionTreeInterpreter as ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter;
110      if (interpreter != null) {
111        interpreter.TargetVariable = ProblemData.TargetVariable;
112      }
113      UpdateEstimationLimits();
114
115    }
116
117    protected override void ParameterizeOperators() {
118      base.ParameterizeOperators();
119      if (Parameters.ContainsKey(EstimationLimitsParameterName)) {
120        var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators);
121        foreach (var op in operators.OfType<ISymbolicDataAnalysisBoundedOperator>()) {
122          op.EstimationLimitsParameter.ActualName = EstimationLimitsParameter.Name;
123        }
124        foreach (var op in operators.OfType<SymbolicTimeSeriesPrognosisSingleObjectiveTrainingBestSolutionAnalyzer>()) {
125          op.MaximizationParameter.ActualName = MaximizationParameter.Name;
126          op.ProblemDataParameter.ActualName = ProblemDataParameter.Name;
127          op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
128          op.SymbolicDataAnalysisTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameter.Name;
129          op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
130        }
131      }
132    }
133
134    public override void Load(ITimeSeriesPrognosisProblemData data) {
135      base.Load(data);
136      this.ProblemData.TrainingPartition.Start =
137        Math.Min(10, this.ProblemData.TrainingPartition.End);
138    }
139  }
140}
Note: See TracBrowser for help on using the repository browser.