Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/Symbolic/GESymbolicRegressionSingleObjectiveProblem.cs @ 10263

Last change on this file since 10263 was 10263, checked in by gkronber, 11 years ago

#2109 implemented a wrapper for evaluators that transforms genotypes to phenotypes for symbolic regression (deleted obsolete evaluators)

File size: 5.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Encodings.IntegerVectorEncoding;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Problems.DataAnalysis;
29using HeuristicLab.Problems.DataAnalysis.Symbolic;
30using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
31
32namespace HeuristicLab.Problems.GrammaticalEvolution {
33  [Item("Grammatical Evolution Symbolic Regression Problem (single objective)", "Represents a single objective symbolic regression problem, implemented in Grammatical Evolution.")]
34  [StorableClass]
35  [Creatable("Problems")]
36  public class GESymbolicRegressionSingleObjectiveProblem : GESymbolicDataAnalysisSingleObjectiveProblem<IRegressionProblemData, IGESymbolicRegressionSingleObjectiveEvaluator, IIntegerVectorCreator>,
37                                                            IRegressionProblem {
38    private const double PunishmentFactor = 10;
39    //private const int InitialMaximumTreeDepth = 8;
40    private const int InitialMaximumTreeLength = 25;
41    private const string EstimationLimitsParameterName = "EstimationLimits";
42    private const string EstimationLimitsParameterDescription = "The limits for the estimated value that can be returned by the symbolic regression model.";
43
44    #region parameter properties
45    public IFixedValueParameter<DoubleLimit> EstimationLimitsParameter {
46      get { return (IFixedValueParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
47    }
48    #endregion
49    #region properties
50    public DoubleLimit EstimationLimits {
51      get { return EstimationLimitsParameter.Value; }
52    }
53    #endregion
54    [StorableConstructor]
55    protected GESymbolicRegressionSingleObjectiveProblem(bool deserializing) : base(deserializing) { }
56    protected GESymbolicRegressionSingleObjectiveProblem(GESymbolicRegressionSingleObjectiveProblem original, Cloner cloner)
57      : base(original, cloner) {
58      RegisterEventHandlers();
59    }
60    public override IDeepCloneable Clone(Cloner cloner) { return new GESymbolicRegressionSingleObjectiveProblem(this, cloner); }
61
62    public GESymbolicRegressionSingleObjectiveProblem()
63      : base(new RegressionProblemData(), new GESymbolicRegressionSingleObjectiveEvaluator(), new UniformRandomIntegerVectorCreator()) {
64      Parameters.Add(new FixedValueParameter<DoubleLimit>(EstimationLimitsParameterName, EstimationLimitsParameterDescription));
65
66      EstimationLimitsParameter.Hidden = true;
67
68
69      ApplyLinearScalingParameter.Value.Value = true;
70      Maximization.Value = true;
71      //MaximumSymbolicExpressionTreeDepth.Value = InitialMaximumTreeDepth;
72      MaximumSymbolicExpressionTreeLength.Value = InitialMaximumTreeLength;
73
74      RegisterEventHandlers();
75      ConfigureGrammarSymbols();
76      InitializeOperators();
77      UpdateEstimationLimits();
78    }
79
80    [StorableHook(HookType.AfterDeserialization)]
81    private void AfterDeserialization() {
82      RegisterEventHandlers();
83    }
84
85    private void RegisterEventHandlers() {
86      SymbolicExpressionTreeGrammarParameter.ValueChanged += (o, e) => ConfigureGrammarSymbols();
87    }
88
89    private void ConfigureGrammarSymbols() {
90      var grammar = SymbolicExpressionTreeGrammar as TypeCoherentExpressionGrammar;
91      if (grammar != null) grammar.ConfigureAsDefaultRegressionGrammar();
92    }
93
94    private void InitializeOperators() {
95      Operators.Add(new SymbolicRegressionSingleObjectiveTrainingBestSolutionAnalyzer());
96      Operators.Add(new SymbolicRegressionSingleObjectiveValidationBestSolutionAnalyzer());
97      Operators.Add(new SymbolicRegressionSingleObjectiveOverfittingAnalyzer());
98      Operators.Add(new SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer());
99      Operators.Add(new SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer());
100
101      ParameterizeOperators();
102    }
103
104    private void UpdateEstimationLimits() {
105      if (ProblemData.TrainingIndices.Any()) {
106        var targetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).ToList();
107        var mean = targetValues.Average();
108        var range = targetValues.Max() - targetValues.Min();
109        EstimationLimits.Upper = mean + PunishmentFactor * range;
110        EstimationLimits.Lower = mean - PunishmentFactor * range;
111      } else {
112        EstimationLimits.Upper = double.MaxValue;
113        EstimationLimits.Lower = double.MinValue;
114      }
115    }
116
117    protected override void OnProblemDataChanged() {
118      base.OnProblemDataChanged();
119      UpdateEstimationLimits();
120    }
121
122    protected override void ParameterizeOperators() {
123      base.ParameterizeOperators();
124      if (Parameters.ContainsKey(EstimationLimitsParameterName)) {
125        var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators);
126        foreach (var op in operators.OfType<ISymbolicDataAnalysisBoundedOperator>()) {
127          op.EstimationLimitsParameter.ActualName = EstimationLimitsParameter.Name;
128        }
129      }
130    }
131  }
132}
Note: See TracBrowser for help on using the repository browser.