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

Last change on this file since 10268 was 10268, checked in by gkronber, 6 years ago

#2109 implemented a grammar especially for GE (the grammar is not configured correctly when used in a classic symbolic regression/classification problem).
To use this grammar in a classical symbolic expression problem, first set the grammar in a GEProblem and load the problem instance (this creates the necessary variable symbols).
After this the configured grammar can be dragged onto the grammar parameter of the classical problem.

File size: 5.6 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      InitializeOperators();
76      UpdateEstimationLimits();
77    }
78
79    [StorableHook(HookType.AfterDeserialization)]
80    private void AfterDeserialization() {
81      RegisterEventHandlers();
82    }
83
84    private void RegisterEventHandlers() {
85      // nothing to do
86    }
87
88
89    private void InitializeOperators() {
90      Operators.Add(new SymbolicRegressionSingleObjectiveTrainingBestSolutionAnalyzer());
91      Operators.Add(new SymbolicRegressionSingleObjectiveValidationBestSolutionAnalyzer());
92      Operators.Add(new SymbolicRegressionSingleObjectiveOverfittingAnalyzer());
93      Operators.Add(new SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer());
94      Operators.Add(new SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer());
95
96      ParameterizeOperators();
97    }
98
99    private void UpdateEstimationLimits() {
100      if (ProblemData.TrainingIndices.Any()) {
101        var targetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).ToList();
102        var mean = targetValues.Average();
103        var range = targetValues.Max() - targetValues.Min();
104        EstimationLimits.Upper = mean + PunishmentFactor * range;
105        EstimationLimits.Lower = mean - PunishmentFactor * range;
106      } else {
107        EstimationLimits.Upper = double.MaxValue;
108        EstimationLimits.Lower = double.MinValue;
109      }
110    }
111
112    protected override void OnProblemDataChanged() {
113      base.OnProblemDataChanged();
114      UpdateEstimationLimits();
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      }
125    }
126  }
127}
Note: See TracBrowser for help on using the repository browser.