Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2434_crossvalidation/HeuristicLab.Problems.GrammaticalEvolution/3.3/Symbolic/GESymbolicRegressionSingleObjectiveProblem.cs @ 16752

Last change on this file since 16752 was 12931, checked in by gkronber, 9 years ago

#2434 merged r12878:12927 from trunk to branch (hive changes)

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