Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis/3.4/SingleObjective/SymbolicTimeSeriesPrognosisSingleObjectiveProblem.cs @ 12012

Last change on this file since 12012 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

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